diff options
Diffstat (limited to 'src/libcore/fmt/mod.rs')
| -rw-r--r-- | src/libcore/fmt/mod.rs | 224 |
1 files changed, 112 insertions, 112 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 07dca9ad214..4c941e2dfe6 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -4,7 +4,7 @@ #![stable(feature = "rust1", since = "1.0.0")] -use crate::cell::{UnsafeCell, Cell, RefCell, Ref, RefMut}; +use crate::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell}; use crate::marker::PhantomData; use crate::mem; use crate::num::flt2dec; @@ -13,9 +13,9 @@ use crate::result; use crate::slice; use crate::str; +mod builders; mod float; mod num; -mod builders; #[stable(feature = "fmt_flags_align", since = "1.28.0")] /// Possible alignments returned by `Formatter::align` @@ -33,10 +33,9 @@ pub enum Alignment { } #[stable(feature = "debug_builders", since = "1.2.0")] -pub use self::builders::{DebugStruct, DebugTuple, DebugSet, DebugList, DebugMap}; +pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple}; -#[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] +#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] #[doc(hidden)] pub mod rt { pub mod v1; @@ -234,7 +233,7 @@ pub struct Formatter<'a> { width: Option<usize>, precision: Option<usize>, - buf: &'a mut (dyn Write+'a), + buf: &'a mut (dyn Write + 'a), curarg: slice::Iter<'a, ArgumentV1<'a>>, args: &'a [ArgumentV1<'a>], } @@ -260,8 +259,7 @@ struct Void { /// types, and then this struct is used to canonicalize arguments to one type. #[derive(Copy, Clone)] #[allow(missing_debug_implementations)] -#[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] +#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] #[doc(hidden)] pub struct ArgumentV1<'a> { value: &'a Void, @@ -275,21 +273,13 @@ impl<'a> ArgumentV1<'a> { } #[doc(hidden)] - #[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] - pub fn new<'b, T>(x: &'b T, - f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> { - unsafe { - ArgumentV1 { - formatter: mem::transmute(f), - value: mem::transmute(x) - } - } + #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] + pub fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> { + unsafe { ArgumentV1 { formatter: mem::transmute(f), value: mem::transmute(x) } } } #[doc(hidden)] - #[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] + #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] pub fn from_usize(x: &usize) -> ArgumentV1<'_> { ArgumentV1::new(x, ArgumentV1::show_usize) } @@ -305,21 +295,23 @@ impl<'a> ArgumentV1<'a> { // flags available in the v1 format of format_args #[derive(Copy, Clone)] -enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, DebugLowerHex, DebugUpperHex } +enum FlagV1 { + SignPlus, + SignMinus, + Alternate, + SignAwareZeroPad, + DebugLowerHex, + DebugUpperHex, +} impl<'a> Arguments<'a> { /// When using the format_args!() macro, this function is used to generate the /// Arguments structure. - #[doc(hidden)] #[inline] - #[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] - pub fn new_v1(pieces: &'a [&'a str], - args: &'a [ArgumentV1<'a>]) -> Arguments<'a> { - Arguments { - pieces, - fmt: None, - args, - } + #[doc(hidden)] + #[inline] + #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] + pub fn new_v1(pieces: &'a [&'a str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> { + Arguments { pieces, fmt: None, args } } /// This function is used to specify nonstandard formatting parameters. @@ -328,29 +320,26 @@ impl<'a> Arguments<'a> { /// `CountIsParam` or `CountIsNextParam` has to point to an argument /// created with `argumentusize`. However, failing to do so doesn't cause /// unsafety, but will ignore invalid . - #[doc(hidden)] #[inline] - #[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] - pub fn new_v1_formatted(pieces: &'a [&'a str], - args: &'a [ArgumentV1<'a>], - fmt: &'a [rt::v1::Argument]) -> Arguments<'a> { - Arguments { - pieces, - fmt: Some(fmt), - args, - } + #[doc(hidden)] + #[inline] + #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] + pub fn new_v1_formatted( + pieces: &'a [&'a str], + args: &'a [ArgumentV1<'a>], + fmt: &'a [rt::v1::Argument], + ) -> Arguments<'a> { + Arguments { pieces, fmt: Some(fmt), args } } /// Estimates the length of the formatted text. /// /// This is intended to be used for setting initial `String` capacity /// when using `format!`. Note: this is neither the lower nor upper bound. - #[doc(hidden)] #[inline] - #[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] + #[doc(hidden)] + #[inline] + #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] pub fn estimated_capacity(&self) -> usize { - let pieces_length: usize = self.pieces.iter() - .map(|x| x.len()).sum(); + let pieces_length: usize = self.pieces.iter().map(|x| x.len()).sum(); if self.args.is_empty() { pieces_length @@ -514,10 +503,13 @@ impl Display for Arguments<'_> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_on_unimplemented( - on(crate_local, label="`{Self}` cannot be formatted using `{{:?}}`", - note="add `#[derive(Debug)]` or manually implement `{Debug}`"), - message="`{Self}` doesn't implement `{Debug}`", - label="`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{Debug}`", + on( + crate_local, + label = "`{Self}` cannot be formatted using `{{:?}}`", + note = "add `#[derive(Debug)]` or manually implement `{Debug}`" + ), + message = "`{Self}` doesn't implement `{Debug}`", + label = "`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{Debug}`" )] #[doc(alias = "{:?}")] #[rustc_diagnostic_item = "debug_trait"] @@ -553,7 +545,9 @@ pub(crate) mod macros { #[rustc_builtin_macro] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[allow_internal_unstable(core_intrinsics)] - pub macro Debug($item:item) { /* compiler built-in */ } + pub macro Debug($item:item) { + /* compiler built-in */ + } } #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(inline)] @@ -594,14 +588,14 @@ pub use macros::Debug; /// ``` #[rustc_on_unimplemented( on( - _Self="std::path::Path", - label="`{Self}` cannot be formatted with the default formatter; call `.display()` on it", - note="call `.display()` or `.to_string_lossy()` to safely print paths, \ - as they may contain non-Unicode data" + _Self = "std::path::Path", + label = "`{Self}` cannot be formatted with the default formatter; call `.display()` on it", + note = "call `.display()` or `.to_string_lossy()` to safely print paths, \ + as they may contain non-Unicode data" ), - message="`{Self}` doesn't implement `{Display}`", - label="`{Self}` cannot be formatted with the default formatter", - note="in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead", + message = "`{Self}` doesn't implement `{Display}`", + label = "`{Self}` cannot be formatted with the default formatter", + note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead" )] #[doc(alias = "{}")] #[stable(feature = "rust1", since = "1.0.0")] @@ -1073,7 +1067,9 @@ impl PostPadding { impl<'a> Formatter<'a> { fn wrap_buf<'b, 'c, F>(&'b mut self, wrap: F) -> Formatter<'c> - where 'b: 'c, F: FnOnce(&'b mut (dyn Write+'b)) -> &'c mut (dyn Write+'c) + where + 'b: 'c, + F: FnOnce(&'b mut (dyn Write + 'b)) -> &'c mut (dyn Write + 'c), { Formatter { // We want to change this @@ -1106,7 +1102,7 @@ impl<'a> Formatter<'a> { // Extract the correct argument let value = match arg.position { - rt::v1::Position::Next => { *self.curarg.next().unwrap() } + rt::v1::Position::Next => *self.curarg.next().unwrap(), rt::v1::Position::At(i) => self.args[i], }; @@ -1118,12 +1114,8 @@ impl<'a> Formatter<'a> { match *cnt { rt::v1::Count::Is(n) => Some(n), rt::v1::Count::Implied => None, - rt::v1::Count::Param(i) => { - self.args[i].as_usize() - } - rt::v1::Count::NextParam => { - self.curarg.next()?.as_usize() - } + rt::v1::Count::Param(i) => self.args[i].as_usize(), + rt::v1::Count::NextParam => self.curarg.next()?.as_usize(), } } @@ -1174,18 +1166,16 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:0>#8}", Foo::new(-1)), "00-Foo 1"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn pad_integral(&mut self, - is_nonnegative: bool, - prefix: &str, - buf: &str) - -> Result { + pub fn pad_integral(&mut self, is_nonnegative: bool, prefix: &str, buf: &str) -> Result { let mut width = buf.len(); let mut sign = None; if !is_nonnegative { - sign = Some('-'); width += 1; + sign = Some('-'); + width += 1; } else if self.sign_plus() { - sign = Some('+'); width += 1; + sign = Some('+'); + width += 1; } let prefix = if self.alternate() { @@ -1201,11 +1191,7 @@ impl<'a> Formatter<'a> { if let Some(c) = sign { f.buf.write_char(c)?; } - if let Some(prefix) = prefix { - f.buf.write_str(prefix) - } else { - Ok(()) - } + if let Some(prefix) = prefix { f.buf.write_str(prefix) } else { Ok(()) } } // The `width` field is more of a `min-width` parameter at this point. @@ -1301,9 +1287,7 @@ impl<'a> Formatter<'a> { None => self.buf.write_str(s), // If we're under the maximum width, check if we're over the minimum // width, if so it's as easy as just emitting the string. - Some(width) if s.chars().count() >= width => { - self.buf.write_str(s) - } + Some(width) if s.chars().count() >= width => self.buf.write_str(s), // If we're under both the maximum and the minimum width, then fill // up the minimum width with the specified string + some alignment. Some(width) => { @@ -1321,17 +1305,16 @@ impl<'a> Formatter<'a> { fn padding( &mut self, padding: usize, - default: rt::v1::Alignment + default: rt::v1::Alignment, ) -> result::Result<PostPadding, Error> { let align = match self.align { rt::v1::Alignment::Unknown => default, - _ => self.align + _ => self.align, }; let (pre_pad, post_pad) = match align { rt::v1::Alignment::Left => (0, padding), - rt::v1::Alignment::Right | - rt::v1::Alignment::Unknown => (padding, 0), + rt::v1::Alignment::Right | rt::v1::Alignment::Unknown => (padding, 0), rt::v1::Alignment::Center => (padding / 2, (padding + 1) / 2), }; @@ -1368,7 +1351,8 @@ impl<'a> Formatter<'a> { // remaining parts go through the ordinary padding process. let len = formatted.len(); - let ret = if width <= len { // no padding + let ret = if width <= len { + // no padding self.write_formatted_parts(&formatted) } else { let post_padding = self.padding(width - len, align)?; @@ -1473,10 +1457,14 @@ impl<'a> Formatter<'a> { /// Flags for formatting #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_deprecated(since = "1.24.0", - reason = "use the `sign_plus`, `sign_minus`, `alternate`, \ - or `sign_aware_zero_pad` methods instead")] - pub fn flags(&self) -> u32 { self.flags } + #[rustc_deprecated( + since = "1.24.0", + reason = "use the `sign_plus`, `sign_minus`, `alternate`, \ + or `sign_aware_zero_pad` methods instead" + )] + pub fn flags(&self) -> u32 { + self.flags + } /// Character used as 'fill' whenever there is alignment. /// @@ -1506,7 +1494,9 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{:t>6}", Foo), "tttttt"); /// ``` #[stable(feature = "fmt_flags", since = "1.5.0")] - pub fn fill(&self) -> char { self.fill } + pub fn fill(&self) -> char { + self.fill + } /// Flag indicating what form of alignment was requested. /// @@ -1574,7 +1564,9 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` #[stable(feature = "fmt_flags", since = "1.5.0")] - pub fn width(&self) -> Option<usize> { self.width } + pub fn width(&self) -> Option<usize> { + self.width + } /// Optionally specified precision for numeric types. /// @@ -1601,7 +1593,9 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{}", Foo(23.2)), "Foo(23.20)"); /// ``` #[stable(feature = "fmt_flags", since = "1.5.0")] - pub fn precision(&self) -> Option<usize> { self.precision } + pub fn precision(&self) -> Option<usize> { + self.precision + } /// Determines if the `+` flag was specified. /// @@ -1629,7 +1623,9 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` #[stable(feature = "fmt_flags", since = "1.5.0")] - pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 } + pub fn sign_plus(&self) -> bool { + self.flags & (1 << FlagV1::SignPlus as u32) != 0 + } /// Determines if the `-` flag was specified. /// @@ -1655,7 +1651,9 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)"); /// ``` #[stable(feature = "fmt_flags", since = "1.5.0")] - pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 } + pub fn sign_minus(&self) -> bool { + self.flags & (1 << FlagV1::SignMinus as u32) != 0 + } /// Determines if the `#` flag was specified. /// @@ -1680,7 +1678,9 @@ impl<'a> Formatter<'a> { /// assert_eq!(&format!("{}", Foo(23)), "23"); /// ``` #[stable(feature = "fmt_flags", since = "1.5.0")] - pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 } + pub fn alternate(&self) -> bool { + self.flags & (1 << FlagV1::Alternate as u32) != 0 + } /// Determines if the `0` flag was specified. /// @@ -1709,9 +1709,13 @@ impl<'a> Formatter<'a> { // FIXME: Decide what public API we want for these two flags. // https://github.com/rust-lang/rust/issues/48584 - fn debug_lower_hex(&self) -> bool { self.flags & (1 << FlagV1::DebugLowerHex as u32) != 0 } + fn debug_lower_hex(&self) -> bool { + self.flags & (1 << FlagV1::DebugLowerHex as u32) != 0 + } - fn debug_upper_hex(&self) -> bool { self.flags & (1 << FlagV1::DebugUpperHex as u32) != 0 } + fn debug_upper_hex(&self) -> bool { + self.flags & (1 << FlagV1::DebugUpperHex as u32) != 0 + } /// Creates a [`DebugStruct`] builder designed to assist with creation of /// [`fmt::Debug`] implementations for structs. @@ -2067,11 +2071,15 @@ impl<T: ?Sized> Pointer for &mut T { #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> Debug for *const T { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { Pointer::fmt(self, f) } + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + Pointer::fmt(self, f) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> Debug for *mut T { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { Pointer::fmt(self, f) } + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + Pointer::fmt(self, f) + } } macro_rules! peel { @@ -2129,9 +2137,7 @@ impl<T: ?Sized> Debug for PhantomData<T> { #[stable(feature = "rust1", since = "1.0.0")] impl<T: Copy + Debug> Debug for Cell<T> { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - f.debug_struct("Cell") - .field("value", &self.get()) - .finish() + f.debug_struct("Cell").field("value", &self.get()).finish() } } @@ -2139,11 +2145,7 @@ impl<T: Copy + Debug> Debug for Cell<T> { impl<T: ?Sized + Debug> Debug for RefCell<T> { fn fmt(&self, f: &mut Formatter<'_>) -> Result { match self.try_borrow() { - Ok(borrow) => { - f.debug_struct("RefCell") - .field("value", &borrow) - .finish() - } + Ok(borrow) => f.debug_struct("RefCell").field("value", &borrow).finish(), Err(_) => { // The RefCell is mutably borrowed so we can't look at its value // here. Show a placeholder instead. @@ -2155,9 +2157,7 @@ impl<T: ?Sized + Debug> Debug for RefCell<T> { } } - f.debug_struct("RefCell") - .field("value", &BorrowedPlaceholder) - .finish() + f.debug_struct("RefCell").field("value", &BorrowedPlaceholder).finish() } } } |
