#mem #safe #union #cache #high-level

nightly high_mem_utils

This crate provides a bunch of mem safe abstractions,some involving transmute

9 releases

✓ Uses Rust 2018 edition

0.2.7 Feb 6, 2020
0.2.6 Jan 8, 2020
0.2.2 Dec 31, 2019
0.1.1 Dec 19, 2019

#156 in Rust patterns

Download history 10/week @ 2020-03-14 31/week @ 2020-03-21 20/week @ 2020-03-28 17/week @ 2020-04-04 66/week @ 2020-04-11 12/week @ 2020-04-18 5/week @ 2020-04-25 2/week @ 2020-05-02 13/week @ 2020-05-23 20/week @ 2020-05-30 11/week @ 2020-06-06 11/week @ 2020-06-13 10/week @ 2020-06-27

115 downloads per month
Used in 2 crates (via col_macros)

Unlicense

30KB
325 lines

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

It 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.

Thsi crate brings serde support for some structs with the feature serde_support enabled.

Version

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.

Usage

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]);

License

This code is licensed under the Unlicense.

Dependencies

~83–265KB