-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathwincode.rs
More file actions
169 lines (154 loc) · 5.13 KB
/
wincode.rs
File metadata and controls
169 lines (154 loc) · 5.13 KB
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
//! Support for serializing and deserializing `Array` using wincode.
use crate::{Array, ArraySize};
use core::mem;
use core::mem::MaybeUninit;
use core::mem::transmute;
use wincode::ReadResult;
use wincode::SchemaRead;
use wincode::SchemaWrite;
use wincode::TypeMeta;
use wincode::WriteResult;
use wincode::ZeroCopy;
use wincode::io::Reader;
use wincode::io::Writer;
pub(crate) struct SliceDropGuard<T> {
ptr: *mut MaybeUninit<T>,
initialized_len: usize,
}
impl<T> SliceDropGuard<T> {
pub(crate) fn new(ptr: *mut MaybeUninit<T>) -> Self {
Self {
ptr,
initialized_len: 0,
}
}
#[inline(always)]
#[allow(clippy::arithmetic_side_effects)]
pub(crate) fn inc_len(&mut self) {
self.initialized_len += 1;
}
}
impl<T> Drop for SliceDropGuard<T> {
#[inline(always)]
fn drop(&mut self) {
unsafe {
core::ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut(
self.ptr.cast::<T>(),
self.initialized_len,
));
}
}
}
// SAFETY:
// - Array<T, U> where T: ZeroCopy is trivially zero-copy. The length is constant,
// so there is no length encoding.
unsafe impl<T, U: ArraySize> ZeroCopy for Array<T, U> where T: ZeroCopy {}
impl<'de, T, U> SchemaRead<'de> for Array<T, U>
where
T: SchemaRead<'de>,
U: ArraySize,
{
type Dst = Array<T::Dst, U>;
const TYPE_META: TypeMeta = const {
match T::TYPE_META {
TypeMeta::Static { size, zero_copy } => TypeMeta::Static {
size: U::USIZE * size,
zero_copy,
},
TypeMeta::Dynamic => TypeMeta::Dynamic,
}
};
#[inline]
fn read(reader: &mut impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>) -> ReadResult<()> {
if let TypeMeta::Static {
zero_copy: true, ..
} = T::TYPE_META
{
// SAFETY: `T::Dst` is zero-copy eligible (no invalid bit patterns, no layout requirements, no endianness checks, etc.).
unsafe { reader.copy_into_t(dst)? };
return Ok(());
}
// SAFETY: MaybeUninit<Array<T::Dst, U>> trivially converts to Array<MaybeUninit<T::Dst>, U>.
let dst = unsafe {
transmute::<&mut MaybeUninit<Self::Dst>, &mut Array<MaybeUninit<T::Dst>, U>>(dst)
};
let base = dst.as_mut_ptr();
let mut guard = SliceDropGuard::<T::Dst>::new(base);
if let TypeMeta::Static { size, .. } = Self::TYPE_META {
// SAFETY: `Self::TYPE_META` specifies a static size, which is `U::USIZE * static_size_of(T)`.
// `U::USIZE` reads of `T` will consume `size` bytes, fully consuming the trusted window.
let reader = &mut unsafe { reader.as_trusted_for(size) }?;
for i in 0..U::USIZE {
let slot = unsafe { &mut *base.add(i) };
T::read(reader, slot)?;
guard.inc_len();
}
} else {
for i in 0..U::USIZE {
let slot = unsafe { &mut *base.add(i) };
T::read(reader, slot)?;
guard.inc_len();
}
}
mem::forget(guard);
Ok(())
}
}
impl<T, U> SchemaWrite for Array<T, U>
where
T: SchemaWrite,
T::Src: Sized,
U: ArraySize,
{
type Src = Array<T::Src, U>;
const TYPE_META: TypeMeta = const {
match T::TYPE_META {
TypeMeta::Static { size, zero_copy } => TypeMeta::Static {
size: U::USIZE * size,
zero_copy,
},
TypeMeta::Dynamic => TypeMeta::Dynamic,
}
};
#[inline]
#[allow(clippy::arithmetic_side_effects)]
fn size_of(value: &Self::Src) -> WriteResult<usize> {
if let TypeMeta::Static { size, .. } = Self::TYPE_META {
return Ok(size);
}
// Extremely unlikely a type-in-memory's size will overflow usize::MAX.
value
.iter()
.map(T::size_of)
.try_fold(0usize, |acc, x| x.map(|x| acc + x))
}
#[inline]
fn write(writer: &mut impl Writer, value: &Self::Src) -> WriteResult<()> {
match Self::TYPE_META {
TypeMeta::Static {
zero_copy: true, ..
} => {
// SAFETY: `T::Src` is zero-copy eligible (no invalid bit patterns, no layout requirements, no endianness checks, etc.).
unsafe { writer.write_slice_t(value)? };
}
TypeMeta::Static {
size,
zero_copy: false,
} => {
// SAFETY: `Self::TYPE_META` specifies a static size, which is `U::USIZE * static_size_of(T)`.
// `U::USIZE` writes of `T` will write `size` bytes, fully initializing the trusted window.
let writer = &mut unsafe { writer.as_trusted_for(size) }?;
for item in value {
T::write(writer, item)?;
}
writer.finish()?;
}
TypeMeta::Dynamic => {
for item in value {
T::write(writer, item)?;
}
}
}
Ok(())
}
}