Skip to main content

SpareWriter

Struct SpareWriter 

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

Appends bytes to the spare capacity of a BytesMut.

Created by BytesMut::spare_writer. Bytes are written into the spare capacity, which is not initialized first, and the writer reserves more capacity when it is full, at least doubling the buffer capacity. The buffer length is updated when the writer is dropped. If the writer is leaked, the buffer may not include all of the written bytes.

Implementations§

Source§

impl SpareWriter<'_>

Source

pub fn push(&mut self, byte: u8)

Appends a byte.

Source

pub fn extend_from_slice(&mut self, src: &[u8])

Appends a byte slice.

Source

pub fn len(&self) -> usize

Returns the number of written bytes.

Source

pub fn is_empty(&self) -> bool

Returns true if no bytes have been written.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more bytes after the written bytes.

§Panics

Panics if the new capacity exceeds u32::MAX minus the buffer header size, just under 4 GiB.

Source

pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>]

Returns the spare capacity after the written bytes.

Bytes written into it are added to the writer with advance.

§Examples
use ntex_bytes::BytesMut;

let mut buf = BytesMut::new();
let mut w = buf.spare_writer(0);
w.reserve(2);
let spare = w.spare_capacity_mut();
spare[0].write(b'o');
spare[1].write(b'k');
// SAFETY: The first two bytes of the spare capacity are initialized
unsafe { w.advance(2) };
drop(w);

assert_eq!(&buf[..], b"ok");
Source

pub unsafe fn advance(&mut self, cnt: usize)

Adds cnt bytes of the spare capacity to the written bytes.

§Safety

The first cnt bytes of spare_capacity_mut must be initialized.

Trait Implementations§

Source§

impl Debug for SpareWriter<'_>

Source§

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

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

impl Drop for SpareWriter<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for SpareWriter<'_>

Source§

impl Sync for SpareWriter<'_>

Source§

impl Write for SpareWriter<'_>

Source§

fn write(&mut self, src: &[u8]) -> Result<usize, Error>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

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

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

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

§

impl<'a> Freeze for SpareWriter<'a>

§

impl<'a> RefUnwindSafe for SpareWriter<'a>

§

impl<'a> Unpin for SpareWriter<'a>

§

impl<'a> UnsafeUnpin for SpareWriter<'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, !>

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.