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
#![doc(html_root_url = "https://docs.rs/high_mem_utils/0.2.3/")]
#![feature(vec_leak, untagged_unions, const_fn, manually_drop_take)]
#![allow(unused_unsafe)]

/*!
This crate provides high-level memory abstractions used for ensure memory and exception safety in some
patterns.

High-level signifies that it only brings safe abstractions for some cases of transmute and others unsafe
functions in the mem or ptr module,does not provide a custom allocator or garbage collector neither depends
on the [core::alloc] unstable lib.

At the moment this crate is nightly only,this will change if the features [`vec_leak`], [`const_fn`],
[`untagged_unions`] and [`manually_drop_take`] get stabilished.

# Examples

```
use high_mem_utils::{CatchStr, DontDrop, DropBy};

let mut string = String::from("Hello world!");
let catch = CatchStr::new(string.clone());

assert_eq!(catch.leaked().to_string(), string); // leaked returns &&mut str,not use to_string
                                                // it's a bit difficult cast rigth now

assert_eq!(catch.seal(), string); // catch consumed
let mut a = [1, 2, 3];

{
    let elem = DropBy::new([2, 3, 4], |e: [u32; 3]| { a = e.clone(); });

    assert_eq!(*elem, Some([2, 3, 4]));
}

assert_eq!(a, [2, 3, 4]);

unsafe {
    let b = DontDrop([1, 2, 3]); // we're not dropping here because we will have two variables
                                 // pointing to the same memory and "b" lives for shorter
    a = [0; 3];
    b.as_ptr().copy_to(a.as_mut_ptr(), 3);
}

assert_eq!(a, [1, 2, 3]);
```

[`vec_leak`]: https://github.com/rust-lang/rust/issues/62195
[`untagged_unions`]: https://github.com/rust-lang/rust/issues/32836
[`manually_drop_take`]: https://github.com/rust-lang/rust/issues/55422
[`const_fn`]: https://github.com/rust-lang/rust/issues/57563
[core::alloc]: https://doc.rust-lang.org/core/alloc/index.html
*/

use std::mem::{take, ManuallyDrop, MaybeUninit, forget};
use std::ops::{Deref, DerefMut};
use std::collections::BTreeMap;
use std::hint::unreachable_unchecked;

/// This macro panics with the given message with debug_assertions on and call
/// [unreachable_unchecked](https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html) when off.
/// 
/// # Safety
/// 
/// You should use this macro for code that must not reach.But all the cases where can are not handled and
/// will be handled on release or otherwise the responsability of do it passed to another caller,via unsafe
/// interfaces. 
#[macro_export]
macro_rules! unreachable_debug {
    () => { unreachable!("entered unreachable code") };
    ($e:expr) => {
        if cfg!(not(debug_assertions)) {
            unsafe { std::hint::unreachable_unchecked() };
        } else {
            panic!($e);
        }
    }
}

/// An union type that can be leaked or sealed(owned),useful when you want to give temporal global access to a particular value.
pub union Catch<'a, T> {
    leaked: &'a mut T,
    sealed: ManuallyDrop<Box<T>>,
}

impl<'a, T> Catch<'a, T> {
    /// Creates a new Catch with a leak, you can lately get the underlying value and consume the Catch
    /// with the [`seal`](#method.seal) method.
    pub fn new(a: Box<T>) -> Self {
        Catch {
            leaked: Box::leak(a),
        }
    }

    /// Returns a a reference to the leaked field,'cause the only ways for construct this union returns
    /// a leaked one,for warranty never transmute stack to heap data,this method does not use transmute
    /// implicitly.
    pub fn leaked(&self) -> &&'a mut T {
        unsafe { &self.leaked }
    }

    /// Consumes the Catch and gets the inner Box\<T\>,preventing the memory leak.
    ///
    /// This does a call to transmute but,as the only ways to construct this union gives you a leaked one
    /// this never trigger undefined behavior by itself.
    pub fn seal(self) -> Box<T> {
        unsafe { ManuallyDrop::into_inner(self.sealed) }
    }

    /// Consumes the Catch and returns a mutable reference pointing to leaked data.
    pub fn leak(self) -> &'a mut T {
        unsafe { self.leaked }
    }

    /// Creates a new Catch from a mutable reference to T,without checking if T is in the heap.
    ///
    /// # Safety
    ///
    /// This function should only be used with data returned by the [`leak`](#method.leak) method from a safely constructed
    /// Catch or with data returned by `Box::leak(t)` otherwise this will trigger undefined behavior if the [`seal`](#method.seal)
    /// method is called.
    pub unsafe fn from_leaked(leaked: &'a mut T) -> Self {
        Catch { leaked }
    }
}

