diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:04 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:47 -0500 |
| commit | a06baa56b95674fc626b3c3fd680d6a65357fe60 (patch) | |
| tree | cd9d867c2ca3cff5c1d6b3bd73377c44649fb075 /src/libcore/fmt | |
| parent | 8eb7c58dbb7b32701af113bc58722d0d1fefb1eb (diff) | |
| download | rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.tar.gz rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.zip | |
Format the world
Diffstat (limited to 'src/libcore/fmt')
| -rw-r--r-- | src/libcore/fmt/builders.rs | 99 | ||||
| -rw-r--r-- | src/libcore/fmt/num.rs | 45 |
2 files changed, 55 insertions, 89 deletions
diff --git a/src/libcore/fmt/builders.rs b/src/libcore/fmt/builders.rs index e6e3454b36f..626eb1e862d 100644 --- a/src/libcore/fmt/builders.rs +++ b/src/libcore/fmt/builders.rs @@ -11,21 +11,18 @@ struct PadAdapterState { impl Default for PadAdapterState { fn default() -> Self { - PadAdapterState { - on_newline: true, - } + PadAdapterState { on_newline: true } } } impl<'buf, 'state> PadAdapter<'buf, 'state> { - fn wrap<'slot, 'fmt: 'buf+'slot>(fmt: &'fmt mut fmt::Formatter<'_>, - slot: &'slot mut Option<Self>, - state: &'state mut PadAdapterState) -> fmt::Formatter<'slot> { + fn wrap<'slot, 'fmt: 'buf + 'slot>( + fmt: &'fmt mut fmt::Formatter<'_>, + slot: &'slot mut Option<Self>, + state: &'state mut PadAdapterState, + ) -> fmt::Formatter<'slot> { fmt.wrap_buf(move |buf| { - *slot = Some(PadAdapter { - buf, - state, - }); + *slot = Some(PadAdapter { buf, state }); slot.as_mut().unwrap() }) } @@ -98,15 +95,12 @@ pub struct DebugStruct<'a, 'b: 'a> { has_fields: bool, } -pub(super) fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, - name: &str) - -> DebugStruct<'a, 'b> { +pub(super) fn debug_struct_new<'a, 'b>( + fmt: &'a mut fmt::Formatter<'b>, + name: &str, +) -> DebugStruct<'a, 'b> { let result = fmt.write_str(name); - DebugStruct { - fmt, - result, - has_fields: false, - } + DebugStruct { fmt, result, has_fields: false } } impl<'a, 'b: 'a> DebugStruct<'a, 'b> { @@ -196,11 +190,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> { pub fn finish(&mut self) -> fmt::Result { if self.has_fields { self.result = self.result.and_then(|_| { - if self.is_pretty() { - self.fmt.write_str("}") - } else { - self.fmt.write_str(" }") - } + if self.is_pretty() { self.fmt.write_str("}") } else { self.fmt.write_str(" }") } }); } self.result @@ -256,12 +246,7 @@ pub(super) fn debug_tuple_new<'a, 'b>( name: &str, ) -> DebugTuple<'a, 'b> { let result = fmt.write_str(name); - DebugTuple { - fmt, - result, - fields: 0, - empty_name: name.is_empty(), - } + DebugTuple { fmt, result, fields: 0, empty_name: name.is_empty() } } impl<'a, 'b: 'a> DebugTuple<'a, 'b> { @@ -423,13 +408,7 @@ pub struct DebugSet<'a, 'b: 'a> { pub(super) fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> { let result = fmt.write_str("{"); - DebugSet { - inner: DebugInner { - fmt, - result, - has_fields: false, - }, - } + DebugSet { inner: DebugInner { fmt, result, has_fields: false } } } impl<'a, 'b: 'a> DebugSet<'a, 'b> { @@ -487,8 +466,9 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> { /// ``` #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b> - where D: fmt::Debug, - I: IntoIterator<Item = D> + where + D: fmt::Debug, + I: IntoIterator<Item = D>, { for entry in entries { self.entry(&entry); @@ -560,13 +540,7 @@ pub struct DebugList<'a, 'b: 'a> { pub(super) fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> { let result = fmt.write_str("["); - DebugList { - inner: DebugInner { - fmt, - result, - has_fields: false, - }, - } + DebugList { inner: DebugInner { fmt, result, has_fields: false } } } impl<'a, 'b: 'a> DebugList<'a, 'b> { @@ -624,8 +598,9 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> { /// ``` #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b> - where D: fmt::Debug, - I: IntoIterator<Item = D> + where + D: fmt::Debug, + I: IntoIterator<Item = D>, { for entry in entries { self.entry(&entry); @@ -702,13 +677,7 @@ pub struct DebugMap<'a, 'b: 'a> { pub(super) fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> { let result = fmt.write_str("{"); - DebugMap { - fmt, - result, - has_fields: false, - has_key: false, - state: Default::default(), - } + DebugMap { fmt, result, has_fields: false, has_key: false, state: Default::default() } } impl<'a, 'b: 'a> DebugMap<'a, 'b> { @@ -771,13 +740,14 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> { /// "{\"whole\": [(\"A\", 10), (\"B\", 11)]}", /// ); /// ``` - #[unstable(feature = "debug_map_key_value", - reason = "recently added", - issue = "62482")] + #[unstable(feature = "debug_map_key_value", reason = "recently added", issue = "62482")] pub fn key(&mut self, key: &dyn fmt::Debug) -> &mut DebugMap<'a, 'b> { self.result = self.result.and_then(|_| { - assert!(!self.has_key, "attempted to begin a new map entry \ - without completing the previous one"); + assert!( + !self.has_key, + "attempted to begin a new map entry \ + without completing the previous one" + ); if self.is_pretty() { if !self.has_fields { @@ -835,9 +805,7 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> { /// "{\"whole\": [(\"A\", 10), (\"B\", 11)]}", /// ); /// ``` - #[unstable(feature = "debug_map_key_value", - reason = "recently added", - issue = "62482")] + #[unstable(feature = "debug_map_key_value", reason = "recently added", issue = "62482")] pub fn value(&mut self, value: &dyn fmt::Debug) -> &mut DebugMap<'a, 'b> { self.result = self.result.and_then(|_| { assert!(self.has_key, "attempted to format a map value before its key"); @@ -885,9 +853,10 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> { /// ``` #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b> - where K: fmt::Debug, - V: fmt::Debug, - I: IntoIterator<Item = (K, V)> + where + K: fmt::Debug, + V: fmt::Debug, + I: IntoIterator<Item = (K, V)>, { for (k, v) in entries { self.entry(&k, &v); diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index 3c7aefc090f..7b20677ffb5 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -2,17 +2,17 @@ // ignore-tidy-undocumented-unsafe - use crate::fmt; +use crate::mem::MaybeUninit; use crate::ops::{Div, Rem, Sub}; -use crate::str; -use crate::slice; use crate::ptr; -use crate::mem::MaybeUninit; +use crate::slice; +use crate::str; #[doc(hidden)] -trait Int: PartialEq + PartialOrd + Div<Output=Self> + Rem<Output=Self> + - Sub<Output=Self> + Copy { +trait Int: + PartialEq + PartialOrd + Div<Output = Self> + Rem<Output = Self> + Sub<Output = Self> + Copy +{ fn zero() -> Self; fn from_u8(u: u8) -> Self; fn to_u8(&self) -> u8; @@ -60,33 +60,32 @@ trait GenericRadix { // Accumulate each digit of the number from the least significant // to the most significant figure. for byte in buf.iter_mut().rev() { - let n = x % base; // Get the current place value. - x = x / base; // Deaccumulate the number. + let n = x % base; // Get the current place value. + x = x / base; // Deaccumulate the number. byte.write(Self::digit(n.to_u8())); // Store the digit in the buffer. curr -= 1; if x == zero { // No more digits left to accumulate. - break + break; }; } } else { // Do the same as above, but accounting for two's complement. for byte in buf.iter_mut().rev() { - let n = zero - (x % base); // Get the current place value. - x = x / base; // Deaccumulate the number. + let n = zero - (x % base); // Get the current place value. + x = x / base; // Deaccumulate the number. byte.write(Self::digit(n.to_u8())); // Store the digit in the buffer. curr -= 1; if x == zero { // No more digits left to accumulate. - break + break; }; } } let buf = &buf[curr..]; - let buf = unsafe { str::from_utf8_unchecked(slice::from_raw_parts( - MaybeUninit::first_ptr(buf), - buf.len() - )) }; + let buf = unsafe { + str::from_utf8_unchecked(slice::from_raw_parts(MaybeUninit::first_ptr(buf), buf.len())) + }; f.pad_integral(is_nonnegative, Self::PREFIX, buf) } } @@ -125,9 +124,9 @@ macro_rules! radix { radix! { Binary, 2, "0b", x @ 0 ..= 1 => b'0' + x } radix! { Octal, 8, "0o", x @ 0 ..= 7 => b'0' + x } radix! { LowerHex, 16, "0x", x @ 0 ..= 9 => b'0' + x, - x @ 10 ..= 15 => b'a' + (x - 10) } +x @ 10 ..= 15 => b'a' + (x - 10) } radix! { UpperHex, 16, "0x", x @ 0 ..= 9 => b'0' + x, - x @ 10 ..= 15 => b'A' + (x - 10) } +x @ 10 ..= 15 => b'A' + (x - 10) } macro_rules! int_base { ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => { @@ -137,7 +136,7 @@ macro_rules! int_base { $Radix.fmt_int(*self as $U, f) } } - } + }; } macro_rules! debug { @@ -155,7 +154,7 @@ macro_rules! debug { } } } - } + }; } macro_rules! integer { @@ -171,7 +170,7 @@ macro_rules! integer { int_base! { LowerHex for $Uint as $Uint -> LowerHex } int_base! { UpperHex for $Uint as $Uint -> UpperHex } debug! { $Uint } - } + }; } integer! { isize, usize } integer! { i8, u8 } @@ -180,9 +179,7 @@ integer! { i32, u32 } integer! { i64, u64 } integer! { i128, u128 } - -static DEC_DIGITS_LUT: &[u8; 200] = - b"0001020304050607080910111213141516171819\ +static DEC_DIGITS_LUT: &[u8; 200] = b"0001020304050607080910111213141516171819\ 2021222324252627282930313233343536373839\ 4041424344454647484950515253545556575859\ 6061626364656667686970717273747576777879\ |
