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§
- Alignment
Corrector Allocator - 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.
- Transmute
Error - 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
AlignmentCorrectorAllocator
s 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