Debian Codegraph @

src/codec/cbor.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# as IoErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#;
3
4 use craterust-analyzer cargo futures_codec 0.4.1 crate/::{Decoderrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#, Encoderrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#};
5 use bytesrust-analyzer cargo bytes 1.12.1 crate/::{Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#, BufMutrust-analyzer cargo bytes 1.12.1 buf/buf_mut/BufMut#, BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#};
6
7 use serderust-analyzer cargo serde 1.0.229 crate/::{Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#, Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#};
8 use serde_cborrust-analyzer cargo serde_cbor 0.11.2 crate/::Errorrust-analyzer cargo serde_cbor 0.11.2 error/Error# as CborErrorrust-analyzer cargo serde_cbor 0.11.2 error/Error#;
9
10 /// A codec for JSON encoding and decoding using serde_cbor
11 /// Enc is the type to encode, Dec is the type to decode
12 /// ```
13 /// # use futures::{executor, SinkExt, TryStreamExt};
14 /// # use futures::io::Cursor;
15 /// use serde::{Serialize, Deserialize};
16 /// use futures_codec::{CborCodec, Framed};
17 ///
18 /// #[derive(Serialize, Deserialize)]
19 /// struct Something {
20 /// pub data: u16,
21 /// }
22 ///
23 /// async move {
24 /// # let mut buf = vec![];
25 /// # let stream = Cursor::new(&mut buf);
26 /// // let stream = ...
27 /// let codec = CborCodec::<Something, Something>::new();
28 /// let mut framed = Framed::new(stream, codec);
29 ///
30 /// while let Some(s) = framed.try_next().await.unwrap() {
31 /// println!("{:?}", s.data);
32 /// }
33 /// };
34 /// ```
35 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
36 pub struct CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#<Enc, Dec> {
37 encrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#enc.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<Enc>,
38 decrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#dec.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<Dec>,
39 }
40
41 /// JSON Codec error enumeration
42 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
43 pub enum CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError# {
44 /// IO error
45 Iorust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#Io#(IoErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#),
46 /// JSON error
47 Cborrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#Cbor#(CborErrorrust-analyzer cargo serde_cbor 0.11.2 error/Error#),
48 }
49
50 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<IoErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> for CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError# {
51 fn fromrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[CborCodecError][`From<IoError>`]from().(e: IoErrorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#) -> CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError# {
52 return CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#::Iorust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#Io#(e);
53 }
54 }
55
56 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<CborErrorrust-analyzer cargo serde_cbor 0.11.2 error/Error#> for CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError# {
57 fn fromrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[CborCodecError][`From<CborError>`]from().(e: CborErrorrust-analyzer cargo serde_cbor 0.11.2 error/Error#) -> CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError# {
58 return CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#::Cborrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#Cbor#(e);
59 }
60 }
61
62 impl<Enc, Dec> CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#<Enc, Dec>
63 where
64 for<'de> Dec: Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'de> + 'static,
65 for<'de> Enc: Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize# + 'static,
66 {
67 /// Creates a new `CborCodec` with the associated types
68 pub fn newrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`]new().() -> CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#<Enc, Dec> {
69 CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec# {
70 encrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#enc.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#,
71 decrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#dec.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#,
72 }
73 }
74 }
75
76 impl<Enc, Dec> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#<Enc, Dec>
77 where
78 for<'de> Dec: Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'de> + 'static,
79 for<'de> Enc: Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize# + 'static,
80 {
81 /// Clone creates a new instance of the `CborCodec`
82 fn clonerust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Clone]clone().(&self) -> CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#<Enc, Dec> {
83 CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#::newrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`]new().()
84 }
85 }
86
87 /// Decoder impl parses cbor objects from bytes
88 impl<Enc, Dec> Decoderrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder# for CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#<Enc, Dec>
89 where
90 for<'de> Dec: Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'de> + 'static,
91 for<'de> Enc: Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize# + 'static,
92 {
93 type Itemrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]Item# = Dec;
94 type Errorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]Error# = CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#;
95
96 fn decoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]decode().(&mut self, buf: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]::Itemrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#Item#>, Selfrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]::Errorrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#Error#> {
97 // Build deserializer
98 let mut de = serde_cborrust-analyzer cargo serde_cbor 0.11.2 crate/::Deserializerrust-analyzer cargo serde_cbor 0.11.2 de/Deserializer#::from_slicerust-analyzer cargo serde_cbor 0.11.2 de/impl#[`Deserializer<SliceRead<'a>>`]from_slice().(&buf);
99
100 // Attempt deserialization
101 let res: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Dec, _> = serderust-analyzer cargo serde 1.0.229 crate/::derust-analyzer cargo serde_core 1.0.229 de/::Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#::deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#deserialize().(&mut de);
102
103 // If we ran out before parsing, return none and try again later
104 let res = match res {
105 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(v) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v)),
106 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) if e.is_eofrust-analyzer cargo serde_cbor 0.11.2 error/impl#[Error]is_eof().() => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
107 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
108 };
109
110 // Update offset from iterator
111 let offset = de.byte_offsetrust-analyzer cargo serde_cbor 0.11.2 de/impl#[`Deserializer<R>`]byte_offset().();
112
113 // Advance buffer
114 buf.advancerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][Buf]advance().(offset);
115
116 res
117 }
118 }
119
120 /// Encoder impl encodes object streams to bytes
121 impl<Enc, Dec> Encoderrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder# for CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#<Enc, Dec>
122 where
123 for<'de> Dec: Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'de> + 'static,
124 for<'de> Enc: Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize# + 'static,
125 {
126 type Itemrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Encoder]Item# = Enc;
127 type Errorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Encoder]Error# = CborCodecErrorrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodecError#;
128
129 fn encoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Encoder]encode().(&mut self, data: Selfrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Encoder]::Itemrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#Item#, buf: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Encoder]::Errorrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#Error#> {
130 // Encode cbor
131 let j = serde_cborrust-analyzer cargo serde_cbor 0.11.2 crate/::to_vecrust-analyzer cargo serde_cbor 0.11.2 ser/to_vec().(&data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
132
133 // Write to buffer
134 buf.reserverust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]reserve().(j.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().());
135 buf.put_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][BufMut]put_slice().(&j);
136
137 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
138 }
139 }
140
141 #[cfg(test)]
142 mod testrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/ {
143 use bytesrust-analyzer cargo bytes 1.12.1 crate/::BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#;
144 use serderust-analyzer cargo serde 1.0.229 crate/::{Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#, Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#};
145
146 use superrust-analyzer cargo futures_codec 0.4.1 codec/cbor/::CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#;
147 use craterust-analyzer cargo futures_codec 0.4.1 crate/::{Decoderrust-analyzer cargo futures_codec 0.4.1 decoder/Decoder#, Encoderrust-analyzer cargo futures_codec 0.4.1 encoder/Encoder#};
148
149 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:, Deserializerust-analyzer cargo serde_derive 1.0.229 Deserialize:)]
150 struct TestStructrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct# {
151 pub namerust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
152 pub datarust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#data.: u16,
153 }
154
155 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
156 fn cbor_codec_encode_decoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/cbor_codec_encode_decode().() {
157 let mut codec = CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#::<TestStructrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#, TestStructrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#>::newrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`]new().();
158 let mut buff = BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::newrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]new().();
159
160 let item1 = TestStructrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct# {
161 namerust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#name.: "Test name".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
162 datarust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#data.: 16,
163 };
164 codec.encoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Encoder]encode().(item1.clonerust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/impl#[TestStruct][Clone]clone().(), &mut buff).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
165
166 let item2 = codec.decoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]decode().(&mut buff).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
167 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(item1, item2);
168
169 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codec.decoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]decode().(&mut buff).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
170
171 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buff.lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().(), 0);
172 }
173
174 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
175 fn cbor_codec_partial_decoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/cbor_codec_partial_decode().() {
176 let mut codec = CborCodecrust-analyzer cargo futures_codec 0.4.1 codec/cbor/CborCodec#::<TestStructrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#, TestStructrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#>::newrust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`]new().();
177 let mut buff = BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::newrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]new().();
178
179 let item1 = TestStructrust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct# {
180 namerust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#name.: "Test name".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
181 datarust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/TestStruct#data.: 34,
182 };
183 codec.encoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Encoder]encode().(item1.clonerust-analyzer cargo futures_codec 0.4.1 codec/cbor/test/impl#[TestStruct][Clone]clone().(), &mut buff).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
184
185 let mut start = buff.clonerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][Clone]clone().().split_torust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]split_to().(4);
186 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(codec.decoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]decode().(&mut start).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
187
188 codec.decoderust-analyzer cargo futures_codec 0.4.1 codec/cbor/impl#[`CborCodec<Enc, Dec>`][Decoder]decode().(&mut buff).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
189
190 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buff.lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().(), 0);
191 }
192 }