feanor-mempool 1.0.0

A simple interface to memory allocation and pooling, designed for use by feanor-math.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#![feature(never_type)]
#![feature(new_uninit)]
#![feature(btree_cursors)]
#![feature(maybe_uninit_slice)]

#![doc = include_str!("../Readme.md")]

pub mod cache;

use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::rc::*;
use std::ptr;

///
/// Trait for memory regions managed by a memory provider.
/// 
/// Note that the memory content of such an object does not have to be
/// initialized. Thus, when implementing it, make sure sure the memory
/// itself is declared as `[MaybeUninit<T>]` or similar, instead of `[T]`.
/// 
/// When any of the functions [`MemoryProviderObject::drop_or_recycle()`],
/// [`MemoryProviderObject::deref_initialized()`] or [`MemoryProviderObject::deref_mut_initialized()`]
/// are called, you can assume that the contained data has been initialized.
/// 
pub trait MemoryProviderObject<T> {

    ///
    /// Returns the data as [`MaybeUninit`]-slice.
    /// 
    fn deref_uninit(&mut self) -> &mut [MaybeUninit<T>];

    ///
    /// Drops the contained data and performs any cleanup that is
    /// necessary when the memory is no longer required. This may
    /// mean deallocating it, or returning it to some memory pool.
    /// 
    /// # Safety
    /// 
    /// The caller must ensure that the contained data is initialized
    /// and valid to be dropped.
    /// 
    unsafe fn drop_or_recycle(self: Box<Self>);

    ///
    /// Returns the data as constant `T`-slice.
    /// 
    /// # Safety
    /// 
    /// The caller must ensure that the contained data is initialized.
    /// 
    unsafe fn deref_initialized(&self) -> &[T];

    ///
    /// Returns the data as mutable `T`-slice.
    /// 
    /// # Safety
    /// 
    /// The caller must ensure that the contained data is initialized.
    /// 
    unsafe fn deref_mut_initialized(&mut self) -> &mut [T];

    ///
    /// Returns some pointer that can be used by a memory pool to verify
    /// the type of a type-erased [`MemoryProviderObject`].
    /// 
    /// Concretely, the use case this is designed for goes as follows:
    /// A [`MemoryProvider`] allocates some `u8` (content is irrelevant)
    /// in a place inaccessible to others. Then, if for some type-erase
    /// `Box<dyn MemoryProviderObject>` the function [`MemoryProviderObject::get_type_identifier()`]
    /// returns a pointer to that element, we know it was created in a context
    /// that can access said object.
    /// 
    fn get_creator_identifier(&self) -> &u8;
}

///
/// A homogeneous array containing elements of type `T`, managed 
/// by a memory provider.
/// 
pub struct ManagedSlice<'a, T> {
    ///
    /// When this is non-`None`, we may assume that the memory stored by the
    /// [`MemoryProviderObject`] is initialized.
    /// 
    content: Option<Box<dyn 'a + MemoryProviderObject<T>>>
}

impl<'a, T> Deref for ManagedSlice<'a, T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        // safe, because by contract of `content`, the memory is initialized
        unsafe { self.content.as_ref().unwrap().deref_initialized() }
    }
}

impl<'a, T> DerefMut for ManagedSlice<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // safe, because by contract of `content`, the memory is initialized
        unsafe { self.content.as_mut().unwrap().deref_mut_initialized() }
    }
}

impl<'a, T> Drop for ManagedSlice<'a, T> {
    fn drop(&mut self) {
        if self.content.is_some() {
            // safe, because by contract of `content`, the memory is initialized
            unsafe { self.content.take().unwrap().drop_or_recycle() }
        }
    }
}

