block-array-cow 0.1.4

In memory array de-duplication, useful for efficient storing of a history of data versions.
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Licensed: Apache 2.0

///
/// Wrap raw pointers
/// This is effectively syntax sugar to avoid;
/// `(*var).attr`, allowing `var.attr` on raw pointers.
///
/// Other utility functions may be exposed,
/// however the intent is to keep this a thin wrapper on raw pointers,
/// not to invent a new way of managing pointers.
///
/// Notes:
///
/// * Guaranteed to have *zero* memory and run-time overhead for release builds.
/// * Supports comparison with regular pointers,
///   convenient for comparing with `std::ptr::null()`.
/// * Supports converting `PtrMut` 'into' `PtrConst`,
///   so functions can be declared which take either.
/// * Can be used as much or as little as your like,
///   To get the value from a pointer: `plain_ptr = PtrMut(ptr)`
///   To get the original pointer: `plain_ptr.as_ptr()`
///

use std::ops::{
    Deref,
    DerefMut,
};


// ---------------------------------------------------------------------------
// Generics (PtrAny)

pub trait PtrAnyImpl<T> {
    /// Either `*mut T` or `*const T`
    type BasePtr;

    fn new(ptr: Self::BasePtr) -> Self;
    /// Gives a native type, from a `mut`.
    /// Beware: this is a workaround for not being able to easily coerce types
    /// when using `PtrAny`.
    fn new_from_mut(ptr: PtrMut<T>) -> Self;
    fn null() -> Self;
    fn is_null(&self) -> bool;
    fn as_ptr(&self) -> Self::BasePtr;
    fn as_const(&self) -> PtrConst<T>;
    /// Utility function to support easy null pointer assignments:
    /// `if let Some(var) = func_returns_pointer() { ... }`
    fn as_option(&self) -> Option<Self> where Self: Sized;
}

pub trait PtrAny<T>:
    Deref<Target=T> +
    Copy +
    Clone +
    PartialEq +
    PtrAnyImpl<T> +
    {}

impl<TPtr, T> PtrAny<T> for TPtr where TPtr:
    Deref<Target=T> +
    Copy +
    Clone +
    PartialEq +
    PtrAnyImpl<T> +
    {}


// ---------------------------------------------------------------------------
// PtrMut

#[repr(C)]
#[derive(Debug, Hash)]
pub struct PtrMut<T> {
    ptr: *mut T,
}

// only for easy access
#[allow(non_snake_case)]
pub fn PtrMut<T>(ptr: *mut T) -> PtrMut<T> {
    PtrMut::new(ptr)
}
pub fn null_mut<T>() -> PtrMut<T> {
    PtrMut::null()
}

impl<T> PtrAnyImpl<T> for PtrMut<T> {
    type BasePtr = *mut T;

    // classmethods
    #[inline(always)]
    fn new(ptr: Self::BasePtr) -> PtrMut<T> {
        PtrMut::new(ptr)
    }
    #[inline(always)]
    fn new_from_mut(ptr: PtrMut<T>) -> PtrMut<T> {
        PtrMut::new_from_mut(ptr)
    }
    #[inline(always)]
    fn null() -> PtrMut<T> {
        PtrMut { ptr: ::std::ptr::null_mut() }
    }

    // methods
    #[inline(always)]
    fn is_null(&self) -> bool {
        self.is_null()
    }
    #[inline(always)]
    fn as_ptr(&self) -> Self::BasePtr {
        self.as_ptr()
    }
    #[inline(always)]
    fn as_option(&self) -> Option<PtrMut<T>> {
        self.as_option()
    }
    #[inline(always)]
    fn as_const(&self) -> PtrConst<T> {
        self.as_const()
    }
}

// PtrAnyImpl
impl<T> PtrMut<T> {
    // classmethods
    #[inline(always)]
    pub fn new(ptr: *mut T) -> PtrMut<T> {
        PtrMut { ptr: ptr as *mut T }
    }
    #[inline(always)]
    fn new_from_mut(ptr: PtrMut<T>) -> PtrMut<T> {
        ptr
    }
    #[inline(always)]
    fn null() -> PtrMut<T> {
        PtrMut { ptr: ::std::ptr::null_mut() }
    }

