├── .github └── workflows │ └── main.yml ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples ├── client.rs └── server.rs └── src └── lib.rs /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | on: 2 | pull_request: 3 | push: 4 | branches: 5 | - master 6 | 7 | name: CI 8 | 9 | jobs: 10 | ci: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@main 15 | - uses: dtolnay/rust-toolchain@master 16 | with: 17 | toolchain: stable 18 | components: rustfmt, clippy 19 | - run: cargo fmt --all --check -- --config=imports_granularity=Crate 20 | - run: cargo clippy --all-targets --workspace -- -D warnings 21 | - run: cargo test --all-targets --workspace 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "tokio-serde" 3 | version = "0.9.0" 4 | edition = "2021" 5 | authors = [ 6 | "Carl Lerche ", 7 | "Artem Vorotnikov ", 8 | "Bastian Köcher ", 9 | ] 10 | license = "MIT OR Apache-2.0" 11 | readme = "README.md" 12 | categories = ["asynchronous", "encoding"] 13 | keywords = ["async", "serde", "serialization"] 14 | repository = "https://github.com/carllerche/tokio-serde" 15 | homepage = "https://github.com/carllerche/tokio-serde" 16 | documentation = "https://docs.rs/tokio-serde" 17 | description = """ 18 | Send and receive Serde encodable types over the network using Tokio. 19 | 20 | This library is used as a building block for serialization format specific 21 | libraries. 22 | """ 23 | 24 | [dependencies] 25 | bytes = "1.0" 26 | educe = { version = "0.5", optional = true, default-features = false } 27 | futures-core = "0.3" 28 | futures-sink = "0.3" 29 | pin-project = "1" 30 | serde = { version = "1", optional = true } 31 | bincode-crate = { package = "bincode", version = "1", optional = true } 32 | serde_json = { version = "1", optional = true } 33 | rmp-serde = { version = "1", optional = true } 34 | serde_cbor = { version = "0.11", optional = true } 35 | 36 | [dev-dependencies] 37 | futures = "0.3" 38 | impls = "1" 39 | tokio = { version = "1.0", features = ["full"] } 40 | tokio-util = { version = "0.7", features = ["codec"] } 41 | static_assertions = "1.1.0" 42 | 43 | [package.metadata.docs.rs] 44 | all-features = true 45 | rustdoc-args = ["--cfg", "docsrs"] 46 | 47 | [features] 48 | bincode = ["educe/Debug", "serde", "bincode-crate"] 49 | json = ["educe/Debug", "educe/Default", "serde", "serde_json"] 50 | messagepack = ["educe/Debug", "educe/Default", "serde", "rmp-serde"] 51 | cbor = ["educe/Debug", "educe/Default", "serde", "serde_cbor"] 52 | 53 | [[example]] 54 | name = "client" 55 | required-features = ["bincode", "cbor", "json", "messagepack"] 56 | 57 | [[example]] 58 | name = "server" 59 | required-features = ["bincode", "cbor", "json", "messagepack"] 60 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Carl Lerche 2 | Copyright (c) 2018 Bastian Köcher 3 | Copyright (c) 2019-2020 Artem Vorotnikov 4 | 5 | Permission is hereby granted, free of charge, to any 6 | person obtaining a copy of this software and associated 7 | documentation files (the "Software"), to deal in the 8 | Software without restriction, including without 9 | limitation the rights to use, copy, modify, merge, 10 | publish, distribute, sublicense, and/or sell copies of 11 | the Software, and to permit persons to whom the Software 12 | is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice 16 | shall be included in all copies or substantial portions 17 | of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 20 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 21 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 22 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 23 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 24 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 26 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | DEALINGS IN THE SOFTWARE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tokio Serialize / Deserialize 2 | 3 | Utilities needed to easily implement a Tokio transport using [serde] for 4 | serialization and deserialization of frame values. 5 | 6 | [Documentation](https://docs.rs/tokio-serde) 7 | 8 | ## Usage 9 | 10 | To use `tokio-serde`, first add this to your `Cargo.toml`: 11 | 12 | ```toml 13 | [dependencies] 14 | tokio-serde = "0.9" 15 | ``` 16 | 17 | Next, add this to your crate: 18 | 19 | ```rust 20 | use tokio_serde::{Serializer, Deserializer, Framed}; 21 | ``` 22 | 23 | [serde]: https://serde.rs 24 | 25 | # License 26 | 27 | This project is licensed under either of 28 | 29 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or 30 | http://www.apache.org/licenses/LICENSE-2.0) 31 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or 32 | http://opensource.org/licenses/MIT) 33 | 34 | at your option. 35 | 36 | ### Contribution 37 | 38 | Unless you explicitly state otherwise, any contribution intentionally submitted 39 | for inclusion in iovec by you, as defined in the Apache-2.0 license, shall be 40 | dual licensed as above, without any additional terms or conditions. 41 | -------------------------------------------------------------------------------- /examples/client.rs: -------------------------------------------------------------------------------- 1 | use futures::prelude::*; 2 | use serde_json::json; 3 | use tokio::net::TcpStream; 4 | use tokio_serde::formats::*; 5 | use tokio_util::codec::{FramedWrite, LengthDelimitedCodec}; 6 | 7 | #[tokio::main] 8 | pub async fn main() { 9 | // Bind a server socket 10 | let socket = TcpStream::connect("127.0.0.1:17653").await.unwrap(); 11 | 12 | // Delimit frames using a length header 13 | let length_delimited = FramedWrite::new(socket, LengthDelimitedCodec::new()); 14 | 15 | // Serialize frames with JSON 16 | let mut serialized = 17 | tokio_serde::SymmetricallyFramed::new(length_delimited, SymmetricalJson::default()); 18 | 19 | // Send the value 20 | serialized 21 | .send(json!({ 22 | "name": "John Doe", 23 | "age": 43, 24 | "phones": [ 25 | "+44 1234567", 26 | "+44 2345678" 27 | ] 28 | })) 29 | .await 30 | .unwrap() 31 | } 32 | -------------------------------------------------------------------------------- /examples/server.rs: -------------------------------------------------------------------------------- 1 | use futures::prelude::*; 2 | use serde_json::Value; 3 | use tokio::net::TcpListener; 4 | use tokio_serde::formats::*; 5 | use tokio_util::codec::{FramedRead, LengthDelimitedCodec}; 6 | 7 | #[tokio::main] 8 | pub async fn main() { 9 | // Bind a server socket 10 | let listener = TcpListener::bind("127.0.0.1:17653").await.unwrap(); 11 | 12 | println!("listening on {:?}", listener.local_addr()); 13 | 14 | loop { 15 | let (socket, _) = listener.accept().await.unwrap(); 16 | 17 | // Delimit frames using a length header 18 | let length_delimited = FramedRead::new(socket, LengthDelimitedCodec::new()); 19 | 20 | // Deserialize frames 21 | let mut deserialized = tokio_serde::SymmetricallyFramed::new( 22 | length_delimited, 23 | SymmetricalJson::::default(), 24 | ); 25 | 26 | // Spawn a task that prints all received messages to STDOUT 27 | tokio::spawn(async move { 28 | while let Some(msg) = deserialized.try_next().await.unwrap() { 29 | println!("GOT: {:?}", msg); 30 | } 31 | }); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! This crate provides the utilities needed to easily implement a Tokio 2 | //! transport using [serde] for serialization and deserialization of frame 3 | //! values. 4 | //! 5 | //! # Introduction 6 | //! 7 | //! This crate provides [transport] combinators that transform a stream of 8 | //! frames encoded as bytes into a stream of frame values. It is expected that 9 | //! the framing happens at another layer. One option is to use a [length 10 | //! delimited] framing transport. 11 | //! 12 | //! The crate provides two traits that must be implemented: [`Serializer`] and 13 | //! [`Deserializer`]. Implementations of these traits are then passed to 14 | //! [`Framed`] along with the upstream [`Stream`] or 15 | //! [`Sink`] that handles the byte encoded frames. 16 | //! 17 | //! By doing this, a transformation pipeline is built. For reading, it looks 18 | //! something like this: 19 | //! 20 | //! * `tokio_serde::Framed` 21 | //! * `tokio_util::codec::FramedRead` 22 | //! * `tokio::net::TcpStream` 23 | //! 24 | //! The write half looks like: 25 | //! 26 | //! * `tokio_serde::Framed` 27 | //! * `tokio_util::codec::FramedWrite` 28 | //! * `tokio::net::TcpStream` 29 | //! 30 | //! # Examples 31 | //! 32 | //! For an example, see how JSON support is implemented: 33 | //! 34 | //! * [server](https://github.com/carllerche/tokio-serde/blob/master/examples/server.rs) 35 | //! * [client](https://github.com/carllerche/tokio-serde/blob/master/examples/client.rs) 36 | //! 37 | //! [serde]: https://serde.rs 38 | //! [serde-json]: https://github.com/serde-rs/json 39 | //! [transport]: https://tokio.rs/docs/going-deeper/transports/ 40 | //! [length delimited]: https://docs.rs/tokio-util/0.2/tokio_util/codec/length_delimited/index.html 41 | //! [`Serializer`]: trait.Serializer.html 42 | //! [`Deserializer`]: trait.Deserializer.html 43 | //! [`Framed`]: struct.Framed.html 44 | //! [`Stream`]: https://docs.rs/futures/0.3/futures/stream/trait.Stream.html 45 | //! [`Sink`]: https://docs.rs/futures/0.3/futures/sink/trait.Sink.html 46 | 47 | #![cfg_attr(docsrs, feature(doc_cfg))] 48 | 49 | use bytes::{Bytes, BytesMut}; 50 | use futures_core::{ready, Stream, TryStream}; 51 | use futures_sink::Sink; 52 | use pin_project::pin_project; 53 | use std::{ 54 | marker::PhantomData, 55 | pin::Pin, 56 | task::{Context, Poll}, 57 | }; 58 | 59 | /// Serializes a value into a destination buffer 60 | /// 61 | /// Implementations of `Serializer` are able to take values of type `T` and 62 | /// convert them to a byte representation. The specific byte format, i.e. JSON, 63 | /// protobuf, binpack, ... is an implementation detail. 64 | /// 65 | /// The `serialize` function takes `&mut self`, allowing for `Serializer` 66 | /// instances to be created with runtime configuration settings. 67 | /// 68 | /// # Examples 69 | /// 70 | /// An integer serializer that allows the width to be configured. 71 | /// 72 | /// ``` 73 | /// use tokio_serde::Serializer; 74 | /// use bytes::{Buf, Bytes, BytesMut, BufMut}; 75 | /// use std::pin::Pin; 76 | /// 77 | /// struct IntSerializer { 78 | /// width: usize, 79 | /// } 80 | /// 81 | /// #[derive(Debug)] 82 | /// enum Error { 83 | /// Overflow, 84 | /// } 85 | /// 86 | /// impl Serializer for IntSerializer { 87 | /// type Error = Error; 88 | /// 89 | /// fn serialize(self: Pin<&mut Self>, item: &u64) -> Result { 90 | /// assert!(self.width <= 8); 91 | /// 92 | /// let max = (1 << (self.width * 8)) - 1; 93 | /// 94 | /// if *item > max { 95 | /// return Err(Error::Overflow); 96 | /// } 97 | /// 98 | /// let mut ret = BytesMut::with_capacity(self.width); 99 | /// ret.put_uint(*item, self.width); 100 | /// Ok(ret.into()) 101 | /// } 102 | /// } 103 | /// 104 | /// let mut serializer = IntSerializer { width: 3 }; 105 | /// 106 | /// let buf = Pin::new(&mut serializer).serialize(&5).unwrap(); 107 | /// assert_eq!(buf, &b"\x00\x00\x05"[..]); 108 | /// ``` 109 | pub trait Serializer { 110 | type Error; 111 | 112 | /// Serializes `item` into a new buffer 113 | /// 114 | /// The serialization format is specific to the various implementations of 115 | /// `Serializer`. If the serialization is successful, a buffer containing 116 | /// the serialized item is returned. If the serialization is unsuccessful, 117 | /// an error is returned. 118 | /// 119 | /// Implementations of this function should not mutate `item` via any sort 120 | /// of internal mutability strategy. 121 | /// 122 | /// See the trait level docs for more detail. 123 | fn serialize(self: Pin<&mut Self>, item: &T) -> Result; 124 | } 125 | 126 | /// Deserializes a value from a source buffer 127 | /// 128 | /// Implementatinos of `Deserializer` take a byte buffer and return a value by 129 | /// parsing the contents of the buffer according to the implementation's format. 130 | /// The specific byte format, i.e. JSON, protobuf, binpack, is an implementation 131 | /// detail 132 | /// 133 | /// The `deserialize` function takes `&mut self`, allowing for `Deserializer` 134 | /// instances to be created with runtime configuration settings. 135 | /// 136 | /// It is expected that the supplied buffer represents a full value and only 137 | /// that value. If after deserializing a value there are remaining bytes the 138 | /// buffer, the deserializer will return an error. 139 | /// 140 | /// # Examples 141 | /// 142 | /// An integer deserializer that allows the width to be configured. 143 | /// 144 | /// ``` 145 | /// use tokio_serde::Deserializer; 146 | /// use bytes::{BytesMut, Buf}; 147 | /// use std::pin::Pin; 148 | /// 149 | /// struct IntDeserializer { 150 | /// width: usize, 151 | /// } 152 | /// 153 | /// #[derive(Debug)] 154 | /// enum Error { 155 | /// Underflow, 156 | /// Overflow 157 | /// } 158 | /// 159 | /// impl Deserializer for IntDeserializer { 160 | /// type Error = Error; 161 | /// 162 | /// fn deserialize(self: Pin<&mut Self>, buf: &BytesMut) -> Result { 163 | /// assert!(self.width <= 8); 164 | /// 165 | /// if buf.len() > self.width { 166 | /// return Err(Error::Overflow); 167 | /// } 168 | /// 169 | /// if buf.len() < self.width { 170 | /// return Err(Error::Underflow); 171 | /// } 172 | /// 173 | /// let ret = std::io::Cursor::new(buf).get_uint(self.width); 174 | /// Ok(ret) 175 | /// } 176 | /// } 177 | /// 178 | /// let mut deserializer = IntDeserializer { width: 3 }; 179 | /// 180 | /// let i = Pin::new(&mut deserializer).deserialize(&b"\x00\x00\x05"[..].into()).unwrap(); 181 | /// assert_eq!(i, 5); 182 | /// ``` 183 | pub trait Deserializer { 184 | type Error; 185 | 186 | /// Deserializes a value from `buf` 187 | /// 188 | /// The serialization format is specific to the various implementations of 189 | /// `Deserializer`. If the deserialization is successful, the value is 190 | /// returned. If the deserialization is unsuccessful, an error is returned. 191 | /// 192 | /// See the trait level docs for more detail. 193 | fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result; 194 | } 195 | 196 | /// Adapts a transport to a value sink by serializing the values and to a stream of values by deserializing them. 197 | /// 198 | /// It is expected that the buffers yielded by the supplied transport be framed. In 199 | /// other words, each yielded buffer must represent exactly one serialized 200 | /// value. 201 | /// 202 | /// The provided transport will receive buffer values containing the 203 | /// serialized value. Each buffer contains exactly one value. This sink will be 204 | /// responsible for writing these buffers to an `AsyncWrite` using some sort of 205 | /// framing strategy. 206 | /// 207 | /// The specific framing strategy is left up to the 208 | /// implementor. One option would be to use [length_delimited] provided by 209 | /// [tokio-util]. 210 | /// 211 | /// [length_delimited]: http://docs.rs/tokio-util/0.2/tokio_util/codec/length_delimited/index.html 212 | /// [tokio-util]: http://crates.io/crates/tokio-util 213 | #[pin_project] 214 | #[derive(Debug)] 215 | pub struct Framed { 216 | #[pin] 217 | inner: Transport, 218 | #[pin] 219 | codec: Codec, 220 | item: PhantomData<(Item, SinkItem)>, 221 | } 222 | 223 | impl Framed { 224 | /// Creates a new `Framed` with the given transport and codec. 225 | pub fn new(inner: Transport, codec: Codec) -> Self { 226 | Self { 227 | inner, 228 | codec, 229 | item: PhantomData, 230 | } 231 | } 232 | 233 | /// Returns a reference to the underlying transport wrapped by `Framed`. 234 | /// 235 | /// Note that care should be taken to not tamper with the underlying transport as 236 | /// it may corrupt the sequence of frames otherwise being worked with. 237 | pub fn get_ref(&self) -> &Transport { 238 | &self.inner 239 | } 240 | 241 | /// Returns a mutable reference to the underlying transport wrapped by 242 | /// `Framed`. 243 | /// 244 | /// Note that care should be taken to not tamper with the underlying transport as 245 | /// it may corrupt the sequence of frames otherwise being worked with. 246 | pub fn get_mut(&mut self) -> &mut Transport { 247 | &mut self.inner 248 | } 249 | 250 | /// Consumes the `Framed`, returning its underlying transport. 251 | /// 252 | /// Note that care should be taken to not tamper with the underlying transport as 253 | /// it may corrupt the sequence of frames otherwise being worked with. 254 | pub fn into_inner(self) -> Transport { 255 | self.inner 256 | } 257 | } 258 | 259 | impl Stream for Framed 260 | where 261 | Transport: TryStream, 262 | Codec::Error: Into, 263 | Codec: Deserializer, 264 | { 265 | type Item = Result; 266 | 267 | fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { 268 | match ready!(self.as_mut().project().inner.try_poll_next(cx)) { 269 | Some(bytes) => Poll::Ready(Some(Ok(self 270 | .as_mut() 271 | .project() 272 | .codec 273 | .deserialize(&bytes?) 274 | .map_err(Into::into)?))), 275 | None => Poll::Ready(None), 276 | } 277 | } 278 | } 279 | 280 | impl Sink for Framed 281 | where 282 | Transport: Sink, 283 | Codec: Serializer, 284 | Codec::Error: Into, 285 | { 286 | type Error = Transport::Error; 287 | 288 | fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { 289 | self.project().inner.poll_ready(cx) 290 | } 291 | 292 | fn start_send(mut self: Pin<&mut Self>, item: SinkItem) -> Result<(), Self::Error> { 293 | let res = self.as_mut().project().codec.serialize(&item); 294 | let bytes = res.map_err(Into::into)?; 295 | 296 | self.as_mut().project().inner.start_send(bytes)?; 297 | 298 | Ok(()) 299 | } 300 | 301 | fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { 302 | self.project().inner.poll_flush(cx) 303 | } 304 | 305 | fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { 306 | ready!(self.as_mut().poll_flush(cx))?; 307 | self.project().inner.poll_close(cx) 308 | } 309 | } 310 | 311 | pub type SymmetricallyFramed = Framed; 312 | 313 | #[cfg(any( 314 | feature = "json", 315 | feature = "bincode", 316 | feature = "messagepack", 317 | feature = "cbor" 318 | ))] 319 | pub mod formats { 320 | #[cfg(feature = "bincode")] 321 | pub use self::bincode::*; 322 | #[cfg(feature = "cbor")] 323 | pub use self::cbor::*; 324 | #[cfg(feature = "json")] 325 | pub use self::json::*; 326 | #[cfg(feature = "messagepack")] 327 | pub use self::messagepack::*; 328 | 329 | use super::{Deserializer, Serializer}; 330 | use bytes::{Bytes, BytesMut}; 331 | use educe::Educe; 332 | use serde::{Deserialize, Serialize}; 333 | use std::{marker::PhantomData, pin::Pin}; 334 | 335 | #[cfg(feature = "bincode")] 336 | mod bincode { 337 | use super::*; 338 | use bincode_crate::config::Options; 339 | use std::io; 340 | 341 | /// Bincode codec using [bincode](https://docs.rs/bincode) crate. 342 | #[cfg_attr(docsrs, doc(cfg(feature = "bincode")))] 343 | #[derive(Educe)] 344 | #[educe(Debug)] 345 | pub struct Bincode { 346 | #[educe(Debug(ignore))] 347 | options: O, 348 | #[educe(Debug(ignore))] 349 | ghost: PhantomData<(Item, SinkItem)>, 350 | } 351 | 352 | impl Default for Bincode { 353 | fn default() -> Self { 354 | Bincode { 355 | options: Default::default(), 356 | ghost: PhantomData, 357 | } 358 | } 359 | } 360 | 361 | impl From for Bincode 362 | where 363 | O: Options, 364 | { 365 | fn from(options: O) -> Self { 366 | Self { 367 | options, 368 | ghost: PhantomData, 369 | } 370 | } 371 | } 372 | 373 | #[cfg_attr(docsrs, doc(cfg(feature = "bincode")))] 374 | pub type SymmetricalBincode = Bincode; 375 | 376 | impl Deserializer for Bincode 377 | where 378 | for<'a> Item: Deserialize<'a>, 379 | O: Options + Clone, 380 | { 381 | type Error = io::Error; 382 | 383 | fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result { 384 | self.options 385 | .clone() 386 | .deserialize(src) 387 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) 388 | } 389 | } 390 | 391 | impl Serializer for Bincode 392 | where 393 | SinkItem: Serialize, 394 | O: Options + Clone, 395 | { 396 | type Error = io::Error; 397 | 398 | fn serialize(self: Pin<&mut Self>, item: &SinkItem) -> Result { 399 | self.options 400 | .clone() 401 | .serialize(item) 402 | .map(From::from) 403 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) 404 | } 405 | } 406 | } 407 | 408 | #[cfg(feature = "json")] 409 | mod json { 410 | use super::*; 411 | use bytes::Buf; 412 | 413 | /// JSON codec using [serde_json](https://docs.rs/serde_json) crate. 414 | #[cfg_attr(docsrs, doc(cfg(feature = "json")))] 415 | #[derive(Educe)] 416 | #[educe(Debug, Default)] 417 | pub struct Json { 418 | #[educe(Debug(ignore))] 419 | ghost: PhantomData<(Item, SinkItem)>, 420 | } 421 | 422 | #[cfg_attr(docsrs, doc(cfg(feature = "json")))] 423 | pub type SymmetricalJson = Json; 424 | 425 | impl Deserializer for Json 426 | where 427 | for<'a> Item: Deserialize<'a>, 428 | { 429 | type Error = serde_json::Error; 430 | 431 | fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result { 432 | serde_json::from_reader(std::io::Cursor::new(src).reader()) 433 | } 434 | } 435 | 436 | impl Serializer for Json 437 | where 438 | SinkItem: Serialize, 439 | { 440 | type Error = serde_json::Error; 441 | 442 | fn serialize(self: Pin<&mut Self>, item: &SinkItem) -> Result { 443 | serde_json::to_vec(item).map(Into::into) 444 | } 445 | } 446 | } 447 | 448 | #[cfg(feature = "messagepack")] 449 | mod messagepack { 450 | use super::*; 451 | use bytes::Buf; 452 | use std::io; 453 | 454 | /// MessagePack codec using [rmp-serde](https://docs.rs/rmp-serde) crate. 455 | #[cfg_attr(docsrs, doc(cfg(feature = "messagepack")))] 456 | #[derive(Educe)] 457 | #[educe(Debug, Default)] 458 | pub struct MessagePack { 459 | #[educe(Debug(ignore))] 460 | ghost: PhantomData<(Item, SinkItem)>, 461 | } 462 | 463 | #[cfg_attr(docsrs, doc(cfg(feature = "messagepack")))] 464 | pub type SymmetricalMessagePack = MessagePack; 465 | 466 | impl Deserializer for MessagePack 467 | where 468 | for<'a> Item: Deserialize<'a>, 469 | { 470 | type Error = io::Error; 471 | 472 | fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result { 473 | rmp_serde::from_read(std::io::Cursor::new(src).reader()) 474 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) 475 | } 476 | } 477 | 478 | impl Serializer for MessagePack 479 | where 480 | SinkItem: Serialize, 481 | { 482 | type Error = io::Error; 483 | 484 | fn serialize(self: Pin<&mut Self>, item: &SinkItem) -> Result { 485 | Ok(rmp_serde::to_vec(item) 486 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))? 487 | .into()) 488 | } 489 | } 490 | } 491 | 492 | #[cfg(feature = "cbor")] 493 | mod cbor { 494 | use super::*; 495 | use std::io; 496 | 497 | /// CBOR codec using [serde_cbor](https://docs.rs/serde_cbor) crate. 498 | #[cfg_attr(docsrs, doc(cfg(feature = "cbor")))] 499 | #[derive(Educe)] 500 | #[educe(Debug, Default)] 501 | pub struct Cbor { 502 | #[educe(Debug(ignore))] 503 | _mkr: PhantomData<(Item, SinkItem)>, 504 | } 505 | 506 | #[cfg_attr(docsrs, doc(cfg(feature = "cbor")))] 507 | pub type SymmetricalCbor = Cbor; 508 | 509 | impl Deserializer for Cbor 510 | where 511 | for<'a> Item: Deserialize<'a>, 512 | { 513 | type Error = io::Error; 514 | 515 | fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result { 516 | serde_cbor::from_slice(src.as_ref()).map_err(into_io_error) 517 | } 518 | } 519 | 520 | impl Serializer for Cbor 521 | where 522 | SinkItem: Serialize, 523 | { 524 | type Error = io::Error; 525 | 526 | fn serialize(self: Pin<&mut Self>, item: &SinkItem) -> Result { 527 | serde_cbor::to_vec(item) 528 | .map_err(into_io_error) 529 | .map(Into::into) 530 | } 531 | } 532 | 533 | fn into_io_error(cbor_err: serde_cbor::Error) -> io::Error { 534 | use io::ErrorKind; 535 | use serde_cbor::error::Category; 536 | use std::error::Error; 537 | 538 | match cbor_err.classify() { 539 | Category::Eof => io::Error::new(ErrorKind::UnexpectedEof, cbor_err), 540 | Category::Syntax => io::Error::new(ErrorKind::InvalidInput, cbor_err), 541 | Category::Data => io::Error::new(ErrorKind::InvalidData, cbor_err), 542 | Category::Io => { 543 | // Extract the underlying io error's type 544 | let kind = cbor_err 545 | .source() 546 | .and_then(|err| err.downcast_ref::()) 547 | .map(|io_err| io_err.kind()) 548 | .unwrap_or(ErrorKind::Other); 549 | io::Error::new(kind, cbor_err) 550 | } 551 | } 552 | } 553 | } 554 | } 555 | 556 | #[cfg(test)] 557 | mod tests { 558 | #[cfg(feature = "bincode")] 559 | #[test] 560 | fn bincode_impls() { 561 | use impls::impls; 562 | use std::fmt::Debug; 563 | 564 | struct Nothing; 565 | type T = crate::formats::Bincode; 566 | 567 | assert!(impls!(T: Debug)); 568 | assert!(impls!(T: Default)); 569 | } 570 | 571 | #[cfg(feature = "json")] 572 | #[test] 573 | fn json_impls() { 574 | use impls::impls; 575 | use std::fmt::Debug; 576 | 577 | struct Nothing; 578 | type T = crate::formats::Json; 579 | 580 | assert!(impls!(T: Debug)); 581 | assert!(impls!(T: Default)); 582 | } 583 | 584 | #[cfg(feature = "messagepack")] 585 | #[test] 586 | fn messagepack_impls() { 587 | use impls::impls; 588 | use std::fmt::Debug; 589 | 590 | struct Nothing; 591 | type T = crate::formats::MessagePack; 592 | 593 | assert!(impls!(T: Debug)); 594 | assert!(impls!(T: Default)); 595 | } 596 | 597 | #[cfg(feature = "cbor")] 598 | #[test] 599 | fn cbor_impls() { 600 | use impls::impls; 601 | use std::fmt::Debug; 602 | 603 | struct Nothing; 604 | type T = crate::formats::Cbor; 605 | 606 | assert!(impls!(T: Debug)); 607 | assert!(impls!(T: Default)); 608 | } 609 | } 610 | --------------------------------------------------------------------------------