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