Skip to content

Commit

Permalink
Move fmt module to __private module (#270)
Browse files Browse the repository at this point in the history
There were a whole bunch of exports and re-exports in the `fmt` module
of things that should not be public API of this crate, but instead are
implementation details. So this limits the amount of things we export
from that module, and it also moves the things we do export the
`__private` module.

Co-authored-by: Kai Ren <[email protected]>
  • Loading branch information
JelteF and tyranron authored Jul 5, 2023
1 parent ae5b263 commit bc732a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
10 changes: 5 additions & 5 deletions impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl<'a> Expansion<'a> {
let ident_str = self.ident.to_string();

let out = quote! {
&mut ::derive_more::fmt::debug_tuple(
&mut ::derive_more::__private::debug_tuple(
__derive_more_f,
#ident_str,
)
Expand All @@ -281,23 +281,23 @@ impl<'a> Expansion<'a> {
Ok::<_, Error>(out)
}
Some(FieldAttribute::Fmt(fmt)) => Ok(quote! {
::derive_more::fmt::DebugTuple::field(
::derive_more::__private::DebugTuple::field(
#out,
&::core::format_args!(#fmt),
)
}),
None => {
let ident = format_ident!("_{i}");
Ok(quote! {
::derive_more::fmt::DebugTuple::field(#out, #ident)
::derive_more::__private::DebugTuple::field(#out, #ident)
})
}
},
)?;
Ok(if exhaustive {
quote! { ::derive_more::fmt::DebugTuple::finish(#out) }
quote! { ::derive_more::__private::DebugTuple::finish(#out) }
} else {
quote! { ::derive_more::fmt::DebugTuple::finish_non_exhaustive(#out) }
quote! { ::derive_more::__private::DebugTuple::finish_non_exhaustive(#out) }
})
}
syn::Fields::Named(named) => {
Expand Down
17 changes: 10 additions & 7 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! [`core::fmt::DebugTuple`] reimplementation with
//! [`DebugTuple::finish_non_exhaustive()`] method.

pub use core::fmt::{Debug, Error, Formatter, Result, Write};
use core::fmt::{Debug, Formatter, Result, Write};

/// Same as [`core::fmt::DebugTuple`], but with
/// [`DebugTuple::finish_non_exhaustive()`] method.
Expand Down Expand Up @@ -34,13 +34,14 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
/// # Example
///
/// ```rust
/// use derive_more::fmt;
/// use core::fmt;
/// use derive_more::__private::debug_tuple;
///
/// struct Foo(i32, String);
///
/// impl fmt::Debug for Foo {
/// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
/// fmt::debug_tuple(fmt, "Foo")
/// debug_tuple(fmt, "Foo")
/// .field(&self.0) // We add the first field.
/// .field(&self.1) // We add the second field.
/// .finish() // We're good to go!
Expand Down Expand Up @@ -78,13 +79,14 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
/// # Example
///
/// ```
/// use derive_more::fmt;
/// use core::fmt;
/// use derive_more::__private::debug_tuple;
///
/// struct Foo(i32, String);
///
/// impl fmt::Debug for Foo {
/// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
/// fmt::debug_tuple(fmt, "Foo")
/// debug_tuple(fmt, "Foo")
/// .field(&self.0)
/// .field(&self.1)
/// .finish() // You need to call it to "finish" the
Expand Down Expand Up @@ -116,13 +118,14 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
/// # Example
///
/// ```rust
/// use derive_more::fmt;
/// use core::fmt;
/// use derive_more::__private::debug_tuple;
///
/// struct Bar(i32, f32);
///
/// impl fmt::Debug for Bar {
/// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
/// fmt::debug_tuple(fmt, "Bar")
/// debug_tuple(fmt, "Bar")
/// .field(&self.0)
/// .finish_non_exhaustive() // Show that some other field(s) exist.
/// }
Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(all(not(feature = "std"), feature = "error"), feature(error_in_core))]
// These links overwrite the ones in `README.md`
// to become proper intra-doc links in Rust docs.
//! [`From`]: crate::From
Expand Down Expand Up @@ -42,6 +40,8 @@
),
doc = include_str!("../README.md")
)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(all(not(feature = "std"), feature = "error"), feature(error_in_core))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(rustdoc::broken_intra_doc_links, rustdoc::private_intra_doc_links)]
#![forbid(non_ascii_idents, unsafe_code)]
Expand All @@ -61,7 +61,7 @@ mod try_unwrap;
pub use self::try_unwrap::TryUnwrapError;

#[cfg(feature = "debug")]
pub mod fmt;
mod fmt;

#[cfg(any(feature = "add", feature = "not"))]
pub mod ops;
Expand All @@ -76,14 +76,17 @@ mod vendor;

// Not public API.
#[doc(hidden)]
#[cfg(feature = "error")]
pub mod __private {
#[cfg(not(feature = "std"))]
#[cfg(all(feature = "error", not(feature = "std")))]
pub use ::core::error::Error;
#[cfg(feature = "std")]
#[cfg(all(feature = "error", feature = "std"))]
pub use ::std::error::Error;

#[cfg(feature = "error")]
pub use crate::vendor::thiserror::aserror::AsDynError;

#[cfg(feature = "debug")]
pub use crate::fmt::{debug_tuple, DebugTuple};
}

#[cfg(not(any(
Expand Down

0 comments on commit bc732a4

Please sign in to comment.