pub struct ComputeStrLength { /* private fields */ }
Available on crate feature fmt only.
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

#![feature(const_mut_refs)]

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

source

pub const fn new() -> Self

Constructs a ComputeStrLength of length 0.

source

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.

source

pub const fn add_len(&mut self, len: usize)

Adds len to the calculated length.

source

pub const fn len(&self) -> usize

The length of the string when formatted.

source

pub const fn is_empty(&self) -> bool

Whether the length of the computed string is zero.

source

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§

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.