impl<'a, T: Default> Catch<'a, T> {
    /// Takes the sealed data and leaves T::default in their place,useful when you want to use the sealed
    /// value but you don't have ownership of the Catch.
    pub fn take(&mut self) -> Box<T> {
        let tmp = unsafe { ManuallyDrop::take(&mut self.sealed) };
        let ptr = self as *mut Self;
        unsafe { ptr.write( Self::new(Box::new(T::default())) ); }
        tmp
    }
}

// /// I have my doubts if implement this `Drop` which drop the sealed data but,as temporal global access does not mean neccesarily a
// /// memory leak and,many of the methods that this type implement returns or consumes the `Catch`.If you do know that your catch may
// /// be dropped,use the (leak)[#method.leak] or (seal)[#method.seal] method,depending at the need of static or owned access.
// impl<'a, T> Drop for Catch<'a, T> {
//    fn drop(&mut self) {
//        unsafe { ManuallyDrop::drop(&mut self.sealed) };
//    }
// }

/// An union slice that can be leaked or sealed(owned),useful when you want to give temporal global access
/// to a particular sequence.
pub union CatchSeq<'a, T> {
    leaked: &'a mut [T],
    sealed: ManuallyDrop<Box<[T]>>,
}

impl<'a, T> CatchSeq<'a, T> {
    /// Creates a new CatchSeq with a leak, you can lately get the underlying sequence and consume the CatchSeq
    /// with the [`seal`](#method.seal) method.
    pub fn new(a: Vec<T>) -> Self {
        CatchSeq {
            leaked: Vec::leak(a),
        }
    }

    /// Returns a a reference to the leaked field,as the only safe ways for construct this union returns a leaked
    /// one,for warranty never transmute stack to heap data,this method does not use transmute implicitly.
    pub fn leaked(&self) -> &&'a mut [T] {
        unsafe { &self.leaked }
    }

    /// Consumes the Catch and gets the inner Vec<T>, preventing the memory leak.
    ///
    /// This does a call to transmute but,as the only safe ways for construct this union returns a leaked
    /// one,this never trigger undefined behavior by itself.
    pub fn seal(self) -> Vec<T> {
        unsafe { ManuallyDrop::into_inner(self.sealed).into_vec() }
    }
    /// Consumes the CatchSeq and returns a mutable reference pointing to leaked data.
    pub fn leak(self) -> &'a mut [T] {
        unsafe { self.leaked }
    }

    /// Creates a new CatchSeq from a `&mut [T]`,without checking if the referent is in the heap.
    ///
    /// # Safety
    ///
    /// This function should only be used with data returned by the [`leak`](#method.leak) method from a safely constructed
    /// CatchSeq or with data returned by `Vec::leak` otherwise this will trigger undefined behavior
    /// if the [`seal`](#method.seal) method is called.
    pub unsafe fn from_leaked(leaked: &'a mut [T]) -> Self {
        CatchSeq { leaked }
    }
}

// /// I have my doubts if implement this `Drop` which drop the sealed data but,as temporal global access does not mean neccesarily a
// /// memory leak and,many of the methods that this type implement returns or consumes the `CatchSeq`.If you do know that your catch
// /// may be dropped,use the (leak)[#method.leak] or (seal)[#method.seal] method,depending at the need of static or owned access.
// impl<'a, T> Drop for CatchSeq<'a, T> {
//    fn drop(&mut self) {
//        unsafe { ManuallyDrop::drop(&mut self.sealed) };
//    }
// }

/// An union string that can be leaked or sealed(owned),useful when you want to give temporal global access
/// to a particular string.
pub union CatchStr<'a> {
    leaked: &'a mut str,
    sealed: ManuallyDrop<Box<str>>,
}

