Skip to content

Commit 1eee9f5

Browse files
committed
A much simpler version of write
1 parent 8362b30 commit 1eee9f5

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

library/core/src/fmt/mod.rs

+10-27
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,14 @@ pub trait Write {
201201
impl<W: Write + ?Sized> SpecWriteFmt for &mut W {
202202
#[inline]
203203
default fn spec_write_fmt(mut self, args: Arguments<'_>) -> Result {
204-
if let Some(s) = args.as_const_str() {
205-
self.write_str(s)
206-
} else {
207-
write(&mut self, args)
208-
}
204+
write(&mut self, args)
209205
}
210206
}
211207

212208
impl<W: Write> SpecWriteFmt for &mut W {
213209
#[inline]
214210
fn spec_write_fmt(self, args: Arguments<'_>) -> Result {
215-
if let Some(s) = args.as_const_str() {
216-
self.write_str(s)
217-
} else {
218-
write(self, args)
219-
}
211+
write(self, args)
220212
}
221213
}
222214

@@ -438,15 +430,6 @@ impl<'a> Arguments<'a> {
438430
_ => None,
439431
}
440432
}
441-
442-
/// Same as [`Arguments::as_str`], but will only return `Some(s)` if it can be determined at compile time.
443-
#[must_use]
444-
#[inline]
445-
fn as_const_str(&self) -> Option<&'static str> {
446-
let s = self.as_str();
447-
// SAFETY: both cases are valid as the result
448-
if unsafe { core::intrinsics::is_val_statically_known(s.is_some()) } { s } else { None }
449-
}
450433
}
451434

452435
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1119,8 +1102,14 @@ pub trait UpperExp {
11191102
/// ```
11201103
///
11211104
/// [`write!`]: crate::write!
1105+
#[inline]
11221106
#[stable(feature = "rust1", since = "1.0.0")]
11231107
pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
1108+
if let Some(s) = args.as_str() { output.write_str(s) } else { write_internal(output, args) }
1109+
}
1110+
1111+
/// Actual implementation of the [`write`], but without the simple string optimization.
1112+
fn write_internal(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11241113
let mut formatter = Formatter::new(output);
11251114
let mut idx = 0;
11261115

@@ -1599,9 +1588,8 @@ impl<'a> Formatter<'a> {
15991588
/// assert_eq!(format!("{:0>8}", Foo(2)), "Foo 2");
16001589
/// ```
16011590
#[stable(feature = "rust1", since = "1.0.0")]
1602-
#[inline]
16031591
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result {
1604-
if let Some(s) = fmt.as_const_str() { self.buf.write_str(s) } else { write(self.buf, fmt) }
1592+
write(self.buf, fmt)
16051593
}
16061594

16071595
/// Flags for formatting
@@ -2290,13 +2278,8 @@ impl Write for Formatter<'_> {
22902278
self.buf.write_char(c)
22912279
}
22922280

2293-
#[inline]
22942281
fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
2295-
if let Some(s) = args.as_const_str() {
2296-
self.buf.write_str(s)
2297-
} else {
2298-
write(self.buf, args)
2299-
}
2282+
write(self.buf, args)
23002283
}
23012284
}
23022285

library/core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@
176176
#![feature(ip)]
177177
#![feature(ip_bits)]
178178
#![feature(is_ascii_octdigit)]
179-
#![feature(is_val_statically_known)]
180179
#![feature(isqrt)]
181180
#![feature(maybe_uninit_uninit_array)]
182181
#![feature(non_null_convenience)]

0 commit comments

Comments
 (0)