Rollup merge of #129017 - its-the-shrimp:core_fmt_from_fn, r=Noratrieb

Replace `std::fmt:FormatterFn` with `std::fmt::from_fn`

Modelled after the suggestion made in [this](https://github.com/rust-lang/rust/issues/117729#issuecomment-1837628559) comment, this should bring this functionality in line with the existing `array::from_fn` & `iter::from_fn`
This commit is contained in:
Matthias Krüger 2024-08-12 23:10:52 +02:00 committed by GitHub
commit 522d43673a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 7 deletions

View File

@ -581,7 +581,7 @@
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub use core::fmt::Error; pub use core::fmt::Error;
#[unstable(feature = "debug_closure_helpers", issue = "117729")] #[unstable(feature = "debug_closure_helpers", issue = "117729")]
pub use core::fmt::FormatterFn; pub use core::fmt::{from_fn, FromFn};
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub use core::fmt::{write, Arguments}; pub use core::fmt::{write, Arguments};
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]

View File

@ -1018,7 +1018,8 @@ fn is_pretty(&self) -> bool {
} }
} }
/// Implements [`fmt::Debug`] and [`fmt::Display`] using a function. /// Creates a type whose [`fmt::Debug`] and [`fmt::Display`] impls are provided with the function
/// `f`.
/// ///
/// # Examples /// # Examples
/// ///
@ -1030,17 +1031,25 @@ fn is_pretty(&self) -> bool {
/// assert_eq!(format!("{}", value), "a"); /// assert_eq!(format!("{}", value), "a");
/// assert_eq!(format!("{:?}", value), "'a'"); /// assert_eq!(format!("{:?}", value), "'a'");
/// ///
/// let wrapped = fmt::FormatterFn(|f| write!(f, "{value:?}")); /// let wrapped = fmt::from_fn(|f| write!(f, "{value:?}"));
/// assert_eq!(format!("{}", wrapped), "'a'"); /// assert_eq!(format!("{}", wrapped), "'a'");
/// assert_eq!(format!("{:?}", wrapped), "'a'"); /// assert_eq!(format!("{:?}", wrapped), "'a'");
/// ``` /// ```
#[unstable(feature = "debug_closure_helpers", issue = "117729")] #[unstable(feature = "debug_closure_helpers", issue = "117729")]
pub struct FormatterFn<F>(pub F) pub fn from_fn<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result>(f: F) -> FromFn<F> {
FromFn(f)
}
/// Implements [`fmt::Debug`] and [`fmt::Display`] using a function.
///
/// Created with [`from_fn`].
#[unstable(feature = "debug_closure_helpers", issue = "117729")]
pub struct FromFn<F>(F)
where where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result; F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result;
#[unstable(feature = "debug_closure_helpers", issue = "117729")] #[unstable(feature = "debug_closure_helpers", issue = "117729")]
impl<F> fmt::Debug for FormatterFn<F> impl<F> fmt::Debug for FromFn<F>
where where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result, F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
{ {
@ -1050,7 +1059,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
} }
#[unstable(feature = "debug_closure_helpers", issue = "117729")] #[unstable(feature = "debug_closure_helpers", issue = "117729")]
impl<F> fmt::Display for FormatterFn<F> impl<F> fmt::Display for FromFn<F>
where where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result, F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
{ {

View File

@ -34,7 +34,7 @@ pub enum Alignment {
} }
#[unstable(feature = "debug_closure_helpers", issue = "117729")] #[unstable(feature = "debug_closure_helpers", issue = "117729")]
pub use self::builders::FormatterFn; pub use self::builders::{from_fn, FromFn};
#[stable(feature = "debug_builders", since = "1.2.0")] #[stable(feature = "debug_builders", since = "1.2.0")]
pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple}; pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};