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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use crate::pointer::Pointer;
use crate::pool::Pool;
use crate::refbox::assume_init;
use crate::refbox::data_ptr;
use crate::refbox::RefBox;
use crate::PoolClone;
use crate::{types::ElementPointer, PoolDefault};
use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Error;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::DerefMut;
use std::ptr::NonNull;
use std::{ops::Deref, pin::Pin};

/// A unique pointer to a pool allocated value of `A`.
pub struct PoolBox<A> {
    pub(crate) handle: ElementPointer<A>,
}

impl<A> PoolBox<A> {
    /// Construct a `PoolBox` with a newly initialised value of `A`.
    ///
    /// This uses [`PoolDefault::default_uninit()`][default_uninit] to initialise a
    /// default value, which may be faster than constructing a `PoolBox` from an
    /// existing value using [`PoolBox::new()`][new], depending on the data
    /// type.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<usize> = Pool::new(256);
    /// let zero = PoolBox::default(&pool);
    /// assert_eq!(0, *zero);
    /// ```
    ///
    /// [new]: #method.new
    /// [default_uninit]: trait.PoolDefault.html#tymethod.default_uninit
    pub fn default(pool: &Pool<A>) -> Self
    where
        A: PoolDefault,
    {
        let mut handle = pool.pop();
        unsafe {
            PoolDefault::default_uninit(data_ptr(&mut handle));
            assume_init(handle)
        }
        .into_box()
    }

    /// Wrap a value in a `PoolBox`.
    ///
    /// This will copy the entire value into the memory handled by the
    /// `PoolBox`, which may be slower than using
    /// [`PoolBox::default()`][default], so it's not recommended to use this to
    /// construct the default value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<usize> = Pool::new(256);
    /// let number = PoolBox::new(&pool, 1337);
    /// assert_eq!(1337, *number);
    /// ```
    ///
    /// [default]: #method.default
    pub fn new(pool: &Pool<A>, value: A) -> Self {
        let mut handle = pool.pop();
        unsafe {
            data_ptr(&mut handle).as_mut_ptr().write(value);
            assume_init(handle)
        }
        .into_box()
    }

    /// Clone a value and return a new `PoolBox` to it.
    ///
    /// This will use [`PoolClone::clone_uninit()`][clone_uninit] to perform the
    /// clone, which may be more efficient than using
    /// [`PoolBox::new(value.clone())`][new].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<Vec<usize>> = Pool::new(1);
    /// let vec = vec![1, 2, 3];
    /// let ref1 = PoolBox::clone_from(&pool, &vec);
    /// assert_eq!(vec, *ref1);
    /// ```
    ///
    /// [new]: #method.new
    /// [clone_uninit]: trait.PoolClone.html#tymethod.clone_uninit
    pub fn clone_from(pool: &Pool<A>, value: &A) -> Self
    where
        A: PoolClone,
    {
        let mut handle = pool.pop();
        unsafe {
            value.clone_uninit(data_ptr(&mut handle));
            assume_init(handle)
        }
        .into_box()
    }

    /// Construct a [`Pin`][Pin]ned `PoolBox` with a default value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<usize> = Pool::new(256);
    /// let zero = PoolBox::pin_default(&pool);
    /// assert_eq!(0, *zero);
    /// ```
    ///
    /// [Pin]: https://doc.rust-lang.org/std/pin/struct.Pin.html
    pub fn pin_default(pool: &Pool<A>) -> Pin<Self>
    where
        A: PoolDefault,
    {
        unsafe { Pin::new_unchecked(Self::default(pool)) }
    }

    /// Construct a [`Pin`][Pin]ned `PoolBox` with the given value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<usize> = Pool::new(256);
    /// let number = PoolBox::pin(&pool, 1337);
    /// assert_eq!(1337, *number);
    /// ```
    ///
    /// [Pin]: https://doc.rust-lang.org/std/pin/struct.Pin.html
    pub fn pin(pool: &Pool<A>, value: A) -> Pin<Self> {
        unsafe { Pin::new_unchecked(Self::new(pool, value)) }
    }

    /// Test two `PoolBox`es for pointer equality.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<usize> = Pool::new(1);
    /// let ref1 = PoolBox::default(&pool);
    /// assert!(PoolBox::ptr_eq(&ref1, &ref1));
    /// ```
    pub fn ptr_eq(left: &Self, right: &Self) -> bool {
        std::ptr::eq(left.handle.get_ptr(), right.handle.get_ptr())
    }

    /// Consume the `PoolBox` and return a pointer to the contents.
    ///
    /// Please note that the only proper way to drop the value pointed to
    /// is by using `PoolBox::from_raw` to turn it back into a `PoolBox`, because
    /// the value is followed by `PoolBox` metadata which also needs to
    /// be dropped.
    pub fn into_raw_non_null(b: PoolBox<A>) -> NonNull<A> {
        let ptr = b.handle.cast();
        std::mem::forget(b);
        ptr
    }

