pub struct Decoder<'a> { /* private fields */ }Expand description
Deserializes CBOR items from a fixed input slice.
Implementations§
Source§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
Sourcepub const fn new(input: &'a [u8]) -> Self
pub const fn new(input: &'a [u8]) -> Self
Creates a decoder reading from the start of input.
Examples found in repository?
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}Sourcepub fn peek_major(&self) -> Option<Major>
pub fn peek_major(&self) -> Option<Major>
Major type of the next item without consuming it.
Sourcepub fn peek_break(&self) -> bool
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.
Sourcepub fn peek_head_byte(&self) -> Option<u8>
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.
Sourcepub fn take_break(&mut self) -> Result<bool, Error>
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.
Sourcepub fn u8(&mut self) -> Result<u8, Error>
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.
Sourcepub fn u64(&mut self) -> Result<u64, Error>
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?
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}Sourcepub fn i8(&mut self) -> Result<i8, Error>
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.
Sourcepub fn i64(&mut self) -> Result<i64, Error>
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?
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}Sourcepub fn bytes(&mut self) -> Result<&'a [u8], Error>
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.
Sourcepub fn str(&mut self) -> Result<&'a str, Error>
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?
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}Sourcepub fn is_null(&mut self) -> Result<bool, Error>
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.
Sourcepub fn simple(&mut self) -> Result<Simple, Error>
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.
Sourcepub fn tag(&mut self) -> Result<u64, Error>
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.
Sourcepub fn array_len(&mut self) -> Result<usize, Error>
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.
Sourcepub fn map_len(&mut self) -> Result<usize, Error>
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.
Sourcepub fn skip(&mut self) -> Result<(), Error>
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?
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}