pub struct Limit<R> { /* private fields */ }Expand description
Limit the number of bytes that can be read out of a reader to the specified limit.
This type wraps another reader and ensures that no more than a specified number of bytes can be read from it. This is useful for implementing bounded reads in serialization contexts.
Constructed through Reader::limit.
§Examples
use musli::Context;
use musli::reader::{Reader, SliceReader};
let cx = musli::context::new();
let data = &[1, 2, 3, 4, 5];
let mut reader = SliceReader::new(data);
let mut limited = reader.limit(3);
// Can read from the limited reader
let byte = limited.read_byte(&cx)?;
assert_eq!(byte, 1);
assert_eq!(limited.remaining(), 2);
// Read two more bytes
limited.read_byte(&cx)?;
limited.read_byte(&cx)?;
assert_eq!(limited.remaining(), 0);Implementations§
Source§impl<R> Limit<R>
impl<R> Limit<R>
Sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
Get the remaining data in the limited reader.
Returns the number of bytes that can still be read from this limited reader before the limit is reached.
§Examples
use musli::reader::{Reader, SliceReader};
let data = &[1, 2, 3, 4, 5];
let mut reader = SliceReader::new(data);
let limited = reader.limit(3);
assert_eq!(limited.remaining(), 3);Trait Implementations§
Source§impl<'de, R> Reader<'de> for Limit<R>where
R: Reader<'de>,
impl<'de, R> Reader<'de> for Limit<R>where
R: Reader<'de>,
Source§fn borrow_mut(&mut self) -> Self::Mut<'_>
fn borrow_mut(&mut self) -> Self::Mut<'_>
Borrow the current reader.
Source§fn skip<C>(&mut self, cx: C, n: usize) -> Result<(), C::Error>where
C: Context,
fn skip<C>(&mut self, cx: C, n: usize) -> Result<(), C::Error>where
C: Context,
Skip over the given number of bytes.
Source§fn read_bytes<C, V>(
&mut self,
cx: C,
n: usize,
visitor: V,
) -> Result<V::Ok, V::Error>
fn read_bytes<C, V>( &mut self, cx: C, n: usize, visitor: V, ) -> Result<V::Ok, V::Error>
Read a slice out of the current reader.
Source§unsafe fn read_bytes_uninit<C>(
&mut self,
cx: C,
ptr: *mut u8,
n: usize,
) -> Result<(), C::Error>where
C: Context,
unsafe fn read_bytes_uninit<C>(
&mut self,
cx: C,
ptr: *mut u8,
n: usize,
) -> Result<(), C::Error>where
C: Context,
Read into the given buffer which might not have been initialized. Read more
Source§fn read<C>(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error>where
C: Context,
fn read<C>(&mut self, cx: C, buf: &mut [u8]) -> Result<(), C::Error>where
C: Context,
Read a slice into the given buffer.
Source§fn read_byte<C>(&mut self, cx: C) -> Result<u8, C::Error>where
C: Context,
fn read_byte<C>(&mut self, cx: C) -> Result<u8, C::Error>where
C: Context,
Read a single byte.
Auto Trait Implementations§
impl<R> Freeze for Limit<R>where
R: Freeze,
impl<R> RefUnwindSafe for Limit<R>where
R: RefUnwindSafe,
impl<R> Send for Limit<R>where
R: Send,
impl<R> Sync for Limit<R>where
R: Sync,
impl<R> Unpin for Limit<R>where
R: Unpin,
impl<R> UnsafeUnpin for Limit<R>where
R: UnsafeUnpin,
impl<R> UnwindSafe for Limit<R>where
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more