Struct fmtbuf::WriteBuf

source ·
pub struct WriteBuf<'a> { /* private fields */ }
Expand description

A write buffer pointing to a &mut [u8].

use fmtbuf::WriteBuf;
use std::fmt::Write;

// The buffer to write into. The contents can be uninitialized, but using a
// bogus `\xff` sigil for demonstration.
let mut buf: [u8; 128] = [0xff; 128];
let mut writer = WriteBuf::new(&mut buf);

// Write data to the buffer.
write!(writer, "some data: {}", 0x01a4).unwrap();

// Finish writing:
let write_len = writer.finish().unwrap();
let written = std::str::from_utf8(&buf[..write_len]).unwrap();
assert_eq!(written, "some data: 420");

Implementations§

source§

impl<'a> WriteBuf<'a>

source

pub fn new(target: &'a mut [u8]) -> Self

Create an instance that will write to the given target. The contents of the target do not need to have been initialized before this, as they will be overwritten by writing.

source

pub fn position(&self) -> usize

Get the position in the target buffer. The value is one past the end of written content and the next position to be written to.

source

pub fn truncated(&self) -> bool

Get if a truncated write has happened.

source

pub fn finish(self) -> Result<usize, usize>

Returns

In both the Ok and Err cases, the WriteBuf::position is returned. The Ok case indicates the truncation did not occur, while Err indicates that it did.

source

pub fn finish_with(self, suffix: &[u8]) -> Result<usize, usize>

Finish the buffer, adding the suffix to the end. A common use case for this is to add a null terminator.

This operates slightly differently than the normal format writing function write_str in that the suffix is always put at the end. The only case where this will not happen is when suffix.len() is less than the size of the buffer originally provided. In this case, the last bit of suffix will be copied.

use fmtbuf::WriteBuf;

let mut buf: [u8; 4] = [0xff; 4];
let mut writer = WriteBuf::new(&mut buf);

// Finish writing with too many bytes:
let write_len = writer.finish_with(b"12345").unwrap_err();
assert_eq!(write_len, 4);
let buf_str = std::str::from_utf8(&buf).unwrap();
assert_eq!(buf_str, "2345");
Returns

The returned value has the same meaning as WriteBuf::finish.

Trait Implementations§

source§

impl<'a> Write for WriteBuf<'a>

source§

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

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<'a> RefUnwindSafe for WriteBuf<'a>

§

impl<'a> Send for WriteBuf<'a>

§

impl<'a> Sync for WriteBuf<'a>

§

impl<'a> Unpin for WriteBuf<'a>

§

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

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.