Skip to content

Commit

Permalink
Make it clearer that the private implementation details are private
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dtolnay committed Jan 9, 2021
1 parent 91bfa8f commit dd1f4b4
Show file tree
Hide file tree
Showing 25 changed files with 1,832 additions and 1,704 deletions.
4 changes: 2 additions & 2 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ impl<'de> Deserialize<'de> for Duration {
b"secs" => Ok(Field::Secs),
b"nanos" => Ok(Field::Nanos),
_ => {
let value = ::export::from_utf8_lossy(value);
let value = ::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&value, FIELDS))
}
}
Expand Down Expand Up @@ -2214,7 +2214,7 @@ mod range {
b"start" => Ok(Field::Start),
b"end" => Ok(Field::End),
_ => {
let value = ::export::from_utf8_lossy(value);
let value = ::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&value, FIELDS))
}
}
Expand Down
39 changes: 0 additions & 39 deletions serde/src/export.rs

This file was deleted.

2 changes: 1 addition & 1 deletion serde/src/integer128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// bother with this macro and may assume support for 128-bit integers.
///
/// ```edition2018
/// # use serde::private::ser::Error;
/// # use serde::__private::ser::Error;
/// #
/// # struct MySerializer;
/// #
Expand Down
11 changes: 6 additions & 5 deletions serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,14 @@ pub use de::{Deserialize, Deserializer};
#[doc(inline)]
pub use ser::{Serialize, Serializer};

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

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

#[cfg(not(feature = "std"))]
mod std_error;
Expand Down
2 changes: 1 addition & 1 deletion serde/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ macro_rules! forward_to_deserialize_any {
macro_rules! forward_to_deserialize_any_method {
($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => {
#[inline]
fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::export::Result<$v::Value, Self::Error>
fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::__private::Result<$v::Value, Self::Error>
where
$v: $crate::de::Visitor<$l>,
{
Expand Down
2 changes: 1 addition & 1 deletion serde/src/private/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! __serialize_unimplemented {
#[macro_export]
macro_rules! __serialize_unimplemented_method {
($func:ident $(<$t:ident>)* ($($arg:ty),*) -> $ret:ident) => {
fn $func $(<$t: ?Sized + $crate::Serialize>)* (self $(, _: $arg)*) -> $crate::export::Result<Self::$ret, Self::Error> {
fn $func $(<$t: ?Sized + $crate::Serialize>)* (self $(, _: $arg)*) -> $crate::__private::Result<Self::$ret, Self::Error> {
unimplemented!()
}
};
Expand Down
40 changes: 40 additions & 0 deletions serde/src/private/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,43 @@ mod macros;

pub mod de;
pub mod ser;

pub use lib::clone::Clone;
pub use lib::convert::{From, Into};
pub use lib::default::Default;
pub use lib::fmt::{self, Formatter};
pub use lib::marker::PhantomData;
pub use lib::option::Option::{self, None, Some};
pub use lib::result::Result::{self, Err, Ok};

pub use self::string::from_utf8_lossy;

#[cfg(any(feature = "alloc", feature = "std"))]
pub use lib::{ToString, Vec};

#[cfg(core_try_from)]
pub use lib::convert::TryFrom;

mod string {
use lib::*;

#[cfg(any(feature = "std", feature = "alloc"))]
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
String::from_utf8_lossy(bytes)
}

// The generated code calls this like:
//
// let value = &_serde::__private::from_utf8_lossy(bytes);
// Err(_serde::de::Error::unknown_variant(value, VARIANTS))
//
// so it is okay for the return type to be different from the std case as long
// as the above works.
#[cfg(not(any(feature = "std", feature = "alloc")))]
pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
// Three unicode replacement characters if it fails. They look like a
// white-on-black question mark. The user will recognize it as invalid
// UTF-8.
str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
}
}
2 changes: 1 addition & 1 deletion serde/src/ser/impossible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ser::{
///
/// ```edition2018
/// # use serde::ser::{Serializer, Impossible};
/// # use serde::private::ser::Error;
/// # use serde::__private::ser::Error;
/// #
/// # struct MySerializer;
/// #
Expand Down
2 changes: 1 addition & 1 deletion serde/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ pub trait Serializer: Sized {
///
/// ```edition2018
/// # use serde::ser::{Serializer, SerializeSeq};
/// # use serde::private::ser::Error;
/// # use serde::__private::ser::Error;
/// #
/// # struct MySerializer;
/// #
Expand Down
Loading

0 comments on commit dd1f4b4

Please sign in to comment.