pub struct Encoder<'b> { /* private fields */ }Expand description
Serializes CBOR items into a fixed buffer.
On success each method advances the cursor by exactly the item’s
preferred-encoding length. On failure (Error::OUT_OF_SPACE) nothing
is written for that item, so the encoder stays valid and reusable.
Implementations§
Source§impl<'b> Encoder<'b>
impl<'b> Encoder<'b>
Sourcepub const fn new(buf: &'b mut [u8]) -> Self
pub const fn new(buf: &'b mut [u8]) -> Self
Creates an encoder writing into buf, starting at offset 0.
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 const fn written(&self) -> usize
pub const fn written(&self) -> usize
Number of bytes successfully written so far.
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 const fn finish(self) -> (&'b [u8], usize)
pub const fn finish(self) -> (&'b [u8], usize)
Consumes the encoder, returning the buffer and the bytes written.
Sourcepub fn uint(&mut self, v: u64) -> Result<&mut Self, Error>
pub fn uint(&mut self, v: u64) -> Result<&mut Self, Error>
Encodes any unsigned integer using its preferred width.
Prefer the typed helpers (u8, u16, …); they compile to
identical code and document the value’s range.
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 nint(&mut self, n: u64) -> Result<&mut Self, Error>
pub fn nint(&mut self, n: u64) -> Result<&mut Self, Error>
Encodes -1 - n for magnitude n (RFC 8949 §3.1).
Magnitudes above i64::MIN’s range remain representable in CBOR,
so this accepts the full u64 range.
Sourcepub fn i64(&mut self, v: i64) -> Result<&mut Self, Error>
pub fn i64(&mut self, v: i64) -> Result<&mut Self, Error>
Encodes a signed integer.
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, b: &[u8]) -> Result<&mut Self, Error>
pub fn bytes(&mut self, b: &[u8]) -> Result<&mut Self, Error>
Encodes a byte string header followed by its contents.
Sourcepub fn str(&mut self, s: &str) -> Result<&mut Self, Error>
pub fn str(&mut self, s: &str) -> Result<&mut Self, Error>
Encodes a text string header followed by its UTF-8 contents.
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 bool(&mut self, v: bool) -> Result<&mut Self, Error>
pub fn bool(&mut self, v: bool) -> Result<&mut Self, Error>
Encodes true or false.
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 null(&mut self) -> Result<&mut Self, Error>
pub fn null(&mut self) -> Result<&mut Self, Error>
Encodes null.
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 undefined(&mut self) -> Result<&mut Self, Error>
pub fn undefined(&mut self) -> Result<&mut Self, Error>
Encodes undefined. RFC 8949 §5.7 discourages its use on the wire,
but the encoding is supported.
Sourcepub fn simple(&mut self, s: Simple) -> Result<&mut Self, Error>
pub fn simple(&mut self, s: Simple) -> Result<&mut Self, Error>
Encodes a simple value.
§Errors
Returns Error::INVALID_SIMPLE when the value would require the
two-byte form while being below 32; RFC 8949 §3.3 forbids that form.
Sourcepub fn tag(&mut self, number: u64) -> Result<&mut Self, Error>
pub fn tag(&mut self, number: u64) -> Result<&mut Self, Error>
Emits a tag header; encode the tagged item immediately after.
Tag semantics live outside this crate (RFC 8949 §3.4): only the tag number travels on the wire.
Sourcepub fn array_len(&mut self, len: usize) -> Result<&mut Self, Error>
pub fn array_len(&mut self, len: usize) -> Result<&mut Self, Error>
Emits an array header for len following items.
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}