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>
impl<T: FormatArgument> Fmt<T>
sourcepub const fn pad_left(self, width: usize, using: char) -> Self
pub const fn pad_left(self, width: usize, using: char) -> Self
Specifies left-aligned padding. width
is measured in chars, rather than bytes.
sourcepub const fn pad_right(self, width: usize, using: char) -> Self
pub const fn pad_right(self, width: usize, using: char) -> Self
Specifies right-aligned padding. width
is measured in chars, rather than bytes.
sourcepub const fn pad_center(self, width: usize, using: char) -> Self
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§
impl<T: Copy + FormatArgument> Copy for Fmt<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Fmt<T>
impl<T> Send for Fmt<T>
impl<T> Sync for Fmt<T>
impl<T> Unpin for Fmt<T>
impl<T> UnwindSafe for Fmt<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more