[][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, const_fn, untagged_unions and const_fn_union get stabilished.

Examples

use high_mem_utils::{Catch, DontDropOpt, DropBy};

let mut string = String::from("Hello world!");
let catch = Catch::new_str(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 = DontDropOpt::new([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_ref().unwrap().as_ptr().copy_to(a.as_mut_ptr(), 3);
}

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

Macros

unreachable_debug

This macro panics with the given message with debug_assertions on and call unreachable_unchecked when off.

Structs

DontDropDeprecated

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.

LazyCache

A lazy-iniatialiazed cache for a Fn closure with a constant constructor.

Enums

CatchTDeprecated

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.

Traits

RawPointer

A trait that represents a raw pointer or NonNull<T>,useful for functions that creates them.

Functions

dangling

Creates a dangling but well-aligned raw pointer.

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.

CatchSeqDeprecated

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

CatchStrDeprecated

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