Skip to main content

Decoder

Struct Decoder 

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

Deserializes CBOR items from a fixed input slice.

Implementations§

Source§

impl<'a> Decoder<'a>

Source

pub const fn new(input: &'a [u8]) -> Self

Creates a decoder reading from the start of input.

Source

pub const fn position(&self) -> usize

Number of bytes consumed so far.

Source

pub const fn remaining(&self) -> usize

Bytes left unread.

Source

pub const fn is_empty(&self) -> bool

true when every input byte has been consumed.

Source

pub fn peek_major(&self) -> Option<Major>

Major type of the next item without consuming it.

Source

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

Decodes an unsigned integer that fits in u8.

§Errors

Error::TYPE_MISMATCH for non-integers; Error::INTEGER_OUT_OF_RANGE when the value exceeds u8::MAX.

Source

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

Decodes an unsigned integer that fits in u16.

§Errors

As Decoder::u8, with the wider target range.

Source

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

Decodes an unsigned integer that fits in u32.

§Errors

As Decoder::u8, with the wider target range.

Source

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

Decodes any CBOR unsigned integer into u64.

§Errors

Returns Error::TYPE_MISMATCH for other major types.

Source

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

Decodes a negative or non-negative integer into i8.

§Errors

Error::TYPE_MISMATCH for non-integers; Error::INTEGER_OUT_OF_RANGE outside the i8 range.

Source

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

Decodes a signed integer into i16.

§Errors

As Decoder::i8, with the wider target range.

Source

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

Decodes a signed integer into i32.

§Errors

As Decoder::i8, with the wider target range.

Source

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

Decodes any CBOR integer (major types 0 and 1) into i64.

§Errors

Error::TYPE_MISMATCH for non-integers; Error::INTEGER_OUT_OF_RANGE below i64::MIN.

Source

pub fn bytes(&mut self) -> Result<&'a [u8], Error>

Borrows a byte string’s contents from the input.

§Errors

Error::TYPE_MISMATCH if not a byte string; Error::TRUNCATED if the declared length exceeds the input.

Source

pub fn str(&mut self) -> Result<&'a str, Error>

Borrows a text string from the input after validating UTF-8.

§Errors

Error::TYPE_MISMATCH if not a text string; Error::INVALID_UTF8 if the payload violates RFC 3629; Error::TRUNCATED on short input.

Source

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

Decodes true or false.

§Errors

Returns Error::TYPE_MISMATCH for any other item.

Source

pub fn is_null(&mut self) -> Result<bool, Error>

Decodes null; returns whether the value was null without consuming anything else. Use Decoder::peek_major for generic lookahead.

§Errors

Propagates decode errors from malformed heads only.

Source

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

Decodes undefined.

§Errors

Returns Error::TYPE_MISMATCH for any other item.

Source

pub fn simple(&mut self) -> Result<Simple, Error>

Decodes any simple value (including booleans and null).

§Errors

Error::TYPE_MISMATCH for floats and non-major-7 items; Error::INVALID_SIMPLE for two-byte encodings of values below 32.

Source

pub fn tag(&mut self) -> Result<u64, Error>

Reads a tag number; the tagged item follows immediately.

Tag semantics are application concerns (RFC 8949 §3.4); this returns only the number.

§Errors

Returns Error::TYPE_MISMATCH if the item is not a tag.

Source

pub fn array_len(&mut self) -> Result<usize, Error>

Reads an array header, returning the number of following items.

§Errors

Returns Error::TYPE_MISMATCH if the item is not an array.

Source

pub fn map_len(&mut self) -> Result<usize, Error>

Reads a map header, returning the number of following key/value pairs.

§Errors

Returns Error::TYPE_MISMATCH if the item is not a map.

Source

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

Consumes exactly one complete data item, whatever its type.

Nested containers are traversed iteratively up to SKIP_LIMIT levels deep; deeper input yields Error::DEPTH_EXCEEDED.

§Errors

Any structural error (Error::TRUNCATED, Error::INVALID_ADDITIONAL_INFO, Error::INVALID_SIMPLE, Error::DEPTH_EXCEEDED).

Trait Implementations§

Source§

impl<'a> Clone for Decoder<'a>

Source§

fn clone(&self) -> Decoder<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Decoder<'a>

Source§

impl<'a> Debug for Decoder<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Decoder<'a>

§

impl<'a> RefUnwindSafe for Decoder<'a>

§

impl<'a> Send for Decoder<'a>

§

impl<'a> Sync for Decoder<'a>

§

impl<'a> Unpin for Decoder<'a>

§

impl<'a> UnsafeUnpin for Decoder<'a>

§

impl<'a> UnwindSafe for Decoder<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.