///
/// Fills the given slice using elements provided by `initializer`.
/// If this function returns with `Ok(())`, then the caller may assume
/// that `mem` is correctly initialized with valid elements of `T`, i.e.
/// may call [`MaybeUninit::slice_assume_init_mut()`].
/// 
/// If this function panics or returns with `Err(E)`, this is not the case,
/// and the caller has to assume that elements of `mem` remain in a state
/// that is not a valid element of type `T`.
/// 
/// Note that if `initializer` panics, this function may leak memory, i.e.
/// will not drop elements previously returned by `initializer`. If `initialier`
/// returns an `Err(E)`, all previously yielded elements are correctly dropped.
/// 
fn try_initialize<T, E, F: FnMut(usize) -> Result<T, E>>(mem: &mut [MaybeUninit<T>], mut initializer: F) -> Result<(), E> {
    let mut i = 0;
    while i < mem.len() {
        // note that this will leak memory if initializer(i) panics
        match initializer(i) {
            Ok(val) => {
                mem[i] = MaybeUninit::new(val);
                i += 1;
            },
            Err(err) => {
                // drop the previously initialized memory
                // note that this does not prevent a memory leak in the panic case
                for j in 0..i {
                    unsafe { mem[j].assume_init_drop() };
                }
                return Err(err);
            }
        };
    }
    return Ok(());
}

///
/// Trait for objects that can provide homogeneous regions of memory (i.e. containing
/// elements of the same type `T`), often for short-term use.
/// This includes naive implementations like [`AllocatingMemoryProvider`] that
/// just allocate memory, or alternatively all kinds of memory pools and recyclers
/// (e.g. [`cache::SizedMemoryPool`]).
/// 
/// This is related to [`std::alloc::Allocator`], but less restrictive, as it may
/// return objects with certain structure. In particular, it naturally allows e.g.
/// memory pools or memory recycling.
/// 
/// It is usually used when certain objects or algorithms need frequent allocations
/// (often all of the same size), either because they need temporary, internal memory,
/// or they represent rings and have to allocate memory for elements. On the other hand,
/// if a struct just needs to store some data during its lifetime, memory pooling is
/// usually not useful, and a standard `Vec` is often used instead.
/// 
pub trait MemoryProvider<'a, T> {

    ///
    /// Provides a region of memory that is not used otherwise, of the given size.
    /// Note that the returned memory is not initialized!
    /// 
    /// # Safety
    /// 
    /// The returned memory is not initialized. In particular, this means that the implementation
    /// of the returned type of [`MemoryProviderObject`] must not store `T`s directly (e.g. in
    /// the form of [`T`]), but only `MaybeUninit<T>`. Since [`Deref`] and [`DerefMut`] change
    /// this into `&[T]` resp. `&mut [T]`, the returned value may not be dereferences until
    /// the caller has ensured proper initialization. This should be done using 
    /// [`MemoryProviderObject::deref_uninit()`].
    /// 
    /// Apart from that, it is also required that `self_rc` and `self` are references 
    /// to the same object.
    /// 
    unsafe fn new(&self, self_rc: &Rc<dyn 'a + MemoryProvider<T>>, size: usize) -> Box<dyn 'a + MemoryProviderObject<T>>;
}

///
/// A [`MemoryProvider`] that can change the size of managed memory regions after
/// they were created.
/// 
pub trait GrowableMemoryProvider<'a, T>: MemoryProvider<'a, T> {