impl<'a> CatchStr<'a> {
    /// Creates a new CatchStr with a leak, you can lately get the underlying string and consume the CatchStr
    /// with the [`seal`](#method.seal) method.
    pub fn new(a: String) -> Self {
        CatchStr {
            leaked: Box::leak(a.into_boxed_str()),
        }
    }

    /// Returns a a reference to the leaked field,as the only safe ways for construct this union returns a leaked
    /// one,for warranty never transmute stack to heap data,this method does not use transmute implicitly.
    pub fn leaked(&self) -> &&'a mut str {
        unsafe { &self.leaked }
    }

    /// Consumes the Catch and gets the inner String, preventing the memory leak.
    ///
    /// This does a call to transmute but,as the only safe ways for construct this union return a leaked
    /// one,this never trigger undefined behavior by itself.
    pub fn seal(self) -> String {
        unsafe { ManuallyDrop::into_inner(self.sealed).into_string() }
    }

    /// Consumes the CatchStr and returns a mutable reference pointing to leaked data.
    pub fn leak(self) -> &'a mut str {
        unsafe { self.leaked }
    }

    /// Creates a new CatchStr from a `&mut str`,without checking if the referent is in the heap.
    ///
    /// # Safety
    ///
    /// This function should only be used with data returned by the [`leak`](#method.leak) method from a safely constructed
    /// CatchStr or with data returned by `Box::leak(string.into_boxed_str())` otherwise this will trigger undefined behavior
    /// if the [`seal`](#method.seal) method is called.
    pub unsafe fn from_leaked(leaked: &'a mut str) -> Self {
        CatchStr { leaked }
    }
}

// /// I have my doubts if implement this `Drop` which drop the sealed data but,as temporal global access does not mean neccesarily a
// /// memory leak and,many of the methods that this type implement returns or consumes the `CatchStr`.If you do know that your catch
// /// may be dropped,use the [leak](#method.leak) or [seal](#method.seal) method,depending at the need of static or owned access.
// impl<'a> Drop for CatchStr<'a> {
//    fn drop(&mut self) {
//        unsafe { ManuallyDrop::drop(&mut self.sealed) };
//    }
// }

/// A wrapper for an implementation of drop that [`mem::take`] the value and [`mem::forget`]s it.
///
/// This might be useful if you want assuring that a particular destructor not run if it can lead to
/// a double-free or another memory issue.
///
/// This type is particularly not recomended for reference types because as such they can never be null
/// and the value is still dropped. Neither on types with a costly initialization because it replaces the
/// forgotten value with the `Default` one,these values should not implement it anyways.
///
/// This type has the same implications that forget except for the fact that this ensures that the value
/// is never dropped even on panic,unless you abort.In general [`DontDropOpt`](./struct.DontDropOpt.html) is preferred.
///
/// It derefs to T.
///
/// [`mem::take`]: https://doc.rust-lang.org/core/mem/fn.take.html
/// [`mem::forget`]: https://doc.rust-lang.org/core/mem/fn.forget.html
#[repr(transparent)]
pub struct DontDrop<T: Default>(pub T);

impl<T: Default> DontDrop<T> {
    /// Returns the field,allowing it to be dropped again.
    pub fn into_inner(&mut self) -> T {
        take(&mut self.0)
    }
}

impl<T: Default> Deref for DontDrop<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: Default> DerefMut for DontDrop<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Default> Drop for DontDrop<T> {
    fn drop(&mut self) {
        forget(take(&mut self.0));
    }
}

/// A wrapper for an implementation of drop that [`mem::forget`] the previous value and replace it with None.
///
/// This might be useful if you want assuring that a particular destructor not run if it can lead to
/// a double-free or another memory issue.
///
/// This type is particularly not recomended for reference types because as such they can never be null
/// and the value is still dropped.
///
/// This type has the same implications that [`mem::forget`] except for the fact that this ensures that the
/// value is never dropped even on panic,unless you abort.
///
/// It derefs to Option<T>.
/// 
/// [`mem::forget`]: https://doc.rust-lang.org/core/mem/fn.forget.html
#[repr(transparent)]
pub struct DontDropOpt<T>(Option<T>);

impl<T> DontDropOpt<T> {
    /// Construct a new `DontDropOpt` from a value,this has no effect if the value is a reference.
    pub fn new(a: T) -> Self {
        DontDropOpt(Some(a))
    }

