core/fmt/
mod.rs

1//! Utilities for formatting and printing strings.
2
3#![stable(feature = "rust1", since = "1.0.0")]
4
5#[cfg(not(feature = "ferrocene_certified"))]
6use crate::cell::{Cell, Ref, RefCell, RefMut, SyncUnsafeCell, UnsafeCell};
7#[cfg(not(feature = "ferrocene_certified"))]
8use crate::char::{EscapeDebugExtArgs, MAX_LEN_UTF8};
9#[cfg(not(feature = "ferrocene_certified"))]
10use crate::hint::assert_unchecked;
11#[cfg(not(feature = "ferrocene_certified"))]
12use crate::marker::{PhantomData, PointeeSized};
13#[cfg(not(feature = "ferrocene_certified"))]
14use crate::num::fmt as numfmt;
15#[cfg(not(feature = "ferrocene_certified"))]
16use crate::ops::Deref;
17#[cfg(not(feature = "ferrocene_certified"))]
18use crate::ptr::NonNull;
19#[cfg(not(feature = "ferrocene_certified"))]
20use crate::{iter, mem, result, str};
21
22#[cfg(not(feature = "ferrocene_certified"))]
23mod builders;
24#[cfg(not(no_fp_fmt_parse))]
25#[cfg(not(feature = "ferrocene_certified"))]
26mod float;
27#[cfg(no_fp_fmt_parse)]
28#[cfg(not(feature = "ferrocene_certified"))]
29mod nofloat;
30#[cfg(not(feature = "ferrocene_certified"))]
31mod num;
32#[cfg(not(feature = "ferrocene_certified"))]
33mod num_buffer;
34#[cfg(not(feature = "ferrocene_certified"))]
35mod rt;
36
37#[stable(feature = "fmt_flags_align", since = "1.28.0")]
38#[rustc_diagnostic_item = "Alignment"]
39/// Possible alignments returned by `Formatter::align`
40#[derive(Copy, Clone, Debug, PartialEq, Eq)]
41#[cfg(not(feature = "ferrocene_certified"))]
42pub enum Alignment {
43    #[stable(feature = "fmt_flags_align", since = "1.28.0")]
44    /// Indication that contents should be left-aligned.
45    Left,
46    #[stable(feature = "fmt_flags_align", since = "1.28.0")]
47    /// Indication that contents should be right-aligned.
48    Right,
49    #[stable(feature = "fmt_flags_align", since = "1.28.0")]
50    /// Indication that contents should be center-aligned.
51    Center,
52}
53
54#[unstable(feature = "int_format_into", issue = "138215")]
55#[cfg(not(feature = "ferrocene_certified"))]
56pub use num_buffer::{NumBuffer, NumBufferTrait};
57
58#[stable(feature = "debug_builders", since = "1.2.0")]
59#[cfg(not(feature = "ferrocene_certified"))]
60pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};
61#[cfg(not(feature = "ferrocene_certified"))]
62#[stable(feature = "fmt_from_fn", since = "CURRENT_RUSTC_VERSION")]
63pub use self::builders::{FromFn, from_fn};
64
65/// The type returned by formatter methods.
66///
67/// # Examples
68///
69/// ```
70/// use std::fmt;
71///
72/// #[derive(Debug)]
73/// struct Triangle {
74///     a: f32,
75///     b: f32,
76///     c: f32
77/// }
78///
79/// impl fmt::Display for Triangle {
80///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81///         write!(f, "({}, {}, {})", self.a, self.b, self.c)
82///     }
83/// }
84///
85/// let pythagorean_triple = Triangle { a: 3.0, b: 4.0, c: 5.0 };
86///
87/// assert_eq!(format!("{pythagorean_triple}"), "(3, 4, 5)");
88/// ```
89#[stable(feature = "rust1", since = "1.0.0")]
90#[cfg(not(feature = "ferrocene_certified"))]
91pub type Result = result::Result<(), Error>;
92
93/// The error type which is returned from formatting a message into a stream.
94///
95/// This type does not support transmission of an error other than that an error
96/// occurred. This is because, despite the existence of this error,
97/// string formatting is considered an infallible operation.
98/// `fmt()` implementors should not return this `Error` unless they received it from their
99/// [`Formatter`]. The only time your code should create a new instance of this
100/// error is when implementing `fmt::Write`, in order to cancel the formatting operation when
101/// writing to the underlying stream fails.
102///
103/// Any extra information must be arranged to be transmitted through some other means,
104/// such as storing it in a field to be consulted after the formatting operation has been
105/// cancelled. (For example, this is how [`std::io::Write::write_fmt()`] propagates IO errors
106/// during writing.)
107///
108/// This type, `fmt::Error`, should not be
109/// confused with [`std::io::Error`] or [`std::error::Error`], which you may also
110/// have in scope.
111///
112/// [`std::io::Error`]: ../../std/io/struct.Error.html
113/// [`std::io::Write::write_fmt()`]: ../../std/io/trait.Write.html#method.write_fmt
114/// [`std::error::Error`]: ../../std/error/trait.Error.html
115///
116/// # Examples
117///
118/// ```rust
119/// use std::fmt::{self, write};
120///
121/// let mut output = String::new();
122/// if let Err(fmt::Error) = write(&mut output, format_args!("Hello {}!", "world")) {
123///     panic!("An error occurred");
124/// }
125/// ```
126#[stable(feature = "rust1", since = "1.0.0")]
127#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
128#[cfg(not(feature = "ferrocene_certified"))]
129pub struct Error;
130
131/// A trait for writing or formatting into Unicode-accepting buffers or streams.
132///
133/// This trait only accepts UTF-8–encoded data and is not [flushable]. If you only
134/// want to accept Unicode and you don't need flushing, you should implement this trait;
135/// otherwise you should implement [`std::io::Write`].
136///
137/// [`std::io::Write`]: ../../std/io/trait.Write.html
138/// [flushable]: ../../std/io/trait.Write.html#tymethod.flush
139#[stable(feature = "rust1", since = "1.0.0")]
140#[rustc_diagnostic_item = "FmtWrite"]
141#[cfg(not(feature = "ferrocene_certified"))]
142pub trait Write {
143    /// Writes a string slice into this writer, returning whether the write
144    /// succeeded.
145    ///
146    /// This method can only succeed if the entire string slice was successfully
147    /// written, and this method will not return until all data has been
148    /// written or an error occurs.
149    ///
150    /// # Errors
151    ///
152    /// This function will return an instance of [`std::fmt::Error`][Error] on error.
153    ///
154    /// The purpose of that error is to abort the formatting operation when the underlying
155    /// destination encounters some error preventing it from accepting more text;
156    /// in particular, it does not communicate any information about *what* error occurred.
157    /// It should generally be propagated rather than handled, at least when implementing
158    /// formatting traits.
159    ///
160    /// # Examples
161    ///
162    /// ```
163    /// use std::fmt::{Error, Write};
164    ///
165    /// fn writer<W: Write>(f: &mut W, s: &str) -> Result<(), Error> {
166    ///     f.write_str(s)
167    /// }
168    ///
169    /// let mut buf = String::new();
170    /// writer(&mut buf, "hola")?;
171    /// assert_eq!(&buf, "hola");
172    /// # std::fmt::Result::Ok(())
173    /// ```
174    #[stable(feature = "rust1", since = "1.0.0")]
175    fn write_str(&mut self, s: &str) -> Result;
176
177    /// Writes a [`char`] into this writer, returning whether the write succeeded.
178    ///
179    /// A single [`char`] may be encoded as more than one byte.
180    /// This method can only succeed if the entire byte sequence was successfully
181    /// written, and this method will not return until all data has been
182    /// written or an error occurs.
183    ///
184    /// # Errors
185    ///
186    /// This function will return an instance of [`Error`] on error.
187    ///
188    /// # Examples
189    ///
190    /// ```
191    /// use std::fmt::{Error, Write};
192    ///
193    /// fn writer<W: Write>(f: &mut W, c: char) -> Result<(), Error> {
194    ///     f.write_char(c)
195    /// }
196    ///
197    /// let mut buf = String::new();
198    /// writer(&mut buf, 'a')?;
199    /// writer(&mut buf, 'b')?;
200    /// assert_eq!(&buf, "ab");
201    /// # std::fmt::Result::Ok(())
202    /// ```
203    #[stable(feature = "fmt_write_char", since = "1.1.0")]
204    fn write_char(&mut self, c: char) -> Result {
205        self.write_str(c.encode_utf8(&mut [0; MAX_LEN_UTF8]))
206    }
207
208    /// Glue for usage of the [`write!`] macro with implementors of this trait.
209    ///
210    /// This method should generally not be invoked manually, but rather through
211    /// the [`write!`] macro itself.
212    ///
213    /// # Errors
214    ///
215    /// This function will return an instance of [`Error`] on error. Please see
216    /// [write_str](Write::write_str) for details.
217    ///
218    /// # Examples
219    ///
220    /// ```
221    /// use std::fmt::{Error, Write};
222    ///
223    /// fn writer<W: Write>(f: &mut W, s: &str) -> Result<(), Error> {
224    ///     f.write_fmt(format_args!("{s}"))
225    /// }
226    ///
227    /// let mut buf = String::new();
228    /// writer(&mut buf, "world")?;
229    /// assert_eq!(&buf, "world");
230    /// # std::fmt::Result::Ok(())
231    /// ```
232    #[stable(feature = "rust1", since = "1.0.0")]
233    fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
234        // We use a specialization for `Sized` types to avoid an indirection
235        // through `&mut self`
236        trait SpecWriteFmt {
237            fn spec_write_fmt(self, args: Arguments<'_>) -> Result;
238        }
239
240        impl<W: Write + ?Sized> SpecWriteFmt for &mut W {
241            #[inline]
242            default fn spec_write_fmt(mut self, args: Arguments<'_>) -> Result {
243                if let Some(s) = args.as_statically_known_str() {
244                    self.write_str(s)
245                } else {
246                    write(&mut self, args)
247                }
248            }
249        }
250
251        impl<W: Write> SpecWriteFmt for &mut W {
252            #[inline]
253            fn spec_write_fmt(self, args: Arguments<'_>) -> Result {
254                if let Some(s) = args.as_statically_known_str() {
255                    self.write_str(s)
256                } else {
257                    write(self, args)
258                }
259            }
260        }
261
262        self.spec_write_fmt(args)
263    }
264}
265
266#[stable(feature = "fmt_write_blanket_impl", since = "1.4.0")]
267#[cfg(not(feature = "ferrocene_certified"))]
268impl<W: Write + ?Sized> Write for &mut W {
269    fn write_str(&mut self, s: &str) -> Result {
270        (**self).write_str(s)
271    }
272
273    fn write_char(&mut self, c: char) -> Result {
274        (**self).write_char(c)
275    }
276
277    fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
278        (**self).write_fmt(args)
279    }
280}
281
282/// The signedness of a [`Formatter`] (or of a [`FormattingOptions`]).
283#[derive(Copy, Clone, Debug, PartialEq, Eq)]
284#[unstable(feature = "formatting_options", issue = "118117")]
285#[cfg(not(feature = "ferrocene_certified"))]
286pub enum Sign {
287    /// Represents the `+` flag.
288    Plus,
289    /// Represents the `-` flag.
290    Minus,
291}
292
293/// Specifies whether the [`Debug`] trait should use lower-/upper-case
294/// hexadecimal or normal integers.
295#[derive(Copy, Clone, Debug, PartialEq, Eq)]
296#[unstable(feature = "formatting_options", issue = "118117")]
297#[cfg(not(feature = "ferrocene_certified"))]
298pub enum DebugAsHex {
299    /// Use lower-case hexadecimal integers for the `Debug` trait (like [the `x?` type](../../std/fmt/index.html#formatting-traits)).
300    Lower,
301    /// Use upper-case hexadecimal integers for the `Debug` trait (like [the `X?` type](../../std/fmt/index.html#formatting-traits)).
302    Upper,
303}
304
305/// Options for formatting.
306///
307/// `FormattingOptions` is a [`Formatter`] without an attached [`Write`] trait.
308/// It is mainly used to construct `Formatter` instances.
309#[derive(Copy, Clone, Debug, PartialEq, Eq)]
310#[unstable(feature = "formatting_options", issue = "118117")]
311#[cfg(not(feature = "ferrocene_certified"))]
312pub struct FormattingOptions {
313    /// Flags, with the following bit fields:
314    ///
315    /// ```text
316    ///   31  30  29  28  27  26  25  24  23  22  21  20                              0
317    /// ┌───┬───────┬───┬───┬───┬───┬───┬───┬───┬───┬──────────────────────────────────┐
318    /// │ 0 │ align │ p │ w │ X?│ x?│'0'│ # │ - │ + │               fill               │
319    /// └───┴───────┴───┴───┴───┴───┴───┴───┴───┴───┴──────────────────────────────────┘
320    ///   │     │     │   │  └─┬───────────────────┘ └─┬──────────────────────────────┘
321    ///   │     │     │   │    │                       └─ The fill character (21 bits char).
322    ///   │     │     │   │    └─ The debug upper/lower hex, zero pad, alternate, and plus/minus flags.
323    ///   │     │     │   └─ Whether a width is set. (The value is stored separately.)
324    ///   │     │     └─ Whether a precision is set. (The value is stored separately.)
325    ///   │     ├─ 0: Align left. (<)
326    ///   │     ├─ 1: Align right. (>)
327    ///   │     ├─ 2: Align center. (^)
328    ///   │     └─ 3: Alignment not set. (default)
329    ///   └─ Always zero.
330    /// ```
331    // Note: This could use a pattern type with range 0x0000_0000..=0x7dd0ffff.
332    // It's unclear if that's useful, though.
333    flags: u32,
334    /// Width if width flag (bit 27) above is set. Otherwise, always 0.
335    width: u16,
336    /// Precision if precision flag (bit 28) above is set. Otherwise, always 0.
337    precision: u16,
338}
339
340// This needs to match with compiler/rustc_ast_lowering/src/format.rs.
341#[cfg(not(feature = "ferrocene_certified"))]
342mod flags {
343    pub(super) const SIGN_PLUS_FLAG: u32 = 1 << 21;
344    pub(super) const SIGN_MINUS_FLAG: u32 = 1 << 22;
345    pub(super) const ALTERNATE_FLAG: u32 = 1 << 23;
346    pub(super) const SIGN_AWARE_ZERO_PAD_FLAG: u32 = 1 << 24;
347    pub(super) const DEBUG_LOWER_HEX_FLAG: u32 = 1 << 25;
348    pub(super) const DEBUG_UPPER_HEX_FLAG: u32 = 1 << 26;
349    pub(super) const WIDTH_FLAG: u32 = 1 << 27;
350    pub(super) const PRECISION_FLAG: u32 = 1 << 28;
351    pub(super) const ALIGN_BITS: u32 = 0b11 << 29;
352    pub(super) const ALIGN_LEFT: u32 = 0 << 29;
353    pub(super) const ALIGN_RIGHT: u32 = 1 << 29;
354    pub(super) const ALIGN_CENTER: u32 = 2 << 29;
355    pub(super) const ALIGN_UNKNOWN: u32 = 3 << 29;
356}
357
358#[cfg(not(feature = "ferrocene_certified"))]
359impl FormattingOptions {
360    /// Construct a new `FormatterBuilder` with the supplied `Write` trait
361    /// object for output that is equivalent to the `{}` formatting
362    /// specifier:
363    ///
364    /// - no flags,
365    /// - filled with spaces,
366    /// - no alignment,
367    /// - no width,
368    /// - no precision, and
369    /// - no [`DebugAsHex`] output mode.
370    #[unstable(feature = "formatting_options", issue = "118117")]
371    pub const fn new() -> Self {
372        Self { flags: ' ' as u32 | flags::ALIGN_UNKNOWN, width: 0, precision: 0 }
373    }
374
375    /// Sets or removes the sign (the `+` or the `-` flag).
376    ///
377    /// - `+`: This is intended for numeric types and indicates that the sign
378    ///   should always be printed. By default only the negative sign of signed
379    ///   values is printed, and the sign of positive or unsigned values is
380    ///   omitted. This flag indicates that the correct sign (+ or -) should
381    ///   always be printed.
382    /// - `-`: Currently not used
383    #[unstable(feature = "formatting_options", issue = "118117")]
384    pub const fn sign(&mut self, sign: Option<Sign>) -> &mut Self {
385        let sign = match sign {
386            None => 0,
387            Some(Sign::Plus) => flags::SIGN_PLUS_FLAG,
388            Some(Sign::Minus) => flags::SIGN_MINUS_FLAG,
389        };
390        self.flags = self.flags & !(flags::SIGN_PLUS_FLAG | flags::SIGN_MINUS_FLAG) | sign;
391        self
392    }
393    /// Sets or unsets the `0` flag.
394    ///
395    /// This is used to indicate for integer formats that the padding to width should both be done with a 0 character as well as be sign-aware
396    #[unstable(feature = "formatting_options", issue = "118117")]
397    pub const fn sign_aware_zero_pad(&mut self, sign_aware_zero_pad: bool) -> &mut Self {
398        if sign_aware_zero_pad {
399            self.flags |= flags::SIGN_AWARE_ZERO_PAD_FLAG;
400        } else {
401            self.flags &= !flags::SIGN_AWARE_ZERO_PAD_FLAG;
402        }
403        self
404    }
405    /// Sets or unsets the `#` flag.
406    ///
407    /// This flag indicates that the "alternate" form of printing should be
408    /// used. The alternate forms are:
409    /// - [`Debug`] : pretty-print the [`Debug`] formatting (adds linebreaks and indentation)
410    /// - [`LowerHex`] as well as [`UpperHex`] - precedes the argument with a `0x`
411    /// - [`Octal`] - precedes the argument with a `0o`
412    /// - [`Binary`] - precedes the argument with a `0b`
413    #[unstable(feature = "formatting_options", issue = "118117")]
414    pub const fn alternate(&mut self, alternate: bool) -> &mut Self {
415        if alternate {
416            self.flags |= flags::ALTERNATE_FLAG;
417        } else {
418            self.flags &= !flags::ALTERNATE_FLAG;
419        }
420        self
421    }
422    /// Sets the fill character.
423    ///
424    /// The optional fill character and alignment is provided normally in
425    /// conjunction with the width parameter. This indicates that if the value
426    /// being formatted is smaller than width some extra characters will be
427    /// printed around it.
428    #[unstable(feature = "formatting_options", issue = "118117")]
429    pub const fn fill(&mut self, fill: char) -> &mut Self {
430        self.flags = self.flags & (u32::MAX << 21) | fill as u32;
431        self
432    }
433    /// Sets or removes the alignment.
434    ///
435    /// The alignment specifies how the value being formatted should be
436    /// positioned if it is smaller than the width of the formatter.
437    #[unstable(feature = "formatting_options", issue = "118117")]
438    pub const fn align(&mut self, align: Option<Alignment>) -> &mut Self {
439        let align: u32 = match align {
440            Some(Alignment::Left) => flags::ALIGN_LEFT,
441            Some(Alignment::Right) => flags::ALIGN_RIGHT,
442            Some(Alignment::Center) => flags::ALIGN_CENTER,
443            None => flags::ALIGN_UNKNOWN,
444        };
445        self.flags = self.flags & !flags::ALIGN_BITS | align;
446        self
447    }
448    /// Sets or removes the width.
449    ///
450    /// This is a parameter for the “minimum width” that the format should take
451    /// up. If the value’s string does not fill up this many characters, then
452    /// the padding specified by [`FormattingOptions::fill`]/[`FormattingOptions::align`]
453    /// will be used to take up the required space.
454    #[unstable(feature = "formatting_options", issue = "118117")]
455    pub const fn width(&mut self, width: Option<u16>) -> &mut Self {
456        if let Some(width) = width {
457            self.flags |= flags::WIDTH_FLAG;
458            self.width = width;
459        } else {
460            self.flags &= !flags::WIDTH_FLAG;
461            self.width = 0;
462        }
463        self
464    }
465    /// Sets or removes the precision.
466    ///
467    /// - For non-numeric types, this can be considered a “maximum width”. If
468    ///   the resulting string is longer than this width, then it is truncated
469    ///   down to this many characters and that truncated value is emitted with
470    ///   proper fill, alignment and width if those parameters are set.
471    /// - For integral types, this is ignored.
472    /// - For floating-point types, this indicates how many digits after the
473    /// decimal point should be printed.
474    #[unstable(feature = "formatting_options", issue = "118117")]
475    pub const fn precision(&mut self, precision: Option<u16>) -> &mut Self {
476        if let Some(precision) = precision {
477            self.flags |= flags::PRECISION_FLAG;
478            self.precision = precision;
479        } else {
480            self.flags &= !flags::PRECISION_FLAG;
481            self.precision = 0;
482        }
483        self
484    }
485    /// Specifies whether the [`Debug`] trait should use lower-/upper-case
486    /// hexadecimal or normal integers
487    #[unstable(feature = "formatting_options", issue = "118117")]
488    pub const fn debug_as_hex(&mut self, debug_as_hex: Option<DebugAsHex>) -> &mut Self {
489        let debug_as_hex = match debug_as_hex {
490            None => 0,
491            Some(DebugAsHex::Lower) => flags::DEBUG_LOWER_HEX_FLAG,
492            Some(DebugAsHex::Upper) => flags::DEBUG_UPPER_HEX_FLAG,
493        };
494        self.flags = self.flags & !(flags::DEBUG_LOWER_HEX_FLAG | flags::DEBUG_UPPER_HEX_FLAG)
495            | debug_as_hex;
496        self
497    }
498
499    /// Returns the current sign (the `+` or the `-` flag).
500    #[unstable(feature = "formatting_options", issue = "118117")]
501    pub const fn get_sign(&self) -> Option<Sign> {
502        if self.flags & flags::SIGN_PLUS_FLAG != 0 {
503            Some(Sign::Plus)
504        } else if self.flags & flags::SIGN_MINUS_FLAG != 0 {
505            Some(Sign::Minus)
506        } else {
507            None
508        }
509    }
510    /// Returns the current `0` flag.
511    #[unstable(feature = "formatting_options", issue = "118117")]
512    pub const fn get_sign_aware_zero_pad(&self) -> bool {
513        self.flags & flags::SIGN_AWARE_ZERO_PAD_FLAG != 0
514    }
515    /// Returns the current `#` flag.
516    #[unstable(feature = "formatting_options", issue = "118117")]
517    pub const fn get_alternate(&self) -> bool {
518        self.flags & flags::ALTERNATE_FLAG != 0
519    }
520    /// Returns the current fill character.
521    #[unstable(feature = "formatting_options", issue = "118117")]
522    pub const fn get_fill(&self) -> char {
523        // SAFETY: We only ever put a valid `char` in the lower 21 bits of the flags field.
524        unsafe { char::from_u32_unchecked(self.flags & 0x1FFFFF) }
525    }
526    /// Returns the current alignment.
527    #[unstable(feature = "formatting_options", issue = "118117")]
528    pub const fn get_align(&self) -> Option<Alignment> {
529        match self.flags & flags::ALIGN_BITS {
530            flags::ALIGN_LEFT => Some(Alignment::Left),
531            flags::ALIGN_RIGHT => Some(Alignment::Right),
532            flags::ALIGN_CENTER => Some(Alignment::Center),
533            _ => None,
534        }
535    }
536    /// Returns the current width.
537    #[unstable(feature = "formatting_options", issue = "118117")]
538    pub const fn get_width(&self) -> Option<u16> {
539        if self.flags & flags::WIDTH_FLAG != 0 { Some(self.width) } else { None }
540    }
541    /// Returns the current precision.
542    #[unstable(feature = "formatting_options", issue = "118117")]
543    pub const fn get_precision(&self) -> Option<u16> {
544        if self.flags & flags::PRECISION_FLAG != 0 { Some(self.precision) } else { None }
545    }
546    /// Returns the current precision.
547    #[unstable(feature = "formatting_options", issue = "118117")]
548    pub const fn get_debug_as_hex(&self) -> Option<DebugAsHex> {
549        if self.flags & flags::DEBUG_LOWER_HEX_FLAG != 0 {
550            Some(DebugAsHex::Lower)
551        } else if self.flags & flags::DEBUG_UPPER_HEX_FLAG != 0 {
552            Some(DebugAsHex::Upper)
553        } else {
554            None
555        }
556    }
557
558    /// Creates a [`Formatter`] that writes its output to the given [`Write`] trait.
559    ///
560    /// You may alternatively use [`Formatter::new()`].
561    #[unstable(feature = "formatting_options", issue = "118117")]
562    pub const fn create_formatter<'a>(self, write: &'a mut (dyn Write + 'a)) -> Formatter<'a> {
563        Formatter { options: self, buf: write }
564    }
565}
566
567#[unstable(feature = "formatting_options", issue = "118117")]
568#[cfg(not(feature = "ferrocene_certified"))]
569impl Default for FormattingOptions {
570    /// Same as [`FormattingOptions::new()`].
571    fn default() -> Self {
572        // The `#[derive(Default)]` implementation would set `fill` to `\0` instead of space.
573        Self::new()
574    }
575}
576
577/// Configuration for formatting.
578///
579/// A `Formatter` represents various options related to formatting. Users do not
580/// construct `Formatter`s directly; a mutable reference to one is passed to
581/// the `fmt` method of all formatting traits, like [`Debug`] and [`Display`].
582///
583/// To interact with a `Formatter`, you'll call various methods to change the
584/// various options related to formatting. For examples, please see the
585/// documentation of the methods defined on `Formatter` below.
586#[allow(missing_debug_implementations)]
587#[stable(feature = "rust1", since = "1.0.0")]
588#[rustc_diagnostic_item = "Formatter"]
589#[cfg(not(feature = "ferrocene_certified"))]
590pub struct Formatter<'a> {
591    options: FormattingOptions,
592
593    buf: &'a mut (dyn Write + 'a),
594}
595
596#[cfg(not(feature = "ferrocene_certified"))]
597impl<'a> Formatter<'a> {
598    /// Creates a new formatter with given [`FormattingOptions`].
599    ///
600    /// If `write` is a reference to a formatter, it is recommended to use
601    /// [`Formatter::with_options`] instead as this can borrow the underlying
602    /// `write`, thereby bypassing one layer of indirection.
603    ///
604    /// You may alternatively use [`FormattingOptions::create_formatter()`].
605    #[unstable(feature = "formatting_options", issue = "118117")]
606    pub const fn new(write: &'a mut (dyn Write + 'a), options: FormattingOptions) -> Self {
607        Formatter { options, buf: write }
608    }
609
610    /// Creates a new formatter based on this one with given [`FormattingOptions`].
611    #[unstable(feature = "formatting_options", issue = "118117")]
612    pub const fn with_options<'b>(&'b mut self, options: FormattingOptions) -> Formatter<'b> {
613        Formatter { options, buf: self.buf }
614    }
615}
616
617/// This structure represents a safely precompiled version of a format string
618/// and its arguments. This cannot be generated at runtime because it cannot
619/// safely be done, so no constructors are given and the fields are private
620/// to prevent modification.
621///
622/// The [`format_args!`] macro will safely create an instance of this structure.
623/// The macro validates the format string at compile-time so usage of the
624/// [`write()`] and [`format()`] functions can be safely performed.
625///
626/// You can use the `Arguments<'a>` that [`format_args!`] returns in `Debug`
627/// and `Display` contexts as seen below. The example also shows that `Debug`
628/// and `Display` format to the same thing: the interpolated format string
629/// in `format_args!`.
630///
631/// ```rust
632/// let debug = format!("{:?}", format_args!("{} foo {:?}", 1, 2));
633/// let display = format!("{}", format_args!("{} foo {:?}", 1, 2));
634/// assert_eq!("1 foo 2", display);
635/// assert_eq!(display, debug);
636/// ```
637///
638/// [`format()`]: ../../std/fmt/fn.format.html
639//
640// Internal representation:
641//
642// fmt::Arguments is represented in one of two ways:
643//
644// 1) String literal representation (e.g. format_args!("hello"))
645//             ┌────────────────────────────────┐
646//   template: │           *const u8            │ ─▷ "hello"
647//             ├──────────────────────────────┬─┤
648//   args:     │             len              │1│ (lowest bit is 1; field contains `len << 1 | 1`)
649//             └──────────────────────────────┴─┘
650//   In this representation, there are no placeholders and `fmt::Arguments::as_str()` returns Some.
651//   The pointer points to the start of a static `str`. The length is given by `args as usize >> 1`.
652//   (The length of a `&str` is isize::MAX at most, so it always fits in a usize minus one bit.)
653//
654//   `fmt::Arguments::from_str()` constructs this representation from a `&'static str`.
655//
656// 2) Placeholders representation (e.g. format_args!("hello {name}\n"))
657//             ┌────────────────────────────────┐
658//   template: │           *const u8            │ ─▷ b"\x06hello \x80\x01\n\x00"
659//             ├────────────────────────────────┤
660//   args:     │     &'a [Argument<'a>; _]     0│ (lower bit is 0 due to alignment of Argument type)
661//             └────────────────────────────────┘
662//   In this representation, the template is a byte sequence encoding both the literal string pieces
663//   and the placeholders (including their options/flags).
664//
665//   The `args` pointer points to an array of `fmt::Argument<'a>` values, of sufficient length to
666//   match the placeholders in the template.
667//
668//   `fmt::Arguments::new()` constructs this representation from a template byte slice and a slice
669//   of arguments. This function is unsafe, as the template is assumed to be valid and the args
670//   slice is assumed to have elements matching the template.
671//
672//   The template byte sequence is the concatenation of parts of the following types:
673//
674//   - Literal string piece:
675//         Pieces that must be formatted verbatim (e.g. "hello " and "\n" in "hello {name}\n")
676//         appear literally in the template byte sequence, prefixed by their length.
677//
678//         For pieces of up to 127 bytes, these are  represented as a single byte containing the
679//         length followed directly by the bytes of the string:
680//         ┌───┬────────────────────────────┐
681//         │len│    `len` bytes (utf-8)     │ (e.g. b"\x06hello ")
682//         └───┴────────────────────────────┘
683//
684//         For larger pieces up to u16::MAX bytes, these are  represented as a 0x80 followed by
685//         their length in 16-bit little endian, followed by the bytes of the string:
686//         ┌────┬─────────┬───────────────────────────┐
687//         │0x80│   len   │   `len` bytes (utf-8)     │ (e.g. b"\x80\x00\x01hello … ")
688//         └────┴─────────┴───────────────────────────┘
689//
690//         Longer pieces are split into multiple pieces of max u16::MAX bytes (at utf-8 boundaries).
691//
692//   - Placeholder:
693//         Placeholders (e.g. `{name}` in "hello {name}") are represented as a byte with the highest
694//         two bits set, followed by zero or more fields depending on the flags in the first byte:
695//         ┌──────────┬┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┬┄┄┄┄┄┄┄┄┄┄┄┬┄┄┄┄┄┄┄┄┄┄┄┬┄┄┄┄┄┄┄┄┄┄┄┐
696//         │0b11______│       flags       ┊   width   ┊ precision ┊ arg_index ┊ (e.g. b"\xC2\x05\0")
697//         └────││││││┴┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┴┄┄┄┄┄┄┄┄┄┄┄┴┄┄┄┄┄┄┄┄┄┄┄┴┄┄┄┄┄┄┄┄┄┄┄┘
698//              ││││││        32 bit          16 bit      16 bit      16 bit
699//              │││││└─ flags present
700//              ││││└─ width present
701//              │││└─ precision present
702//              ││└─ arg_index present
703//              │└─ width indirect
704//              └─ precision indirect
705//
706//         All fields other than the first byte are optional and only present when their
707//         corresponding flag is set in the first byte.
708//
709//         So, a fully default placeholder without any options is just a single byte:
710//         ┌──────────┐
711//         │0b11000000│ (b"\xC0")
712//         └──────────┘
713//
714//         The fields are stored as little endian.
715//
716//         The `flags` fields corresponds to the `flags` field of `FormattingOptions`.
717//         See doc comment of `FormattingOptions::flags` for details.
718//
719//         The `width` and `precision` fields correspond to their respective fields in
720//         `FormattingOptions`. However, if their "indirect" flag is set, the field contains the
721//         index in the `args` array where the dynamic width or precision is stored, rather than the
722//         value directly.
723//
724//         The `arg_index` field is the index into the `args` array for the argument to be
725//         formatted.
726//
727//         If omitted, the flags, width and precision of the default FormattingOptions::new() are
728//         used.
729//
730//         If the `arg_index` is omitted, the next argument in the `args` array is used (starting
731//         at 0).
732//
733//   - End:
734//         A single zero byte marks the end of the template:
735//         ┌───┐
736//         │ 0 │ ("\0")
737//         └───┘
738//
739//         (Note that a zero byte may also occur naturally as part of the string pieces or flags,
740//         width, precision and arg_index fields above. That is, the template byte sequence ends
741//         with a 0 byte, but isn't terminated by the first 0 byte.)
742//
743#[lang = "format_arguments"]
744#[stable(feature = "rust1", since = "1.0.0")]
745#[derive(Copy, Clone)]
746#[cfg(not(feature = "ferrocene_certified"))]
747pub struct Arguments<'a> {
748    template: NonNull<u8>,
749    args: NonNull<rt::Argument<'a>>,
750}
751
752/// Used by the format_args!() macro to create a fmt::Arguments object.
753#[doc(hidden)]
754#[rustc_diagnostic_item = "FmtArgumentsNew"]
755#[unstable(feature = "fmt_internals", issue = "none")]
756#[cfg(not(feature = "ferrocene_certified"))]
757impl<'a> Arguments<'a> {
758    // SAFETY: The caller must ensure that the provided template and args encode a valid
759    // fmt::Arguments, as documented above.
760    #[inline]
761    pub unsafe fn new<const N: usize, const M: usize>(
762        template: &'a [u8; N],
763        args: &'a [rt::Argument<'a>; M],
764    ) -> Arguments<'a> {
765        // SAFETY: Responsibility of the caller.
766        unsafe { Arguments { template: mem::transmute(template), args: mem::transmute(args) } }
767    }
768
769    #[inline]
770    pub const fn from_str(s: &'static str) -> Arguments<'a> {
771        // SAFETY: This is the "static str" representation of fmt::Arguments; see above.
772        unsafe {
773            Arguments {
774                template: mem::transmute(s.as_ptr()),
775                args: mem::transmute(s.len() << 1 | 1),
776            }
777        }
778    }
779
780    // Same as `from_str`, but not const.
781    // Used by format_args!() expansion when arguments are inlined,
782    // e.g. format_args!("{}", 123), which is not allowed in const.
783    #[inline]
784    pub fn from_str_nonconst(s: &'static str) -> Arguments<'a> {
785        Arguments::from_str(s)
786    }
787}
788
789#[doc(hidden)]
790#[unstable(feature = "fmt_internals", issue = "none")]
791#[cfg(not(feature = "ferrocene_certified"))]
792impl<'a> Arguments<'a> {
793    /// Estimates the length of the formatted text.
794    ///
795    /// This is intended to be used for setting initial `String` capacity
796    /// when using `format!`. Note: this is neither the lower nor upper bound.
797    #[inline]
798    pub fn estimated_capacity(&self) -> usize {
799        if let Some(s) = self.as_str() {
800            return s.len();
801        }
802        // Iterate over the template, counting the length of literal pieces.
803        let mut length = 0usize;
804        let mut starts_with_placeholder = false;
805        let mut template = self.template;
806        loop {
807            // SAFETY: We can assume the template is valid.
808            unsafe {
809                let n = template.read();
810                template = template.add(1);
811                if n == 0 {
812                    // End of template.
813                    break;
814                } else if n < 128 {
815                    // Short literal string piece.
816                    length += n as usize;
817                    template = template.add(n as usize);
818                } else if n == 128 {
819                    // Long literal string piece.
820                    let len = usize::from(u16::from_le_bytes(template.cast_array().read()));
821                    length += len;
822                    template = template.add(2 + len);
823                } else {
824                    assert_unchecked(n >= 0xC0);
825                    // Placeholder piece.
826                    if length == 0 {
827                        starts_with_placeholder = true;
828                    }
829                    // Skip remainder of placeholder:
830                    let skip = (n & 1 != 0) as usize * 4 // flags (32 bit)
831                        + (n & 2 != 0) as usize * 2  // width     (16 bit)
832                        + (n & 4 != 0) as usize * 2  // precision (16 bit)
833                        + (n & 8 != 0) as usize * 2; // arg_index (16 bit)
834                    template = template.add(skip as usize);
835                }
836            }
837        }
838
839        if starts_with_placeholder && length < 16 {
840            // If the format string starts with a placeholder,
841            // don't preallocate anything, unless length
842            // of literal pieces is significant.
843            0
844        } else {
845            // There are some placeholders, so any additional push
846            // will reallocate the string. To avoid that,
847            // we're "pre-doubling" the capacity here.
848            length.wrapping_mul(2)
849        }
850    }
851}
852
853#[cfg(not(feature = "ferrocene_certified"))]
854impl<'a> Arguments<'a> {
855    /// Gets the formatted string, if it has no arguments to be formatted at runtime.
856    ///
857    /// This can be used to avoid allocations in some cases.
858    ///
859    /// # Guarantees
860    ///
861    /// For `format_args!("just a literal")`, this function is guaranteed to
862    /// return `Some("just a literal")`.
863    ///
864    /// For most cases with placeholders, this function will return `None`.
865    ///
866    /// However, the compiler may perform optimizations that can cause this
867    /// function to return `Some(_)` even if the format string contains
868    /// placeholders. For example, `format_args!("Hello, {}!", "world")` may be
869    /// optimized to `format_args!("Hello, world!")`, such that `as_str()`
870    /// returns `Some("Hello, world!")`.
871    ///
872    /// The behavior for anything but the trivial case (without placeholders)
873    /// is not guaranteed, and should not be relied upon for anything other
874    /// than optimization.
875    ///
876    /// # Examples
877    ///
878    /// ```rust
879    /// use std::fmt::Arguments;
880    ///
881    /// fn write_str(_: &str) { /* ... */ }
882    ///
883    /// fn write_fmt(args: &Arguments<'_>) {
884    ///     if let Some(s) = args.as_str() {
885    ///         write_str(s)
886    ///     } else {
887    ///         write_str(&args.to_string());
888    ///     }
889    /// }
890    /// ```
891    ///
892    /// ```rust
893    /// assert_eq!(format_args!("hello").as_str(), Some("hello"));
894    /// assert_eq!(format_args!("").as_str(), Some(""));
895    /// assert_eq!(format_args!("{:?}", std::env::current_dir()).as_str(), None);
896    /// ```
897    #[stable(feature = "fmt_as_str", since = "1.52.0")]
898    #[rustc_const_stable(feature = "const_arguments_as_str", since = "1.84.0")]
899    #[must_use]
900    #[inline]
901    pub const fn as_str(&self) -> Option<&'static str> {
902        // SAFETY: During const eval, `self.args` must have come from a usize,
903        // not a pointer, because that's the only way to create a fmt::Arguments in const.
904        // (I.e. only fmt::Arguments::from_str is const, fmt::Arguments::new is not.)
905        //
906        // Outside const eval, transmuting a pointer to a usize is fine.
907        let bits: usize = unsafe { mem::transmute(self.args) };
908        if bits & 1 == 1 {
909            // SAFETY: This fmt::Arguments stores a &'static str. See encoding documentation above.
910            Some(unsafe {
911                str::from_utf8_unchecked(crate::slice::from_raw_parts(
912                    self.template.as_ptr(),
913                    bits >> 1,
914                ))
915            })
916        } else {
917            None
918        }
919    }
920
921    /// Same as [`Arguments::as_str`], but will only return `Some(s)` if it can be determined at compile time.
922    #[unstable(feature = "fmt_internals", reason = "internal to standard library", issue = "none")]
923    #[must_use]
924    #[inline]
925    #[doc(hidden)]
926    pub fn as_statically_known_str(&self) -> Option<&'static str> {
927        let s = self.as_str();
928        if core::intrinsics::is_val_statically_known(s.is_some()) { s } else { None }
929    }
930}
931
932// Manually implementing these results in better error messages.
933#[stable(feature = "rust1", since = "1.0.0")]
934#[cfg(not(feature = "ferrocene_certified"))]
935impl !Send for Arguments<'_> {}
936#[stable(feature = "rust1", since = "1.0.0")]
937#[cfg(not(feature = "ferrocene_certified"))]
938impl !Sync for Arguments<'_> {}
939
940#[stable(feature = "rust1", since = "1.0.0")]
941#[cfg(not(feature = "ferrocene_certified"))]
942impl Debug for Arguments<'_> {
943    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
944        Display::fmt(self, fmt)
945    }
946}
947
948#[stable(feature = "rust1", since = "1.0.0")]
949#[cfg(not(feature = "ferrocene_certified"))]
950impl Display for Arguments<'_> {
951    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
952        write(fmt.buf, *self)
953    }
954}
955
956/// `?` formatting.
957///
958/// `Debug` should format the output in a programmer-facing, debugging context.
959///
960/// Generally speaking, you should just `derive` a `Debug` implementation.
961///
962/// When used with the alternate format specifier `#?`, the output is pretty-printed.
963///
964/// For more information on formatters, see [the module-level documentation][module].
965///
966/// [module]: ../../std/fmt/index.html
967///
968/// This trait can be used with `#[derive]` if all fields implement `Debug`. When
969/// `derive`d for structs, it will use the name of the `struct`, then `{`, then a
970/// comma-separated list of each field's name and `Debug` value, then `}`. For
971/// `enum`s, it will use the name of the variant and, if applicable, `(`, then the
972/// `Debug` values of the fields, then `)`.
973///
974/// # Stability
975///
976/// Derived `Debug` formats are not stable, and so may change with future Rust
977/// versions. Additionally, `Debug` implementations of types provided by the
978/// standard library (`std`, `core`, `alloc`, etc.) are not stable, and
979/// may also change with future Rust versions.
980///
981/// # Examples
982///
983/// Deriving an implementation:
984///
985/// ```
986/// #[derive(Debug)]
987/// struct Point {
988///     x: i32,
989///     y: i32,
990/// }
991///
992/// let origin = Point { x: 0, y: 0 };
993///
994/// assert_eq!(
995///     format!("The origin is: {origin:?}"),
996///     "The origin is: Point { x: 0, y: 0 }",
997/// );
998/// ```
999///
1000/// Manually implementing:
1001///
1002/// ```
1003/// use std::fmt;
1004///
1005/// struct Point {
1006///     x: i32,
1007///     y: i32,
1008/// }
1009///
1010/// impl fmt::Debug for Point {
1011///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1012///         f.debug_struct("Point")
1013///          .field("x", &self.x)
1014///          .field("y", &self.y)
1015///          .finish()
1016///     }
1017/// }
1018///
1019/// let origin = Point { x: 0, y: 0 };
1020///
1021/// assert_eq!(
1022///     format!("The origin is: {origin:?}"),
1023///     "The origin is: Point { x: 0, y: 0 }",
1024/// );
1025/// ```
1026///
1027/// There are a number of helper methods on the [`Formatter`] struct to help you with manual
1028/// implementations, such as [`debug_struct`].
1029///
1030/// [`debug_struct`]: Formatter::debug_struct
1031///
1032/// Types that do not wish to use the standard suite of debug representations
1033/// provided by the `Formatter` trait (`debug_struct`, `debug_tuple`,
1034/// `debug_list`, `debug_set`, `debug_map`) can do something totally custom by
1035/// manually writing an arbitrary representation to the `Formatter`.
1036///
1037/// ```
1038/// # use std::fmt;
1039/// # struct Point {
1040/// #     x: i32,
1041/// #     y: i32,
1042/// # }
1043/// #
1044/// impl fmt::Debug for Point {
1045///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1046///         write!(f, "Point [{} {}]", self.x, self.y)
1047///     }
1048/// }
1049/// ```
1050///
1051/// `Debug` implementations using either `derive` or the debug builder API
1052/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
1053///
1054/// Pretty-printing with `#?`:
1055///
1056/// ```
1057/// #[derive(Debug)]
1058/// struct Point {
1059///     x: i32,
1060///     y: i32,
1061/// }
1062///
1063/// let origin = Point { x: 0, y: 0 };
1064///
1065/// let expected = "The origin is: Point {
1066///     x: 0,
1067///     y: 0,
1068/// }";
1069/// assert_eq!(format!("The origin is: {origin:#?}"), expected);
1070/// ```
1071#[stable(feature = "rust1", since = "1.0.0")]
1072#[rustc_on_unimplemented(
1073    on(
1074        crate_local,
1075        note = "add `#[derive(Debug)]` to `{Self}` or manually `impl {This} for {Self}`"
1076    ),
1077    on(
1078        from_desugaring = "FormatLiteral",
1079        label = "`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{This}`"
1080    ),
1081    message = "`{Self}` doesn't implement `{This}`"
1082)]
1083#[doc(alias = "{:?}")]
1084#[rustc_diagnostic_item = "Debug"]
1085#[rustc_trivial_field_reads]
1086#[cfg(not(feature = "ferrocene_certified"))]
1087pub trait Debug: PointeeSized {
1088    #[doc = include_str!("fmt_trait_method_doc.md")]
1089    ///
1090    /// # Examples
1091    ///
1092    /// ```
1093    /// use std::fmt;
1094    ///
1095    /// struct Position {
1096    ///     longitude: f32,
1097    ///     latitude: f32,
1098    /// }
1099    ///
1100    /// impl fmt::Debug for Position {
1101    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1102    ///         f.debug_tuple("")
1103    ///          .field(&self.longitude)
1104    ///          .field(&self.latitude)
1105    ///          .finish()
1106    ///     }
1107    /// }
1108    ///
1109    /// let position = Position { longitude: 1.987, latitude: 2.983 };
1110    /// assert_eq!(format!("{position:?}"), "(1.987, 2.983)");
1111    ///
1112    /// assert_eq!(format!("{position:#?}"), "(
1113    ///     1.987,
1114    ///     2.983,
1115    /// )");
1116    /// ```
1117    #[stable(feature = "rust1", since = "1.0.0")]
1118    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1119}
1120
1121// Separate module to reexport the macro `Debug` from prelude without the trait `Debug`.
1122#[cfg(not(feature = "ferrocene_certified"))]
1123pub(crate) mod macros {
1124    /// Derive macro generating an impl of the trait `Debug`.
1125    #[rustc_builtin_macro]
1126    #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
1127    #[allow_internal_unstable(core_intrinsics, fmt_helpers_for_derive)]
1128    pub macro Debug($item:item) {
1129        /* compiler built-in */
1130    }
1131}
1132#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
1133#[doc(inline)]
1134#[cfg(not(feature = "ferrocene_certified"))]
1135pub use macros::Debug;
1136
1137/// Format trait for an empty format, `{}`.
1138///
1139/// Implementing this trait for a type will automatically implement the
1140/// [`ToString`][tostring] trait for the type, allowing the usage
1141/// of the [`.to_string()`][tostring_function] method. Prefer implementing
1142/// the `Display` trait for a type, rather than [`ToString`][tostring].
1143///
1144/// `Display` is similar to [`Debug`], but `Display` is for user-facing
1145/// output, and so cannot be derived.
1146///
1147/// For more information on formatters, see [the module-level documentation][module].
1148///
1149/// [module]: ../../std/fmt/index.html
1150/// [tostring]: ../../std/string/trait.ToString.html
1151/// [tostring_function]: ../../std/string/trait.ToString.html#tymethod.to_string
1152///
1153/// # Completeness and parseability
1154///
1155/// `Display` for a type might not necessarily be a lossless or complete representation of the type.
1156/// It may omit internal state, precision, or other information the type does not consider important
1157/// for user-facing output, as determined by the type. As such, the output of `Display` might not be
1158/// possible to parse, and even if it is, the result of parsing might not exactly match the original
1159/// value.
1160///
1161/// However, if a type has a lossless `Display` implementation whose output is meant to be
1162/// conveniently machine-parseable and not just meant for human consumption, then the type may wish
1163/// to accept the same format in `FromStr`, and document that usage. Having both `Display` and
1164/// `FromStr` implementations where the result of `Display` cannot be parsed with `FromStr` may
1165/// surprise users.
1166///
1167/// # Internationalization
1168///
1169/// Because a type can only have one `Display` implementation, it is often preferable
1170/// to only implement `Display` when there is a single most "obvious" way that
1171/// values can be formatted as text. This could mean formatting according to the
1172/// "invariant" culture and "undefined" locale, or it could mean that the type
1173/// display is designed for a specific culture/locale, such as developer logs.
1174///
1175/// If not all values have a justifiably canonical textual format or if you want
1176/// to support alternative formats not covered by the standard set of possible
1177/// [formatting traits], the most flexible approach is display adapters: methods
1178/// like [`str::escape_default`] or [`Path::display`] which create a wrapper
1179/// implementing `Display` to output the specific display format.
1180///
1181/// [formatting traits]: ../../std/fmt/index.html#formatting-traits
1182/// [`Path::display`]: ../../std/path/struct.Path.html#method.display
1183///
1184/// # Examples
1185///
1186/// Implementing `Display` on a type:
1187///
1188/// ```
1189/// use std::fmt;
1190///
1191/// struct Point {
1192///     x: i32,
1193///     y: i32,
1194/// }
1195///
1196/// impl fmt::Display for Point {
1197///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1198///         write!(f, "({}, {})", self.x, self.y)
1199///     }
1200/// }
1201///
1202/// let origin = Point { x: 0, y: 0 };
1203///
1204/// assert_eq!(format!("The origin is: {origin}"), "The origin is: (0, 0)");
1205/// ```
1206#[rustc_on_unimplemented(
1207    on(
1208        any(Self = "std::path::Path", Self = "std::path::PathBuf"),
1209        label = "`{Self}` cannot be formatted with the default formatter; call `.display()` on it",
1210        note = "call `.display()` or `.to_string_lossy()` to safely print paths, \
1211                as they may contain non-Unicode data",
1212    ),
1213    on(
1214        from_desugaring = "FormatLiteral",
1215        note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead",
1216        label = "`{Self}` cannot be formatted with the default formatter",
1217    ),
1218    message = "`{Self}` doesn't implement `{This}`"
1219)]
1220#[doc(alias = "{}")]
1221#[rustc_diagnostic_item = "Display"]
1222#[stable(feature = "rust1", since = "1.0.0")]
1223#[cfg(not(feature = "ferrocene_certified"))]
1224pub trait Display: PointeeSized {
1225    #[doc = include_str!("fmt_trait_method_doc.md")]
1226    ///
1227    /// # Examples
1228    ///
1229    /// ```
1230    /// use std::fmt;
1231    ///
1232    /// struct Position {
1233    ///     longitude: f32,
1234    ///     latitude: f32,
1235    /// }
1236    ///
1237    /// impl fmt::Display for Position {
1238    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1239    ///         write!(f, "({}, {})", self.longitude, self.latitude)
1240    ///     }
1241    /// }
1242    ///
1243    /// assert_eq!(
1244    ///     "(1.987, 2.983)",
1245    ///     format!("{}", Position { longitude: 1.987, latitude: 2.983, }),
1246    /// );
1247    /// ```
1248    #[stable(feature = "rust1", since = "1.0.0")]
1249    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1250}
1251
1252/// `o` formatting.
1253///
1254/// The `Octal` trait should format its output as a number in base-8.
1255///
1256/// For primitive signed integers (`i8` to `i128`, and `isize`),
1257/// negative values are formatted as the two’s complement representation.
1258///
1259/// The alternate flag, `#`, adds a `0o` in front of the output.
1260///
1261/// For more information on formatters, see [the module-level documentation][module].
1262///
1263/// [module]: ../../std/fmt/index.html
1264///
1265/// # Examples
1266///
1267/// Basic usage with `i32`:
1268///
1269/// ```
1270/// let x = 42; // 42 is '52' in octal
1271///
1272/// assert_eq!(format!("{x:o}"), "52");
1273/// assert_eq!(format!("{x:#o}"), "0o52");
1274///
1275/// assert_eq!(format!("{:o}", -16), "37777777760");
1276/// ```
1277///
1278/// Implementing `Octal` on a type:
1279///
1280/// ```
1281/// use std::fmt;
1282///
1283/// struct Length(i32);
1284///
1285/// impl fmt::Octal for Length {
1286///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1287///         let val = self.0;
1288///
1289///         fmt::Octal::fmt(&val, f) // delegate to i32's implementation
1290///     }
1291/// }
1292///
1293/// let l = Length(9);
1294///
1295/// assert_eq!(format!("l as octal is: {l:o}"), "l as octal is: 11");
1296///
1297/// assert_eq!(format!("l as octal is: {l:#06o}"), "l as octal is: 0o0011");
1298/// ```
1299#[stable(feature = "rust1", since = "1.0.0")]
1300#[cfg(not(feature = "ferrocene_certified"))]
1301pub trait Octal: PointeeSized {
1302    #[doc = include_str!("fmt_trait_method_doc.md")]
1303    #[stable(feature = "rust1", since = "1.0.0")]
1304    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1305}
1306
1307/// `b` formatting.
1308///
1309/// The `Binary` trait should format its output as a number in binary.
1310///
1311/// For primitive signed integers ([`i8`] to [`i128`], and [`isize`]),
1312/// negative values are formatted as the two’s complement representation.
1313///
1314/// The alternate flag, `#`, adds a `0b` in front of the output.
1315///
1316/// For more information on formatters, see [the module-level documentation][module].
1317///
1318/// [module]: ../../std/fmt/index.html
1319///
1320/// # Examples
1321///
1322/// Basic usage with [`i32`]:
1323///
1324/// ```
1325/// let x = 42; // 42 is '101010' in binary
1326///
1327/// assert_eq!(format!("{x:b}"), "101010");
1328/// assert_eq!(format!("{x:#b}"), "0b101010");
1329///
1330/// assert_eq!(format!("{:b}", -16), "11111111111111111111111111110000");
1331/// ```
1332///
1333/// Implementing `Binary` on a type:
1334///
1335/// ```
1336/// use std::fmt;
1337///
1338/// struct Length(i32);
1339///
1340/// impl fmt::Binary for Length {
1341///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1342///         let val = self.0;
1343///
1344///         fmt::Binary::fmt(&val, f) // delegate to i32's implementation
1345///     }
1346/// }
1347///
1348/// let l = Length(107);
1349///
1350/// assert_eq!(format!("l as binary is: {l:b}"), "l as binary is: 1101011");
1351///
1352/// assert_eq!(
1353///     // Note that the `0b` prefix added by `#` is included in the total width, so we
1354///     // need to add two to correctly display all 32 bits.
1355///     format!("l as binary is: {l:#034b}"),
1356///     "l as binary is: 0b00000000000000000000000001101011"
1357/// );
1358/// ```
1359#[stable(feature = "rust1", since = "1.0.0")]
1360#[cfg(not(feature = "ferrocene_certified"))]
1361pub trait Binary: PointeeSized {
1362    #[doc = include_str!("fmt_trait_method_doc.md")]
1363    #[stable(feature = "rust1", since = "1.0.0")]
1364    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1365}
1366
1367/// `x` formatting.
1368///
1369/// The `LowerHex` trait should format its output as a number in hexadecimal, with `a` through `f`
1370/// in lower case.
1371///
1372/// For primitive signed integers (`i8` to `i128`, and `isize`),
1373/// negative values are formatted as the two’s complement representation.
1374///
1375/// The alternate flag, `#`, adds a `0x` in front of the output.
1376///
1377/// For more information on formatters, see [the module-level documentation][module].
1378///
1379/// [module]: ../../std/fmt/index.html
1380///
1381/// # Examples
1382///
1383/// Basic usage with `i32`:
1384///
1385/// ```
1386/// let y = 42; // 42 is '2a' in hex
1387///
1388/// assert_eq!(format!("{y:x}"), "2a");
1389/// assert_eq!(format!("{y:#x}"), "0x2a");
1390///
1391/// assert_eq!(format!("{:x}", -16), "fffffff0");
1392/// ```
1393///
1394/// Implementing `LowerHex` on a type:
1395///
1396/// ```
1397/// use std::fmt;
1398///
1399/// struct Length(i32);
1400///
1401/// impl fmt::LowerHex for Length {
1402///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1403///         let val = self.0;
1404///
1405///         fmt::LowerHex::fmt(&val, f) // delegate to i32's implementation
1406///     }
1407/// }
1408///
1409/// let l = Length(9);
1410///
1411/// assert_eq!(format!("l as hex is: {l:x}"), "l as hex is: 9");
1412///
1413/// assert_eq!(format!("l as hex is: {l:#010x}"), "l as hex is: 0x00000009");
1414/// ```
1415#[stable(feature = "rust1", since = "1.0.0")]
1416#[cfg(not(feature = "ferrocene_certified"))]
1417pub trait LowerHex: PointeeSized {
1418    #[doc = include_str!("fmt_trait_method_doc.md")]
1419    #[stable(feature = "rust1", since = "1.0.0")]
1420    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1421}
1422
1423/// `X` formatting.
1424///
1425/// The `UpperHex` trait should format its output as a number in hexadecimal, with `A` through `F`
1426/// in upper case.
1427///
1428/// For primitive signed integers (`i8` to `i128`, and `isize`),
1429/// negative values are formatted as the two’s complement representation.
1430///
1431/// The alternate flag, `#`, adds a `0x` in front of the output.
1432///
1433/// For more information on formatters, see [the module-level documentation][module].
1434///
1435/// [module]: ../../std/fmt/index.html
1436///
1437/// # Examples
1438///
1439/// Basic usage with `i32`:
1440///
1441/// ```
1442/// let y = 42; // 42 is '2A' in hex
1443///
1444/// assert_eq!(format!("{y:X}"), "2A");
1445/// assert_eq!(format!("{y:#X}"), "0x2A");
1446///
1447/// assert_eq!(format!("{:X}", -16), "FFFFFFF0");
1448/// ```
1449///
1450/// Implementing `UpperHex` on a type:
1451///
1452/// ```
1453/// use std::fmt;
1454///
1455/// struct Length(i32);
1456///
1457/// impl fmt::UpperHex for Length {
1458///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1459///         let val = self.0;
1460///
1461///         fmt::UpperHex::fmt(&val, f) // delegate to i32's implementation
1462///     }
1463/// }
1464///
1465/// let l = Length(i32::MAX);
1466///
1467/// assert_eq!(format!("l as hex is: {l:X}"), "l as hex is: 7FFFFFFF");
1468///
1469/// assert_eq!(format!("l as hex is: {l:#010X}"), "l as hex is: 0x7FFFFFFF");
1470/// ```
1471#[stable(feature = "rust1", since = "1.0.0")]
1472#[cfg(not(feature = "ferrocene_certified"))]
1473pub trait UpperHex: PointeeSized {
1474    #[doc = include_str!("fmt_trait_method_doc.md")]
1475    #[stable(feature = "rust1", since = "1.0.0")]
1476    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1477}
1478
1479/// `p` formatting.
1480///
1481/// The `Pointer` trait should format its output as a memory location. This is commonly presented
1482/// as hexadecimal. For more information on formatters, see [the module-level documentation][module].
1483///
1484/// Printing of pointers is not a reliable way to discover how Rust programs are implemented.
1485/// The act of reading an address changes the program itself, and may change how the data is represented
1486/// in memory, and may affect which optimizations are applied to the code.
1487///
1488/// The printed pointer values are not guaranteed to be stable nor unique identifiers of objects.
1489/// Rust allows moving values to different memory locations, and may reuse the same memory locations
1490/// for different purposes.
1491///
1492/// There is no guarantee that the printed value can be converted back to a pointer.
1493///
1494/// [module]: ../../std/fmt/index.html
1495///
1496/// # Examples
1497///
1498/// Basic usage with `&i32`:
1499///
1500/// ```
1501/// let x = &42;
1502///
1503/// let address = format!("{x:p}"); // this produces something like '0x7f06092ac6d0'
1504/// ```
1505///
1506/// Implementing `Pointer` on a type:
1507///
1508/// ```
1509/// use std::fmt;
1510///
1511/// struct Length(i32);
1512///
1513/// impl fmt::Pointer for Length {
1514///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1515///         // use `as` to convert to a `*const T`, which implements Pointer, which we can use
1516///
1517///         let ptr = self as *const Self;
1518///         fmt::Pointer::fmt(&ptr, f)
1519///     }
1520/// }
1521///
1522/// let l = Length(42);
1523///
1524/// println!("l is in memory here: {l:p}");
1525///
1526/// let l_ptr = format!("{l:018p}");
1527/// assert_eq!(l_ptr.len(), 18);
1528/// assert_eq!(&l_ptr[..2], "0x");
1529/// ```
1530#[stable(feature = "rust1", since = "1.0.0")]
1531#[rustc_diagnostic_item = "Pointer"]
1532#[cfg(not(feature = "ferrocene_certified"))]
1533pub trait Pointer: PointeeSized {
1534    #[doc = include_str!("fmt_trait_method_doc.md")]
1535    #[stable(feature = "rust1", since = "1.0.0")]
1536    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1537}
1538
1539/// `e` formatting.
1540///
1541/// The `LowerExp` trait should format its output in scientific notation with a lower-case `e`.
1542///
1543/// For more information on formatters, see [the module-level documentation][module].
1544///
1545/// [module]: ../../std/fmt/index.html
1546///
1547/// # Examples
1548///
1549/// Basic usage with `f64`:
1550///
1551/// ```
1552/// let x = 42.0; // 42.0 is '4.2e1' in scientific notation
1553///
1554/// assert_eq!(format!("{x:e}"), "4.2e1");
1555/// ```
1556///
1557/// Implementing `LowerExp` on a type:
1558///
1559/// ```
1560/// use std::fmt;
1561///
1562/// struct Length(i32);
1563///
1564/// impl fmt::LowerExp for Length {
1565///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1566///         let val = f64::from(self.0);
1567///         fmt::LowerExp::fmt(&val, f) // delegate to f64's implementation
1568///     }
1569/// }
1570///
1571/// let l = Length(100);
1572///
1573/// assert_eq!(
1574///     format!("l in scientific notation is: {l:e}"),
1575///     "l in scientific notation is: 1e2"
1576/// );
1577///
1578/// assert_eq!(
1579///     format!("l in scientific notation is: {l:05e}"),
1580///     "l in scientific notation is: 001e2"
1581/// );
1582/// ```
1583#[stable(feature = "rust1", since = "1.0.0")]
1584#[cfg(not(feature = "ferrocene_certified"))]
1585pub trait LowerExp: PointeeSized {
1586    #[doc = include_str!("fmt_trait_method_doc.md")]
1587    #[stable(feature = "rust1", since = "1.0.0")]
1588    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1589}
1590
1591/// `E` formatting.
1592///
1593/// The `UpperExp` trait should format its output in scientific notation with an upper-case `E`.
1594///
1595/// For more information on formatters, see [the module-level documentation][module].
1596///
1597/// [module]: ../../std/fmt/index.html
1598///
1599/// # Examples
1600///
1601/// Basic usage with `f64`:
1602///
1603/// ```
1604/// let x = 42.0; // 42.0 is '4.2E1' in scientific notation
1605///
1606/// assert_eq!(format!("{x:E}"), "4.2E1");
1607/// ```
1608///
1609/// Implementing `UpperExp` on a type:
1610///
1611/// ```
1612/// use std::fmt;
1613///
1614/// struct Length(i32);
1615///
1616/// impl fmt::UpperExp for Length {
1617///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1618///         let val = f64::from(self.0);
1619///         fmt::UpperExp::fmt(&val, f) // delegate to f64's implementation
1620///     }
1621/// }
1622///
1623/// let l = Length(100);
1624///
1625/// assert_eq!(
1626///     format!("l in scientific notation is: {l:E}"),
1627///     "l in scientific notation is: 1E2"
1628/// );
1629///
1630/// assert_eq!(
1631///     format!("l in scientific notation is: {l:05E}"),
1632///     "l in scientific notation is: 001E2"
1633/// );
1634/// ```
1635#[stable(feature = "rust1", since = "1.0.0")]
1636#[cfg(not(feature = "ferrocene_certified"))]
1637pub trait UpperExp: PointeeSized {
1638    #[doc = include_str!("fmt_trait_method_doc.md")]
1639    #[stable(feature = "rust1", since = "1.0.0")]
1640    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
1641}
1642
1643/// Takes an output stream and an `Arguments` struct that can be precompiled with
1644/// the `format_args!` macro.
1645///
1646/// The arguments will be formatted according to the specified format string
1647/// into the output stream provided.
1648///
1649/// # Examples
1650///
1651/// Basic usage:
1652///
1653/// ```
1654/// use std::fmt;
1655///
1656/// let mut output = String::new();
1657/// fmt::write(&mut output, format_args!("Hello {}!", "world"))
1658///     .expect("Error occurred while trying to write in String");
1659/// assert_eq!(output, "Hello world!");
1660/// ```
1661///
1662/// Please note that using [`write!`] might be preferable. Example:
1663///
1664/// ```
1665/// use std::fmt::Write;
1666///
1667/// let mut output = String::new();
1668/// write!(&mut output, "Hello {}!", "world")
1669///     .expect("Error occurred while trying to write in String");
1670/// assert_eq!(output, "Hello world!");
1671/// ```
1672///
1673/// [`write!`]: crate::write!
1674#[cfg(not(feature = "ferrocene_certified"))]
1675#[stable(feature = "rust1", since = "1.0.0")]
1676pub fn write(output: &mut dyn Write, fmt: Arguments<'_>) -> Result {
1677    if let Some(s) = fmt.as_str() {
1678        return output.write_str(s);
1679    }
1680
1681    let mut template = fmt.template;
1682    let args = fmt.args;
1683
1684    let mut arg_index = 0;
1685
1686    // See comment on `fmt::Arguments` for the details of how the template is encoded.
1687
1688    // This must match the encoding from `expand_format_args` in
1689    // compiler/rustc_ast_lowering/src/format.rs.
1690    loop {
1691        // SAFETY: We can assume the template is valid.
1692        let n = unsafe {
1693            let n = template.read();
1694            template = template.add(1);
1695            n
1696        };
1697
1698        if n == 0 {
1699            // End of template.
1700            return Ok(());
1701        } else if n < 0x80 {
1702            // Literal string piece of length `n`.
1703
1704            // SAFETY: We can assume the strings in the template are valid.
1705            let s = unsafe {
1706                let s = crate::str::from_raw_parts(template.as_ptr(), n as usize);
1707                template = template.add(n as usize);
1708                s
1709            };
1710            output.write_str(s)?;
1711        } else if n == 0x80 {
1712            // Literal string piece with a 16-bit length.
1713
1714            // SAFETY: We can assume the strings in the template are valid.
1715            let s = unsafe {
1716                let len = usize::from(u16::from_le_bytes(template.cast_array().read()));
1717                template = template.add(2);
1718                let s = crate::str::from_raw_parts(template.as_ptr(), len);
1719                template = template.add(len);
1720                s
1721            };
1722            output.write_str(s)?;
1723        } else if n == 0xC0 {
1724            // Placeholder for next argument with default options.
1725            //
1726            // Having this as a separate case improves performance for the common case.
1727
1728            // SAFETY: We can assume the template only refers to arguments that exist.
1729            unsafe {
1730                args.add(arg_index)
1731                    .as_ref()
1732                    .fmt(&mut Formatter::new(output, FormattingOptions::new()))?;
1733            }
1734            arg_index += 1;
1735        } else {
1736            // SAFETY: We can assume the template is valid.
1737            unsafe { assert_unchecked(n > 0xC0) };
1738
1739            // Placeholder with custom options.
1740
1741            let mut opt = FormattingOptions::new();
1742
1743            // SAFETY: We can assume the template is valid.
1744            unsafe {
1745                if n & 1 != 0 {
1746                    opt.flags = u32::from_le_bytes(template.cast_array().read());
1747                    template = template.add(4);
1748                }
1749                if n & 2 != 0 {
1750                    opt.width = u16::from_le_bytes(template.cast_array().read());
1751                    template = template.add(2);
1752                }
1753                if n & 4 != 0 {
1754                    opt.precision = u16::from_le_bytes(template.cast_array().read());
1755                    template = template.add(2);
1756                }
1757                if n & 8 != 0 {
1758                    arg_index = usize::from(u16::from_le_bytes(template.cast_array().read()));
1759                    template = template.add(2);
1760                }
1761            }
1762            if n & 16 != 0 {
1763                // Dynamic width from a usize argument.
1764                // SAFETY: We can assume the template only refers to arguments that exist.
1765                unsafe {
1766                    opt.width = args.add(opt.width as usize).as_ref().as_u16().unwrap_unchecked();
1767                }
1768            }
1769            if n & 32 != 0 {
1770                // Dynamic precision from a usize argument.
1771                // SAFETY: We can assume the template only refers to arguments that exist.
1772                unsafe {
1773                    opt.precision =
1774                        args.add(opt.precision as usize).as_ref().as_u16().unwrap_unchecked();
1775                }
1776            }
1777
1778            // SAFETY: We can assume the template only refers to arguments that exist.
1779            unsafe {
1780                args.add(arg_index).as_ref().fmt(&mut Formatter::new(output, opt))?;
1781            }
1782            arg_index += 1;
1783        }
1784    }
1785}
1786
1787/// Padding after the end of something. Returned by `Formatter::padding`.
1788#[must_use = "don't forget to write the post padding"]
1789#[cfg(not(feature = "ferrocene_certified"))]
1790pub(crate) struct PostPadding {
1791    fill: char,
1792    padding: u16,
1793}
1794
1795#[cfg(not(feature = "ferrocene_certified"))]
1796impl PostPadding {
1797    fn new(fill: char, padding: u16) -> PostPadding {
1798        PostPadding { fill, padding }
1799    }
1800
1801    /// Writes this post padding.
1802    pub(crate) fn write(self, f: &mut Formatter<'_>) -> Result {
1803        for _ in 0..self.padding {
1804            f.buf.write_char(self.fill)?;
1805        }
1806        Ok(())
1807    }
1808}
1809
1810#[cfg(not(feature = "ferrocene_certified"))]
1811impl<'a> Formatter<'a> {
1812    fn wrap_buf<'b, 'c, F>(&'b mut self, wrap: F) -> Formatter<'c>
1813    where
1814        'b: 'c,
1815        F: FnOnce(&'b mut (dyn Write + 'b)) -> &'c mut (dyn Write + 'c),
1816    {
1817        Formatter {
1818            // We want to change this
1819            buf: wrap(self.buf),
1820
1821            // And preserve these
1822            options: self.options,
1823        }
1824    }
1825
1826    // Helper methods used for padding and processing formatting arguments that
1827    // all formatting traits can use.
1828
1829    /// Performs the correct padding for an integer which has already been
1830    /// emitted into a str. The str should *not* contain the sign for the
1831    /// integer, that will be added by this method.
1832    ///
1833    /// # Arguments
1834    ///
1835    /// * is_nonnegative - whether the original integer was either positive or zero.
1836    /// * prefix - if the '#' character (Alternate) is provided, this
1837    ///   is the prefix to put in front of the number.
1838    /// * buf - the byte array that the number has been formatted into
1839    ///
1840    /// This function will correctly account for the flags provided as well as
1841    /// the minimum width. It will not take precision into account.
1842    ///
1843    /// # Examples
1844    ///
1845    /// ```
1846    /// use std::fmt;
1847    ///
1848    /// struct Foo { nb: i32 }
1849    ///
1850    /// impl Foo {
1851    ///     fn new(nb: i32) -> Foo {
1852    ///         Foo {
1853    ///             nb,
1854    ///         }
1855    ///     }
1856    /// }
1857    ///
1858    /// impl fmt::Display for Foo {
1859    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1860    ///         // We need to remove "-" from the number output.
1861    ///         let tmp = self.nb.abs().to_string();
1862    ///
1863    ///         formatter.pad_integral(self.nb >= 0, "Foo ", &tmp)
1864    ///     }
1865    /// }
1866    ///
1867    /// assert_eq!(format!("{}", Foo::new(2)), "2");
1868    /// assert_eq!(format!("{}", Foo::new(-1)), "-1");
1869    /// assert_eq!(format!("{}", Foo::new(0)), "0");
1870    /// assert_eq!(format!("{:#}", Foo::new(-1)), "-Foo 1");
1871    /// assert_eq!(format!("{:0>#8}", Foo::new(-1)), "00-Foo 1");
1872    /// ```
1873    #[stable(feature = "rust1", since = "1.0.0")]
1874    pub fn pad_integral(&mut self, is_nonnegative: bool, prefix: &str, buf: &str) -> Result {
1875        let mut width = buf.len();
1876
1877        let mut sign = None;
1878        if !is_nonnegative {
1879            sign = Some('-');
1880            width += 1;
1881        } else if self.sign_plus() {
1882            sign = Some('+');
1883            width += 1;
1884        }
1885
1886        let prefix = if self.alternate() {
1887            width += prefix.chars().count();
1888            Some(prefix)
1889        } else {
1890            None
1891        };
1892
1893        // Writes the sign if it exists, and then the prefix if it was requested
1894        #[inline(never)]
1895        fn write_prefix(f: &mut Formatter<'_>, sign: Option<char>, prefix: Option<&str>) -> Result {
1896            if let Some(c) = sign {
1897                f.buf.write_char(c)?;
1898            }
1899            if let Some(prefix) = prefix { f.buf.write_str(prefix) } else { Ok(()) }
1900        }
1901
1902        // The `width` field is more of a `min-width` parameter at this point.
1903        let min = self.options.width;
1904        if width >= usize::from(min) {
1905            // We're over the minimum width, so then we can just write the bytes.
1906            write_prefix(self, sign, prefix)?;
1907            self.buf.write_str(buf)
1908        } else if self.sign_aware_zero_pad() {
1909            // The sign and prefix goes before the padding if the fill character
1910            // is zero
1911            let old_options = self.options;
1912            self.options.fill('0').align(Some(Alignment::Right));
1913            write_prefix(self, sign, prefix)?;
1914            let post_padding = self.padding(min - width as u16, Alignment::Right)?;
1915            self.buf.write_str(buf)?;
1916            post_padding.write(self)?;
1917            self.options = old_options;
1918            Ok(())
1919        } else {
1920            // Otherwise, the sign and prefix goes after the padding
1921            let post_padding = self.padding(min - width as u16, Alignment::Right)?;
1922            write_prefix(self, sign, prefix)?;
1923            self.buf.write_str(buf)?;
1924            post_padding.write(self)
1925        }
1926    }
1927
1928    /// Takes a string slice and emits it to the internal buffer after applying
1929    /// the relevant formatting flags specified.
1930    ///
1931    /// The flags recognized for generic strings are:
1932    ///
1933    /// * width - the minimum width of what to emit
1934    /// * fill/align - what to emit and where to emit it if the string
1935    ///                provided needs to be padded
1936    /// * precision - the maximum length to emit, the string is truncated if it
1937    ///               is longer than this length
1938    ///
1939    /// Notably this function ignores the `flag` parameters.
1940    ///
1941    /// # Examples
1942    ///
1943    /// ```
1944    /// use std::fmt;
1945    ///
1946    /// struct Foo;
1947    ///
1948    /// impl fmt::Display for Foo {
1949    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1950    ///         formatter.pad("Foo")
1951    ///     }
1952    /// }
1953    ///
1954    /// assert_eq!(format!("{Foo:<4}"), "Foo ");
1955    /// assert_eq!(format!("{Foo:0>4}"), "0Foo");
1956    /// ```
1957    #[stable(feature = "rust1", since = "1.0.0")]
1958    pub fn pad(&mut self, s: &str) -> Result {
1959        // Make sure there's a fast path up front.
1960        if self.options.flags & (flags::WIDTH_FLAG | flags::PRECISION_FLAG) == 0 {
1961            return self.buf.write_str(s);
1962        }
1963
1964        // The `precision` field can be interpreted as a maximum width for the
1965        // string being formatted.
1966        let (s, char_count) = if let Some(max_char_count) = self.options.get_precision() {
1967            let mut iter = s.char_indices();
1968            let remaining = match iter.advance_by(usize::from(max_char_count)) {
1969                Ok(()) => 0,
1970                Err(remaining) => remaining.get(),
1971            };
1972            // SAFETY: The offset of `.char_indices()` is guaranteed to be
1973            // in-bounds and between character boundaries.
1974            let truncated = unsafe { s.get_unchecked(..iter.offset()) };
1975            (truncated, usize::from(max_char_count) - remaining)
1976        } else {
1977            // Use the optimized char counting algorithm for the full string.
1978            (s, s.chars().count())
1979        };
1980
1981        // The `width` field is more of a minimum width parameter at this point.
1982        if char_count < usize::from(self.options.width) {
1983            // If we're under the minimum width, then fill up the minimum width
1984            // with the specified string + some alignment.
1985            let post_padding =
1986                self.padding(self.options.width - char_count as u16, Alignment::Left)?;
1987            self.buf.write_str(s)?;
1988            post_padding.write(self)
1989        } else {
1990            // If we're over the minimum width or there is no minimum width, we
1991            // can just emit the string.
1992            self.buf.write_str(s)
1993        }
1994    }
1995
1996    /// Writes the pre-padding and returns the unwritten post-padding.
1997    ///
1998    /// Callers are responsible for ensuring post-padding is written after the
1999    /// thing that is being padded.
2000    pub(crate) fn padding(
2001        &mut self,
2002        padding: u16,
2003        default: Alignment,
2004    ) -> result::Result<PostPadding, Error> {
2005        let align = self.options.get_align().unwrap_or(default);
2006        let fill = self.options.get_fill();
2007
2008        let padding_left = match align {
2009            Alignment::Left => 0,
2010            Alignment::Right => padding,
2011            Alignment::Center => padding / 2,
2012        };
2013
2014        for _ in 0..padding_left {
2015            self.buf.write_char(fill)?;
2016        }
2017
2018        Ok(PostPadding::new(fill, padding - padding_left))
2019    }
2020
2021    /// Takes the formatted parts and applies the padding.
2022    ///
2023    /// Assumes that the caller already has rendered the parts with required precision,
2024    /// so that `self.precision` can be ignored.
2025    ///
2026    /// # Safety
2027    ///
2028    /// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8.
2029    unsafe fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
2030        if self.options.width == 0 {
2031            // this is the common case and we take a shortcut
2032            // SAFETY: Per the precondition.
2033            unsafe { self.write_formatted_parts(formatted) }
2034        } else {
2035            // for the sign-aware zero padding, we render the sign first and
2036            // behave as if we had no sign from the beginning.
2037            let mut formatted = formatted.clone();
2038            let mut width = self.options.width;
2039            let old_options = self.options;
2040            if self.sign_aware_zero_pad() {
2041                // a sign always goes first
2042                let sign = formatted.sign;
2043                self.buf.write_str(sign)?;
2044
2045                // remove the sign from the formatted parts
2046                formatted.sign = "";
2047                width = width.saturating_sub(sign.len() as u16);
2048                self.options.fill('0').align(Some(Alignment::Right));
2049            }
2050
2051            // remaining parts go through the ordinary padding process.
2052            let len = formatted.len();
2053            let ret = if usize::from(width) <= len {
2054                // no padding
2055                // SAFETY: Per the precondition.
2056                unsafe { self.write_formatted_parts(&formatted) }
2057            } else {
2058                let post_padding = self.padding(width - len as u16, Alignment::Right)?;
2059                // SAFETY: Per the precondition.
2060                unsafe {
2061                    self.write_formatted_parts(&formatted)?;
2062                }
2063                post_padding.write(self)
2064            };
2065            self.options = old_options;
2066            ret
2067        }
2068    }
2069
2070    /// # Safety
2071    ///
2072    /// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8.
2073    unsafe fn write_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
2074        unsafe fn write_bytes(buf: &mut dyn Write, s: &[u8]) -> Result {
2075            // SAFETY: This is used for `numfmt::Part::Num` and `numfmt::Part::Copy`.
2076            // It's safe to use for `numfmt::Part::Num` since every char `c` is between
2077            // `b'0'` and `b'9'`, which means `s` is valid UTF-8. It's safe to use for
2078            // `numfmt::Part::Copy` due to this function's precondition.
2079            buf.write_str(unsafe { str::from_utf8_unchecked(s) })
2080        }
2081
2082        if !formatted.sign.is_empty() {
2083            self.buf.write_str(formatted.sign)?;
2084        }
2085        for part in formatted.parts {
2086            match *part {
2087                numfmt::Part::Zero(mut nzeroes) => {
2088                    const ZEROES: &str = // 64 zeroes
2089                        "0000000000000000000000000000000000000000000000000000000000000000";
2090                    while nzeroes > ZEROES.len() {
2091                        self.buf.write_str(ZEROES)?;
2092                        nzeroes -= ZEROES.len();
2093                    }
2094                    if nzeroes > 0 {
2095                        self.buf.write_str(&ZEROES[..nzeroes])?;
2096                    }
2097                }
2098                numfmt::Part::Num(mut v) => {
2099                    let mut s = [0; 5];
2100                    let len = part.len();
2101                    for c in s[..len].iter_mut().rev() {
2102                        *c = b'0' + (v % 10) as u8;
2103                        v /= 10;
2104                    }
2105                    // SAFETY: Per the precondition.
2106                    unsafe {
2107                        write_bytes(self.buf, &s[..len])?;
2108                    }
2109                }
2110                // SAFETY: Per the precondition.
2111                numfmt::Part::Copy(buf) => unsafe {
2112                    write_bytes(self.buf, buf)?;
2113                },
2114            }
2115        }
2116        Ok(())
2117    }
2118
2119    /// Writes some data to the underlying buffer contained within this
2120    /// formatter.
2121    ///
2122    /// # Examples
2123    ///
2124    /// ```
2125    /// use std::fmt;
2126    ///
2127    /// struct Foo;
2128    ///
2129    /// impl fmt::Display for Foo {
2130    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2131    ///         formatter.write_str("Foo")
2132    ///         // This is equivalent to:
2133    ///         // write!(formatter, "Foo")
2134    ///     }
2135    /// }
2136    ///
2137    /// assert_eq!(format!("{Foo}"), "Foo");
2138    /// assert_eq!(format!("{Foo:0>8}"), "Foo");
2139    /// ```
2140    #[stable(feature = "rust1", since = "1.0.0")]
2141    pub fn write_str(&mut self, data: &str) -> Result {
2142        self.buf.write_str(data)
2143    }
2144
2145    /// Glue for usage of the [`write!`] macro with implementors of this trait.
2146    ///
2147    /// This method should generally not be invoked manually, but rather through
2148    /// the [`write!`] macro itself.
2149    ///
2150    /// Writes some formatted information into this instance.
2151    ///
2152    /// # Examples
2153    ///
2154    /// ```
2155    /// use std::fmt;
2156    ///
2157    /// struct Foo(i32);
2158    ///
2159    /// impl fmt::Display for Foo {
2160    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2161    ///         formatter.write_fmt(format_args!("Foo {}", self.0))
2162    ///     }
2163    /// }
2164    ///
2165    /// assert_eq!(format!("{}", Foo(-1)), "Foo -1");
2166    /// assert_eq!(format!("{:0>8}", Foo(2)), "Foo 2");
2167    /// ```
2168    #[stable(feature = "rust1", since = "1.0.0")]
2169    #[inline]
2170    pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result {
2171        if let Some(s) = fmt.as_statically_known_str() {
2172            self.buf.write_str(s)
2173        } else {
2174            write(self.buf, fmt)
2175        }
2176    }
2177
2178    /// Returns flags for formatting.
2179    #[must_use]
2180    #[stable(feature = "rust1", since = "1.0.0")]
2181    #[deprecated(
2182        since = "1.24.0",
2183        note = "use the `sign_plus`, `sign_minus`, `alternate`, \
2184                or `sign_aware_zero_pad` methods instead"
2185    )]
2186    pub fn flags(&self) -> u32 {
2187        // Extract the debug upper/lower hex, zero pad, alternate, and plus/minus flags
2188        // to stay compatible with older versions of Rust.
2189        self.options.flags >> 21 & 0x3F
2190    }
2191
2192    /// Returns the character used as 'fill' whenever there is alignment.
2193    ///
2194    /// # Examples
2195    ///
2196    /// ```
2197    /// use std::fmt;
2198    ///
2199    /// struct Foo;
2200    ///
2201    /// impl fmt::Display for Foo {
2202    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2203    ///         let c = formatter.fill();
2204    ///         if let Some(width) = formatter.width() {
2205    ///             for _ in 0..width {
2206    ///                 write!(formatter, "{c}")?;
2207    ///             }
2208    ///             Ok(())
2209    ///         } else {
2210    ///             write!(formatter, "{c}")
2211    ///         }
2212    ///     }
2213    /// }
2214    ///
2215    /// // We set alignment to the right with ">".
2216    /// assert_eq!(format!("{Foo:G>3}"), "GGG");
2217    /// assert_eq!(format!("{Foo:t>6}"), "tttttt");
2218    /// ```
2219    #[must_use]
2220    #[stable(feature = "fmt_flags", since = "1.5.0")]
2221    pub fn fill(&self) -> char {
2222        self.options.get_fill()
2223    }
2224
2225    /// Returns a flag indicating what form of alignment was requested.
2226    ///
2227    /// # Examples
2228    ///
2229    /// ```
2230    /// use std::fmt::{self, Alignment};
2231    ///
2232    /// struct Foo;
2233    ///
2234    /// impl fmt::Display for Foo {
2235    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2236    ///         let s = if let Some(s) = formatter.align() {
2237    ///             match s {
2238    ///                 Alignment::Left    => "left",
2239    ///                 Alignment::Right   => "right",
2240    ///                 Alignment::Center  => "center",
2241    ///             }
2242    ///         } else {
2243    ///             "into the void"
2244    ///         };
2245    ///         write!(formatter, "{s}")
2246    ///     }
2247    /// }
2248    ///
2249    /// assert_eq!(format!("{Foo:<}"), "left");
2250    /// assert_eq!(format!("{Foo:>}"), "right");
2251    /// assert_eq!(format!("{Foo:^}"), "center");
2252    /// assert_eq!(format!("{Foo}"), "into the void");
2253    /// ```
2254    #[must_use]
2255    #[stable(feature = "fmt_flags_align", since = "1.28.0")]
2256    pub fn align(&self) -> Option<Alignment> {
2257        self.options.get_align()
2258    }
2259
2260    /// Returns the optionally specified integer width that the output should be.
2261    ///
2262    /// # Examples
2263    ///
2264    /// ```
2265    /// use std::fmt;
2266    ///
2267    /// struct Foo(i32);
2268    ///
2269    /// impl fmt::Display for Foo {
2270    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2271    ///         if let Some(width) = formatter.width() {
2272    ///             // If we received a width, we use it
2273    ///             write!(formatter, "{:width$}", format!("Foo({})", self.0), width = width)
2274    ///         } else {
2275    ///             // Otherwise we do nothing special
2276    ///             write!(formatter, "Foo({})", self.0)
2277    ///         }
2278    ///     }
2279    /// }
2280    ///
2281    /// assert_eq!(format!("{:10}", Foo(23)), "Foo(23)   ");
2282    /// assert_eq!(format!("{}", Foo(23)), "Foo(23)");
2283    /// ```
2284    #[must_use]
2285    #[stable(feature = "fmt_flags", since = "1.5.0")]
2286    pub fn width(&self) -> Option<usize> {
2287        if self.options.flags & flags::WIDTH_FLAG == 0 {
2288            None
2289        } else {
2290            Some(self.options.width as usize)
2291        }
2292    }
2293
2294    /// Returns the optionally specified precision for numeric types.
2295    /// Alternatively, the maximum width for string types.
2296    ///
2297    /// # Examples
2298    ///
2299    /// ```
2300    /// use std::fmt;
2301    ///
2302    /// struct Foo(f32);
2303    ///
2304    /// impl fmt::Display for Foo {
2305    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2306    ///         if let Some(precision) = formatter.precision() {
2307    ///             // If we received a precision, we use it.
2308    ///             write!(formatter, "Foo({1:.*})", precision, self.0)
2309    ///         } else {
2310    ///             // Otherwise we default to 2.
2311    ///             write!(formatter, "Foo({:.2})", self.0)
2312    ///         }
2313    ///     }
2314    /// }
2315    ///
2316    /// assert_eq!(format!("{:.4}", Foo(23.2)), "Foo(23.2000)");
2317    /// assert_eq!(format!("{}", Foo(23.2)), "Foo(23.20)");
2318    /// ```
2319    #[must_use]
2320    #[stable(feature = "fmt_flags", since = "1.5.0")]
2321    pub fn precision(&self) -> Option<usize> {
2322        if self.options.flags & flags::PRECISION_FLAG == 0 {
2323            None
2324        } else {
2325            Some(self.options.precision as usize)
2326        }
2327    }
2328
2329    /// Determines if the `+` flag was specified.
2330    ///
2331    /// # Examples
2332    ///
2333    /// ```
2334    /// use std::fmt;
2335    ///
2336    /// struct Foo(i32);
2337    ///
2338    /// impl fmt::Display for Foo {
2339    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2340    ///         if formatter.sign_plus() {
2341    ///             write!(formatter,
2342    ///                    "Foo({}{})",
2343    ///                    if self.0 < 0 { '-' } else { '+' },
2344    ///                    self.0.abs())
2345    ///         } else {
2346    ///             write!(formatter, "Foo({})", self.0)
2347    ///         }
2348    ///     }
2349    /// }
2350    ///
2351    /// assert_eq!(format!("{:+}", Foo(23)), "Foo(+23)");
2352    /// assert_eq!(format!("{:+}", Foo(-23)), "Foo(-23)");
2353    /// assert_eq!(format!("{}", Foo(23)), "Foo(23)");
2354    /// ```
2355    #[must_use]
2356    #[stable(feature = "fmt_flags", since = "1.5.0")]
2357    pub fn sign_plus(&self) -> bool {
2358        self.options.flags & flags::SIGN_PLUS_FLAG != 0
2359    }
2360
2361    /// Determines if the `-` flag was specified.
2362    ///
2363    /// # Examples
2364    ///
2365    /// ```
2366    /// use std::fmt;
2367    ///
2368    /// struct Foo(i32);
2369    ///
2370    /// impl fmt::Display for Foo {
2371    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2372    ///         if formatter.sign_minus() {
2373    ///             // You want a minus sign? Have one!
2374    ///             write!(formatter, "-Foo({})", self.0)
2375    ///         } else {
2376    ///             write!(formatter, "Foo({})", self.0)
2377    ///         }
2378    ///     }
2379    /// }
2380    ///
2381    /// assert_eq!(format!("{:-}", Foo(23)), "-Foo(23)");
2382    /// assert_eq!(format!("{}", Foo(23)), "Foo(23)");
2383    /// ```
2384    #[must_use]
2385    #[stable(feature = "fmt_flags", since = "1.5.0")]
2386    pub fn sign_minus(&self) -> bool {
2387        self.options.flags & flags::SIGN_MINUS_FLAG != 0
2388    }
2389
2390    /// Determines if the `#` flag was specified.
2391    ///
2392    /// # Examples
2393    ///
2394    /// ```
2395    /// use std::fmt;
2396    ///
2397    /// struct Foo(i32);
2398    ///
2399    /// impl fmt::Display for Foo {
2400    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2401    ///         if formatter.alternate() {
2402    ///             write!(formatter, "Foo({})", self.0)
2403    ///         } else {
2404    ///             write!(formatter, "{}", self.0)
2405    ///         }
2406    ///     }
2407    /// }
2408    ///
2409    /// assert_eq!(format!("{:#}", Foo(23)), "Foo(23)");
2410    /// assert_eq!(format!("{}", Foo(23)), "23");
2411    /// ```
2412    #[must_use]
2413    #[stable(feature = "fmt_flags", since = "1.5.0")]
2414    pub fn alternate(&self) -> bool {
2415        self.options.flags & flags::ALTERNATE_FLAG != 0
2416    }
2417
2418    /// Determines if the `0` flag was specified.
2419    ///
2420    /// # Examples
2421    ///
2422    /// ```
2423    /// use std::fmt;
2424    ///
2425    /// struct Foo(i32);
2426    ///
2427    /// impl fmt::Display for Foo {
2428    ///     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2429    ///         assert!(formatter.sign_aware_zero_pad());
2430    ///         assert_eq!(formatter.width(), Some(4));
2431    ///         // We ignore the formatter's options.
2432    ///         write!(formatter, "{}", self.0)
2433    ///     }
2434    /// }
2435    ///
2436    /// assert_eq!(format!("{:04}", Foo(23)), "23");
2437    /// ```
2438    #[must_use]
2439    #[stable(feature = "fmt_flags", since = "1.5.0")]
2440    pub fn sign_aware_zero_pad(&self) -> bool {
2441        self.options.flags & flags::SIGN_AWARE_ZERO_PAD_FLAG != 0
2442    }
2443
2444    // FIXME: Decide what public API we want for these two flags.
2445    // https://github.com/rust-lang/rust/issues/48584
2446    fn debug_lower_hex(&self) -> bool {
2447        self.options.flags & flags::DEBUG_LOWER_HEX_FLAG != 0
2448    }
2449    fn debug_upper_hex(&self) -> bool {
2450        self.options.flags & flags::DEBUG_UPPER_HEX_FLAG != 0
2451    }
2452
2453    /// Creates a [`DebugStruct`] builder designed to assist with creation of
2454    /// [`fmt::Debug`] implementations for structs.
2455    ///
2456    /// [`fmt::Debug`]: self::Debug
2457    ///
2458    /// # Examples
2459    ///
2460    /// ```rust
2461    /// use std::fmt;
2462    /// use std::net::Ipv4Addr;
2463    ///
2464    /// struct Foo {
2465    ///     bar: i32,
2466    ///     baz: String,
2467    ///     addr: Ipv4Addr,
2468    /// }
2469    ///
2470    /// impl fmt::Debug for Foo {
2471    ///     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2472    ///         fmt.debug_struct("Foo")
2473    ///             .field("bar", &self.bar)
2474    ///             .field("baz", &self.baz)
2475    ///             .field("addr", &format_args!("{}", self.addr))
2476    ///             .finish()
2477    ///     }
2478    /// }
2479    ///
2480    /// assert_eq!(
2481    ///     "Foo { bar: 10, baz: \"Hello World\", addr: 127.0.0.1 }",
2482    ///     format!("{:?}", Foo {
2483    ///         bar: 10,
2484    ///         baz: "Hello World".to_string(),
2485    ///         addr: Ipv4Addr::new(127, 0, 0, 1),
2486    ///     })
2487    /// );
2488    /// ```
2489    #[stable(feature = "debug_builders", since = "1.2.0")]
2490    pub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a> {
2491        builders::debug_struct_new(self, name)
2492    }
2493
2494    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2495    /// binaries. `debug_struct_fields_finish` is more general, but this is
2496    /// faster for 1 field.
2497    #[doc(hidden)]
2498    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2499    pub fn debug_struct_field1_finish<'b>(
2500        &'b mut self,
2501        name: &str,
2502        name1: &str,
2503        value1: &dyn Debug,
2504    ) -> Result {
2505        let mut builder = builders::debug_struct_new(self, name);
2506        builder.field(name1, value1);
2507        builder.finish()
2508    }
2509
2510    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2511    /// binaries. `debug_struct_fields_finish` is more general, but this is
2512    /// faster for 2 fields.
2513    #[doc(hidden)]
2514    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2515    pub fn debug_struct_field2_finish<'b>(
2516        &'b mut self,
2517        name: &str,
2518        name1: &str,
2519        value1: &dyn Debug,
2520        name2: &str,
2521        value2: &dyn Debug,
2522    ) -> Result {
2523        let mut builder = builders::debug_struct_new(self, name);
2524        builder.field(name1, value1);
2525        builder.field(name2, value2);
2526        builder.finish()
2527    }
2528
2529    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2530    /// binaries. `debug_struct_fields_finish` is more general, but this is
2531    /// faster for 3 fields.
2532    #[doc(hidden)]
2533    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2534    pub fn debug_struct_field3_finish<'b>(
2535        &'b mut self,
2536        name: &str,
2537        name1: &str,
2538        value1: &dyn Debug,
2539        name2: &str,
2540        value2: &dyn Debug,
2541        name3: &str,
2542        value3: &dyn Debug,
2543    ) -> Result {
2544        let mut builder = builders::debug_struct_new(self, name);
2545        builder.field(name1, value1);
2546        builder.field(name2, value2);
2547        builder.field(name3, value3);
2548        builder.finish()
2549    }
2550
2551    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2552    /// binaries. `debug_struct_fields_finish` is more general, but this is
2553    /// faster for 4 fields.
2554    #[doc(hidden)]
2555    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2556    pub fn debug_struct_field4_finish<'b>(
2557        &'b mut self,
2558        name: &str,
2559        name1: &str,
2560        value1: &dyn Debug,
2561        name2: &str,
2562        value2: &dyn Debug,
2563        name3: &str,
2564        value3: &dyn Debug,
2565        name4: &str,
2566        value4: &dyn Debug,
2567    ) -> Result {
2568        let mut builder = builders::debug_struct_new(self, name);
2569        builder.field(name1, value1);
2570        builder.field(name2, value2);
2571        builder.field(name3, value3);
2572        builder.field(name4, value4);
2573        builder.finish()
2574    }
2575
2576    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2577    /// binaries. `debug_struct_fields_finish` is more general, but this is
2578    /// faster for 5 fields.
2579    #[doc(hidden)]
2580    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2581    pub fn debug_struct_field5_finish<'b>(
2582        &'b mut self,
2583        name: &str,
2584        name1: &str,
2585        value1: &dyn Debug,
2586        name2: &str,
2587        value2: &dyn Debug,
2588        name3: &str,
2589        value3: &dyn Debug,
2590        name4: &str,
2591        value4: &dyn Debug,
2592        name5: &str,
2593        value5: &dyn Debug,
2594    ) -> Result {
2595        let mut builder = builders::debug_struct_new(self, name);
2596        builder.field(name1, value1);
2597        builder.field(name2, value2);
2598        builder.field(name3, value3);
2599        builder.field(name4, value4);
2600        builder.field(name5, value5);
2601        builder.finish()
2602    }
2603
2604    /// Shrinks `derive(Debug)` code, for faster compilation and smaller binaries.
2605    /// For the cases not covered by `debug_struct_field[12345]_finish`.
2606    #[doc(hidden)]
2607    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2608    pub fn debug_struct_fields_finish<'b>(
2609        &'b mut self,
2610        name: &str,
2611        names: &[&str],
2612        values: &[&dyn Debug],
2613    ) -> Result {
2614        assert_eq!(names.len(), values.len());
2615        let mut builder = builders::debug_struct_new(self, name);
2616        for (name, value) in iter::zip(names, values) {
2617            builder.field(name, value);
2618        }
2619        builder.finish()
2620    }
2621
2622    /// Creates a `DebugTuple` builder designed to assist with creation of
2623    /// `fmt::Debug` implementations for tuple structs.
2624    ///
2625    /// # Examples
2626    ///
2627    /// ```rust
2628    /// use std::fmt;
2629    /// use std::marker::PhantomData;
2630    ///
2631    /// struct Foo<T>(i32, String, PhantomData<T>);
2632    ///
2633    /// impl<T> fmt::Debug for Foo<T> {
2634    ///     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2635    ///         fmt.debug_tuple("Foo")
2636    ///             .field(&self.0)
2637    ///             .field(&self.1)
2638    ///             .field(&format_args!("_"))
2639    ///             .finish()
2640    ///     }
2641    /// }
2642    ///
2643    /// assert_eq!(
2644    ///     "Foo(10, \"Hello\", _)",
2645    ///     format!("{:?}", Foo(10, "Hello".to_string(), PhantomData::<u8>))
2646    /// );
2647    /// ```
2648    #[stable(feature = "debug_builders", since = "1.2.0")]
2649    pub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a> {
2650        builders::debug_tuple_new(self, name)
2651    }
2652
2653    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2654    /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2655    /// for 1 field.
2656    #[doc(hidden)]
2657    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2658    pub fn debug_tuple_field1_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug) -> Result {
2659        let mut builder = builders::debug_tuple_new(self, name);
2660        builder.field(value1);
2661        builder.finish()
2662    }
2663
2664    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2665    /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2666    /// for 2 fields.
2667    #[doc(hidden)]
2668    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2669    pub fn debug_tuple_field2_finish<'b>(
2670        &'b mut self,
2671        name: &str,
2672        value1: &dyn Debug,
2673        value2: &dyn Debug,
2674    ) -> Result {
2675        let mut builder = builders::debug_tuple_new(self, name);
2676        builder.field(value1);
2677        builder.field(value2);
2678        builder.finish()
2679    }
2680
2681    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2682    /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2683    /// for 3 fields.
2684    #[doc(hidden)]
2685    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2686    pub fn debug_tuple_field3_finish<'b>(
2687        &'b mut self,
2688        name: &str,
2689        value1: &dyn Debug,
2690        value2: &dyn Debug,
2691        value3: &dyn Debug,
2692    ) -> Result {
2693        let mut builder = builders::debug_tuple_new(self, name);
2694        builder.field(value1);
2695        builder.field(value2);
2696        builder.field(value3);
2697        builder.finish()
2698    }
2699
2700    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2701    /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2702    /// for 4 fields.
2703    #[doc(hidden)]
2704    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2705    pub fn debug_tuple_field4_finish<'b>(
2706        &'b mut self,
2707        name: &str,
2708        value1: &dyn Debug,
2709        value2: &dyn Debug,
2710        value3: &dyn Debug,
2711        value4: &dyn Debug,
2712    ) -> Result {
2713        let mut builder = builders::debug_tuple_new(self, name);
2714        builder.field(value1);
2715        builder.field(value2);
2716        builder.field(value3);
2717        builder.field(value4);
2718        builder.finish()
2719    }
2720
2721    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2722    /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2723    /// for 5 fields.
2724    #[doc(hidden)]
2725    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2726    pub fn debug_tuple_field5_finish<'b>(
2727        &'b mut self,
2728        name: &str,
2729        value1: &dyn Debug,
2730        value2: &dyn Debug,
2731        value3: &dyn Debug,
2732        value4: &dyn Debug,
2733        value5: &dyn Debug,
2734    ) -> Result {
2735        let mut builder = builders::debug_tuple_new(self, name);
2736        builder.field(value1);
2737        builder.field(value2);
2738        builder.field(value3);
2739        builder.field(value4);
2740        builder.field(value5);
2741        builder.finish()
2742    }
2743
2744    /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2745    /// binaries. For the cases not covered by `debug_tuple_field[12345]_finish`.
2746    #[doc(hidden)]
2747    #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2748    pub fn debug_tuple_fields_finish<'b>(
2749        &'b mut self,
2750        name: &str,
2751        values: &[&dyn Debug],
2752    ) -> Result {
2753        let mut builder = builders::debug_tuple_new(self, name);
2754        for value in values {
2755            builder.field(value);
2756        }
2757        builder.finish()
2758    }
2759
2760    /// Creates a `DebugList` builder designed to assist with creation of
2761    /// `fmt::Debug` implementations for list-like structures.
2762    ///
2763    /// # Examples
2764    ///
2765    /// ```rust
2766    /// use std::fmt;
2767    ///
2768    /// struct Foo(Vec<i32>);
2769    ///
2770    /// impl fmt::Debug for Foo {
2771    ///     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2772    ///         fmt.debug_list().entries(self.0.iter()).finish()
2773    ///     }
2774    /// }
2775    ///
2776    /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]");
2777    /// ```
2778    #[stable(feature = "debug_builders", since = "1.2.0")]
2779    pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a> {
2780        builders::debug_list_new(self)
2781    }
2782
2783    /// Creates a `DebugSet` builder designed to assist with creation of
2784    /// `fmt::Debug` implementations for set-like structures.
2785    ///
2786    /// # Examples
2787    ///
2788    /// ```rust
2789    /// use std::fmt;
2790    ///
2791    /// struct Foo(Vec<i32>);
2792    ///
2793    /// impl fmt::Debug for Foo {
2794    ///     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2795    ///         fmt.debug_set().entries(self.0.iter()).finish()
2796    ///     }
2797    /// }
2798    ///
2799    /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
2800    /// ```
2801    ///
2802    /// [`format_args!`]: crate::format_args
2803    ///
2804    /// In this more complex example, we use [`format_args!`] and `.debug_set()`
2805    /// to build a list of match arms:
2806    ///
2807    /// ```rust
2808    /// use std::fmt;
2809    ///
2810    /// struct Arm<'a, L, R>(&'a (L, R));
2811    /// struct Table<'a, K, V>(&'a [(K, V)], V);
2812    ///
2813    /// impl<'a, L, R> fmt::Debug for Arm<'a, L, R>
2814    /// where
2815    ///     L: 'a + fmt::Debug, R: 'a + fmt::Debug
2816    /// {
2817    ///     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2818    ///         L::fmt(&(self.0).0, fmt)?;
2819    ///         fmt.write_str(" => ")?;
2820    ///         R::fmt(&(self.0).1, fmt)
2821    ///     }
2822    /// }
2823    ///
2824    /// impl<'a, K, V> fmt::Debug for Table<'a, K, V>
2825    /// where
2826    ///     K: 'a + fmt::Debug, V: 'a + fmt::Debug
2827    /// {
2828    ///     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2829    ///         fmt.debug_set()
2830    ///         .entries(self.0.iter().map(Arm))
2831    ///         .entry(&Arm(&(format_args!("_"), &self.1)))
2832    ///         .finish()
2833    ///     }
2834    /// }
2835    /// ```
2836    #[stable(feature = "debug_builders", since = "1.2.0")]
2837    pub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a> {
2838        builders::debug_set_new(self)
2839    }
2840
2841    /// Creates a `DebugMap` builder designed to assist with creation of
2842    /// `fmt::Debug` implementations for map-like structures.
2843    ///
2844    /// # Examples
2845    ///
2846    /// ```rust
2847    /// use std::fmt;
2848    ///
2849    /// struct Foo(Vec<(String, i32)>);
2850    ///
2851    /// impl fmt::Debug for Foo {
2852    ///     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2853    ///         fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
2854    ///     }
2855    /// }
2856    ///
2857    /// assert_eq!(
2858    ///     format!("{:?}",  Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])),
2859    ///     r#"{"A": 10, "B": 11}"#
2860    ///  );
2861    /// ```
2862    #[stable(feature = "debug_builders", since = "1.2.0")]
2863    pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a> {
2864        builders::debug_map_new(self)
2865    }
2866
2867    /// Returns the sign of this formatter (`+` or `-`).
2868    #[unstable(feature = "formatting_options", issue = "118117")]
2869    pub const fn sign(&self) -> Option<Sign> {
2870        self.options.get_sign()
2871    }
2872
2873    /// Returns the formatting options this formatter corresponds to.
2874    #[unstable(feature = "formatting_options", issue = "118117")]
2875    pub const fn options(&self) -> FormattingOptions {
2876        self.options
2877    }
2878}
2879
2880#[stable(since = "1.2.0", feature = "formatter_write")]
2881#[cfg(not(feature = "ferrocene_certified"))]
2882impl Write for Formatter<'_> {
2883    fn write_str(&mut self, s: &str) -> Result {
2884        self.buf.write_str(s)
2885    }
2886
2887    fn write_char(&mut self, c: char) -> Result {
2888        self.buf.write_char(c)
2889    }
2890
2891    #[inline]
2892    fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
2893        if let Some(s) = args.as_statically_known_str() {
2894            self.buf.write_str(s)
2895        } else {
2896            write(self.buf, args)
2897        }
2898    }
2899}
2900
2901#[stable(feature = "rust1", since = "1.0.0")]
2902#[cfg(not(feature = "ferrocene_certified"))]
2903impl Display for Error {
2904    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2905        Display::fmt("an error occurred when formatting an argument", f)
2906    }
2907}
2908
2909// Implementations of the core formatting traits
2910
2911#[cfg(not(feature = "ferrocene_certified"))]
2912macro_rules! fmt_refs {
2913    ($($tr:ident),*) => {
2914        $(
2915        #[stable(feature = "rust1", since = "1.0.0")]
2916        impl<T: PointeeSized + $tr> $tr for &T {
2917            fn fmt(&self, f: &mut Formatter<'_>) -> Result { $tr::fmt(&**self, f) }
2918        }
2919        #[stable(feature = "rust1", since = "1.0.0")]
2920        impl<T: PointeeSized + $tr> $tr for &mut T {
2921            fn fmt(&self, f: &mut Formatter<'_>) -> Result { $tr::fmt(&**self, f) }
2922        }
2923        )*
2924    }
2925}
2926
2927#[cfg(not(feature = "ferrocene_certified"))]
2928fmt_refs! { Debug, Display, Octal, Binary, LowerHex, UpperHex, LowerExp, UpperExp }
2929
2930#[unstable(feature = "never_type", issue = "35121")]
2931#[cfg(not(feature = "ferrocene_certified"))]
2932impl Debug for ! {
2933    #[inline]
2934    fn fmt(&self, _: &mut Formatter<'_>) -> Result {
2935        *self
2936    }
2937}
2938
2939#[unstable(feature = "never_type", issue = "35121")]
2940#[cfg(not(feature = "ferrocene_certified"))]
2941impl Display for ! {
2942    #[inline]
2943    fn fmt(&self, _: &mut Formatter<'_>) -> Result {
2944        *self
2945    }
2946}
2947
2948#[stable(feature = "rust1", since = "1.0.0")]
2949#[cfg(not(feature = "ferrocene_certified"))]
2950impl Debug for bool {
2951    #[inline]
2952    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2953        Display::fmt(self, f)
2954    }
2955}
2956
2957#[stable(feature = "rust1", since = "1.0.0")]
2958#[cfg(not(feature = "ferrocene_certified"))]
2959impl Display for bool {
2960    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2961        Display::fmt(if *self { "true" } else { "false" }, f)
2962    }
2963}
2964
2965#[stable(feature = "rust1", since = "1.0.0")]
2966#[cfg(not(feature = "ferrocene_certified"))]
2967impl Debug for str {
2968    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2969        f.write_char('"')?;
2970
2971        // substring we know is printable
2972        let mut printable_range = 0..0;
2973
2974        fn needs_escape(b: u8) -> bool {
2975            b > 0x7E || b < 0x20 || b == b'\\' || b == b'"'
2976        }
2977
2978        // the loop here first skips over runs of printable ASCII as a fast path.
2979        // other chars (unicode, or ASCII that needs escaping) are then handled per-`char`.
2980        let mut rest = self;
2981        while rest.len() > 0 {
2982            let Some(non_printable_start) = rest.as_bytes().iter().position(|&b| needs_escape(b))
2983            else {
2984                printable_range.end += rest.len();
2985                break;
2986            };
2987
2988            printable_range.end += non_printable_start;
2989            // SAFETY: the position was derived from an iterator, so is known to be within bounds, and at a char boundary
2990            rest = unsafe { rest.get_unchecked(non_printable_start..) };
2991
2992            let mut chars = rest.chars();
2993            if let Some(c) = chars.next() {
2994                let esc = c.escape_debug_ext(EscapeDebugExtArgs {
2995                    escape_grapheme_extended: true,
2996                    escape_single_quote: false,
2997                    escape_double_quote: true,
2998                });
2999                if esc.len() != 1 {
3000                    f.write_str(&self[printable_range.clone()])?;
3001                    Display::fmt(&esc, f)?;
3002                    printable_range.start = printable_range.end + c.len_utf8();
3003                }
3004                printable_range.end += c.len_utf8();
3005            }
3006            rest = chars.as_str();
3007        }
3008
3009        f.write_str(&self[printable_range])?;
3010
3011        f.write_char('"')
3012    }
3013}
3014
3015#[stable(feature = "rust1", since = "1.0.0")]
3016#[cfg(not(feature = "ferrocene_certified"))]
3017impl Display for str {
3018    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3019        f.pad(self)
3020    }
3021}
3022
3023#[stable(feature = "rust1", since = "1.0.0")]
3024#[cfg(not(feature = "ferrocene_certified"))]
3025impl Debug for char {
3026    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3027        f.write_char('\'')?;
3028        let esc = self.escape_debug_ext(EscapeDebugExtArgs {
3029            escape_grapheme_extended: true,
3030            escape_single_quote: true,
3031            escape_double_quote: false,
3032        });
3033        Display::fmt(&esc, f)?;
3034        f.write_char('\'')
3035    }
3036}
3037
3038#[stable(feature = "rust1", since = "1.0.0")]
3039#[cfg(not(feature = "ferrocene_certified"))]
3040impl Display for char {
3041    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3042        if f.options.flags & (flags::WIDTH_FLAG | flags::PRECISION_FLAG) == 0 {
3043            f.write_char(*self)
3044        } else {
3045            f.pad(self.encode_utf8(&mut [0; MAX_LEN_UTF8]))
3046        }
3047    }
3048}
3049
3050#[stable(feature = "rust1", since = "1.0.0")]
3051#[cfg(not(feature = "ferrocene_certified"))]
3052impl<T: PointeeSized> Pointer for *const T {
3053    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3054        if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() {
3055            pointer_fmt_inner(self.expose_provenance(), f)
3056        } else {
3057            f.debug_struct("Pointer")
3058                .field_with("addr", |f| pointer_fmt_inner(self.expose_provenance(), f))
3059                .field("metadata", &core::ptr::metadata(*self))
3060                .finish()
3061        }
3062    }
3063}
3064
3065/// Since the formatting will be identical for all pointer types, uses a
3066/// non-monomorphized implementation for the actual formatting to reduce the
3067/// amount of codegen work needed.
3068///
3069/// This uses `ptr_addr: usize` and not `ptr: *const ()` to be able to use this for
3070/// `fn(...) -> ...` without using [problematic] "Oxford Casts".
3071///
3072/// [problematic]: https://github.com/rust-lang/rust/issues/95489
3073#[cfg(not(feature = "ferrocene_certified"))]
3074pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result {
3075    let old_options = f.options;
3076
3077    // The alternate flag is already treated by LowerHex as being special-
3078    // it denotes whether to prefix with 0x. We use it to work out whether
3079    // or not to zero extend, and then unconditionally set it to get the
3080    // prefix.
3081    if f.options.get_alternate() {
3082        f.options.sign_aware_zero_pad(true);
3083
3084        if f.options.get_width().is_none() {
3085            f.options.width(Some((usize::BITS / 4) as u16 + 2));
3086        }
3087    }
3088    f.options.alternate(true);
3089
3090    let ret = LowerHex::fmt(&ptr_addr, f);
3091
3092    f.options = old_options;
3093
3094    ret
3095}
3096
3097#[stable(feature = "rust1", since = "1.0.0")]
3098#[cfg(not(feature = "ferrocene_certified"))]
3099impl<T: PointeeSized> Pointer for *mut T {
3100    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3101        Pointer::fmt(&(*self as *const T), f)
3102    }
3103}
3104
3105#[stable(feature = "rust1", since = "1.0.0")]
3106#[cfg(not(feature = "ferrocene_certified"))]
3107impl<T: PointeeSized> Pointer for &T {
3108    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3109        Pointer::fmt(&(*self as *const T), f)
3110    }
3111}
3112
3113#[stable(feature = "rust1", since = "1.0.0")]
3114#[cfg(not(feature = "ferrocene_certified"))]
3115impl<T: PointeeSized> Pointer for &mut T {
3116    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3117        Pointer::fmt(&(&**self as *const T), f)
3118    }
3119}
3120
3121// Implementation of Display/Debug for various core types
3122
3123#[stable(feature = "rust1", since = "1.0.0")]
3124#[cfg(not(feature = "ferrocene_certified"))]
3125impl<T: PointeeSized> Debug for *const T {
3126    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3127        Pointer::fmt(self, f)
3128    }
3129}
3130#[stable(feature = "rust1", since = "1.0.0")]
3131#[cfg(not(feature = "ferrocene_certified"))]
3132impl<T: PointeeSized> Debug for *mut T {
3133    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3134        Pointer::fmt(self, f)
3135    }
3136}
3137
3138#[cfg(not(feature = "ferrocene_certified"))]
3139macro_rules! peel {
3140    ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
3141}
3142
3143#[cfg(not(feature = "ferrocene_certified"))]
3144macro_rules! tuple {
3145    () => ();
3146    ( $($name:ident,)+ ) => (
3147        maybe_tuple_doc! {
3148            $($name)+ @
3149            #[stable(feature = "rust1", since = "1.0.0")]
3150            impl<$($name:Debug),+> Debug for ($($name,)+) {
3151                #[allow(non_snake_case, unused_assignments)]
3152                fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3153                    let mut builder = f.debug_tuple("");
3154                    let ($(ref $name,)+) = *self;
3155                    $(
3156                        builder.field(&$name);
3157                    )+
3158
3159                    builder.finish()
3160                }
3161            }
3162        }
3163        peel! { $($name,)+ }
3164    )
3165}
3166
3167#[cfg(not(feature = "ferrocene_certified"))]
3168macro_rules! maybe_tuple_doc {
3169    ($a:ident @ #[$meta:meta] $item:item) => {
3170        #[doc(fake_variadic)]
3171        #[doc = "This trait is implemented for tuples up to twelve items long."]
3172        #[$meta]
3173        $item
3174    };
3175    ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
3176        #[doc(hidden)]
3177        #[$meta]
3178        $item
3179    };
3180}
3181
3182#[cfg(not(feature = "ferrocene_certified"))]
3183tuple! { E, D, C, B, A, Z, Y, X, W, V, U, T, }
3184
3185#[stable(feature = "rust1", since = "1.0.0")]
3186#[cfg(not(feature = "ferrocene_certified"))]
3187impl<T: Debug> Debug for [T] {
3188    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3189        f.debug_list().entries(self.iter()).finish()
3190    }
3191}
3192
3193#[stable(feature = "rust1", since = "1.0.0")]
3194#[cfg(not(feature = "ferrocene_certified"))]
3195impl Debug for () {
3196    #[inline]
3197    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3198        f.pad("()")
3199    }
3200}
3201#[stable(feature = "rust1", since = "1.0.0")]
3202#[cfg(not(feature = "ferrocene_certified"))]
3203impl<T: ?Sized> Debug for PhantomData<T> {
3204    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3205        write!(f, "PhantomData<{}>", crate::any::type_name::<T>())
3206    }
3207}
3208
3209#[stable(feature = "rust1", since = "1.0.0")]
3210#[cfg(not(feature = "ferrocene_certified"))]
3211impl<T: Copy + Debug> Debug for Cell<T> {
3212    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3213        f.debug_struct("Cell").field("value", &self.get()).finish()
3214    }
3215}
3216
3217#[stable(feature = "rust1", since = "1.0.0")]
3218#[cfg(not(feature = "ferrocene_certified"))]
3219impl<T: ?Sized + Debug> Debug for RefCell<T> {
3220    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3221        let mut d = f.debug_struct("RefCell");
3222        match self.try_borrow() {
3223            Ok(borrow) => d.field("value", &borrow),
3224            Err(_) => d.field("value", &format_args!("<borrowed>")),
3225        };
3226        d.finish()
3227    }
3228}
3229
3230#[stable(feature = "rust1", since = "1.0.0")]
3231#[cfg(not(feature = "ferrocene_certified"))]
3232impl<T: ?Sized + Debug> Debug for Ref<'_, T> {
3233    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3234        Debug::fmt(&**self, f)
3235    }
3236}
3237
3238#[stable(feature = "rust1", since = "1.0.0")]
3239#[cfg(not(feature = "ferrocene_certified"))]
3240impl<T: ?Sized + Debug> Debug for RefMut<'_, T> {
3241    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3242        Debug::fmt(&*(self.deref()), f)
3243    }
3244}
3245
3246#[stable(feature = "core_impl_debug", since = "1.9.0")]
3247#[cfg(not(feature = "ferrocene_certified"))]
3248impl<T: ?Sized> Debug for UnsafeCell<T> {
3249    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3250        f.debug_struct("UnsafeCell").finish_non_exhaustive()
3251    }
3252}
3253
3254#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
3255#[cfg(not(feature = "ferrocene_certified"))]
3256impl<T: ?Sized> Debug for SyncUnsafeCell<T> {
3257    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3258        f.debug_struct("SyncUnsafeCell").finish_non_exhaustive()
3259    }
3260}
3261
3262// If you expected tests to be here, look instead at coretests/tests/fmt/;
3263// it's a lot easier than creating all of the rt::Piece structures here.
3264// There are also tests in alloctests/tests/fmt.rs, for those that need allocations.