    ///
    /// Returns a memory region of the new size that is not used otherwise.
    /// Entries contained in both slices are kept, the rest of the slice is uninitialized.
    /// 
    /// Note that when `new_size` is smaller than the old length, the rest of the
    /// entries are forgotten - the caller should first drop those to prevent a memory leak.
    /// 
    /// # Safety
    /// 
    /// The function *may not* assume that `data` has indeed been created using this memory
    /// provider. The implementation must check this, and may then either continue to work correctly
    /// (despite an "illegal" parameter), or panic. To check this, consider using
    /// [`MemoryProviderObject::get_type_identifier()`].
    /// 
    /// Otherwise, the contract is very similar to [`MemoryProvider::new()`]:
    /// 
    /// The additional entries (if the new length is larger than the old one) of the returned
    /// memory are not initialized. In particular, this means that the implementation
    /// of the returned type of [`MemoryProviderObject`] must not store `T`s directly (e.g. in
    /// the form of [`T`]), but only `MaybeUninit<T>`. Since [`Deref`] and [`DerefMut`] change
    /// this into `&[T]` resp. `&mut [T]`, the returned value may not be dereferences until
    /// the caller has ensured proper initialization. This should be done using 
    /// [`MemoryProviderObject::deref_uninit()`].
    /// 
    /// Apart from that, it is also required that `self_rc` and `self` are references 
    /// to the same object.
    /// 
    unsafe fn change_size(&self, data: Box<dyn 'a + MemoryProviderObject<T>>, new_size: usize) -> Box<dyn 'a + MemoryProviderObject<T>>;

    ///
    /// Contract exactly as in [`MemoryProvider`] - best delegate the call using [`GrowableMemoryProvider::upcast()`]
    /// if necessary.
    /// 
    unsafe fn new(&self, self_rc: &Rc<dyn 'a + GrowableMemoryProvider<T>>, size: usize) -> Box<dyn 'a + MemoryProviderObject<T>>;

    fn upcast(self: Rc<Self>) -> Rc<dyn 'a + MemoryProvider<'a, T>>;
}

///
/// Shared pointer to a [`MemoryProvider`].
/// 
/// Note that it usually does not make sense to clone [`MemoryProvider`]s themselves, especially
/// when they contain a memory pool. Thus they are usually shared between many locations, which
/// can be done by [`MemProviderRc`].
/// 
pub struct MemProviderRc<'a, T> {
    pub ptr: Rc<dyn 'a + MemoryProvider<'a, T>>
}

///
/// Shared pointer to a [`GrowableMemoryProvider`].
/// 
/// Note that it usually does not make sense to clone [`GrowableMemoryProvider`]s themselves, especially
/// when they contain a memory pool. Thus they are usually shared between many locations, which
/// can be done by [`GrowableMemProviderRc`].
/// 
pub struct GrowableMemProviderRc<'a, T> {
    pub ptr: Rc<dyn 'a + GrowableMemoryProvider<'a, T>>
}

impl<'a, T> Clone for GrowableMemProviderRc<'a, T> {
    fn clone(&self) -> Self {
        Self {
            ptr: self.ptr.clone()
        }
    }
}

impl<'a, T> MemProviderRc<'a, T> {
    
    pub fn new_init<F: FnMut(usize) -> T>(&self, size: usize, mut initializer: F) -> ManagedSlice<'a, T> {
        self.try_new_init::<!, _>(size, |i| Ok(initializer(i))).unwrap_or_else(|x| x)
    }

    pub fn try_new_init<E, F: FnMut(usize) -> Result<T, E>>(&self, size: usize, initializer: F) -> Result<ManagedSlice<'a, T>, E> {
        let mut data: Box<dyn 'a + MemoryProviderObject<T>> = unsafe {
            self.ptr.new(&self.ptr, size)
        };
        try_initialize(data.as_mut().deref_uninit(), initializer)?;
        return Ok(ManagedSlice { content: Some(data) });
    }
}

impl<'a, T> GrowableMemProviderRc<'a, T> {
    
