|
198 | 198 | //! [`containers`] match the layout implied by your `serde` types. |
199 | 199 | //! - Length encodings are pluggable via [`SeqLen`](len::SeqLen). |
200 | 200 | //! |
201 | | -//! # Zero copy deserialization |
| 201 | +//! # Zero-copy deserialization |
202 | 202 | //! |
203 | | -//! `wincode` supports zero copy deserialization of contiguous byte slices |
204 | | -//! (serialized with `Vec<u8>`, `Box<[u8]>`, `[u8; N]`, etc.). |
| 203 | +//! `wincode`'s zero-copy deserialization is built on the following primitives: |
| 204 | +//! - [`u8`] |
| 205 | +//! - [`i8`] |
205 | 206 | //! |
| 207 | +//! Within `wincode`, any type that is composed entirely of these primitives is |
| 208 | +//! eligible for zero-copy deserialization. |
| 209 | +//! |
| 210 | +//! Let `Z` be the set of zero-copy types. |
| 211 | +//! |
| 212 | +//! The following higher order types are eligible for zero-copy deserialization: |
| 213 | +//! - `&[Z]` |
| 214 | +//! - `&Z` |
| 215 | +//! - `&[Z; N]` |
| 216 | +//! |
| 217 | +//! Structs deriving [`SchemaWrite`] and [`SchemaRead`] are eligible for zero-copy deserialization |
| 218 | +//! as long as they are composed entirely of the above zero-copy types and are annotated with |
| 219 | +//! `#[repr(transparent)]` or `#[repr(C)]`. |
| 220 | +//! Note that this is **not** true for tuples, as Rust does not currently guarantee tuple layout. |
| 221 | +//! |
| 222 | +//! ## Examples |
| 223 | +//! |
| 224 | +//! ### `&[u8]` |
206 | 225 | //! ``` |
207 | | -//! # #[cfg(feature = "derive")] { |
| 226 | +//! # #[cfg(all(feature = "alloc", feature = "derive"))] { |
208 | 227 | //! use wincode::{SchemaWrite, SchemaRead}; |
209 | 228 | //! |
210 | 229 | //! # #[derive(Debug, PartialEq, Eq)] |
|
216 | 235 | //! let bytes: Vec<u8> = vec![1, 2, 3, 4, 5]; |
217 | 236 | //! let byte_ref = ByteRef { bytes: &bytes }; |
218 | 237 | //! let serialized = wincode::serialize(&byte_ref).unwrap(); |
219 | | -//! let deserialized = wincode::deserialize(&serialized).unwrap(); |
| 238 | +//! let deserialized: ByteRef<'_> = wincode::deserialize(&serialized).unwrap(); |
220 | 239 | //! assert_eq!(byte_ref, deserialized); |
221 | 240 | //! # } |
222 | 241 | //! ``` |
| 242 | +//! |
| 243 | +//! ### struct newtype |
| 244 | +//! ``` |
| 245 | +//! # #[cfg(all(feature = "alloc", feature = "derive"))] { |
| 246 | +//! # use rand::random; |
| 247 | +//! # use std::array; |
| 248 | +//! use wincode::{SchemaWrite, SchemaRead}; |
| 249 | +//! |
| 250 | +//! # #[derive(Debug, PartialEq, Eq)] |
| 251 | +//! #[derive(SchemaWrite, SchemaRead)] |
| 252 | +//! #[repr(transparent)] |
| 253 | +//! struct Signature([u8; 64]); |
| 254 | +//! |
| 255 | +//! # #[derive(Debug, PartialEq, Eq)] |
| 256 | +//! #[derive(SchemaWrite, SchemaRead)] |
| 257 | +//! struct Data<'a> { |
| 258 | +//! signature: &'a Signature, |
| 259 | +//! data: &'a [u8], |
| 260 | +//! } |
| 261 | +//! |
| 262 | +//! let signature = Signature(array::from_fn(|_| random())); |
| 263 | +//! let data = Data { |
| 264 | +//! signature: &signature, |
| 265 | +//! data: &[1, 2, 3, 4, 5], |
| 266 | +//! }; |
| 267 | +//! let serialized = wincode::serialize(&data).unwrap(); |
| 268 | +//! let deserialized: Data<'_> = wincode::deserialize(&serialized).unwrap(); |
| 269 | +//! assert_eq!(data, deserialized); |
| 270 | +//! # } |
| 271 | +//! ``` |
| 272 | +//! |
| 273 | +//! ### `&[u8; N]` |
| 274 | +//! ``` |
| 275 | +//! # #[cfg(all(feature = "alloc", feature = "derive"))] { |
| 276 | +//! use wincode::{SchemaWrite, SchemaRead}; |
| 277 | +//! |
| 278 | +//! # #[derive(Debug, PartialEq, Eq)] |
| 279 | +//! #[derive(SchemaWrite, SchemaRead)] |
| 280 | +//! struct HeaderRef<'a> { |
| 281 | +//! magic: &'a [u8; 7], |
| 282 | +//! } |
| 283 | +//! |
| 284 | +//! let header = HeaderRef { magic: b"W1NC0D3" }; |
| 285 | +//! let serialized = wincode::serialize(&header).unwrap(); |
| 286 | +//! let deserialized: HeaderRef<'_> = wincode::deserialize(&serialized).unwrap(); |
| 287 | +//! assert_eq!(header, deserialized); |
| 288 | +//! # } |
| 289 | +//! ``` |
| 290 | +//! |
223 | 291 | //! # Derive attributes |
224 | 292 | //! |
225 | 293 | //! ## Top level |
|
0 commit comments