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