pub struct Cursor<T> { /* private fields */ }Expand description
Cursor wraps an in-memory buffer, providing Reader and Writer functionality
for types implementing AsRef<[u8]>.
This can be especially useful for wrapping Readers and Writers that are consumed by
reading or writing like &[u8] or &mut [MaybeUninit<u8>], making them reusable.
§Examples
Using Cursor to write to a MaybeUninit<[u8; N]>.
use wincode::io::{Cursor, Reader, Writer};
fn rand_bytes() -> [u8; 8] {
random::<u64>().to_le_bytes()
}
let mut data = MaybeUninit::<[u8; 8]>::uninit();
let mut cursor = Cursor::new(&mut data);
let bytes = rand_bytes();
cursor.write(&bytes).unwrap();
assert_eq!(unsafe { data.assume_init() }, bytes);
// We can write over the same buffer multiple times with a new Cursor.
let mut cursor = Cursor::new(&mut data);
let bytes = rand_bytes();
cursor.write(&bytes).unwrap();
assert_eq!(unsafe { data.assume_init() }, bytes);Using Cursor to write to a Vec’s spare capacity.
use wincode::io::{Cursor, Reader, Writer};
let mut data = Vec::with_capacity(8);
let mut cursor = Cursor::new(&mut data);
let bytes = rand_bytes();
cursor.write(&bytes).unwrap();
assert_eq!(data, bytes);
// We can write over the same buffer multiple times with a new Cursor.
let mut cursor = Cursor::new(&mut data);
let bytes = rand_bytes();
cursor.write(&bytes).unwrap();
assert_eq!(data, bytes);Implementations§
Source§impl<T> Cursor<T>
impl<T> Cursor<T>
pub const fn new(inner: T) -> Self
Sourcepub const fn set_position(&mut self, pos: usize)
pub const fn set_position(&mut self, pos: usize)
Sets the position of the cursor.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the cursor and returns the inner value.
Trait Implementations§
Source§impl<'a, T> Reader<'a> for Cursor<T>
impl<'a, T> Reader<'a> for Cursor<T>
Source§type Trusted<'b> = TrustedSliceReader<'a, 'b>
where
Self: 'b
type Trusted<'b> = TrustedSliceReader<'a, 'b> where Self: 'b
Source§fn fill_buf(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
fn fill_buf(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
n_bytes from the internal buffer without advancing. Implementations may
read more data internally to satisfy future requests. Returns fewer than n_bytes at EOF. Read moreSource§fn fill_exact(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
fn fill_exact(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
n_bytes without advancing. Read moreSource§unsafe fn consume_unchecked(&mut self, amt: usize)
unsafe fn consume_unchecked(&mut self, amt: usize)
amt bytes without bounds checks. Read moreSource§fn consume(&mut self, amt: usize) -> ReadResult<()>
fn consume(&mut self, amt: usize) -> ReadResult<()>
amt bytes, returning an error if the source does not have enough bytes.Source§unsafe fn as_trusted_for(
&mut self,
n_bytes: usize,
) -> ReadResult<Self::Trusted<'_>>
unsafe fn as_trusted_for( &mut self, n_bytes: usize, ) -> ReadResult<Self::Trusted<'_>>
Source§fn fill_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]>
fn fill_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]>
Source§fn borrow_exact(&mut self, len: usize) -> ReadResult<&'a [u8]>
fn borrow_exact(&mut self, len: usize) -> ReadResult<&'a [u8]>
Source§fn borrow_exact_mut(&mut self, len: usize) -> ReadResult<&'a mut [u8]>
fn borrow_exact_mut(&mut self, len: usize) -> ReadResult<&'a mut [u8]>
Source§fn peek(&mut self) -> ReadResult<&u8>
fn peek(&mut self) -> ReadResult<&u8>
Source§fn copy_into_slice(&mut self, dst: &mut [MaybeUninit<u8>]) -> ReadResult<()>
fn copy_into_slice(&mut self, dst: &mut [MaybeUninit<u8>]) -> ReadResult<()>
Source§fn copy_into_array<const N: usize>(
&mut self,
dst: &mut MaybeUninit<[u8; N]>,
) -> ReadResult<()>
fn copy_into_array<const N: usize>( &mut self, dst: &mut MaybeUninit<[u8; N]>, ) -> ReadResult<()>
Source§unsafe fn copy_into_t<T>(&mut self, dst: &mut MaybeUninit<T>) -> ReadResult<()>
unsafe fn copy_into_t<T>(&mut self, dst: &mut MaybeUninit<T>) -> ReadResult<()>
Source§unsafe fn copy_into_slice_t<T>(
&mut self,
dst: &mut [MaybeUninit<T>],
) -> ReadResult<()>
unsafe fn copy_into_slice_t<T>( &mut self, dst: &mut [MaybeUninit<T>], ) -> ReadResult<()>
Source§impl Writer for Cursor<&mut [MaybeUninit<u8>]>
impl Writer for Cursor<&mut [MaybeUninit<u8>]>
Source§type Trusted<'b> = TrustedSliceWriter<'b>
where
Self: 'b
type Trusted<'b> = TrustedSliceWriter<'b> where Self: 'b
Source§fn write(&mut self, src: &[u8]) -> WriteResult<()>
fn write(&mut self, src: &[u8]) -> WriteResult<()>
src.len() bytes from the given src into the writer.Source§unsafe fn as_trusted_for(
&mut self,
n_bytes: usize,
) -> WriteResult<Self::Trusted<'_>>
unsafe fn as_trusted_for( &mut self, n_bytes: usize, ) -> WriteResult<Self::Trusted<'_>>
Source§fn finish(&mut self) -> WriteResult<()>
fn finish(&mut self) -> WriteResult<()>
Source§unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
T as bytes into the source. Read moreSource§unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
[T] as bytes into the source. Read moreSource§impl<const N: usize> Writer for Cursor<&mut MaybeUninit<[u8; N]>>
impl<const N: usize> Writer for Cursor<&mut MaybeUninit<[u8; N]>>
Source§type Trusted<'b> = TrustedSliceWriter<'b>
where
Self: 'b
type Trusted<'b> = TrustedSliceWriter<'b> where Self: 'b
Source§fn write(&mut self, src: &[u8]) -> WriteResult<()>
fn write(&mut self, src: &[u8]) -> WriteResult<()>
src.len() bytes from the given src into the writer.Source§unsafe fn as_trusted_for(
&mut self,
n_bytes: usize,
) -> WriteResult<Self::Trusted<'_>>
unsafe fn as_trusted_for( &mut self, n_bytes: usize, ) -> WriteResult<Self::Trusted<'_>>
Source§fn finish(&mut self) -> WriteResult<()>
fn finish(&mut self) -> WriteResult<()>
Source§unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
T as bytes into the source. Read moreSource§unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
[T] as bytes into the source. Read moreSource§impl Writer for Cursor<&mut Vec<u8>>
Available on crate feature alloc only.Writer implementation for &mut Vec<u8> that overwrites the underlying vector’s memory.
The vector will grow as needed.
impl Writer for Cursor<&mut Vec<u8>>
alloc only.Writer implementation for &mut Vec<u8> that overwrites the underlying vector’s memory.
The vector will grow as needed.
§Examples
Overwriting an existing vector.
let mut vec = vec![0; 3];
let mut cursor = Cursor::new(&mut vec);
let bytes = [1, 2, 3, 4];
cursor.write(&bytes).unwrap();
assert_eq!(&vec, &[1, 2, 3, 4]);Growing a vector.
let mut vec = vec![];
let mut cursor = Cursor::new(&mut vec);
let bytes = [1, 2, 3];
cursor.write(&bytes).unwrap();
assert_eq!(&vec, &[1, 2, 3]);Source§fn write(&mut self, src: &[u8]) -> WriteResult<()>
fn write(&mut self, src: &[u8]) -> WriteResult<()>
src.len() bytes from the given src into the writer.Source§fn finish(&mut self) -> WriteResult<()>
fn finish(&mut self) -> WriteResult<()>
Source§unsafe fn as_trusted_for(
&mut self,
n_bytes: usize,
) -> WriteResult<Self::Trusted<'_>>
unsafe fn as_trusted_for( &mut self, n_bytes: usize, ) -> WriteResult<Self::Trusted<'_>>
Source§unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
T as bytes into the source. Read moreSource§unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
[T] as bytes into the source. Read moreSource§impl Writer for Cursor<Vec<u8>>
Available on crate feature alloc only.Writer implementation for Vec<u8> that overwrites the underlying vector’s memory.
The vector will grow as needed.
impl Writer for Cursor<Vec<u8>>
alloc only.Writer implementation for Vec<u8> that overwrites the underlying vector’s memory.
The vector will grow as needed.
§Examples
Overwriting an existing vector.
let mut cursor = Cursor::new(vec![0; 3]);
let bytes = [1, 2, 3, 4];
cursor.write(&bytes).unwrap();
assert_eq!(cursor.into_inner(), &[1, 2, 3, 4]);Growing a vector.
let mut cursor = Cursor::new(vec![]);
let bytes = [1, 2, 3];
cursor.write(&bytes).unwrap();
assert_eq!(cursor.into_inner(), &[1, 2, 3]);Source§fn write(&mut self, src: &[u8]) -> WriteResult<()>
fn write(&mut self, src: &[u8]) -> WriteResult<()>
src.len() bytes from the given src into the writer.Source§fn finish(&mut self) -> WriteResult<()>
fn finish(&mut self) -> WriteResult<()>
Source§unsafe fn as_trusted_for(
&mut self,
n_bytes: usize,
) -> WriteResult<Self::Trusted<'_>>
unsafe fn as_trusted_for( &mut self, n_bytes: usize, ) -> WriteResult<Self::Trusted<'_>>
Source§unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
unsafe fn write_t<T: ?Sized>(&mut self, src: &T) -> WriteResult<()>
T as bytes into the source. Read moreSource§unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
unsafe fn write_slice_t<T>(&mut self, src: &[T]) -> WriteResult<()>
[T] as bytes into the source. Read more