Skip to main content

Buf

Struct Buf 

Source
pub struct Buf<'a> { /* private fields */ }
Expand description

A fixed-capacity buffer over caller-owned bytes: the allocator-free way to format a value.

use tzcraft::write::Buf;
use tzcraft::write::Write;

let mut storage = [0u8; 64];
let mut buf = Buf::new(&mut storage);
buf.write_str("hello")?;
assert_eq!(buf.as_str(), "hello");

Implementations§

Source§

impl<'a> Buf<'a>

Source

pub fn new(data: &'a mut [u8]) -> Buf<'a>

Wrap data as an empty sink; content is appended from offset 0.

Source

pub const fn len(&self) -> usize

Number of bytes written so far.

Source

pub const fn is_empty(&self) -> bool

Whether nothing has been written yet.

Source

pub const fn capacity(&self) -> usize

Total capacity of the backing slice.

Source

pub fn as_bytes(&self) -> &[u8]

The written bytes.

Source

pub fn as_str(&self) -> &str

The written bytes as a &str.

This never fails: every byte written through Write is valid UTF-8 by construction (the formatter only emits ASCII digits, ASCII punctuation and literal UTF-8 text from format strings).

Source

pub fn clear(&mut self)

Reset to empty, reusing the backing slice.

Trait Implementations§

Source§

impl<'a> Debug for Buf<'a>

Source§

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

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

impl Write for Buf<'_>

Source§

fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>

Append a UTF-8 byte slice. Implementations used as text sinks return an error for invalid UTF-8.
Source§

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

Append a &str (its UTF-8 encoding is written verbatim).
Source§

fn write_byte(&mut self, b: u8) -> Result<()>

Append one byte.
Source§

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

Append one character, UTF-8 encoded.

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for Buf<'a>

§

impl<'a> Freeze for Buf<'a>

§

impl<'a> RefUnwindSafe for Buf<'a>

§

impl<'a> Send for Buf<'a>

§

impl<'a> Sync for Buf<'a>

§

impl<'a> Unpin for Buf<'a>

§

impl<'a> UnsafeUnpin for Buf<'a>

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

Source§

type Error = !

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.