    /// Consume the `PoolBox` and return a pointer to the contents.
    ///
    /// The pointer is guaranteed to be non-null.
    ///
    /// Please note that the only proper way to drop the value pointed to
    /// is by using `PoolBox::from_raw` to turn it back into a `PoolBox`, because
    /// the value is followed by `PoolBox` metadata which also needs to
    /// be dropped.
    pub fn into_raw(b: PoolBox<A>) -> *mut A {
        Self::into_raw_non_null(b).as_ptr()
    }

    /// Turn a raw pointer back into a `PoolBox`.
    ///
    /// The pointer must be non-null and obtained from a previous call to
    /// `PoolBox::into_raw` or `PoolBox::into_raw_non_null`.
    ///
    /// # Safety
    ///
    /// This must *only* be called on pointers obtained through `PoolBox::into_raw`.
    /// It's not OK to call it on a pointer to a value of `A` you've allocated
    /// yourself.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<usize> = Pool::new(1);
    /// let ref1 = PoolBox::new(&pool, 31337);
    ///
    /// // Turn the PoolBox into a raw pointer and see if it still works.
    /// let ptr = PoolBox::into_raw(ref1);
    /// assert_eq!(31337, unsafe { *ptr });
    ///
    /// // Turn it back into a PoolBox and see, again, if it still works.
    /// let ref2 = unsafe { PoolBox::from_raw(ptr) };
    /// assert_eq!(31337, *ref2);
    /// ```
    pub unsafe fn from_raw(ptr: *mut A) -> Self {
        Self {
            handle: ElementPointer::wrap(ptr.cast()),
        }
    }

    fn box_ref(&self) -> &RefBox<A> {
        unsafe { &*self.handle.get_ptr() }
    }

    fn box_ref_mut(&mut self) -> &mut RefBox<A> {
        unsafe { &mut *self.handle.get_ptr() }
    }
}

impl<A> Drop for PoolBox<A> {
    fn drop(&mut self) {
        let handle = unsafe { Box::from_raw(self.handle.get_ptr()) };
        handle.return_to_pool();
    }
}

impl<A> Clone for PoolBox<A>
where
    A: PoolClone,
{
    /// Clone a `PoolBox` and its contents.
    ///
    /// This will use [`PoolClone::clone_uninit()`][clone_uninit] to perform the
    /// clone, which may be more efficient than using
    /// [`PoolBox::new(value.clone())`][new].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use refpool::{Pool, PoolBox};
    /// let pool: Pool<Vec<usize>> = Pool::new(1);
    /// let vec1 = PoolBox::new(&pool, vec![1, 2, 3]);
    /// let vec2 = vec1.clone();
    /// assert_eq!(vec1, vec2);
    /// ```
    ///
    /// [new]: #method.new
    /// [clone_uninit]: trait.PoolClone.html#tymethod.clone_uninit
    fn clone(&self) -> Self {
        let mut handle = self.box_ref().pool.pop();
        unsafe {
            self.deref().clone_uninit(data_ptr(&mut handle));
            assume_init(handle)
        }
        .into_box()
    }
}

impl<A> Deref for PoolBox<A> {
    type Target = A;
    fn deref(&self) -> &Self::Target {
        self.box_ref().value_as_ref()
    }
}

impl<A> DerefMut for PoolBox<A> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.box_ref_mut().value_as_mut()
    }
}

impl<A> AsRef<A> for PoolBox<A> {
    fn as_ref(&self) -> &A {
        self.deref()
    }
}

impl<A> AsMut<A> for PoolBox<A> {
    fn as_mut(&mut self) -> &mut A {
        self.deref_mut()
    }
}

impl<A> Borrow<A> for PoolBox<A> {
    fn borrow(&self) -> &A {
        self.deref()
    }
}

impl<A> BorrowMut<A> for PoolBox<A> {
    fn borrow_mut(&mut self) -> &mut A {
        self.deref_mut()
    }
}

impl<A> PartialEq for PoolBox<A>
where
    A: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        (**self) == (**other)
    }
}

impl<A> Eq for PoolBox<A> where A: Eq {}

impl<A> PartialOrd for PoolBox<A>
where
    A: PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        (**self).partial_cmp(&**other)
    }
}

impl<A> Ord for PoolBox<A>
where
    A: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        (**self).cmp(&**other)
    }
}

impl<A> Hash for PoolBox<A>
where
    A: Hash,
{
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        (**self).hash(hasher)
    }
}

impl<A> Display for PoolBox<A>
where
    A: Display,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        (**self).fmt(f)
    }
}

impl<A> Debug for PoolBox<A>
where
    A: Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        (**self).fmt(f)
    }
}

impl<A> std::fmt::Pointer for PoolBox<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        std::fmt::Pointer::fmt(&(&**self as *const A), f)
    }
}