Crate transvec[][src]

Expand description

This is a way to “transmute” Vecs soundly.

#![feature(allocator_api)] // this requires the allocator api because the way that this
// handles deallocating hooks into the allocator api
use transvec::transmute_vec;
let input_vec: Vec<u16> = vec![1, 2, 3, 4, 5, 6, 7, 8];
let output: Vec<u8, _> = match transmute_vec(input_vec) {
    Ok(x) => x,
    // the "transmute" can fail, if the alignment/capacity/length is incorrect
    // consider using `transmute_vec_may_copy`
    Err((old_vec, err)) => return println!("Error: {:?}", err),
};
if cfg!(target_endian = "big") {
    assert_eq!(
        &output,
        &[0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]
    );
} else {
    assert_eq!(
        &output,
        &[1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0]
    );
}

Structs

Handling correcting the alignment fed into the inner allocator.

Enums

Whether or not a copy occured. Also the copy variant doesn’t have the custom allocator, and is therefore one usize smaller.

Error for Vec transmutes. It will always be Alignment -> Length -> Capacity

Functions

Changes from any allocator to an AlignmentCorrectorAllocator.

Removes AlignmentCorrectorAllocators after they’ve been realloced.

Allows transmuting of a Vec to another vec of a different size, with 0 copies.

If alignment is the same this function is preferred over transmute_vec.

transmute_vec_basic but on fail it copies instead.

transmute_vec_may_copy but it tells you whether or not a copy occured and returns a normal Vec if it doesn’t.

Same as transmute_vec but in case of an error it copies instead. If it’s over the length it removes whatever doesn’t fit.

Same as transmute_vec but in case of an error it copies instead. If it’s over the length it removes whatever doesn’t fit.

Transmute between two types of Vecs