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.

Examples found in repository?
examples/cbor-size.rs (line 104)
90fn run_once() -> usize {
91    let mut buf = [0u8; 512];
92
93    // ---- always available: definite-length core --------------------
94    let mut e = Encoder::new(&mut buf);
95    let input: u64 = black_box(0xa1_2b_3c_4d_5e_6f_70_81);
96    let neg: i64 = black_box(-25_000);
97    let text: &str = black_box("heartbeat");
98    ok!(e.uint(input));
99    ok!(e.i64(neg));
100    ok!(e.str(text));
101    ok!(ok!(e.array_len(2)).null().and_then(|e| e.bool(true)));
102    let n = e.written();
103
104    let mut d = Decoder::new(black_box(&buf[..n]));
105    let _ = black_box(ok!(d.u64()));
106    let _ = black_box(ok!(d.i64()));
107    let _ = black_box(ok!(d.str()));
108    ok!(d.skip());
109
110    #[cfg(feature = "float")]
111    float_tier(&mut buf);
112
113    #[cfg(feature = "indefinite")]
114    indefinite_tier(&mut buf);
115
116    #[cfg(feature = "bignum")]
117    bignum_tier(&mut buf);
118
119    #[cfg(feature = "diagnostic")]
120    diagnostic_tier();
121
122    #[cfg(feature = "canonical")]
123    canonical_tier(&mut buf);
124
125    #[cfg(all(feature = "raw", feature = "indefinite"))]
126    raw_tier(&mut buf);
127
128    n
129}
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 peek_break(&self) -> bool

true when the next byte is the break marker terminating an indefinite-length item (RFC 8949 §3.2); does not consume anything.

Source

pub fn peek_head_byte(&self) -> Option<u8>

Raw initial byte of the next item, if any input remains; does not consume anything. Lets callers inspect the additional-information field (byte & 0x1f) before committing to a typed getter.

Source

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

Consumes a break marker if present; reports whether one was there.

§Errors

Error::UNEXPECTED_BREAK is never produced here; the Err half exists for API symmetry and currently cannot occur.

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.

Examples found in repository?
examples/cbor-size.rs (line 105)
90fn run_once() -> usize {
91    let mut buf = [0u8; 512];
92
93    // ---- always available: definite-length core --------------------
94    let mut e = Encoder::new(&mut buf);
95    let input: u64 = black_box(0xa1_2b_3c_4d_5e_6f_70_81);
96    let neg: i64 = black_box(-25_000);
97    let text: &str = black_box("heartbeat");
98    ok!(e.uint(input));
99    ok!(e.i64(neg));
100    ok!(e.str(text));
101    ok!(ok!(e.array_len(2)).null().and_then(|e| e.bool(true)));
102    let n = e.written();
103
104    let mut d = Decoder::new(black_box(&buf[..n]));
105    let _ = black_box(ok!(d.u64()));
106    let _ = black_box(ok!(d.i64()));
107    let _ = black_box(ok!(d.str()));
108    ok!(d.skip());
109
110    #[cfg(feature = "float")]
111    float_tier(&mut buf);
112
113    #[cfg(feature = "indefinite")]
114    indefinite_tier(&mut buf);
115
116    #[cfg(feature = "bignum")]
117    bignum_tier(&mut buf);
118
119    #[cfg(feature = "diagnostic")]
120    diagnostic_tier();
121
122    #[cfg(feature = "canonical")]
123    canonical_tier(&mut buf);
124
125    #[cfg(all(feature = "raw", feature = "indefinite"))]
126    raw_tier(&mut buf);
127
128    n
129}
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.