    // methods
    #[inline(always)]
    pub fn is_null(&self) -> bool {
        self.ptr == ::std::ptr::null_mut()
    }
    #[inline(always)]
    pub fn as_ptr(&self) -> *mut T {
        self.ptr
    }
    #[inline(always)]
    pub fn as_option(&self) -> Option<PtrMut<T>> {
        return if self.ptr.is_null() == false { Some(*self) } else { None };
    }
}

impl<T> PtrMut<T> {

    /// Utility function to support easy null pointer assignments:
    /// `if let Some(var) = func_returns_pointer() { ... }`

    // only for 'PtrMut'
    #[inline(always)]
    pub fn as_const(&self) -> PtrConst<T> {
        PtrConst::new(self.ptr as *const T)
    }
}

impl<T> Copy for PtrMut<T> { }
impl<T> Clone for PtrMut<T> {
    #[inline(always)]
    fn clone(&self) -> PtrMut<T> { *self }
}

impl<T> Deref for PtrMut<T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &T {
        unsafe { &*self.ptr }
    }
}

impl<T> DerefMut for PtrMut<T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut *self.ptr }
    }
}

// Expose other helpers

impl<T> PartialEq for PtrMut<T> {
    fn eq(&self, other: &PtrMut<T>) -> bool {
        self.ptr == other.ptr
    }
}

impl<T> PartialEq<PtrConst<T>> for PtrMut<T> {
    fn eq(&self, other: &PtrConst<T>) -> bool {
        self.ptr as *const T == other.ptr
    }
}

// PtrMut == *mut
impl<T> PartialEq<*mut T> for PtrMut<T> {
    fn eq(&self, other: &*mut T) -> bool {
        self.ptr == *other
    }
}
// PtrMut == *const
impl<T> PartialEq<*const T> for PtrMut<T> {
    fn eq(&self, other: &*const T) -> bool {
        self.ptr as *const T == *other
    }
}

// PtrMut order
impl<T> PartialOrd<PtrMut<T>> for PtrMut<T> {
    fn partial_cmp(&self, other: &PtrMut<T>) -> Option<::std::cmp::Ordering> {
        (self.ptr as usize).partial_cmp(&((other.ptr) as usize))
    }
}
impl<T> Ord for PtrMut<T> {
    fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
        (self.ptr as usize).cmp(&((other.ptr) as usize))
    }
}
impl<T> Eq for PtrMut<T> {}


// ---------------------------------------------------------------------------
// PtrConst

#[repr(C)]
#[derive(Debug, Hash)]
pub struct PtrConst<T> {
    ptr: *const T,
}

// only for easy access
#[allow(non_snake_case)]
pub fn PtrConst<T>(ptr: *const T) -> PtrConst<T> {
    PtrConst::new(ptr)
}
pub fn null_const<T>() -> PtrConst<T> {
    PtrConst::null()
}

impl<T> PtrAnyImpl<T> for PtrConst<T> {
    type BasePtr = *const T;

    #[inline(always)]
    fn new(ptr: Self::BasePtr) -> PtrConst<T> {
        PtrConst::new(ptr)
    }
    #[inline(always)]
    fn new_from_mut(ptr: PtrMut<T>) -> PtrConst<T> {
        PtrConst::new_from_mut(ptr)
    }
    #[inline(always)]
    fn null() -> PtrConst<T> {
        PtrConst::null()
    }

    #[inline(always)]
    fn is_null(&self) -> bool {
        self.is_null()
    }
    #[inline(always)]
    fn as_ptr(&self) -> Self::BasePtr {
        self.as_ptr()
    }
    #[inline(always)]
    fn as_option(&self) -> Option<PtrConst<T>> {
        self.as_option()
    }
    #[inline(always)]
    fn as_const(&self) -> PtrConst<T> {
        self.as_const()
    }

}

