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