powerfmt::smart_display

Trait SmartDisplay

Source
pub trait SmartDisplay: Display {
    type Metadata;

    // Required method
    fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>;

    // Provided methods
    fn fmt_with_metadata(
        &self,
        f: &mut Formatter<'_>,
        _metadata: Metadata<'_, Self>,
    ) -> Result { ... }
    fn fmt(&self, f: &mut Formatter<'_>) -> Result { ... }
}
Expand description

Format trait that allows authors to provide additional information.

This trait is similar to Display, but allows the author to provide additional information to the formatter. This information is provided in the form of a custom metadata type.

The only required piece of metadata is the width of the value. This is before it is passed to the formatter (i.e. it does not include any padding added by the formatter). Other information can be stored in a custom metadata type as needed. This information may be made available to downstream users, but it is not required.

Note: While both fmt_with_metadata and fmt have default implementations, it is strongly recommended to implement only fmt_with_metadata. fmt should be implemented if and only if the type does not require any of the calculated metadata. In that situation, fmt_with_metadata should be omitted.

Required Associated Types§

Source

type Metadata

User-provided metadata type.

Required Methods§

Source

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Compute any information needed to format the value. This must, at a minimum, determine the width of the value before any padding is added by the formatter.

If the type uses other types that implement SmartDisplay verbatim, the inner types should have their metadata calculated and included in the returned value.

§Lifetimes

This method’s return type contains a lifetime to self. This ensures that the metadata will neither outlive the value nor be invalidated by a mutation of the value (barring interior mutability).

#[derive(Debug)]
struct WrappedBuffer(WriteBuffer<128>);

#[smart_display::delegate]
impl SmartDisplay for WrappedBuffer {
    type Metadata = ();

    fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> {
        Metadata::new(self.0.len(), self, ())
    }

    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad(self.0.as_str())
    }
}

let mut buf = WrappedBuffer(WriteBuffer::new());
let metadata = buf.metadata(FormatterOptions::default());
// We cannot mutate the buffer while it is borrowed and use its previous metadata on the
// following line.
write!(buf.0, "Hello, world!")?;
assert_eq!(metadata.width(), 13);

Provided Methods§

Source

fn fmt_with_metadata( &self, f: &mut Formatter<'_>, _metadata: Metadata<'_, Self>, ) -> Result

Format the value using the given formatter and metadata. The formatted output should have the width indicated by the metadata. This is before any padding is added by the formatter.

If the metadata is not needed, you should implement the fmt method instead.

Source

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Format the value using the given formatter. This is the same as Display::fmt.

The default implementation of this method calls fmt_with_metadata with the result of metadata. Generally speaking, this method should not be implemented. You should implement the fmt_with_metadata method instead.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl SmartDisplay for Infallible

Source§

type Metadata = Infallible

Source§

fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, _: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for bool

Source§

type Metadata = ()

Source§

fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for char

Source§

type Metadata = ()

Source§

fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for i8

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for i16

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for i32

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for i64

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for i128

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for isize

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for str

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for u8

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for u16

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for u32

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for u64

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for u128

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl SmartDisplay for usize

Source§

type Metadata = ()

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> SmartDisplay for &T
where T: SmartDisplay + ?Sized,

Source§

type Metadata = <T as SmartDisplay>::Metadata

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> SmartDisplay for &mut T
where T: SmartDisplay + ?Sized,

Source§

type Metadata = <T as SmartDisplay>::Metadata

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> SmartDisplay for Ref<'_, T>
where T: SmartDisplay + ?Sized,

Source§

type Metadata = <T as SmartDisplay>::Metadata

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> SmartDisplay for RefMut<'_, T>
where T: SmartDisplay + ?Sized,

Source§

type Metadata = <T as SmartDisplay>::Metadata

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> SmartDisplay for Wrapping<T>
where T: SmartDisplay,

Source§

type Metadata = <T as SmartDisplay>::Metadata

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Source§

impl<T> SmartDisplay for Pin<&T>
where T: SmartDisplay + ?Sized,

Source§

type Metadata = <T as SmartDisplay>::Metadata

Source§

fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Implementors§

Source§

impl<const SIZE: usize> SmartDisplay for WriteBuffer<SIZE>