| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
//! Documentation on implementing custom VarULE types. |
| 6 |
//! |
| 7 |
//! This module contains documentation for defining custom VarULE types, |
| 8 |
//! especially those using complex custom dynamically sized types. |
| 9 |
//! |
| 10 |
//! In *most cases* you should be able to create custom VarULE types using |
| 11 |
//! [`#[make_varule]`](crate::make_ule). |
| 12 |
//! |
| 13 |
//! # Example |
| 14 |
//! |
| 15 |
//! For example, if your regular stack type is: |
| 16 |
//! |
| 17 |
//! ```rust |
| 18 |
//! use zerofrom::ZeroFrom; |
| 19 |
//! use zerovec::ule::*; |
| 20 |
//! use zerovec::ZeroVec; |
| 21 |
//! |
| 22 |
//! #[derive(serde::Serialize, serde::Deserialize)] |
| 23 |
//! struct Foo<'a> { |
| 24 |
//! field1: char, |
| 25 |
//! field2: u32, |
| 26 |
//! #[serde(borrow)] |
| 27 |
//! field3: ZeroVec<'a, u32>, |
| 28 |
//! } |
| 29 |
//! ``` |
| 30 |
//! |
| 31 |
//! then the ULE type will be implemented as follows. Ideally, you should have |
| 32 |
//! `EncodeAsVarULE` and `ZeroFrom` implementations on `Foo` pertaining to `FooULE`, |
| 33 |
//! as well as a `Serialize` impl on `FooULE` and a `Deserialize` impl on `Box<FooULE>` |
| 34 |
//! to enable human-readable serialization and deserialization. |
| 35 |
//! |
| 36 |
//! ```rust |
| 37 |
//! use zerovec::{ZeroVec, VarZeroVec, ZeroSlice}; |
| 38 |
//! use zerovec::ule::*; |
| 39 |
//! use zerofrom::ZeroFrom; |
| 40 |
//! use core::mem; |
| 41 |
//! |
| 42 |
//! # #[derive(serde::Serialize, serde::Deserialize)] |
| 43 |
//! # struct Foo<'a> { |
| 44 |
//! # field1: char, |
| 45 |
//! # field2: u32, |
| 46 |
//! # #[serde(borrow)] |
| 47 |
//! # field3: ZeroVec<'a, u32> |
| 48 |
//! # } |
| 49 |
//! |
| 50 |
//! // Must be repr(C, packed) for safety of VarULE! |
| 51 |
//! // Must also only contain ULE types |
| 52 |
//! #[repr(C, packed)] |
| 53 |
//! struct FooULE { |
| 54 |
//! field1: <char as AsULE>::ULE, |
| 55 |
//! field2: <u32 as AsULE>::ULE, |
| 56 |
//! field3: ZeroSlice<u32>, |
| 57 |
//! } |
| 58 |
//! |
| 59 |
//! // Safety (based on the safety checklist on the VarULE trait): |
| 60 |
//! // 1. FooULE does not include any uninitialized or padding bytes. (achieved by `#[repr(C, packed)]` on |
| 61 |
//! // a struct with only ULE fields) |
| 62 |
//! // 2. FooULE is aligned to 1 byte. (achieved by `#[repr(C, packed)]` on |
| 63 |
//! // a struct with only ULE fields) |
| 64 |
//! // 3. The impl of `validate_bytes()` returns an error if any byte is not valid. |
| 65 |
//! // 4. The impl of `validate_bytes()` returns an error if the slice cannot be used in its entirety |
| 66 |
//! // 5. The impl of `from_bytes_unchecked()` returns a reference to the same data. |
| 67 |
//! // 6. The other VarULE methods use the default impl. |
| 68 |
//! // 7. FooULE byte equality is semantic equality |
| 69 |
//! unsafe impl VarULE for FooULE { |
| 70 |
//! fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> { |
| 71 |
//! // validate each field |
| 72 |
//! <char as AsULE>::ULE::validate_bytes(&bytes[0..3]).map_err(|_| UleError::parse::<Self>())?; |
| 73 |
//! <u32 as AsULE>::ULE::validate_bytes(&bytes[3..7]).map_err(|_| UleError::parse::<Self>())?; |
| 74 |
//! let _ = ZeroVec::<u32>::parse_bytes(&bytes[7..]).map_err(|_| UleError::parse::<Self>())?; |
| 75 |
//! Ok(()) |
| 76 |
//! } |
| 77 |
//! unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self { |
| 78 |
//! let ptr = bytes.as_ptr(); |
| 79 |
//! let len = bytes.len(); |
| 80 |
//! // subtract the length of the char and u32 to get the length of the array |
| 81 |
//! let len_new = (len - 7) / 4; |
| 82 |
//! // it's hard constructing custom DSTs, we fake a pointer/length construction |
| 83 |
//! // eventually we can use the Pointer::Metadata APIs when they stabilize |
| 84 |
//! let fake_slice = core::ptr::slice_from_raw_parts(ptr as *const <u32 as AsULE>::ULE, len_new); |
| 85 |
//! &*(fake_slice as *const Self) |
| 86 |
//! } |
| 87 |
//! } |
| 88 |
//! |
| 89 |
//! unsafe impl EncodeAsVarULE<FooULE> for Foo<'_> { |
| 90 |
//! fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R { |
| 91 |
//! // take each field, convert to ULE byte slices, and pass them through |
| 92 |
//! cb(&[<char as AsULE>::ULE::slice_as_bytes(&[self.field1.to_unaligned()]), |
| 93 |
//! <u32 as AsULE>::ULE::slice_as_bytes(&[self.field2.to_unaligned()]), |
| 94 |
//! // the ZeroVec is already in the correct slice format |
| 95 |
//! self.field3.as_bytes()]) |
| 96 |
//! } |
| 97 |
//! } |
| 98 |
//! |
| 99 |
//! impl<'a> ZeroFrom<'a, FooULE> for Foo<'a> { |
| 100 |
//! fn zero_from(other: &'a FooULE) -> Self { |
| 101 |
//! Self { |
| 102 |
//! field1: AsULE::from_unaligned(other.field1), |
| 103 |
//! field2: AsULE::from_unaligned(other.field2), |
| 104 |
//! field3: ZeroFrom::zero_from(&other.field3), |
| 105 |
//! } |
| 106 |
//! } |
| 107 |
//! } |
| 108 |
//! |
| 109 |
//! |
| 110 |
//! impl serde::Serialize for FooULE |
| 111 |
//! { |
| 112 |
//! fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 113 |
//! where |
| 114 |
//! S: serde::Serializer, |
| 115 |
//! { |
| 116 |
//! Foo::zero_from(self).serialize(serializer) |
| 117 |
//! } |
| 118 |
//! } |
| 119 |
//! |
| 120 |
//! impl<'de> serde::Deserialize<'de> for Box<FooULE> |
| 121 |
//! { |
| 122 |
//! fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 123 |
//! where |
| 124 |
//! D: serde::Deserializer<'de>, |
| 125 |
//! { |
| 126 |
//! let mut foo = Foo::deserialize(deserializer)?; |
| 127 |
//! Ok(encode_varule_to_box(&foo)) |
| 128 |
//! } |
| 129 |
//! } |
| 130 |
//! |
| 131 |
//! fn main() { |
| 132 |
//! let mut foos = [Foo {field1: 'u', field2: 983, field3: ZeroVec::alloc_from_slice(&[1212,2309,500,7000])}, |
| 133 |
//! Foo {field1: 'l', field2: 1010, field3: ZeroVec::alloc_from_slice(&[1932, 0, 8888, 91237])}]; |
| 134 |
//! |
| 135 |
//! let vzv = VarZeroVec::<_>::from(&foos); |
| 136 |
//! |
| 137 |
//! assert_eq!(char::from_unaligned(vzv.get(0).unwrap().field1), 'u'); |
| 138 |
//! assert_eq!(u32::from_unaligned(vzv.get(0).unwrap().field2), 983); |
| 139 |
//! assert_eq!(&vzv.get(0).unwrap().field3, &[1212,2309,500,7000][..]); |
| 140 |
//! |
| 141 |
//! assert_eq!(char::from_unaligned(vzv.get(1).unwrap().field1), 'l'); |
| 142 |
//! assert_eq!(u32::from_unaligned(vzv.get(1).unwrap().field2), 1010); |
| 143 |
//! assert_eq!(&vzv.get(1).unwrap().field3, &[1932, 0, 8888, 91237][..]); |
| 144 |
//! } |
| 145 |
//! ``` |