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
Implementation detail: Do not use
When the length doesn’t line up correctly in [transmute_vec_may_copy]
.
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
Allows transmuting of a Vec to another vec of a different size, with 0 copies.
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.
You may want to use transmute_vec_copy_enum
.