Trait SmartDisplay
trait SmartDisplay: Display
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.
Associated Types
type MetadataUser-provided metadata type.
Required Methods
fn metadata(self: &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
SmartDisplayverbatim, 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).# use fmt; # use Write; # use WriteBuffer; # use ; ; let mut buf = WrappedBuffer; let metadata = buf.metadata; // We cannot mutate the buffer while it is borrowed and use its previous metadata on the // following line. write!?; assert_eq!; # Ok::
Provided Methods
fn fmt_with_metadata(self: &Self, f: &mut Formatter<'_>, _metadata: Metadata<'_, Self>) -> ResultFormat 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
fmtmethod instead.fn fmt(self: &Self, f: &mut Formatter<'_>) -> ResultFormat the value using the given formatter. This is the same as
Display::fmt.The default implementation of this method calls
fmt_with_metadatawith the result ofmetadata. Generally speaking, this method should not be implemented. You should implement thefmt_with_metadatamethod instead.
Implementors
impl<T> SmartDisplay for Ref<'_, T>impl SmartDisplay for u8impl SmartDisplay for u64impl SmartDisplay for i8impl SmartDisplay for Infallibleimpl SmartDisplay for i64impl<T> SmartDisplay for Pin<&T>impl SmartDisplay for charimpl<T> SmartDisplay for &mut Timpl<T> SmartDisplay for RefMut<'_, T>impl SmartDisplay for u32impl SmartDisplay for usizeimpl SmartDisplay for i32impl SmartDisplay for strimpl SmartDisplay for isizeimpl<T> SmartDisplay for &Timpl<SIZE: usize> SmartDisplay for WriteBuffer<SIZE>impl<T> SmartDisplay for Wrapping<T>impl SmartDisplay for u16impl SmartDisplay for u128impl SmartDisplay for i16impl SmartDisplay for boolimpl SmartDisplay for i128