// PtrAnyImpl
impl<T> PtrConst<T> {
    // classmethods
    #[inline(always)]
    fn new(ptr: *const T) -> PtrConst<T> {
        PtrConst { ptr: ptr as *const T }
    }
    #[inline(always)]
    fn new_from_mut(ptr: PtrMut<T>) -> PtrConst<T> {
        ptr.as_const()
    }
    #[inline(always)]
    fn null() -> PtrConst<T> {
        PtrConst { ptr: ::std::ptr::null() }
    }

    // methods
    #[inline(always)]
    fn is_null(&self) -> bool {
        self.ptr == ::std::ptr::null()
    }
    #[inline(always)]
    pub fn as_ptr(&self) -> *const T {
        self.ptr
    }
    #[inline(always)]
    pub fn as_option(&self) -> Option<PtrConst<T>> {
        return if self.ptr.is_null() == false { Some(*self) } else { None };
    }

    #[inline(always)]
    fn as_const(&self) -> PtrConst<T> { PtrConst::new(self.ptr) }
}

impl<T> PtrConst<T> {

    /// Only for 'PtrConst'
    ///
    /// Unlike other functions in this module that are _not_ marked unsafe,
    /// this is something that should really be avoided,
    /// since const-correctness should be maintained strictly
    /// (that's why we have `PtrConst` and `PtrMut`).
    ///
    /// This is needed the case when we have a function which
    /// returns a values who's mutable state is based on the input.
    ///
    /// This way we can avoid writing it twice,
    /// by writing the immutable version once (which will be assured not to modify the input)
    /// then write a mutable wrapper that gets the output
    /// and performs the unsafe case on the output.
    ///
    /// Later it may be worth trying to use generic functions here,
    /// but for now allow unsafe casting.
    ///
    #[inline(always)]
    #[allow(dead_code)]
    pub unsafe fn as_mut(&self) -> PtrMut<T> {
        PtrMut::new(self.ptr as *mut T)
    }
}

impl<T> Copy for PtrConst<T> { }
impl<T> Clone for PtrConst<T> {
    #[inline(always)]
    fn clone(&self) -> PtrConst<T> { *self }
}

impl<T> Deref for PtrConst<T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &T {
        unsafe { &*self.ptr }
    }
}

// no DerefMut for PtrConst, only PtrMut
/*
impl<T> DerefMut for PtrConst<T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut *self.ptr }
    }
}
*/

// Expose other helpers

impl<T> PartialEq for PtrConst<T> {
    fn eq(&self, other: &PtrConst<T>) -> bool {
        self.ptr == other.ptr
    }
}

impl<T> PartialEq<PtrMut<T>> for PtrConst<T> {
    fn eq(&self, other: &PtrMut<T>) -> bool {
        self.ptr == other.ptr as *const T
    }
}

// PtrConst == *mut
impl<T> PartialEq<*mut T> for PtrConst<T> {
    fn eq(&self, other: &*mut T) -> bool {
        self.ptr == *other
    }
}

// PtrConst == *const
impl<T> PartialEq<*const T> for PtrConst<T> {
    fn eq(&self, other: &*const T) -> bool {
        self.ptr == *other
    }
}

// PtrConst order
impl<T> PartialOrd<PtrConst<T>> for PtrConst<T> {
    fn partial_cmp(&self, other: &PtrConst<T>) -> Option<::std::cmp::Ordering> {
        (self.ptr as usize).partial_cmp(&((other.ptr) as usize))
    }
}
impl<T> Ord for PtrConst<T> {
    fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
        (self.ptr as usize).cmp(&((other.ptr) as usize))
    }
}
impl<T> Eq for PtrConst<T> {}

impl<T> From<PtrMut<T>> for PtrConst<T> {
    fn from(value: PtrMut<T>) -> PtrConst<T> {
        PtrConst::new(value.ptr)
    }
}