Struct FormatBuffer

Source
pub struct FormatBuffer<const N: usize> { /* private fields */ }
Expand description

A self-contained buffer that can be used with both Rust’s formatting utilities and libc’s sprintf.

Because those tools panic on errors, overflowing writes are truncated rather than returning an error. A non-zero truncation count is included in Display output of this object, and can be checked via the truncated method.

The generic parameter N is the internal size of the buffer. One byte is reserved for NULL to support conversion to CStr.

To format a message with Rust’s formatting:

use core::fmt::Write;
let mut buf = FormatBuffer::<1000>::new();
let x = 42;
write!(&mut buf, "{x}").unwrap();
assert_eq!(buf.as_str(), "42");
let y = 43;
write!(&mut buf, " {y}").unwrap();
assert_eq!(buf.as_str(), "42 43");

Implementations§

Source§

impl<const N: usize> FormatBuffer<N>

Source

pub fn new() -> Self

Source

pub fn capacity_remaining(&self) -> usize

Remaining capacity in bytes.

Source

pub fn capacity_remaining_including_null(&self) -> usize

Source

pub fn truncated(&self) -> usize

How many bytes (not chars) have been truncated. This shouldn’t be relied on for an exact count; in particular the accounting is not precise in sprintf if utf8 replacement characters need to be inserted.

Source

pub fn reset(&mut self)

Reset to empty. This may be cheaper than assigning a fresh FormatBuffer::new, since the latter requires copying the uninitialized buffer. (Though such a copy could get optimized to the same cost depending on opt level, inlining, etc.)

Source

pub fn as_str(&self) -> &str

str representation of internal buffer.

If you’d like to render the buffer including any non-zero truncation count, use the Display attribute instead.

Source

pub fn as_cstr(&self) -> Option<&CStr>

Returns None if the buffer has interior NULL bytes.

Source

pub unsafe fn sprintf(&mut self, fmt: &CStr, args: VaList<'_>)

Appends the result of formatting fmt and args, following the conventions of libc’s sprintf.

Any non-utf8 sequences in the resulting string are replaced with the utf8 replacement character. If truncation occurs, the truncation count doesn’t necessarily account for all such substitutions.

Currently calls libc’s vsnprintf internally and panics on unexpected error. TODO: Ideally we’d find or create our own reimplementation of vsnprintf instead, since vsnprintf isn’t guaranteed to be async-signal-safe.

§Safety

fmt and args must be consistent, as with arguments to libc’s sprintf.

Trait Implementations§

Source§

impl<const N: usize> Default for FormatBuffer<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> Display for FormatBuffer<N>

Source§

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

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

impl<const N: usize> Write for FormatBuffer<N>

Source§

fn write_str(&mut self, src: &str) -> Result<(), Error>

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for FormatBuffer<N>

§

impl<const N: usize> RefUnwindSafe for FormatBuffer<N>

§

impl<const N: usize> Send for FormatBuffer<N>

§

impl<const N: usize> Sync for FormatBuffer<N>

§

impl<const N: usize> Unpin for FormatBuffer<N>

§

impl<const N: usize> UnwindSafe for FormatBuffer<N>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.