refutil 0.1.1

Various utilities for reference, lifetime and memory management
Documentation
use std::mem::MaybeUninit;

use refutil::DropMut;

struct Foo(pub i32);

impl Drop for Foo {
    fn drop(&mut self) {}
}

#[test]
fn create_raw() {
    let obj = Box::leak(Box::new(Foo(42)));
    
    // SAFETY: reference comes from a leaked box so it won't be dropped or 
    // otherwise used
    let mut obj = unsafe { DropMut::new(obj) };
    (*obj).0 *= 42;

    // SAFETY: pointer comes from a leaked box
    drop(unsafe { Box::from_raw(&raw mut *obj.take_ref()) });
}

#[test]
fn create_uninit() {
    let mut storage = MaybeUninit::uninit();

    let mut ref1 = DropMut::at_uninit(&mut storage, Foo(1));
    (*ref1).0 -= 1;
    drop(ref1);

    let mut ref2 = DropMut::at_uninit(&mut storage, Foo(2));
    (*ref2).0 -= 2;
    drop(ref2);
}