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 // Padding widths are capped at `u16`, so reaching this branch means
2064 // the formatted output is also shorter than `u16::MAX`.
2065 let len = match u16::try_from(len) {
2066 Ok(len) => len,
2067 Err(_) => unreachable!(),
2068 };
2069 let post_padding = self.padding(width - len, Alignment::Right)?;
2070 // SAFETY: Per the precondition.
2071 unsafe {
2072 self.write_formatted_parts(&formatted)?;
2073 }
2074 post_padding.write(self)
2075 };
2076 self.options = old_options;
2077 ret
2078 }
2079 }
2080
2081 /// # Safety
2082 ///
2083 /// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8.
2084 #[ferrocene::prevalidated]
2085 unsafe fn write_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
2086 #[ferrocene::prevalidated]
2087 unsafe fn write_bytes(buf: &mut dyn Write, s: &[u8]) -> Result {
2088 // SAFETY: This is used for `numfmt::Part::Num` and `numfmt::Part::Copy`.
2089 // It's safe to use for `numfmt::Part::Num` since every char `c` is between
2090 // `b'0'` and `b'9'`, which means `s` is valid UTF-8. It's safe to use for
2091 // `numfmt::Part::Copy` due to this function's precondition.
2092 buf.write_str(unsafe { str::from_utf8_unchecked(s) })
2093 }
2094
2095 if !formatted.sign.is_empty() {
2096 self.buf.write_str(formatted.sign)?;
2097 }
2098 for part in formatted.parts {
2099 match *part {
2100 numfmt::Part::Zero(mut nzeroes) => {
2101 const ZEROES: &str = // 64 zeroes
2102 "0000000000000000000000000000000000000000000000000000000000000000";
2103 while nzeroes > ZEROES.len() {
2104 self.buf.write_str(ZEROES)?;
2105 nzeroes -= ZEROES.len();
2106 }
2107 if nzeroes > 0 {
2108 self.buf.write_str(&ZEROES[..nzeroes])?;
2109 }
2110 }
2111 numfmt::Part::Num(mut v) => {
2112 let mut s = [0; 5];
2113 let len = part.len();
2114 for c in s[..len].iter_mut().rev() {
2115 *c = b'0' + (v % 10) as u8;
2116 v /= 10;
2117 }
2118 // SAFETY: Per the precondition.
2119 unsafe {
2120 write_bytes(self.buf, &s[..len])?;
2121 }
2122 }
2123 // SAFETY: Per the precondition.
2124 numfmt::Part::Copy(buf) => unsafe {
2125 write_bytes(self.buf, buf)?;
2126 },
2127 }
2128 }
2129 Ok(())
2130 }
2131
2132 /// Writes some data to the underlying buffer contained within this
2133 /// formatter.
2134 ///
2135 /// # Examples
2136 ///
2137 /// ```
2138 /// use std::fmt;
2139 ///
2140 /// struct Foo;
2141 ///
2142 /// impl fmt::Display for Foo {
2143 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2144 /// formatter.write_str("Foo")
2145 /// // This is equivalent to:
2146 /// // write!(formatter, "Foo")
2147 /// }
2148 /// }
2149 ///
2150 /// assert_eq!(format!("{Foo}"), "Foo");
2151 /// assert_eq!(format!("{Foo:0>8}"), "Foo");
2152 /// ```
2153 #[stable(feature = "rust1", since = "1.0.0")]
2154 #[ferrocene::prevalidated]
2155 pub fn write_str(&mut self, data: &str) -> Result {
2156 self.buf.write_str(data)
2157 }
2158
2159 /// Glue for usage of the [`write!`] macro with implementors of this trait.
2160 ///
2161 /// This method should generally not be invoked manually, but rather through
2162 /// the [`write!`] macro itself.
2163 ///
2164 /// Writes some formatted information into this instance.
2165 ///
2166 /// # Examples
2167 ///
2168 /// ```
2169 /// use std::fmt;
2170 ///
2171 /// struct Foo(i32);
2172 ///
2173 /// impl fmt::Display for Foo {
2174 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2175 /// formatter.write_fmt(format_args!("Foo {}", self.0))
2176 /// }
2177 /// }
2178 ///
2179 /// assert_eq!(format!("{}", Foo(-1)), "Foo -1");
2180 /// assert_eq!(format!("{:0>8}", Foo(2)), "Foo 2");
2181 /// ```
2182 #[stable(feature = "rust1", since = "1.0.0")]
2183 #[inline]
2184 #[ferrocene::prevalidated]
2185 pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result {
2186 if let Some(s) = fmt.as_statically_known_str() {
2187 self.buf.write_str(s)
2188 } else {
2189 write(self.buf, fmt)
2190 }
2191 }
2192
2193 /// Returns flags for formatting.
2194 #[must_use]
2195 #[stable(feature = "rust1", since = "1.0.0")]
2196 #[deprecated(
2197 since = "1.24.0",
2198 note = "use the `sign_plus`, `sign_minus`, `alternate`, \
2199 or `sign_aware_zero_pad` methods instead"
2200 )]
2201 #[ferrocene::prevalidated]
2202 pub fn flags(&self) -> u32 {
2203 // Extract the debug upper/lower hex, zero pad, alternate, and plus/minus flags
2204 // to stay compatible with older versions of Rust.
2205 self.options.flags >> 21 & 0x3F
2206 }
2207
2208 /// Returns the character used as 'fill' whenever there is alignment.
2209 ///
2210 /// # Examples
2211 ///
2212 /// ```
2213 /// use std::fmt;
2214 ///
2215 /// struct Foo;
2216 ///
2217 /// impl fmt::Display for Foo {
2218 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2219 /// let c = formatter.fill();
2220 /// if let Some(width) = formatter.width() {
2221 /// for _ in 0..width {
2222 /// write!(formatter, "{c}")?;
2223 /// }
2224 /// Ok(())
2225 /// } else {
2226 /// write!(formatter, "{c}")
2227 /// }
2228 /// }
2229 /// }
2230 ///
2231 /// // We set alignment to the right with ">".
2232 /// assert_eq!(format!("{Foo:G>3}"), "GGG");
2233 /// assert_eq!(format!("{Foo:t>6}"), "tttttt");
2234 /// ```
2235 #[must_use]
2236 #[stable(feature = "fmt_flags", since = "1.5.0")]
2237 #[ferrocene::prevalidated]
2238 pub fn fill(&self) -> char {
2239 self.options.get_fill()
2240 }
2241
2242 /// Returns a flag indicating what form of alignment was requested.
2243 ///
2244 /// # Examples
2245 ///
2246 /// ```
2247 /// use std::fmt::{self, Alignment};
2248 ///
2249 /// struct Foo;
2250 ///
2251 /// impl fmt::Display for Foo {
2252 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2253 /// let s = if let Some(s) = formatter.align() {
2254 /// match s {
2255 /// Alignment::Left => "left",
2256 /// Alignment::Right => "right",
2257 /// Alignment::Center => "center",
2258 /// }
2259 /// } else {
2260 /// "into the void"
2261 /// };
2262 /// write!(formatter, "{s}")
2263 /// }
2264 /// }
2265 ///
2266 /// assert_eq!(format!("{Foo:<}"), "left");
2267 /// assert_eq!(format!("{Foo:>}"), "right");
2268 /// assert_eq!(format!("{Foo:^}"), "center");
2269 /// assert_eq!(format!("{Foo}"), "into the void");
2270 /// ```
2271 #[must_use]
2272 #[stable(feature = "fmt_flags_align", since = "1.28.0")]
2273 #[ferrocene::prevalidated]
2274 pub fn align(&self) -> Option<Alignment> {
2275 self.options.get_align()
2276 }
2277
2278 /// Returns the optionally specified integer width that the output should be.
2279 ///
2280 /// # Examples
2281 ///
2282 /// ```
2283 /// use std::fmt;
2284 ///
2285 /// struct Foo(i32);
2286 ///
2287 /// impl fmt::Display for Foo {
2288 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2289 /// if let Some(width) = formatter.width() {
2290 /// // If we received a width, we use it
2291 /// write!(formatter, "{:width$}", format!("Foo({})", self.0), width = width)
2292 /// } else {
2293 /// // Otherwise we do nothing special
2294 /// write!(formatter, "Foo({})", self.0)
2295 /// }
2296 /// }
2297 /// }
2298 ///
2299 /// assert_eq!(format!("{:10}", Foo(23)), "Foo(23) ");
2300 /// assert_eq!(format!("{}", Foo(23)), "Foo(23)");
2301 /// ```
2302 #[must_use]
2303 #[stable(feature = "fmt_flags", since = "1.5.0")]
2304 #[ferrocene::prevalidated]
2305 pub fn width(&self) -> Option<usize> {
2306 if self.options.flags & flags::WIDTH_FLAG == 0 {
2307 None
2308 } else {
2309 Some(self.options.width as usize)
2310 }
2311 }
2312
2313 /// Returns the optionally specified precision for numeric types.
2314 /// Alternatively, the maximum width for string types.
2315 ///
2316 /// # Examples
2317 ///
2318 /// ```
2319 /// use std::fmt;
2320 ///
2321 /// struct Foo(f32);
2322 ///
2323 /// impl fmt::Display for Foo {
2324 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2325 /// if let Some(precision) = formatter.precision() {
2326 /// // If we received a precision, we use it.
2327 /// write!(formatter, "Foo({1:.*})", precision, self.0)
2328 /// } else {
2329 /// // Otherwise we default to 2.
2330 /// write!(formatter, "Foo({:.2})", self.0)
2331 /// }
2332 /// }
2333 /// }
2334 ///
2335 /// assert_eq!(format!("{:.4}", Foo(23.2)), "Foo(23.2000)");
2336 /// assert_eq!(format!("{}", Foo(23.2)), "Foo(23.20)");
2337 /// ```
2338 #[must_use]
2339 #[stable(feature = "fmt_flags", since = "1.5.0")]
2340 #[ferrocene::prevalidated]
2341 pub fn precision(&self) -> Option<usize> {
2342 if self.options.flags & flags::PRECISION_FLAG == 0 {
2343 None
2344 } else {
2345 Some(self.options.precision as usize)
2346 }
2347 }
2348
2349 /// Determines if the `+` flag was specified.
2350 ///
2351 /// # Examples
2352 ///
2353 /// ```
2354 /// use std::fmt;
2355 ///
2356 /// struct Foo(i32);
2357 ///
2358 /// impl fmt::Display for Foo {
2359 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2360 /// if formatter.sign_plus() {
2361 /// write!(formatter,
2362 /// "Foo({}{})",
2363 /// if self.0 < 0 { '-' } else { '+' },
2364 /// self.0.abs())
2365 /// } else {
2366 /// write!(formatter, "Foo({})", self.0)
2367 /// }
2368 /// }
2369 /// }
2370 ///
2371 /// assert_eq!(format!("{:+}", Foo(23)), "Foo(+23)");
2372 /// assert_eq!(format!("{:+}", Foo(-23)), "Foo(-23)");
2373 /// assert_eq!(format!("{}", Foo(23)), "Foo(23)");
2374 /// ```
2375 #[must_use]
2376 #[stable(feature = "fmt_flags", since = "1.5.0")]
2377 #[ferrocene::prevalidated]
2378 pub fn sign_plus(&self) -> bool {
2379 self.options.flags & flags::SIGN_PLUS_FLAG != 0
2380 }
2381
2382 /// Determines if the `-` flag was specified.
2383 ///
2384 /// # Examples
2385 ///
2386 /// ```
2387 /// use std::fmt;
2388 ///
2389 /// struct Foo(i32);
2390 ///
2391 /// impl fmt::Display for Foo {
2392 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2393 /// if formatter.sign_minus() {
2394 /// // You want a minus sign? Have one!
2395 /// write!(formatter, "-Foo({})", self.0)
2396 /// } else {
2397 /// write!(formatter, "Foo({})", self.0)
2398 /// }
2399 /// }
2400 /// }
2401 ///
2402 /// assert_eq!(format!("{:-}", Foo(23)), "-Foo(23)");
2403 /// assert_eq!(format!("{}", Foo(23)), "Foo(23)");
2404 /// ```
2405 #[must_use]
2406 #[stable(feature = "fmt_flags", since = "1.5.0")]
2407 #[ferrocene::prevalidated]
2408 pub fn sign_minus(&self) -> bool {
2409 self.options.flags & flags::SIGN_MINUS_FLAG != 0
2410 }
2411
2412 /// Determines if the `#` flag was specified.
2413 ///
2414 /// # Examples
2415 ///
2416 /// ```
2417 /// use std::fmt;
2418 ///
2419 /// struct Foo(i32);
2420 ///
2421 /// impl fmt::Display for Foo {
2422 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2423 /// if formatter.alternate() {
2424 /// write!(formatter, "Foo({})", self.0)
2425 /// } else {
2426 /// write!(formatter, "{}", self.0)
2427 /// }
2428 /// }
2429 /// }
2430 ///
2431 /// assert_eq!(format!("{:#}", Foo(23)), "Foo(23)");
2432 /// assert_eq!(format!("{}", Foo(23)), "23");
2433 /// ```
2434 #[must_use]
2435 #[stable(feature = "fmt_flags", since = "1.5.0")]
2436 #[ferrocene::prevalidated]
2437 pub fn alternate(&self) -> bool {
2438 self.options.flags & flags::ALTERNATE_FLAG != 0
2439 }
2440
2441 /// Determines if the `0` flag was specified.
2442 ///
2443 /// # Examples
2444 ///
2445 /// ```
2446 /// use std::fmt;
2447 ///
2448 /// struct Foo(i32);
2449 ///
2450 /// impl fmt::Display for Foo {
2451 /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2452 /// assert!(formatter.sign_aware_zero_pad());
2453 /// assert_eq!(formatter.width(), Some(4));
2454 /// // We ignore the formatter's options.
2455 /// write!(formatter, "{}", self.0)
2456 /// }
2457 /// }
2458 ///
2459 /// assert_eq!(format!("{:04}", Foo(23)), "23");
2460 /// ```
2461 #[must_use]
2462 #[stable(feature = "fmt_flags", since = "1.5.0")]
2463 #[ferrocene::prevalidated]
2464 pub fn sign_aware_zero_pad(&self) -> bool {
2465 self.options.flags & flags::SIGN_AWARE_ZERO_PAD_FLAG != 0
2466 }
2467
2468 // FIXME: Decide what public API we want for these two flags.
2469 // https://github.com/rust-lang/rust/issues/48584
2470 #[ferrocene::prevalidated]
2471 fn debug_lower_hex(&self) -> bool {
2472 self.options.flags & flags::DEBUG_LOWER_HEX_FLAG != 0
2473 }
2474 #[ferrocene::prevalidated]
2475 fn debug_upper_hex(&self) -> bool {
2476 self.options.flags & flags::DEBUG_UPPER_HEX_FLAG != 0
2477 }
2478
2479 /// Creates a [`DebugStruct`] builder designed to assist with creation of
2480 /// [`fmt::Debug`] implementations for structs.
2481 ///
2482 /// [`fmt::Debug`]: self::Debug
2483 ///
2484 /// # Examples
2485 ///
2486 /// ```rust
2487 /// use std::fmt;
2488 /// use std::net::Ipv4Addr;
2489 ///
2490 /// struct Foo {
2491 /// bar: i32,
2492 /// baz: String,
2493 /// addr: Ipv4Addr,
2494 /// }
2495 ///
2496 /// impl fmt::Debug for Foo {
2497 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2498 /// fmt.debug_struct("Foo")
2499 /// .field("bar", &self.bar)
2500 /// .field("baz", &self.baz)
2501 /// .field("addr", &format_args!("{}", self.addr))
2502 /// .finish()
2503 /// }
2504 /// }
2505 ///
2506 /// assert_eq!(
2507 /// "Foo { bar: 10, baz: \"Hello World\", addr: 127.0.0.1 }",
2508 /// format!("{:?}", Foo {
2509 /// bar: 10,
2510 /// baz: "Hello World".to_string(),
2511 /// addr: Ipv4Addr::new(127, 0, 0, 1),
2512 /// })
2513 /// );
2514 /// ```
2515 #[stable(feature = "debug_builders", since = "1.2.0")]
2516 #[ferrocene::prevalidated]
2517 pub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a> {
2518 builders::debug_struct_new(self, name)
2519 }
2520
2521 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2522 /// binaries. `debug_struct_fields_finish` is more general, but this is
2523 /// faster for 1 field.
2524 #[doc(hidden)]
2525 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2526 #[ferrocene::prevalidated]
2527 pub fn debug_struct_field1_finish<'b>(
2528 &'b mut self,
2529 name: &str,
2530 name1: &str,
2531 value1: &dyn Debug,
2532 ) -> Result {
2533 let mut builder = builders::debug_struct_new(self, name);
2534 builder.field(name1, value1);
2535 builder.finish()
2536 }
2537
2538 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2539 /// binaries. `debug_struct_fields_finish` is more general, but this is
2540 /// faster for 2 fields.
2541 #[doc(hidden)]
2542 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2543 #[ferrocene::prevalidated]
2544 pub fn debug_struct_field2_finish<'b>(
2545 &'b mut self,
2546 name: &str,
2547 name1: &str,
2548 value1: &dyn Debug,
2549 name2: &str,
2550 value2: &dyn Debug,
2551 ) -> Result {
2552 let mut builder = builders::debug_struct_new(self, name);
2553 builder.field(name1, value1);
2554 builder.field(name2, value2);
2555 builder.finish()
2556 }
2557
2558 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2559 /// binaries. `debug_struct_fields_finish` is more general, but this is
2560 /// faster for 3 fields.
2561 #[doc(hidden)]
2562 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2563 #[ferrocene::prevalidated]
2564 pub fn debug_struct_field3_finish<'b>(
2565 &'b mut self,
2566 name: &str,
2567 name1: &str,
2568 value1: &dyn Debug,
2569 name2: &str,
2570 value2: &dyn Debug,
2571 name3: &str,
2572 value3: &dyn Debug,
2573 ) -> Result {
2574 let mut builder = builders::debug_struct_new(self, name);
2575 builder.field(name1, value1);
2576 builder.field(name2, value2);
2577 builder.field(name3, value3);
2578 builder.finish()
2579 }
2580
2581 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2582 /// binaries. `debug_struct_fields_finish` is more general, but this is
2583 /// faster for 4 fields.
2584 #[doc(hidden)]
2585 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2586 #[ferrocene::prevalidated]
2587 pub fn debug_struct_field4_finish<'b>(
2588 &'b mut self,
2589 name: &str,
2590 name1: &str,
2591 value1: &dyn Debug,
2592 name2: &str,
2593 value2: &dyn Debug,
2594 name3: &str,
2595 value3: &dyn Debug,
2596 name4: &str,
2597 value4: &dyn Debug,
2598 ) -> Result {
2599 let mut builder = builders::debug_struct_new(self, name);
2600 builder.field(name1, value1);
2601 builder.field(name2, value2);
2602 builder.field(name3, value3);
2603 builder.field(name4, value4);
2604 builder.finish()
2605 }
2606
2607 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2608 /// binaries. `debug_struct_fields_finish` is more general, but this is
2609 /// faster for 5 fields.
2610 #[doc(hidden)]
2611 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2612 #[ferrocene::prevalidated]
2613 pub fn debug_struct_field5_finish<'b>(
2614 &'b mut self,
2615 name: &str,
2616 name1: &str,
2617 value1: &dyn Debug,
2618 name2: &str,
2619 value2: &dyn Debug,
2620 name3: &str,
2621 value3: &dyn Debug,
2622 name4: &str,
2623 value4: &dyn Debug,
2624 name5: &str,
2625 value5: &dyn Debug,
2626 ) -> Result {
2627 let mut builder = builders::debug_struct_new(self, name);
2628 builder.field(name1, value1);
2629 builder.field(name2, value2);
2630 builder.field(name3, value3);
2631 builder.field(name4, value4);
2632 builder.field(name5, value5);
2633 builder.finish()
2634 }
2635
2636 /// Shrinks `derive(Debug)` code, for faster compilation and smaller binaries.
2637 /// For the cases not covered by `debug_struct_field[12345]_finish`.
2638 #[doc(hidden)]
2639 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2640 #[ferrocene::prevalidated]
2641 pub fn debug_struct_fields_finish<'b>(
2642 &'b mut self,
2643 name: &str,
2644 names: &[&str],
2645 values: &[&dyn Debug],
2646 ) -> Result {
2647 assert_eq!(names.len(), values.len());
2648 let mut builder = builders::debug_struct_new(self, name);
2649 for (name, value) in iter::zip(names, values) {
2650 builder.field(name, value);
2651 }
2652 builder.finish()
2653 }
2654
2655 /// Creates a `DebugTuple` builder designed to assist with creation of
2656 /// `fmt::Debug` implementations for tuple structs.
2657 ///
2658 /// # Examples
2659 ///
2660 /// ```rust
2661 /// use std::fmt;
2662 /// use std::marker::PhantomData;
2663 ///
2664 /// struct Foo<T>(i32, String, PhantomData<T>);
2665 ///
2666 /// impl<T> fmt::Debug for Foo<T> {
2667 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2668 /// fmt.debug_tuple("Foo")
2669 /// .field(&self.0)
2670 /// .field(&self.1)
2671 /// .field(&format_args!("_"))
2672 /// .finish()
2673 /// }
2674 /// }
2675 ///
2676 /// assert_eq!(
2677 /// "Foo(10, \"Hello\", _)",
2678 /// format!("{:?}", Foo(10, "Hello".to_string(), PhantomData::<u8>))
2679 /// );
2680 /// ```
2681 #[stable(feature = "debug_builders", since = "1.2.0")]
2682 #[ferrocene::prevalidated]
2683 pub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a> {
2684 builders::debug_tuple_new(self, name)
2685 }
2686
2687 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2688 /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2689 /// for 1 field.
2690 #[doc(hidden)]
2691 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2692 #[ferrocene::prevalidated]
2693 pub fn debug_tuple_field1_finish<'b>(&'b mut self, name: &str, value1: &dyn Debug) -> Result {
2694 let mut builder = builders::debug_tuple_new(self, name);
2695 builder.field(value1);
2696 builder.finish()
2697 }
2698
2699 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2700 /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2701 /// for 2 fields.
2702 #[doc(hidden)]
2703 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2704 #[ferrocene::prevalidated]
2705 pub fn debug_tuple_field2_finish<'b>(
2706 &'b mut self,
2707 name: &str,
2708 value1: &dyn Debug,
2709 value2: &dyn Debug,
2710 ) -> Result {
2711 let mut builder = builders::debug_tuple_new(self, name);
2712 builder.field(value1);
2713 builder.field(value2);
2714 builder.finish()
2715 }
2716
2717 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2718 /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2719 /// for 3 fields.
2720 #[doc(hidden)]
2721 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2722 #[ferrocene::prevalidated]
2723 pub fn debug_tuple_field3_finish<'b>(
2724 &'b mut self,
2725 name: &str,
2726 value1: &dyn Debug,
2727 value2: &dyn Debug,
2728 value3: &dyn Debug,
2729 ) -> Result {
2730 let mut builder = builders::debug_tuple_new(self, name);
2731 builder.field(value1);
2732 builder.field(value2);
2733 builder.field(value3);
2734 builder.finish()
2735 }
2736
2737 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2738 /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2739 /// for 4 fields.
2740 #[doc(hidden)]
2741 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2742 #[ferrocene::prevalidated]
2743 pub fn debug_tuple_field4_finish<'b>(
2744 &'b mut self,
2745 name: &str,
2746 value1: &dyn Debug,
2747 value2: &dyn Debug,
2748 value3: &dyn Debug,
2749 value4: &dyn Debug,
2750 ) -> Result {
2751 let mut builder = builders::debug_tuple_new(self, name);
2752 builder.field(value1);
2753 builder.field(value2);
2754 builder.field(value3);
2755 builder.field(value4);
2756 builder.finish()
2757 }
2758
2759 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2760 /// binaries. `debug_tuple_fields_finish` is more general, but this is faster
2761 /// for 5 fields.
2762 #[doc(hidden)]
2763 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2764 #[ferrocene::prevalidated]
2765 pub fn debug_tuple_field5_finish<'b>(
2766 &'b mut self,
2767 name: &str,
2768 value1: &dyn Debug,
2769 value2: &dyn Debug,
2770 value3: &dyn Debug,
2771 value4: &dyn Debug,
2772 value5: &dyn Debug,
2773 ) -> Result {
2774 let mut builder = builders::debug_tuple_new(self, name);
2775 builder.field(value1);
2776 builder.field(value2);
2777 builder.field(value3);
2778 builder.field(value4);
2779 builder.field(value5);
2780 builder.finish()
2781 }
2782
2783 /// Shrinks `derive(Debug)` code, for faster compilation and smaller
2784 /// binaries. For the cases not covered by `debug_tuple_field[12345]_finish`.
2785 #[doc(hidden)]
2786 #[unstable(feature = "fmt_helpers_for_derive", issue = "none")]
2787 #[ferrocene::prevalidated]
2788 pub fn debug_tuple_fields_finish<'b>(
2789 &'b mut self,
2790 name: &str,
2791 values: &[&dyn Debug],
2792 ) -> Result {
2793 let mut builder = builders::debug_tuple_new(self, name);
2794 for value in values {
2795 builder.field(value);
2796 }
2797 builder.finish()
2798 }
2799
2800 /// Creates a `DebugList` builder designed to assist with creation of
2801 /// `fmt::Debug` implementations for list-like structures.
2802 ///
2803 /// # Examples
2804 ///
2805 /// ```rust
2806 /// use std::fmt;
2807 ///
2808 /// struct Foo(Vec<i32>);
2809 ///
2810 /// impl fmt::Debug for Foo {
2811 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2812 /// fmt.debug_list().entries(self.0.iter()).finish()
2813 /// }
2814 /// }
2815 ///
2816 /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]");
2817 /// ```
2818 #[stable(feature = "debug_builders", since = "1.2.0")]
2819 #[ferrocene::prevalidated]
2820 pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a> {
2821 builders::debug_list_new(self)
2822 }
2823
2824 /// Creates a `DebugSet` builder designed to assist with creation of
2825 /// `fmt::Debug` implementations for set-like structures.
2826 ///
2827 /// # Examples
2828 ///
2829 /// ```rust
2830 /// use std::fmt;
2831 ///
2832 /// struct Foo(Vec<i32>);
2833 ///
2834 /// impl fmt::Debug for Foo {
2835 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2836 /// fmt.debug_set().entries(self.0.iter()).finish()
2837 /// }
2838 /// }
2839 ///
2840 /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
2841 /// ```
2842 ///
2843 /// [`format_args!`]: crate::format_args
2844 ///
2845 /// In this more complex example, we use [`format_args!`] and `.debug_set()`
2846 /// to build a list of match arms:
2847 ///
2848 /// ```rust
2849 /// use std::fmt;
2850 ///
2851 /// struct Arm<'a, L, R>(&'a (L, R));
2852 /// struct Table<'a, K, V>(&'a [(K, V)], V);
2853 ///
2854 /// impl<'a, L, R> fmt::Debug for Arm<'a, L, R>
2855 /// where
2856 /// L: 'a + fmt::Debug, R: 'a + fmt::Debug
2857 /// {
2858 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2859 /// L::fmt(&(self.0).0, fmt)?;
2860 /// fmt.write_str(" => ")?;
2861 /// R::fmt(&(self.0).1, fmt)
2862 /// }
2863 /// }
2864 ///
2865 /// impl<'a, K, V> fmt::Debug for Table<'a, K, V>
2866 /// where
2867 /// K: 'a + fmt::Debug, V: 'a + fmt::Debug
2868 /// {
2869 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2870 /// fmt.debug_set()
2871 /// .entries(self.0.iter().map(Arm))
2872 /// .entry(&Arm(&(format_args!("_"), &self.1)))
2873 /// .finish()
2874 /// }
2875 /// }
2876 /// ```
2877 #[stable(feature = "debug_builders", since = "1.2.0")]
2878 #[ferrocene::prevalidated]
2879 pub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a> {
2880 builders::debug_set_new(self)
2881 }
2882
2883 /// Creates a `DebugMap` builder designed to assist with creation of
2884 /// `fmt::Debug` implementations for map-like structures.
2885 ///
2886 /// # Examples
2887 ///
2888 /// ```rust
2889 /// use std::fmt;
2890 ///
2891 /// struct Foo(Vec<(String, i32)>);
2892 ///
2893 /// impl fmt::Debug for Foo {
2894 /// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2895 /// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
2896 /// }
2897 /// }
2898 ///
2899 /// assert_eq!(
2900 /// format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])),
2901 /// r#"{"A": 10, "B": 11}"#
2902 /// );
2903 /// ```
2904 #[stable(feature = "debug_builders", since = "1.2.0")]
2905 #[ferrocene::prevalidated]
2906 pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a> {
2907 builders::debug_map_new(self)
2908 }
2909
2910 /// Returns the sign of this formatter (`+` or `-`).
2911 #[unstable(feature = "formatting_options", issue = "118117")]
2912 #[ferrocene::prevalidated]
2913 pub const fn sign(&self) -> Option<Sign> {
2914 self.options.get_sign()
2915 }
2916
2917 /// Returns the formatting options this formatter corresponds to.
2918 #[unstable(feature = "formatting_options", issue = "118117")]
2919 #[ferrocene::prevalidated]
2920 pub const fn options(&self) -> FormattingOptions {
2921 self.options
2922 }
2923}
2924
2925#[stable(since = "1.2.0", feature = "formatter_write")]
2926impl Write for Formatter<'_> {
2927 #[ferrocene::prevalidated]
2928 fn write_str(&mut self, s: &str) -> Result {
2929 self.buf.write_str(s)
2930 }
2931
2932 #[ferrocene::prevalidated]
2933 fn write_char(&mut self, c: char) -> Result {
2934 self.buf.write_char(c)
2935 }
2936
2937 #[inline]
2938 #[ferrocene::prevalidated]
2939 fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
2940 if let Some(s) = args.as_statically_known_str() {
2941 self.buf.write_str(s)
2942 } else {
2943 write(self.buf, args)
2944 }
2945 }
2946}
2947
2948#[stable(feature = "rust1", since = "1.0.0")]
2949impl Display for Error {
2950 #[ferrocene::prevalidated]
2951 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2952 Display::fmt("an error occurred when formatting an argument", f)
2953 }
2954}
2955
2956// Implementations of the core formatting traits
2957
2958macro_rules! fmt_refs {
2959 ($($tr:ident),*) => {
2960 $(
2961 #[stable(feature = "rust1", since = "1.0.0")]
2962 impl<T: PointeeSized + $tr> $tr for &T {
2963 #[ferrocene::prevalidated]
2964 fn fmt(&self, f: &mut Formatter<'_>) -> Result { $tr::fmt(&**self, f) }
2965 }
2966 #[stable(feature = "rust1", since = "1.0.0")]
2967 impl<T: PointeeSized + $tr> $tr for &mut T {
2968 #[ferrocene::prevalidated]
2969 fn fmt(&self, f: &mut Formatter<'_>) -> Result { $tr::fmt(&**self, f) }
2970 }
2971 )*
2972 }
2973}
2974
2975fmt_refs! { Debug, Display, Octal, Binary, LowerHex, UpperHex, LowerExp, UpperExp }
2976
2977#[unstable(feature = "never_type", issue = "35121")]
2978impl Debug for ! {
2979 #[inline]
2980 #[ferrocene::prevalidated]
2981 fn fmt(&self, _: &mut Formatter<'_>) -> Result {
2982 *self
2983 }
2984}
2985
2986#[unstable(feature = "never_type", issue = "35121")]
2987impl Display for ! {
2988 #[inline]
2989 #[ferrocene::prevalidated]
2990 fn fmt(&self, _: &mut Formatter<'_>) -> Result {
2991 *self
2992 }
2993}
2994
2995#[stable(feature = "rust1", since = "1.0.0")]
2996impl Debug for bool {
2997 #[inline]
2998 #[ferrocene::prevalidated]
2999 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3000 Display::fmt(self, f)
3001 }
3002}
3003
3004#[stable(feature = "rust1", since = "1.0.0")]
3005impl Display for bool {
3006 #[ferrocene::prevalidated]
3007 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3008 Display::fmt(if *self { "true" } else { "false" }, f)
3009 }
3010}
3011
3012#[stable(feature = "rust1", since = "1.0.0")]
3013impl Debug for str {
3014 #[ferrocene::prevalidated]
3015 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3016 f.write_char('"')?;
3017
3018 // substring we know is printable
3019 let mut printable_range = 0..0;
3020
3021 #[ferrocene::prevalidated]
3022 fn needs_escape(b: u8) -> bool {
3023 b > 0x7E || b < 0x20 || b == b'\\' || b == b'"'
3024 }
3025
3026 // the loop here first skips over runs of printable ASCII as a fast path.
3027 // other chars (unicode, or ASCII that needs escaping) are then handled per-`char`.
3028 let mut rest = self;
3029 while rest.len() > 0 {
3030 let Some(non_printable_start) = rest.as_bytes().iter().position(|&b| needs_escape(b))
3031 else {
3032 printable_range.end += rest.len();
3033 break;
3034 };
3035
3036 printable_range.end += non_printable_start;
3037 // SAFETY: the position was derived from an iterator, so is known to be within bounds, and at a char boundary
3038 rest = unsafe { rest.get_unchecked(non_printable_start..) };
3039
3040 let mut chars = rest.chars();
3041 if let Some(c) = chars.next() {
3042 let esc = c.escape_debug_ext(EscapeDebugExtArgs {
3043 escape_grapheme_extended: true,
3044 escape_single_quote: false,
3045 escape_double_quote: true,
3046 });
3047 if esc.len() != 1 {
3048 f.write_str(&self[printable_range.clone()])?;
3049 Display::fmt(&esc, f)?;
3050 printable_range.start = printable_range.end + c.len_utf8();
3051 }
3052 printable_range.end += c.len_utf8();
3053 } else {
3054 #[ferrocene::annotation(
3055 "
3056 This branch is effectively unreachable as the `chars` iterator is guaranteed to
3057 have at least one element. This is because `rest` is non-empty and
3058 `non_printable_start` is guaranteed to point to a char boundary due to the
3059 definition of `needs_escape` and the fact that strings in Rust are UTF-8.
3060 "
3061 )]
3062 {}
3063 }
3064 rest = chars.as_str();
3065 }
3066
3067 f.write_str(&self[printable_range])?;
3068
3069 f.write_char('"')
3070 }
3071}
3072
3073#[stable(feature = "rust1", since = "1.0.0")]
3074impl Display for str {
3075 #[ferrocene::prevalidated]
3076 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3077 f.pad(self)
3078 }
3079}
3080
3081#[stable(feature = "rust1", since = "1.0.0")]
3082impl Debug for char {
3083 #[ferrocene::prevalidated]
3084 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3085 f.write_char('\'')?;
3086 let esc = self.escape_debug_ext(EscapeDebugExtArgs {
3087 escape_grapheme_extended: true,
3088 escape_single_quote: true,
3089 escape_double_quote: false,
3090 });
3091 Display::fmt(&esc, f)?;
3092 f.write_char('\'')
3093 }
3094}
3095
3096#[stable(feature = "rust1", since = "1.0.0")]
3097impl Display for char {
3098 #[ferrocene::prevalidated]
3099 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3100 if f.options.flags & (flags::WIDTH_FLAG | flags::PRECISION_FLAG) == 0 {
3101 f.write_char(*self)
3102 } else {
3103 f.pad(self.encode_utf8(&mut [0; char::MAX_LEN_UTF8]))
3104 }
3105 }
3106}
3107
3108#[stable(feature = "rust1", since = "1.0.0")]
3109impl<T: PointeeSized> Pointer for *const T {
3110 #[ferrocene::prevalidated]
3111 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3112 if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() {
3113 pointer_fmt_inner(self.expose_provenance(), f)
3114 } else {
3115 f.debug_struct("Pointer")
3116 .field_with("addr", |f| pointer_fmt_inner(self.expose_provenance(), f))
3117 .field("metadata", &core::ptr::metadata(*self))
3118 .finish()
3119 }
3120 }
3121}
3122
3123/// Since the formatting will be identical for all pointer types, uses a
3124/// non-monomorphized implementation for the actual formatting to reduce the
3125/// amount of codegen work needed.
3126///
3127/// This uses `ptr_addr: usize` and not `ptr: *const ()` to be able to use this for
3128/// `fn(...) -> ...` without using [problematic] "Oxford Casts".
3129///
3130/// [problematic]: https://github.com/rust-lang/rust/issues/95489
3131#[ferrocene::prevalidated]
3132pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result {
3133 let old_options = f.options;
3134
3135 // The alternate flag is already treated by LowerHex as being special-
3136 // it denotes whether to prefix with 0x. We use it to work out whether
3137 // or not to zero extend, and then unconditionally set it to get the
3138 // prefix.
3139 if f.options.get_alternate() {
3140 f.options.sign_aware_zero_pad(true);
3141
3142 if f.options.get_width().is_none() {
3143 f.options.width(Some((usize::BITS / 4) as u16 + 2));
3144 }
3145 }
3146 f.options.alternate(true);
3147
3148 let ret = LowerHex::fmt(&ptr_addr, f);
3149
3150 f.options = old_options;
3151
3152 ret
3153}
3154
3155#[stable(feature = "rust1", since = "1.0.0")]
3156impl<T: PointeeSized> Pointer for *mut T {
3157 #[ferrocene::prevalidated]
3158 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3159 Pointer::fmt(&(*self as *const T), f)
3160 }
3161}
3162
3163#[stable(feature = "rust1", since = "1.0.0")]
3164impl<T: PointeeSized> Pointer for &T {
3165 #[ferrocene::prevalidated]
3166 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3167 Pointer::fmt(&(*self as *const T), f)
3168 }
3169}
3170
3171#[stable(feature = "rust1", since = "1.0.0")]
3172impl<T: PointeeSized> Pointer for &mut T {
3173 #[ferrocene::prevalidated]
3174 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3175 Pointer::fmt(&(&**self as *const T), f)
3176 }
3177}
3178
3179// Implementation of Display/Debug for various core types
3180
3181#[stable(feature = "rust1", since = "1.0.0")]
3182impl<T: PointeeSized> Debug for *const T {
3183 #[ferrocene::prevalidated]
3184 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3185 Pointer::fmt(self, f)
3186 }
3187}
3188#[stable(feature = "rust1", since = "1.0.0")]
3189impl<T: PointeeSized> Debug for *mut T {
3190 #[ferrocene::prevalidated]
3191 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3192 Pointer::fmt(self, f)
3193 }
3194}
3195
3196macro_rules! peel {
3197 ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
3198}
3199
3200macro_rules! tuple {
3201 () => ();
3202 ( $($name:ident,)+ ) => (
3203 maybe_tuple_doc! {
3204 $($name)+ @
3205 #[stable(feature = "rust1", since = "1.0.0")]
3206 impl<$($name:Debug),+> Debug for ($($name,)+) {
3207 #[allow(non_snake_case, unused_assignments)]
3208 #[ferrocene::prevalidated]
3209 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3210 let mut builder = f.debug_tuple("");
3211 let ($(ref $name,)+) = *self;
3212 $(
3213 builder.field(&$name);
3214 )+
3215
3216 builder.finish()
3217 }
3218 }
3219 }
3220 peel! { $($name,)+ }
3221 )
3222}
3223
3224macro_rules! maybe_tuple_doc {
3225 ($a:ident @ #[$meta:meta] $item:item) => {
3226 #[doc(fake_variadic)]
3227 #[doc = "This trait is implemented for tuples up to twelve items long."]
3228 #[$meta]
3229 $item
3230 };
3231 ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
3232 #[doc(hidden)]
3233 #[$meta]
3234 $item
3235 };
3236}
3237
3238tuple! { E, D, C, B, A, Z, Y, X, W, V, U, T, }
3239
3240#[stable(feature = "rust1", since = "1.0.0")]
3241impl<T: Debug> Debug for [T] {
3242 #[ferrocene::prevalidated]
3243 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3244 f.debug_list().entries(self.iter()).finish()
3245 }
3246}
3247
3248#[stable(feature = "rust1", since = "1.0.0")]
3249impl Debug for () {
3250 #[inline]
3251 #[ferrocene::prevalidated]
3252 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3253 f.pad("()")
3254 }
3255}
3256#[stable(feature = "rust1", since = "1.0.0")]
3257impl<T: ?Sized> Debug for PhantomData<T> {
3258 #[ferrocene::prevalidated]
3259 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3260 write!(f, "PhantomData<{}>", crate::any::type_name::<T>())
3261 }
3262}
3263
3264#[stable(feature = "rust1", since = "1.0.0")]
3265impl<T: Copy + Debug> Debug for Cell<T> {
3266 #[ferrocene::prevalidated]
3267 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3268 f.debug_struct("Cell").field("value", &self.get()).finish()
3269 }
3270}
3271
3272#[stable(feature = "rust1", since = "1.0.0")]
3273impl<T: ?Sized + Debug> Debug for RefCell<T> {
3274 #[ferrocene::prevalidated]
3275 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3276 let mut d = f.debug_struct("RefCell");
3277 match self.try_borrow() {
3278 Ok(borrow) => d.field("value", &borrow),
3279 Err(_) => d.field("value", &format_args!("<borrowed>")),
3280 };
3281 d.finish()
3282 }
3283}
3284
3285#[stable(feature = "rust1", since = "1.0.0")]
3286impl<T: ?Sized + Debug> Debug for Ref<'_, T> {
3287 #[ferrocene::prevalidated]
3288 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3289 Debug::fmt(&**self, f)
3290 }
3291}
3292
3293#[stable(feature = "rust1", since = "1.0.0")]
3294impl<T: ?Sized + Debug> Debug for RefMut<'_, T> {
3295 #[ferrocene::prevalidated]
3296 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3297 Debug::fmt(&*(self.deref()), f)
3298 }
3299}
3300
3301#[stable(feature = "core_impl_debug", since = "1.9.0")]
3302impl<T: ?Sized> Debug for UnsafeCell<T> {
3303 #[ferrocene::prevalidated]
3304 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3305 f.debug_struct("UnsafeCell").finish_non_exhaustive()
3306 }
3307}
3308
3309#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
3310impl<T: ?Sized> Debug for SyncUnsafeCell<T> {
3311 #[ferrocene::prevalidated]
3312 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
3313 f.debug_struct("SyncUnsafeCell").finish_non_exhaustive()
3314 }
3315}
3316
3317// If you expected tests to be here, look instead at coretests/tests/fmt/;
3318// it's a lot easier than creating all of the rt::Piece structures here.
3319// There are also tests in alloctests/tests/fmt.rs, for those that need allocations.
3320
3321/// Ferrocene addition: Hidden module to test crate-internal functionality
3322#[doc(hidden)]
3323#[unstable(feature = "ferrocene_test", issue = "none")]
3324pub mod ferrocene_test;