Examples found in repository?
examples/cbor-size.rs (line 106)
90fn run_once() -> usize {
91    let mut buf = [0u8; 512];
92
93    // ---- always available: definite-length core --------------------
94    let mut e = Encoder::new(&mut buf);
95    let input: u64 = black_box(0xa1_2b_3c_4d_5e_6f_70_81);
96    let neg: i64 = black_box(-25_000);
97    let text: &str = black_box("heartbeat");
98    ok!(e.uint(input));
99    ok!(e.i64(neg));
100    ok!(e.str(text));
101    ok!(ok!(e.array_len(2)).null().and_then(|e| e.bool(true)));
102    let n = e.written();
103
104    let mut d = Decoder::new(black_box(&buf[..n]));
105    let _ = black_box(ok!(d.u64()));
106    let _ = black_box(ok!(d.i64()));
107    let _ = black_box(ok!(d.str()));
108    ok!(d.skip());
109
110    #[cfg(feature = "float")]
111    float_tier(&mut buf);
112
113    #[cfg(feature = "indefinite")]
114    indefinite_tier(&mut buf);
115
116    #[cfg(feature = "bignum")]
117    bignum_tier(&mut buf);
118
119    #[cfg(feature = "diagnostic")]
120    diagnostic_tier();
121
122    #[cfg(feature = "canonical")]
123    canonical_tier(&mut buf);
124
125    #[cfg(all(feature = "raw", feature = "indefinite"))]
126    raw_tier(&mut buf);
127
128    n
129}
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.

Examples found in repository?
examples/cbor-size.rs (line 107)
90fn run_once() -> usize {
91    let mut buf = [0u8; 512];
92
93    // ---- always available: definite-length core --------------------
94    let mut e = Encoder::new(&mut buf);
95    let input: u64 = black_box(0xa1_2b_3c_4d_5e_6f_70_81);
96    let neg: i64 = black_box(-25_000);
97    let text: &str = black_box("heartbeat");
98    ok!(e.uint(input));
99    ok!(e.i64(neg));
100    ok!(e.str(text));
101    ok!(ok!(e.array_len(2)).null().and_then(|e| e.bool(true)));
102    let n = e.written();
103
104    let mut d = Decoder::new(black_box(&buf[..n]));
105    let _ = black_box(ok!(d.u64()));
106    let _ = black_box(ok!(d.i64()));
107    let _ = black_box(ok!(d.str()));
108    ok!(d.skip());
109
110    #[cfg(feature = "float")]
111    float_tier(&mut buf);
112
113    #[cfg(feature = "indefinite")]
114    indefinite_tier(&mut buf);
115
116    #[cfg(feature = "bignum")]
117    bignum_tier(&mut buf);
118
119    #[cfg(feature = "diagnostic")]
120    diagnostic_tier();
121
122    #[cfg(feature = "canonical")]
123    canonical_tier(&mut buf);
124
125    #[cfg(all(feature = "raw", feature = "indefinite"))]
126    raw_tier(&mut buf);
127
128    n
129}
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).

Examples found in repository?
examples/cbor-size.rs (line 108)
90fn run_once() -> usize {
91    let mut buf = [0u8; 512];
92
93    // ---- always available: definite-length core --------------------
94    let mut e = Encoder::new(&mut buf);
95    let input: u64 = black_box(0xa1_2b_3c_4d_5e_6f_70_81);
96    let neg: i64 = black_box(-25_000);
97    let text: &str = black_box("heartbeat");
98    ok!(e.uint(input));
99    ok!(e.i64(neg));
100    ok!(e.str(text));
101    ok!(ok!(e.array_len(2)).null().and_then(|e| e.bool(true)));
102    let n = e.written();
103
104    let mut d = Decoder::new(black_box(&buf[..n]));
105    let _ = black_box(ok!(d.u64()));
106    let _ = black_box(ok!(d.i64()));
107    let _ = black_box(ok!(d.str()));
108    ok!(d.skip());
109
110    #[cfg(feature = "float")]
111    float_tier(&mut buf);
112
113    #[cfg(feature = "indefinite")]
114    indefinite_tier(&mut buf);
115
116    #[cfg(feature = "bignum")]
117    bignum_tier(&mut buf);
118
119    #[cfg(feature = "diagnostic")]
120    diagnostic_tier();
121
122    #[cfg(feature = "canonical")]
123    canonical_tier(&mut buf);
124
125    #[cfg(all(feature = "raw", feature = "indefinite"))]
126    raw_tier(&mut buf);
127
128    n
129}

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

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

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.