-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtraits.rs
282 lines (233 loc) · 10 KB
/
traits.rs
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
//! The various storages available.
use core::{alloc::AllocError, convert::TryInto, marker::Unsize, mem::MaybeUninit, ptr::{self, NonNull, Pointee}};
//
// Element Storage
//
/// A storage for storing elements one at a time.
///
/// This trait is further refined into:
///
/// - `SingleElementStorage`, which stores up to a single element at any one time.
/// - `MultiElementStorage`, which may store multiple elements at any one time.
pub trait ElementStorage {
/// The Handle used to obtain the elements.
type Handle<T: ?Sized + Pointee> : Clone + Copy;
/// Destroys the value stored within the storage.
///
/// # Safety
///
/// - Assumes `handle` is valid, and the meta-data of the value it represents is valid.
/// - This invalidates the value behind the `handle`, hence `resolve` or `coerce` are no longer safe to be called
/// on either it or any of its copies.
unsafe fn destroy<T: ?Sized + Pointee>(&mut self, handle: Self::Handle<T>) {
// Safety:
// - `handle` is assumed to be valid.
let element = self.resolve_mut(handle);
// Safety:
// - `element` is valid.
ptr::drop_in_place(element.as_ptr());
self.deallocate(handle);
}
/// Deallocate the memory without destroying the value within the storage.
///
/// # Safety
///
/// - Assumes `handle` is valid, and the meta-data of the value it represents is valid.
/// - This invalidates the `handle`, and all of its copies.
unsafe fn deallocate<T: ?Sized + Pointee>(&mut self, handle: Self::Handle<T>);
/// Gets a pointer to the storage to the element.
///
/// # Safety
///
/// - Assumes that `handle` is valid.
/// - The pointer is only valid as long as the storage is not moved and the `handle` remains valid.
/// - The pointer is only usable to create non-mutable references.
unsafe fn resolve<T: ?Sized + Pointee>(&self, handle: Self::Handle<T>) -> NonNull<T>;
/// Gets a pointer to the storage to the element.
///
/// # Safety
///
/// - Assumes that `handle` is valid.
/// - The pointer is only valid as long as the storage is not moved and the `handle` remains valid.
unsafe fn resolve_mut<T: ?Sized + Pointee>(&mut self, handle: Self::Handle<T>) -> NonNull<T>;
/// Coerces the type of the handle.
///
/// # Safety
///
/// - Assumes that `handle` is valid, and was issued by this instance.
unsafe fn coerce<U: ?Sized + Pointee, T: ?Sized + Pointee + Unsize<U>>(&self, handle: Self::Handle<T>) -> Self::Handle<U>;
}
/// A single element storage.
///
/// Examples of use include: Box.
pub trait SingleElementStorage : ElementStorage {
/// Stores a `value` within the storage.
///
/// If a value is already stored, it is overwritten and `drop` is not executed.
fn create<T: Pointee>(&mut self, value: T) -> Result<Self::Handle<T>, T> {
let meta = (&value as *const T).to_raw_parts().1;
if let Ok(handle) = self.allocate(meta) {
// Safety:
// - `handle` is valid.
let pointer = unsafe { self.resolve_mut(handle) };
// Safety:
// - `pointer` points to a suitable memory area for `T`.
unsafe { ptr::write(pointer.as_ptr(), value) };
Ok(handle)
} else {
Err(value)
}
}
/// Attempts to allocate memory, and returns a handle to it.
///
/// This may fail if memory cannot be allocated for it.
///
/// If a value is already stored, the memory area may overlap.
fn allocate<T: ?Sized + Pointee>(&mut self, meta: T::Metadata) -> Result<Self::Handle<T>, AllocError>;
}
/// A multi elements storage.
///
/// Examples of use include: BTreeMap, LinkedList, SkipList.
pub trait MultiElementStorage : ElementStorage{
/// Attempts to store `value` in a newly allocated memory slot.
///
/// This may fail if memory cannot be allocated for it.
///
/// # Safety
///
/// - The Handle obtained is only valid until `self.destroy` or `self.deallocate` is invoked on it, or one of its
/// copies.
/// - This may relocate all existing elements, pointers should be re-acquired through their handles.
fn create<T: Pointee>(&mut self, value: T) -> Result<Self::Handle<T>, T> {
let meta = (&value as *const T).to_raw_parts().1;
if let Ok(handle) = self.allocate(meta) {
// Safety:
// - `handle` is valid.
let pointer = unsafe { self.resolve_mut(handle) };
// Safety:
// - `pointer` points to a suitable memory area for `T`.
unsafe { ptr::write(pointer.as_ptr(), value) };
Ok(handle)
} else {
Err(value)
}
}
/// Allocates memory, and returns a handle to it.
///
/// This may fail if memory cannot be allocated for it.
fn allocate<T: ?Sized + Pointee>(&mut self, meta: T::Metadata) -> Result<Self::Handle<T>, AllocError>;
}
//
// Range Storage
//
/// Capacity type for range storage.
pub trait Capacity : Sized + Clone + Copy {
/// The maximum possible value of this type.
fn max() -> Self;
/// Create from usize.
fn from_usize(capacity: usize) -> Option<Self>;
/// Convert back to usize.
fn into_usize(self) -> usize;
}
/// A storage for (contigous) ranges of elements.
///
/// This trait is further refined into:
///
/// - `SingleRangeStorage`, which stores up to one single range at any one time.
/// - `MultiRangeStorage`, which may store multiple ranges at any one time.
pub trait RangeStorage {
/// The Handle used to obtain the range.
type Handle<T> : Clone + Copy;
/// The Capacity type used by the storage.
///
/// The collection may which to use it for related values to keep them as compact as possible.
type Capacity : Capacity;
/// Indicates the maximum capacity of a single range possibly available for an element of type `T`.
fn maximum_capacity<T>(&self) -> Self::Capacity;
/// Deallocates the memory of the range associated to `handle`, without invoking any destructor.
///
/// # Safety
///
/// - Assumes `handles` points to an allocated memory slot, makes no assumption about whether its value is valid.
/// - This invalidates `handle`, and all of its copies.
unsafe fn deallocate<T>(&mut self, handle: Self::Handle<T>);
/// Gets a pointer to the storage to the range of elements.
///
/// The pointer is only valid as long as the storage is not moved, or the range is not resized.
///
/// # Safety
///
/// - Assumes that `handle` is valid, and was issued by this instance.
/// - The pointer is only valid as long as the storage is not moved and the `handle` remains valid.
/// - The pointer is only usable to create non-mutable references.
unsafe fn resolve<T>(&self, handle: Self::Handle<T>) -> NonNull<[MaybeUninit<T>]>;
/// Gets a pointer to the storage to the range of elements.
///
/// The pointer is only valid as long as the storage is not moved, or the range is not resized.
///
/// # Safety
///
/// - Assumes that `handle` is valid, and was issued by this instance.
/// - The pointer is only valid as long as the storage is not moved and the `handle` remains valid.
unsafe fn resolve_mut<T>(&mut self, handle: Self::Handle<T>) -> NonNull<[MaybeUninit<T>]>;
/// Attempts to grow the internal storage to accomodate at least `new_capacity` elements in total.
///
/// If the attempt succeeds, a new handle is returned and `handle` is invalidated.
unsafe fn try_grow<T>(&mut self, _handle: Self::Handle<T>, _new_capacity: Self::Capacity) -> Result<Self::Handle<T>, AllocError> {
Err(AllocError)
}
/// Attempts to shrink the internal storage to accomodate at least `new_capacity` elements in total.
///
/// If the attempt succeeds, a new handle is returned and `handle` is invalidated.
unsafe fn try_shrink<T>(&mut self, _handle: Self::Handle<T>, _new_capacity: Self::Capacity) -> Result<Self::Handle<T>, AllocError> {
Err(AllocError)
}
}
/// A single range storage.
///
/// Examples of use include: Vec, VecDeque.
pub trait SingleRangeStorage : RangeStorage {
/// Allocates memory for a new `Handle`, large enough to at least accomodate the required `capacity`.
///
/// Does not `deallocate` the current handles, nor drop their content. It merely invalidates them.
fn allocate<T>(&mut self, capacity: Self::Capacity) -> Result<Self::Handle<T>, AllocError>;
}
/// A multi elements storage.
///
/// Examples of use include: CompactHashMap.
pub trait MultiRangeStorage : RangeStorage{
/// Allocates memory for a new `Handle`, large enough to at least accomodate the required `capacity`.
///
/// This may fail if memory cannot be allocated for it.
///
/// # Safety
///
/// - The Handle obtained is only valid until `self.destroy` or `self.deallocate` is invoked on it, or one of its
/// copies.
/// - This may relocate all existing ranges, which should be re-acquired through their handles.
fn allocate<T>(&mut self, capacity: Self::Capacity) -> Result<Self::Handle<T>, AllocError>;
}
//
// Implementations of Capacity.
//
impl Capacity for usize {
fn max() -> usize { usize::MAX }
fn from_usize(capacity: usize) -> Option<Self> { Some(capacity) }
fn into_usize(self) -> usize { self }
}
impl Capacity for u8 {
fn max() -> Self { u8::MAX }
fn from_usize(capacity: usize) -> Option<Self> { capacity.try_into().ok() }
fn into_usize(self) -> usize { self as usize }
}
impl Capacity for u16 {
fn max() -> Self { u16::MAX }
fn from_usize(capacity: usize) -> Option<Self> { capacity.try_into().ok() }
fn into_usize(self) -> usize { self as usize }
}
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl Capacity for u32 {
fn max() -> Self { u32::MAX }
fn from_usize(capacity: usize) -> Option<Self> { capacity.try_into().ok() }
fn into_usize(self)-> usize { self as usize }
}