    pub fn as_memory_provider(self) -> MemProviderRc<'a, T> {
        MemProviderRc { ptr: self.ptr.upcast() }
    }

    pub fn shrink(&self, el: &mut ManagedSlice<'a, T>, new_size: usize) {
        let old_size = el.len();
        assert!(new_size < old_size);
        let mut data = el.content.take().unwrap();
        for i in new_size..old_size {
            unsafe { data.deref_uninit()[i].assume_init_drop() };
        }
        el.content = Some(unsafe {
            self.ptr.change_size(data, new_size)
        });
        debug_assert_eq!(el.len(), new_size);
    }

    pub fn try_grow_init<E, F: FnMut(usize) -> Result<T, E>>(&self, el: &mut ManagedSlice<'a, T>, new_size: usize, mut initializer: F) -> Result<(), E> {
        let old_size = el.len();
        assert!(new_size > old_size);
        let mut new_data = unsafe {
            self.ptr.change_size(el.content.take().unwrap(), new_size)
        };
        debug_assert_eq!(new_data.deref_uninit().len(), new_size);
        match try_initialize(&mut new_data.deref_uninit()[old_size..], |i| initializer(i + old_size)) {
            Ok(()) => {
                el.content = Some(new_data);
                return Ok(());
            },
            Err(e) => {
                for i in 0..old_size {
                    unsafe { new_data.deref_uninit()[i].assume_init_drop() };
                }
                return Err(e);
            }
        }
    }

    pub fn grow_init<F: FnMut(usize) -> T>(&self, el: &mut ManagedSlice<'a, T>, new_size: usize, mut initializer: F) {
        self.try_grow_init::<!, _>(el, new_size, |i| Ok(initializer(i))).unwrap_or_else(|x| x)
    }

    pub fn new_init<F: FnMut(usize) -> T>(&self, size: usize, mut initializer: F) -> ManagedSlice<'a, T> {
        self.try_new_init::<!, _>(size, |i| Ok(initializer(i))).unwrap_or_else(|x| x)
    }

    pub fn try_new_init<E, F: FnMut(usize) -> Result<T, E>>(&self, size: usize, initializer: F) -> Result<ManagedSlice<'a, T>, E> {
        let mut data: Box<dyn 'a + MemoryProviderObject<T>> = unsafe {
            <_ as GrowableMemoryProvider<T>>::new(&*self.ptr, &self.ptr, size)
        };
        try_initialize(data.as_mut().deref_uninit(), initializer)?;
        return Ok(ManagedSlice { content: Some(data) });
    }
}

static VEC_MEMORY_OBJECT_TYPE_IDENTIFIER: u8 = 0;

///
/// We can implement [`MemoryProviderObject`] for a no-op wrapper around `[T]`.
/// However, it is impossible to put this behind a trait object `dyn MemoryProviderObject<T>`,
/// probably because this would require a "super fat pointer".
/// Therefore, we have to accept the additional level of indirection.
/// 
impl<T> MemoryProviderObject<T> for Vec<MaybeUninit<T>> {

    unsafe fn drop_or_recycle(mut self: Box<Self>) {
        for i in 0..self.len() {
            self[i].assume_init_drop();
        }
        drop(self)
    }

    unsafe fn deref_initialized(&self) -> &[T] {
        MaybeUninit::slice_assume_init_ref(self.as_ref())
    }

    unsafe fn deref_mut_initialized(&mut self) -> &mut [T] {
        MaybeUninit::slice_assume_init_mut(self.as_mut())
    }

    fn deref_uninit(&mut self) -> &mut [MaybeUninit<T>] {
        &mut *self
    }

    fn get_creator_identifier(&self) -> &u8 {
        &VEC_MEMORY_OBJECT_TYPE_IDENTIFIER
    }
}

///
/// Default memory provider that just allocates memory using standard allocation,
/// and drops it after it goes out of scope.
/// 
#[derive(Copy, Clone)]
pub struct AllocatingMemoryProvider;

impl AllocatingMemoryProvider {

    unsafe fn new_base<'a, T>(size: usize) -> Box<dyn 'a + MemoryProviderObject<T>>
        where T: 'a
    {
        Box::new(Box::new_uninit_slice(size).into_vec())
    }

    unsafe fn change_size_base<'a, T>(mut data: Box<dyn 'a + MemoryProviderObject<T>>, new_size: usize) -> Box<dyn 'a + MemoryProviderObject<T>> {
        // this ensures that `data` indeed points to a `Vec<MaybeUninit<T>>`
        assert!(ptr::addr_eq(data.get_creator_identifier(), &VEC_MEMORY_OBJECT_TYPE_IDENTIFIER));
        // this is safe since data indeed points to `Vec<MaybeUninit<T>>`
        let data_cast = &mut *(&mut *data as *mut (dyn 'a + MemoryProviderObject<T>) as *mut () as *mut Vec<MaybeUninit<T>>);
        data_cast.resize_with(new_size, || MaybeUninit::uninit());
        return data;
    }
}