    /// Returns the value,allowing it to be dropped again.
    pub fn into_inner(&mut self) -> Option<T> {
        self.0.take()
    }

    /// Unwraps the Option<T>,allowing it to be dropped again.
    /// 
    /// # Safety
    /// 
    /// This will panic if the type contained is None with debug_assertions enabled,otherwise triggers UB.
    pub unsafe fn into_inner_unchecked(&mut self) -> T {
        self.0.take().unwrap_or_else(|| unreachable_debug!("Called into_inner_unchecked with None value on DontDropOpt in debug."))
    } 
}

impl<T> Drop for DontDropOpt<T> {
    fn drop(&mut self) {
        forget((&mut self.0).take());
    }
}

impl<T> Deref for DontDropOpt<T> {
    type Target = Option<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for DontDropOpt<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}


/// A wrapper that calls the given closure at Drop. Useful when you have a conditional assign of one
/// that,once assigned,you want to warranty a call to it with the given T,and then drop it.
/// 
/// Currently this type has a value field of an `Option<T>`,because the closure needs to take ownership
/// doing use of the [take](https://doc.rust-lang.org/std/option/enum.Option.html#method.take) method on
/// `Option`.In case of `None` because there's no meaningful value for the closure,drop returns at that point.
///
/// It derefs to Option<T>.
pub struct DropBy<T, F: FnMut(T)> {
    pub value: Option<T>,
    pub clos: F
}

impl<T, F: FnMut(T)> DropBy<T, F> {
    /// Creates a new `DropBy` with the value and the closure that takes the value at their `Drop` impl.
    pub const fn new(value: T, clos: F) -> Self {
        let value = Some(value);
        DropBy { value, clos }
    }

    /// Takes the value,disabling any code in the closure.
    pub fn into_inner(&mut self) -> Option<T> {
        self.value.take()
    }

    /// Takes and unwraps the value,disabling any code in the closure.
    /// 
    /// # Safety
    /// 
    /// This will panic if the type contained is `None` with debug_assertions enabled,otherwise triggers UB.
    pub unsafe fn into_inner_unchecked(&mut self) -> T {
        self.value.take().unwrap_or_else(|| unreachable_debug!("Called into_inner_unchecked with None value on DropBy in debug."))
    } 
}

