Skip to content

Commit 57d6835

Browse files
committed
Make helper types CowStrVisitor and CowBytesVisitor public
1 parent 1d7899d commit 57d6835

File tree

3 files changed

+338
-124
lines changed

3 files changed

+338
-124
lines changed

serde/src/private/de.rs

Lines changed: 5 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::de::{
77
};
88

99
#[cfg(any(feature = "std", feature = "alloc"))]
10-
use crate::de::{MapAccess, Unexpected};
10+
use crate::de::MapAccess;
1111

1212
#[cfg(any(feature = "std", feature = "alloc"))]
1313
pub use self::content::{
@@ -66,72 +66,9 @@ where
6666
D: Deserializer<'de>,
6767
R: From<Cow<'a, str>>,
6868
{
69-
struct CowStrVisitor;
70-
71-
#[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)]
72-
impl<'a> Visitor<'a> for CowStrVisitor {
73-
type Value = Cow<'a, str>;
74-
75-
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
76-
formatter.write_str("a string")
77-
}
78-
79-
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
80-
where
81-
E: Error,
82-
{
83-
Ok(Cow::Owned(v.to_owned()))
84-
}
85-
86-
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
87-
where
88-
E: Error,
89-
{
90-
Ok(Cow::Borrowed(v))
91-
}
92-
93-
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
94-
where
95-
E: Error,
96-
{
97-
Ok(Cow::Owned(v))
98-
}
99-
100-
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
101-
where
102-
E: Error,
103-
{
104-
match str::from_utf8(v) {
105-
Ok(s) => Ok(Cow::Owned(s.to_owned())),
106-
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
107-
}
108-
}
109-
110-
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
111-
where
112-
E: Error,
113-
{
114-
match str::from_utf8(v) {
115-
Ok(s) => Ok(Cow::Borrowed(s)),
116-
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
117-
}
118-
}
119-
120-
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
121-
where
122-
E: Error,
123-
{
124-
match String::from_utf8(v) {
125-
Ok(s) => Ok(Cow::Owned(s)),
126-
Err(e) => Err(Error::invalid_value(
127-
Unexpected::Bytes(&e.into_bytes()),
128-
&self,
129-
)),
130-
}
131-
}
132-
}
133-
134-
deserializer.deserialize_str(CowStrVisitor).map(From::from)
69+
deserializer
70+
.deserialize_str(serde_core::de::value::CowStrVisitor)
71+
.map(From::from)
13572
}
13673

13774
#[cfg(any(feature = "std", feature = "alloc"))]
@@ -140,61 +77,8 @@ where
14077
D: Deserializer<'de>,
14178
R: From<Cow<'a, [u8]>>,
14279
{
143-
struct CowBytesVisitor;
144-
145-
#[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)]
146-
impl<'a> Visitor<'a> for CowBytesVisitor {
147-
type Value = Cow<'a, [u8]>;
148-
149-
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
150-
formatter.write_str("a byte array")
151-
}
152-
153-
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
154-
where
155-
E: Error,
156-
{
157-
Ok(Cow::Owned(v.as_bytes().to_vec()))
158-
}
159-
160-
fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
161-
where
162-
E: Error,
163-
{
164-
Ok(Cow::Borrowed(v.as_bytes()))
165-
}
166-
167-
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
168-
where
169-
E: Error,
170-
{
171-
Ok(Cow::Owned(v.into_bytes()))
172-
}
173-
174-
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
175-
where
176-
E: Error,
177-
{
178-
Ok(Cow::Owned(v.to_vec()))
179-
}
180-
181-
fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
182-
where
183-
E: Error,
184-
{
185-
Ok(Cow::Borrowed(v))
186-
}
187-
188-
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
189-
where
190-
E: Error,
191-
{
192-
Ok(Cow::Owned(v))
193-
}
194-
}
195-
19680
deserializer
197-
.deserialize_bytes(CowBytesVisitor)
81+
.deserialize_bytes(serde_core::de::value::CowBytesVisitor)
19882
.map(From::from)
19983
}
20084

0 commit comments

Comments
 (0)