sarah-quinones / aligned-vec

MIT License
21 stars 7 forks source link

`Into`/`From` impls for `Vec`/`AVec` #3

Closed SK83RJOSH closed 7 months ago

SK83RJOSH commented 1 year ago

It would be really handy to be able to go from an unaligned vec to an aligned vec and vice versa like so:

let vec = vec![0_u8; 128];
let vec: AVec<_> = vec.into();
let vec: Vec<_> = vec.into();

Additionally, it would be handy to go between AVec of different alignment values in the same way:

let vec: AVec<_, ConstAlign<32>> = avec_rt![[16]| 0_u8; 128].into();
let vec: AVec<_, ConstAlign<16>> = avec_rt![[16]| 0_u8; 128].into();
sarah-quinones commented 1 year ago

this is not possible in the general case, at least not without reallocating, since memory allocated with one alignment must be deallocated with the same alignment

SK83RJOSH commented 1 year ago

That's fine by me, it's just a nicety to make things less cumbersome. Particularly when moving data out of std/alloc::vec. Right now I rely on from_slice, but that has a runtime alignment parameter and I find it somewhat error prone since I use explicit const alignments as mentioned in my other issue. :)

sarah-quinones commented 1 year ago

with const alignments, i think you can just always pass 0 as an alignment argument but i forgot. it's been a while since i worked on this crate :sweat_smile: it's also undocumented. i'll try to find some time to make that part easier to work with

sarah-quinones commented 7 months ago

i documented the fact that you can pass in 0 as the alignment value, which will cause the vector to use the minimum possible alignment that satisfies both the type and alignment type requirements