Crate transvec

Source
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§

AlignmentCorrectorAllocator
Handling correcting the alignment fed into the inner allocator.

Enums§

CopyNot
Whether or not a copy occured. Also the copy variant doesn’t have the custom allocator, and is therefore one usize smaller.
TransmuteError
Error for Vec transmutes. It will always be Alignment -> Length -> Capacity

Functions§

add_alignment_allocator
Changes from any allocator to an AlignmentCorrectorAllocator.
remove_alignment_allocator
Removes AlignmentCorrectorAllocators after they’ve been realloced.
transmute_vec
Allows transmuting of a Vec to another vec of a different size, with 0 copies.
transmute_vec_basic
If alignment is the same this function is preferred over transmute_vec.
transmute_vec_basic_copy
transmute_vec_basic but on fail it copies instead.
transmute_vec_basic_copy_unsafe
transmute_vec_basic_copy but without Pod bounds.
transmute_vec_basic_unsafe
transmute_vec_basic but without Pod bounds.
transmute_vec_copy_enum
transmute_vec_may_copy but it tells you whether or not a copy occured and returns a normal Vec if it doesn’t.
transmute_vec_copy_enum_unsafe
transmute_vec_copy_enum but without Pod bounds.
transmute_vec_may_copy
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_vec_may_copy_unsafe
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_vec_unsafe
Transmute between two types of Vecs