1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use crate::decode::{get, Decode, DecodeBearer, DecodeError};
use crate::encode::{Encode, EncodeBearer, EncodeError};
use crate::r#impl::ArrayDecodingGuard;
use alloc::boxed::Box;
use alloc::collections::VecDeque;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::mem::{ManuallyDrop, MaybeUninit};
macro_rules! impl_heap_ptr {
($type:ident) => {
impl<T> Encode for $type<T>
where
T: Encode + ?Sized,
{
fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
T::encode(bearer, &*src)
}
fn size_of(&self) -> usize {
(**self).size_of()
}
}
const _: () = {
/// Panic-safety guard for decoding heap pointer for [`Decode`]
/// implementation of heap pointer type
///
/// # Drop safety
///
/// Owner must call `mem::forget` when underlying value is initialized to
/// avoid double dropping
struct HeapPtrDecodingGuard<T> {
ptr: *mut MaybeUninit<T>,
}
impl<T> HeapPtrDecodingGuard<T> {
/// Create a new [`HeapPtrDecodingGuard`].
///
/// # Safety
///
/// - owner must call `mem::forget` when underlying value is initialized to
/// avoid double dropping
unsafe fn new(ptr: *mut MaybeUninit<T>) -> Self {
Self { ptr }
}
}
impl<T> Drop for HeapPtrDecodingGuard<T> {
#[cold]
fn drop(&mut self) {
drop(unsafe { $type::from_raw(self.ptr) });
}
}
unsafe impl<T> Decode for $type<T>
where
T: Decode,
{
fn decode(
bearer: &mut impl DecodeBearer,
dst: &mut MaybeUninit<Self>,
) -> Result<(), DecodeError> {
let value = $type::<T>::new_uninit();
let ptr = $type::into_raw(value) as *mut _;
let guard = unsafe {
// Safety: guard forgot after `T::decode` call finished
HeapPtrDecodingGuard::new(ptr)
};
T::decode(bearer, unsafe {
// Safety: dereferenced pointer is derived from valid
// instance of $type
&mut *ptr
})?;
core::mem::drop(guard);
dst.write(unsafe {
// Safety:
// - `ptr` points to valid instance of $type constructed above.
// - `Decode::decode` guarantees to initialize the slot provided
$type::from_raw(ptr).assume_init()
});
Ok(())
}
}
};
};
}
impl_heap_ptr!(Box);
impl_heap_ptr!(Arc);
impl_heap_ptr!(Rc);
macro_rules! impl_heap_array_decode {
($type:ident) => {
const _: () = {
/// Panic-safety guard for decoding heap-allocated array for [`Decode`]
/// implementation of heap pointer type
///
/// # Drop safety
///
/// Owner must call `mem::forget` when underlying array is fully initialized to
/// avoid double dropping
struct HeapPointerDecodingGuard<T> {
array_guard: ManuallyDrop<ArrayDecodingGuard<T>>,
alloc_ptr: *mut [MaybeUninit<T>],
}
impl<T> HeapPointerDecodingGuard<T> {
/// Create a new [`HeapPointerDecodingGuard`].
///
/// # Safety
///
/// - `ptr` must point to valid `$type` holding `[MaybeUninit<T>]`
///
/// - owner must call `mem::forget` when underlying array is fully initialized to
/// avoid double dropping
unsafe fn new(ptr: *mut [MaybeUninit<T>]) -> Self {
Self {
array_guard: ManuallyDrop::new(unsafe {
// Safety: caller guarantees to call `mem::forget` when array is
// fully initialized
ArrayDecodingGuard::new(
// Safety: constructor requires assigned `ptr` to point
// to valid `$type`
(*ptr).as_mut_ptr(),
)
}),
alloc_ptr: ptr,
}
}
/// Get a mutable reference to last slot of inner array of this pointer.
fn slot(&mut self) -> &mut MaybeUninit<T> {
self.array_guard.slot()
}
/// Increment inner length by one.
///
/// # Safety
///
/// - incrementing the length must not overflow underlying array or isize::MAX
unsafe fn add(&mut self) {
unsafe {
// Safety: caller guarantees to comply with `ArrayDecodingGuard::add()`
// requirements
self.array_guard.add()
}
}
}
impl<T> Drop for HeapPointerDecodingGuard<T> {
#[cold]
fn drop(&mut self) {
unsafe {
ManuallyDrop::drop(&mut self.array_guard);
drop($type::from_raw(self.alloc_ptr))
}
}
}
unsafe impl<T> Decode for $type<[T]>
where
T: Decode,
{
fn decode(
bearer: &mut impl DecodeBearer,
dst: &mut MaybeUninit<Self>,
) -> Result<(), DecodeError> {
let len = get::<u32>(bearer)? as usize;
// This check is necessary since `write` method of `guard` requires
// length cursor to not exceed `isize::MAX` for pointer addition
if len > isize::MAX as usize {
return Err(DecodeError::Other("Length exceeds isize::MAX"));
}
let slice = $type::<[T]>::new_uninit_slice(len);
let slice_ptr = $type::into_raw(slice) as *mut [MaybeUninit<T>];
let mut guard = unsafe {
// Safety: `buf_ptr` is valid for `$type::from_raw`
HeapPointerDecodingGuard::new(slice_ptr)
};
for _ in 0..len {
let slot = guard.slot();
T::decode(bearer, slot)?;
unsafe {
// Safety: we're iterating up to `len`, which is length
// of the underlying `$type` of `guard`
guard.add();
}
}
core::mem::forget(guard);
unsafe {
// Safety: `Decode::decode` guarantees to initialize the slot provided
dst.write($type::from_raw(slice_ptr).assume_init());
}
Ok(())
}
}
};
};
}
impl_heap_array_decode!(Box);
impl_heap_array_decode!(Arc);
impl_heap_array_decode!(Rc);
impl<T> Encode for Vec<T>
where
T: Encode,
{
fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
<[T]>::encode(bearer, src)
}
fn size_of(&self) -> usize {
self.len() * size_of::<T>()
}
}
unsafe impl<T> Decode for Vec<T>
where
T: Decode,
{
fn decode(
bearer: &mut impl DecodeBearer,
dst: &mut MaybeUninit<Self>,
) -> Result<(), DecodeError> {
let len = get::<u32>(bearer)? as usize;
// `Vec` constructor will panic if `len` exceeds isize::MAX
if len > isize::MAX as usize {
return Err(DecodeError::Other("Length exceeds isize::MAX"));
}
let mut vec = Vec::<T>::with_capacity(len);
let mut ptr = vec.as_mut_ptr().cast();
for slot in 0..len {
T::decode(bearer, unsafe {
// Safety: `ptr` points to valid span of memory within
// the bounds of `vec` capacity
&mut *ptr
})?;
unsafe {
// Safety: `ptr` incremented strictly up to `len`, which
// equal to capacity of `vec`.
// Note: unlike heap pointers and arrays, Vec already has a
// drop-guard behavior we achieve using `set_len`
ptr = ptr.add(1);
vec.set_len(slot + 1);
}
}
dst.write(vec);
Ok(())
}
}
impl<T> Encode for VecDeque<T>
where
T: Encode,
{
fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
u32::encode(bearer, &(src.len() as u32))?;
let mut write_slice_plain = |slice: &[T]| -> Result<(), EncodeError> {
for elem in slice {
T::encode(bearer, elem)?;
}
Ok(())
};
let (front, back) = src.as_slices();
write_slice_plain(front)?;
write_slice_plain(back)?;
Ok(())
}
fn size_of(&self) -> usize {
self.len() * size_of::<T>()
}
}
unsafe impl<T> Decode for VecDeque<T>
where
T: Decode,
{
fn decode(
bearer: &mut impl DecodeBearer,
dst: &mut MaybeUninit<Self>,
) -> Result<(), DecodeError> {
let vec = get::<Vec<T>>(bearer)?;
dst.write(vec.into());
Ok(())
}
}
impl Encode for String {
fn encode(bearer: &mut impl EncodeBearer, src: &Self) -> Result<(), EncodeError> {
str::encode(bearer, src.as_str())
}
fn size_of(&self) -> usize {
self.len()
}
}
unsafe impl Decode for String {
fn decode(
bearer: &mut impl DecodeBearer,
dst: &mut MaybeUninit<Self>,
) -> Result<(), DecodeError> {
let bytes = get(bearer)?;
dst.write(String::from_utf8(bytes).map_err(|_| DecodeError::InvalidPattern)?);
Ok(())
}
}