pub struct ComputeStrLength { /* private fields */ }
Expand description
For computing how long a formatted string would be.
This is what the formatc
macro uses to precalculate the length of its returned &str
.
§Example
use const_format::fmt::{ComputeStrLength, Error, Formatter, FormattingFlags, StrWriter};
use const_format::{try_, writec, unwrap};
const fn write_sum(mut f: Formatter<'_>) -> Result<(), Error> {
let l = 7u8;
let r = 8u8;
writec!(f, "{} + {} = {}", l, r, l + r)
}
const LEN: usize = {
let mut computer = ComputeStrLength::new();
unwrap!(write_sum(computer.make_formatter(FormattingFlags::NEW)));
computer.len()
};
// The type annotation coerces a `&mut StrWriter<[u8; LEN]>`
// to a `&mut StrWriter<[u8]>` (the type parameter defaults to `[u8]`)
let writer: &mut StrWriter = &mut StrWriter::new([0; LEN]);
write_sum(writer.make_formatter(FormattingFlags::NEW)).unwrap();
assert_eq!(writer.as_str(), "7 + 8 = 15");
assert_eq!(writer.len(), LEN);
assert_eq!(writer.capacity(), LEN);
Implementations§
Source§impl ComputeStrLength
impl ComputeStrLength
Sourcepub const fn make_formatter(&mut self, flags: FormattingFlags) -> Formatter<'_>
pub const fn make_formatter(&mut self, flags: FormattingFlags) -> Formatter<'_>
Constructs a Formatter
,
which instead of writing to a buffer it adds the computed length into this.
Sourcepub const fn borrow_mutably(&mut self) -> &mut Self
pub const fn borrow_mutably(&mut self) -> &mut Self
For borrowing this mutably in macros,just takes and returns a &mut Self
.
Auto Trait Implementations§
impl Freeze for ComputeStrLength
impl RefUnwindSafe for ComputeStrLength
impl Send for ComputeStrLength
impl Sync for ComputeStrLength
impl Unpin for ComputeStrLength
impl UnwindSafe for ComputeStrLength
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
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 8 bytes