Struct bcder::decode::LimitedSource

source ·
pub struct LimitedSource<S> { /* private fields */ }
Expand description

A source that can be limited to a certain number of octets.

This type wraps another source and allows access to be limited to a certain number of octets. It will never provide access to more than that number of octets. Any attempt to advance over more octets will fail with a malformed error.

The limit is, however, independent of the underlying source. It can be larger than the number of octets actually available in the source.

The limit can be changed or even removed at any time.

Implementations§

source§

impl<S> LimitedSource<S>

source

pub fn new(source: S) -> Self

Creates a new limited source for the given source.

The return limited source will have no limit just yet.

source

pub fn unwrap(self) -> S

Unwraps the value and returns the source it was created from.

source

pub fn limit(&self) -> Option<usize>

Returns the current limit.

Returns None if there is no limit. Otherwise, the number returned is the number of remaining octets before the limit is reached. This does not necessarily mean that that many octets are actually available in the underlying source.

source

pub fn limit_further(&mut self, limit: Option<usize>) -> Option<usize>

Sets a more strict limit.

The method will panic (!) if you are trying to set a new limit that is larger than the current limit or if you are trying to remove the limit by passing None if there currently is a limit set.

Returns the old limit.

source

pub fn set_limit(&mut self, limit: Option<usize>)

Unconditionally sets a new limit.

If you pass None, the limit will be removed.

source§

impl<S: Source> LimitedSource<S>

source

pub fn skip_all(&mut self) -> Result<(), DecodeError<S::Error>>

Skip over all remaining octets until the current limit is reached.

If there currently is no limit, the method will panic. Otherwise it will simply advance to the end of the limit which may be something the underlying source doesn’t like and thus produce an error.

source

pub fn take_all(&mut self) -> Result<Bytes, DecodeError<S::Error>>

Returns a bytes value containing all octets until the current limit.

If there currently is no limit, the method will panic. Otherwise it tries to acquire a bytes value for the octets from the current position to the end of the limit and advance to the end of the limit.

This will result in a source error if the underlying source returns an error. It will result in a content error if the underlying source ends before the limit is reached.

source

pub fn exhausted(&mut self) -> Result<(), DecodeError<S::Error>>

Checks whether the end of the limit has been reached.

If a limit is currently set, the method will return a malformed error if it is larger than zero, i.e., if there are octets left to advance over before reaching the limit.

If there is no limit set, the method will try to access one single octet and return a malformed error if that is actually possible, i.e., if there are octets left in the underlying source.

Any source errors are passed through. If there the data is not exhausted as described above, a content error is created.

Trait Implementations§

source§

impl<S: Clone> Clone for LimitedSource<S>

source§

fn clone(&self) -> LimitedSource<S>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<S: Debug> Debug for LimitedSource<S>

source§

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

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

impl<S: Source> Source for LimitedSource<S>

§

type Error = <S as Source>::Error

The error produced when the source failed to read more data.
source§

fn pos(&self) -> Pos

Returns the current logical postion within the sequence of data.
source§

fn request(&mut self, len: usize) -> Result<usize, Self::Error>

Request at least len bytes to be available. Read more
source§

fn advance(&mut self, len: usize)

Advance the source by len bytes. Read more
source§

fn slice(&self) -> &[u8]

Returns a bytes slice with the available data. Read more
source§

fn bytes(&self, start: usize, end: usize) -> Bytes

Produces a Bytes value from part of the data. Read more
source§

fn skip(&mut self, len: usize) -> Result<usize, Self::Error>

Skip over the next len bytes. Read more
source§

fn take_u8(&mut self) -> Result<u8, DecodeError<Self::Error>>

Takes a single octet from the source. Read more
source§

fn take_opt_u8(&mut self) -> Result<Option<u8>, Self::Error>

Takes an optional octet from the source. Read more
source§

fn content_err(&self, err: impl Into<ContentError>) -> DecodeError<Self::Error>

Returns a content error at the current position of the source.

Auto Trait Implementations§

§

impl<S> RefUnwindSafe for LimitedSource<S>
where S: RefUnwindSafe,

§

impl<S> Send for LimitedSource<S>
where S: Send,

§

impl<S> Sync for LimitedSource<S>
where S: Sync,

§

impl<S> Unpin for LimitedSource<S>
where S: Unpin,

§

impl<S> UnwindSafe for LimitedSource<S>
where S: 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> IntoSource for T
where T: Source,

§

type Source = T

source§

fn into_source(self) -> <T as IntoSource>::Source

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.