impl<'a, T> MemoryProvider<'a, T> for AllocatingMemoryProvider
    where T: 'a
{    
    unsafe fn new(&self, _self_rc: &Rc<dyn 'a + MemoryProvider<T>>, size: usize) -> Box<dyn 'a + MemoryProviderObject<T>> {
        Self::new_base(size)
    }
}

impl<'a, T> GrowableMemoryProvider<'a, T> for AllocatingMemoryProvider
    where T: 'a
{
    unsafe fn change_size(&self, data: Box<dyn 'a + MemoryProviderObject<T>>, new_size: usize) -> Box<dyn 'a + MemoryProviderObject<T>> {
        Self::change_size_base(data, new_size)
    }

    unsafe fn new(&self, _self_rc: &Rc<dyn 'a + GrowableMemoryProvider<T>>, size: usize) -> Box<dyn 'a + MemoryProviderObject<T>> {
        Self::new_base(size)
    }

    fn upcast(self: Rc<Self>) -> Rc<dyn 'a + MemoryProvider<'a, T>> {
        unsafe { Rc::from_raw(Rc::into_raw(self)) }
    }
}

///
/// Memory provider that allocates memory using standard allocation, but writes
/// a log message each time.
/// 
/// The standard use case is to just use [`DefaultMemoryProvider`] everywhere, which
/// defaults to [`AllocatingMemoryProvider`]. Should you suspect repeated allocations, you can
/// toggle the feature `log_memory`, which will transparently change [`DefaultMemoryProvider`]
/// to refer to [`LoggingMemoryProvider`]. Searching the log, one can then find the locations
/// where to replace [`DefaultMemoryProvider`] with a caching variant, e.g.
/// [`cache::SizedMemoryPool`]. 
/// 
#[derive(Clone, Copy)]
pub struct LoggingMemoryProvider {
    description: &'static str
}

impl LoggingMemoryProvider {

    ///
    /// Creates a new [`LoggingMemoryProvider`] that includes the given description whenever
    /// logging an allocation.
    /// 
    pub const fn new(description: &'static str) -> Self {
        LoggingMemoryProvider { description }
    }
}

impl<'a, T> MemoryProvider<'a, T> for LoggingMemoryProvider
    where T: 'a
{ 
    unsafe fn new(&self, _self_rc: &Rc<dyn 'a + MemoryProvider<T>>, size: usize) -> Box<dyn 'a + MemoryProviderObject<T>> {
        println!("[{}]: Allocating {} entries of size {}", self.description, size, std::mem::size_of::<T>());
        AllocatingMemoryProvider::new_base(size)
    }
}

impl<'a, T> GrowableMemoryProvider<'a, T> for LoggingMemoryProvider
    where T: 'a
{
    unsafe fn change_size(&self, data: Box<dyn 'a + MemoryProviderObject<T>>, new_size: usize) -> Box<dyn 'a + MemoryProviderObject<T>> {
        AllocatingMemoryProvider::change_size_base(data, new_size)
    }

    unsafe fn new(&self, _self_rc: &Rc<dyn 'a + GrowableMemoryProvider<T>>, size: usize) -> Box<dyn 'a + MemoryProviderObject<T>> {
        AllocatingMemoryProvider::new_base(size)
    }

    fn upcast(self: Rc<Self>) -> Rc<dyn 'a + MemoryProvider<'a, T>> {
        unsafe { Rc::from_raw(Rc::into_raw(self)) }
    }
}
///
/// Default memory provider which will use standard allocation.
/// 
/// Defaults to [`AllocatingMemoryProvider`], but using the feature `log_memory`
/// it can refer to [`LoggingMemoryProvider`] instead.
/// 
#[cfg(not(feature = "log_memory"))]
pub type DefaultMemoryProvider = AllocatingMemoryProvider;

///
/// Default memory provider which will use standard allocation.
/// 
/// Defaults to [`AllocatingMemoryProvider`], but using the feature `log_memory`
/// it can refer to [`LoggingMemoryProvider`] instead.
/// 
#[cfg(feature = "log_memory")]
pub type DefaultMemoryProvider = &'static LoggingMemoryProvider;

///
/// Returns an expression that evaluates to an identifier for the current function.
/// The exact syntax of that expression is not specified, and the value should
/// only be used for debugging/tracing purposes.
/// 
#[macro_export]
macro_rules! current_function {
    () => {{
        struct LocalMemoryProvider;
        std::any::type_name::<LocalMemoryProvider>()
    }}
}

///
/// Returns an instance of the current default memory provider.
/// 
/// The used memory provider can change depending on enabled features
/// and conditional compilation.
/// 
#[macro_export]
#[cfg(not(feature = "log_memory"))]
macro_rules! default_memory_provider {
    () => {
        $crate::GrowableMemProviderRc { ptr: std::rc::Rc::new($crate::AllocatingMemoryProvider) }
    };
}

///
/// Returns an instance of the current default memory provider.
/// 
/// The used memory provider can change depending on enabled features
/// and conditional compilation.
/// 
#[macro_export]
#[cfg(feature = "log_memory")]
macro_rules! default_memory_provider {
    () => {
        $crate::GrowableMemProviderRc { ptr: std::rc::Rc::new($crate::LoggingMemoryProvider::new($crate::current_function!())) }
    };
}

#[cfg(test)]
use std::cell::RefCell;
#[cfg(test)]
use std::collections::HashSet;

#[cfg(test)]
pub(self) struct TraceDrop {
    content: i32,
    drop_tracer: Rc<RefCell<HashSet<i32>>>
}

#[cfg(test)]
impl Drop for TraceDrop {
        
    fn drop(&mut self) {
        self.drop_tracer.as_ref().borrow_mut().insert(self.content);
    }
}

#[test]
fn test_new_init_drop_after_error() {
    let drop_tracer = Rc::new(RefCell::new(HashSet::new()));

    let result = default_memory_provider!().try_new_init(16, |i| if i < 8 {
        Ok(TraceDrop { content: i as i32, drop_tracer: drop_tracer.clone() })
    } else {
        Err(i)
    });
    assert_eq!(8, result.err().unwrap());
    assert_eq!((0..8).collect::<HashSet<_>>(), *drop_tracer.as_ref().borrow());
}

#[test]
fn test_new_init_drop() {
    let drop_tracer = Rc::new(RefCell::new(HashSet::new()));
    {
        default_memory_provider!().new_init(12, |i| TraceDrop { content: i as i32, drop_tracer: drop_tracer.clone() });
    }
    assert_eq!((0..12).collect::<HashSet<_>>(), *drop_tracer.as_ref().borrow());
}

#[test]
fn test_new_init() {
    let result = default_memory_provider!().new_init(10, |i| i);
    assert_eq!(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..], &*result);
}

#[test]
fn test_type_erasure() {
    let memory_provider: MemProviderRc<i32> = default_memory_provider!().as_memory_provider();
    memory_provider.new_init(100, |i| i as i32);
}

#[test]
fn test_grow_init() {
    let mut base = default_memory_provider!().new_init(5, |i| i as i32);
    assert_eq!(&[0, 1, 2, 3, 4][..], &*base);

    default_memory_provider!().grow_init(&mut base, 7, |i| i as i32);
    assert_eq!(&[0, 1, 2, 3, 4, 5, 6][..], &*base);
}

#[test]
fn test_shrink_drop() {
    let drop_tracer = Rc::new(RefCell::new(HashSet::new()));
    let mut result = default_memory_provider!().new_init(5, |i| TraceDrop { content: i as i32, drop_tracer: drop_tracer.clone() });
    default_memory_provider!().shrink(&mut result, 2);

    assert_eq!((2..5).collect::<HashSet<_>>(), *drop_tracer.as_ref().borrow());
}

#[test]
fn test_try_grow_error_drop() {
    let drop_tracer = Rc::new(RefCell::new(HashSet::new()));
    let mut result = default_memory_provider!().new_init(5, |i| TraceDrop { content: i as i32, drop_tracer: drop_tracer.clone() });
    let error = default_memory_provider!().try_grow_init(&mut result, 7, |i| if i <= 5 {
        Ok(TraceDrop { content: i as i32, drop_tracer: drop_tracer.clone() })
    } else {
        Err(())
    });
    assert!(error.is_err());
    assert!(result.content.is_none());
    assert_eq!((0..6).collect::<HashSet<_>>(), *drop_tracer.as_ref().borrow());
}