Struct Cursor

Source
pub struct Cursor<T, B: Buffer<T>> { /* private fields */ }
Expand description

A cursor for safely writing into an uninitialized buffer.

A Cursor is returned from Buffer::cursor, which provides way to write to a Buffer without needing to use unsafe.

§Examples

let mut buf = Vec::with_capacity(256);
let mut cursor = Cursor::new(spare_capacity(&mut buf));
let _nread = read(&input, &mut cursor).unwrap();
let _nread = read(&input, &mut cursor).unwrap();
let _nread = read(&input, &mut cursor).unwrap();
let total_read = cursor.finish();

Implementations§

Source§

impl<T, B: Buffer<T>> Cursor<T, B>

Source

pub const fn new(b: B) -> Self

Construct a new Cursor.

Source

pub fn remaining(&self) -> usize

Return the remaining amount of space in the buffer.

Source

pub fn write(&mut self, t: T)

Write an element to the buffer.

§Panics

Panics if this cursor has already reached the end of the buffer.

Source

pub fn write_slice(&mut self, t: &[T])
where T: Copy,

Write multiple elements to the buffer.

§Panics

Panics if this cursor is already within t.len() elements of the end of the buffer.

Source

pub fn finish(self) -> B::Output

Finish writing to the buffer and return the output value.

Trait Implementations§

Source§

impl<T, B: Buffer<T>> Buffer<T> for &mut Cursor<T, B>

Source§

type Output = usize

The type of the value returned by functions with Buffer arguments.
Source§

fn buffer_ptr(&mut self) -> *mut T

Return a raw mutable pointer to the underlying buffer. Read more
Source§

fn buffer_len(&self) -> usize

Return the length in elements of the underlying buffer.
Source§

unsafe fn assume_init(self, len: usize) -> Self::Output

Assert that len elements were written to, and provide a return value. Read more
Source§

fn cursor(self) -> Cursor<T, Self>

Return a Cursor for safely writing to the buffer. Read more

Auto Trait Implementations§

§

impl<T, B> Freeze for Cursor<T, B>
where B: Freeze,

§

impl<T, B> RefUnwindSafe for Cursor<T, B>

§

impl<T, B> Send for Cursor<T, B>
where B: Send, T: Send,

§

impl<T, B> Sync for Cursor<T, B>
where B: Sync, T: Sync,

§

impl<T, B> Unpin for Cursor<T, B>
where B: Unpin, T: Unpin,

§

impl<T, B> UnwindSafe for Cursor<T, B>
where B: UnwindSafe, T: UnwindSafe,

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 = 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.