Skip to content

Commit dd1f4b4

Browse files
committed
Make it clearer that the private implementation details are private
Both of these modules are doc(hidden) and commented "Not public API", but incorrect downstream code sometimes still references them. Naming the module __private will make it more likely to be noticed in code review.
1 parent 91bfa8f commit dd1f4b4

25 files changed

+1832
-1704
lines changed

serde/src/de/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,7 @@ impl<'de> Deserialize<'de> for Duration {
19061906
b"secs" => Ok(Field::Secs),
19071907
b"nanos" => Ok(Field::Nanos),
19081908
_ => {
1909-
let value = ::export::from_utf8_lossy(value);
1909+
let value = ::__private::from_utf8_lossy(value);
19101910
Err(Error::unknown_field(&value, FIELDS))
19111911
}
19121912
}
@@ -2214,7 +2214,7 @@ mod range {
22142214
b"start" => Ok(Field::Start),
22152215
b"end" => Ok(Field::End),
22162216
_ => {
2217-
let value = ::export::from_utf8_lossy(value);
2217+
let value = ::__private::from_utf8_lossy(value);
22182218
Err(Error::unknown_field(&value, FIELDS))
22192219
}
22202220
}

serde/src/export.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

serde/src/integer128.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/// bother with this macro and may assume support for 128-bit integers.
1111
///
1212
/// ```edition2018
13-
/// # use serde::private::ser::Error;
13+
/// # use serde::__private::ser::Error;
1414
/// #
1515
/// # struct MySerializer;
1616
/// #

serde/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,14 @@ pub use de::{Deserialize, Deserializer};
266266
#[doc(inline)]
267267
pub use ser::{Serialize, Serializer};
268268

269-
// Generated code uses these to support no_std. Not public API.
269+
// Used by generated code and doc tests. Not public API.
270270
#[doc(hidden)]
271-
pub mod export;
271+
#[path = "private/mod.rs"]
272+
pub mod __private;
272273

273-
// Helpers used by generated code and doc tests. Not public API.
274-
#[doc(hidden)]
275-
pub mod private;
274+
#[allow(unused_imports)]
275+
use self::__private as export;
276+
use self::__private as private;
276277

277278
#[cfg(not(feature = "std"))]
278279
mod std_error;

serde/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ macro_rules! forward_to_deserialize_any {
124124
macro_rules! forward_to_deserialize_any_method {
125125
($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => {
126126
#[inline]
127-
fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::export::Result<$v::Value, Self::Error>
127+
fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::__private::Result<$v::Value, Self::Error>
128128
where
129129
$v: $crate::de::Visitor<$l>,
130130
{

serde/src/private/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ macro_rules! __serialize_unimplemented {
3737
#[macro_export]
3838
macro_rules! __serialize_unimplemented_method {
3939
($func:ident $(<$t:ident>)* ($($arg:ty),*) -> $ret:ident) => {
40-
fn $func $(<$t: ?Sized + $crate::Serialize>)* (self $(, _: $arg)*) -> $crate::export::Result<Self::$ret, Self::Error> {
40+
fn $func $(<$t: ?Sized + $crate::Serialize>)* (self $(, _: $arg)*) -> $crate::__private::Result<Self::$ret, Self::Error> {
4141
unimplemented!()
4242
}
4343
};

serde/src/private/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,43 @@ mod macros;
22

33
pub mod de;
44
pub mod ser;
5+
6+
pub use lib::clone::Clone;
7+
pub use lib::convert::{From, Into};
8+
pub use lib::default::Default;
9+
pub use lib::fmt::{self, Formatter};
10+
pub use lib::marker::PhantomData;
11+
pub use lib::option::Option::{self, None, Some};
12+
pub use lib::result::Result::{self, Err, Ok};
13+
14+
pub use self::string::from_utf8_lossy;
15+
16+
#[cfg(any(feature = "alloc", feature = "std"))]
17+
pub use lib::{ToString, Vec};
18+
19+
#[cfg(core_try_from)]
20+
pub use lib::convert::TryFrom;
21+
22+
mod string {
23+
use lib::*;
24+
25+
#[cfg(any(feature = "std", feature = "alloc"))]
26+
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
27+
String::from_utf8_lossy(bytes)
28+
}
29+
30+
// The generated code calls this like:
31+
//
32+
// let value = &_serde::__private::from_utf8_lossy(bytes);
33+
// Err(_serde::de::Error::unknown_variant(value, VARIANTS))
34+
//
35+
// so it is okay for the return type to be different from the std case as long
36+
// as the above works.
37+
#[cfg(not(any(feature = "std", feature = "alloc")))]
38+
pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
39+
// Three unicode replacement characters if it fails. They look like a
40+
// white-on-black question mark. The user will recognize it as invalid
41+
// UTF-8.
42+
str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
43+
}
44+
}

serde/src/ser/impossible.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ser::{
1717
///
1818
/// ```edition2018
1919
/// # use serde::ser::{Serializer, Impossible};
20-
/// # use serde::private::ser::Error;
20+
/// # use serde::__private::ser::Error;
2121
/// #
2222
/// # struct MySerializer;
2323
/// #

serde/src/ser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ pub trait Serializer: Sized {
711711
///
712712
/// ```edition2018
713713
/// # use serde::ser::{Serializer, SerializeSeq};
714-
/// # use serde::private::ser::Error;
714+
/// # use serde::__private::ser::Error;
715715
/// #
716716
/// # struct MySerializer;
717717
/// #

0 commit comments

Comments
 (0)