Struct compile_fmt::Fmt

source ·
pub struct Fmt<T: FormatArgument> { /* private fields */ }
Expand description

Formatting specification for an Argument.

A format is necessary to specify for dynamic arguments of compile_args! and related macros (i.e., for arguments that are not constants). For now, the only meaningful format customization is provided for strings (&str). All other arguments have the only available format that can be created using fmt().

Examples

Clipping string to certain width

use compile_fmt::{compile_args, clip, fmt};

const fn format_clipped_str(s: &str) -> impl AsRef<str> {
    compile_args!(
        "Clipped string: '", s => clip(8, "…"),
        "', original length: ", s.len() => fmt::<usize>()
    )
}

let s = format_clipped_str("very long string indeed");
assert_eq!(
    s.as_ref(),
    "Clipped string: 'very lon…', original length: 23"
);

Padding

const fn format_with_padding(value: u32) -> impl AsRef<str> {
    compile_args!(
        "Number: ", value => fmt::<u32>().pad_right(4, '0')
    )
}

let s = format_with_padding(42);
assert_eq!(s.as_ref(), "Number: 0042");
let s = format_with_padding(19_999);
assert_eq!(s.as_ref(), "Number: 19999");
// ^ If the string before padding contains more chars than in the padding spec,
// padding is not applied at all.

Any Unicode char can be used as padding:

let s = compile_args!(
    "Number: ", 42 => fmt::<u32>().pad_left(4, '💣')
);
assert_eq!(s.as_str(), "Number: 42💣💣");

Strings can be padded as well:

const fn pad_str(s: &str) -> impl AsRef<str> {
    compile_args!("[", s => clip(8, "").pad_center(8, ' '), "]")
}

assert_eq!(pad_str("test").as_ref(), "[  test  ]");
assert_eq!(pad_str("test!").as_ref(), "[ test!  ]");

Implementations§

source§

impl<T: FormatArgument> Fmt<T>

source

pub const fn pad_left(self, width: usize, using: char) -> Self

Specifies left-aligned padding. width is measured in chars, rather than bytes.

source

pub const fn pad_right(self, width: usize, using: char) -> Self

Specifies right-aligned padding. width is measured in chars, rather than bytes.

source

pub const fn pad_center(self, width: usize, using: char) -> Self

Specifies center-aligned padding. width is measured in chars, rather than bytes.

Trait Implementations§

source§

impl<T: Clone + FormatArgument> Clone for Fmt<T>where T::Details: Clone,

source§

fn clone(&self) -> Fmt<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + FormatArgument> Debug for Fmt<T>where T::Details: Debug,

source§

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

Formats the value using the given formatter. Read more
source§

impl<T: Copy + FormatArgument> Copy for Fmt<T>where T::Details: Copy,

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Fmt<T>where <T as FormatArgument>::Details: RefUnwindSafe,

§

impl<T> Send for Fmt<T>where <T as FormatArgument>::Details: Send,

§

impl<T> Sync for Fmt<T>where <T as FormatArgument>::Details: Sync,

§

impl<T> Unpin for Fmt<T>where <T as FormatArgument>::Details: Unpin,

§

impl<T> UnwindSafe for Fmt<T>where <T as FormatArgument>::Details: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.