impl<T, F: FnMut(T)> Deref for DropBy<T, F> {
    type Target = Option<T>;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<T, F: FnMut(T)> DerefMut for DropBy<T, F> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

impl<T, F: FnMut(T)> Drop for DropBy<T, F> {
    fn drop(&mut self) {
        
        let value = match self.value.take() {
            Some(a) => a,
            _ => return,
        };

        (self.clos)(value);
    }
}

/// An enum that can be all sorts of Catch's over T,useful when you do not known if you gonna have a Box,Vec or String and you want to
/// grant static temporal access to any of them safely.
pub enum CatchT<'a, T> {
    Catch(Catch<'a, T>),
    CatchSeq(CatchSeq<'a, T>),
    CatchStr(CatchStr<'a>),
}

/// A lazy-iniatialiazed cache for a Fn closure with a constant constructor.
pub struct LazyCache<P: Ord, V: Clone, C: Fn(P) -> V> {
    cache: MaybeUninit<BTreeMap<P, V>>,
    pub closure: C,
    init: bool
}

impl<P: Ord + Clone, V: Clone, C: Fn(P) -> V> LazyCache<P, V, C> {
    /// Construct a cache for a closure that is initialized in the first call to [call_cache](#method.call_cache).
    /// 
    /// This is particularly useful for fn pointers of recurrently used functions because it can be used
    /// on statics,althought you need make them mutable for actually call [call_cache](#method.call_cache).
    pub const fn new(closure: C) -> Self {
        Self {cache: MaybeUninit::uninit(), closure, init: false}
    }

    /// Construct a cache for a closure providing an iniatialized cache.
    pub const fn with_cache(cache: BTreeMap<P, V>, closure: C) -> Self {
        Self {cache: MaybeUninit::new(cache), closure, init: true }
    }

    /// Cosntruct a cache for a closure providing a maybe unininitialized one along with the init state.
    /// 
    /// If you do know that `init` is true the [`with_cache`](#method.with_cache) is preferred.
    ///  
    /// # Safety
    /// 
    /// This function is unsafe due to being unable to prove that the init value is correct and `true`
    /// only for initialiazed caches. 
    /// 
    /// Considering that a bad use can lead to try to read or destruct uninitializated memory the
    /// use of this function is discouraged and it only exist to give flexibility while maintaining the
    /// fields private.
    pub const unsafe fn with_cache_unchecked(cache: MaybeUninit<BTreeMap<P, V>>, closure: C, init: bool) -> Self {
        Self {cache, closure, init}
    }
    
    fn init(&mut self) {
        unsafe {
            self.cache.as_mut_ptr().write(BTreeMap::new());
            self.init = true;
        }
    }

    /// calls the inner closure with the given arg after init the cache if it did not before.
    /// 
    /// # Panics
    /// 
    /// If the given closure panic the variable reading the cache from the MaybeUninit is guaranteed to not
    /// drop and the [Drop](#impl-Drop) impl will do the job.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use high_mem_utils::LazyCache;
    /// 
    /// fn foo(num: u32) -> u32 {
    ///     num
    /// }
    /// 
    /// let mut cache = LazyCache::new(foo);
    /// 
    /// assert_eq!(cache.call_cache(2), 2);
    /// assert_eq!(cache.call_cache(2), 2);
    /// assert_eq!(cache.call_cache(4), 4);
    /// ```
    pub fn call_cache(&mut self, arg: P) -> V {
    
        if !self.init {
            self.init();
        }

        let mut_ref = unsafe { (&mut *self.cache.as_mut_ptr()) };
    
        let value = if mut_ref.contains_key(&arg) {
            (mut_ref.get(&arg).unwrap()).clone()
        } else {
            let temp = (self.closure)(arg.clone());
            mut_ref.insert(arg, temp.clone());
            temp
        };
        
        value
    }

    /// This method removes an argument from the cache and returns the value or None if there's no one
    /// or the cache is uninitialized.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use high_mem_utils::LazyCache;
    /// 
    /// fn foo(num: u32) -> u32 {
    ///     num
    /// }
    /// 
    /// let mut cache = LazyCache::new(foo);
    /// 
    /// assert_eq!(cache.call_cache(2), 2);
    /// assert_eq!(cache.pop(&2), Some(2));
    /// assert_eq!(cache.pop(&2), None);
    /// ```
    pub fn pop(&mut self, arg: &P) -> Option<V> {
        if !self.init { 
            return None;
        }

        let rem = (unsafe { &mut *self.cache.as_mut_ptr() }).remove(arg);

        rem
    }

    /// Clears the cache,removing all their values.This is no-op if the cache is not initialized.
    pub fn clear(&mut self) {
        if !self.init {
            return;
        }

        (unsafe { &mut *self.cache.as_mut_ptr() }).clear();
    }

    /// Returns true if the cache is initialized,not neccesarily filled with an argument. Use \![is_empty](#method.is_empty)
    /// for that purpose.
    pub fn is_init(&self) -> bool {
        self.init
    }

    /// Returns true if the cache has no arguments or if it's not initialized.
    pub fn is_empty(&self) -> bool {
        if self.init {
            (unsafe { &*self.cache.as_ptr() }).is_empty()
        } else {
            true
        }
    }

    /// This method takes a closure that receives a mutable reference to the cache.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use high_mem_utils::LazyCache;
    /// 
    /// let mut cache = LazyCache::new(|x| x);
    /// 
    /// cache.cache(|c| {c.insert(2, 2);});
    /// assert_eq!(cache.pop(&2), Some(2));
    /// assert_eq!(cache.call_cache(2), 2);
    /// assert_eq!(cache.pop(&2), Some(2));
    /// assert_eq!(cache.pop(&2), None);
    /// ```
    pub fn cache<F: Fn(&mut BTreeMap<P, V>)>(&mut self, f: F) {
        if !self.init {
            self.init();
        }

        unsafe {
            f(&mut *self.cache.as_mut_ptr());
        }
    }
}

/// This impl drops the cache only if it's initialiazed.
impl<P: Ord, V: Clone, C: Fn(P) -> V> Drop for LazyCache<P, V, C> {
    fn drop(&mut self) {
        if self.init { 
            unsafe { self.cache.as_mut_ptr().drop_in_place() }
        }
    }   
}