From 1eee9f580724fe73ac96248b2d9f10e5a9911744 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Thu, 15 Feb 2024 17:43:48 -0500 Subject: [PATCH] A much simpler version of write --- library/core/src/fmt/mod.rs | 37 ++++++++++--------------------------- library/core/src/lib.rs | 1 - 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 47ebf83505337..17e58eaac8846 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -201,22 +201,14 @@ pub trait Write { impl SpecWriteFmt for &mut W { #[inline] default fn spec_write_fmt(mut self, args: Arguments<'_>) -> Result { - if let Some(s) = args.as_const_str() { - self.write_str(s) - } else { - write(&mut self, args) - } + write(&mut self, args) } } impl SpecWriteFmt for &mut W { #[inline] fn spec_write_fmt(self, args: Arguments<'_>) -> Result { - if let Some(s) = args.as_const_str() { - self.write_str(s) - } else { - write(self, args) - } + write(self, args) } } @@ -438,15 +430,6 @@ impl<'a> Arguments<'a> { _ => None, } } - - /// Same as [`Arguments::as_str`], but will only return `Some(s)` if it can be determined at compile time. - #[must_use] - #[inline] - fn as_const_str(&self) -> Option<&'static str> { - let s = self.as_str(); - // SAFETY: both cases are valid as the result - if unsafe { core::intrinsics::is_val_statically_known(s.is_some()) } { s } else { None } - } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1119,8 +1102,14 @@ pub trait UpperExp { /// ``` /// /// [`write!`]: crate::write! +#[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result { + if let Some(s) = args.as_str() { output.write_str(s) } else { write_internal(output, args) } +} + +/// Actual implementation of the [`write`], but without the simple string optimization. +fn write_internal(output: &mut dyn Write, args: Arguments<'_>) -> Result { let mut formatter = Formatter::new(output); let mut idx = 0; @@ -1599,9 +1588,8 @@ impl<'a> Formatter<'a> { /// assert_eq!(format!("{:0>8}", Foo(2)), "Foo 2"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[inline] pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result { - if let Some(s) = fmt.as_const_str() { self.buf.write_str(s) } else { write(self.buf, fmt) } + write(self.buf, fmt) } /// Flags for formatting @@ -2290,13 +2278,8 @@ impl Write for Formatter<'_> { self.buf.write_char(c) } - #[inline] fn write_fmt(&mut self, args: Arguments<'_>) -> Result { - if let Some(s) = args.as_const_str() { - self.buf.write_str(s) - } else { - write(self.buf, args) - } + write(self.buf, args) } } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index ffaf8af0b11d2..b54680a61b4d8 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -176,7 +176,6 @@ #![feature(ip)] #![feature(ip_bits)] #![feature(is_ascii_octdigit)] -#![feature(is_val_statically_known)] #![feature(isqrt)] #![feature(maybe_uninit_uninit_array)] #![feature(non_null_convenience)]