[][src]Crate high_mem_utils

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 and untagged_unions 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 
                                                // is 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| { a = e.clone(); });

    assert_eq!(*elem, [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]);

Structs

DontDrop

A wrapper for an implementation of drop that mem::take the value and mem::forgets it.

DontDropOpt

A wrapper for an implementation of drop that mem::forget the previous value and replace it with None.

DropBy

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.

Unions

Catch

An union type that can be leaked or sealed(owned),useful when you want to give temporal global access to a particular value.

CatchSeq

An union slice that can be leaked or sealed(owned),useful when you want to give temporal global access to a particular sequence.

CatchStr

An union string that can be leaked or sealed(owned),useful when you want to give temporal global access to a particular string.