byte-slice-cast icon indicating copy to clipboard operation
byte-slice-cast copied to clipboard

Tuple support

Open andreiltd opened this issue 4 years ago • 5 comments

Would you be willing to pull the patch that adds support for tuples? It looks like the code is good to go and it is only a matter of adding impl_trait!((T, T)) for each type and then testing it.

I wouldn't go deep with the size of tuples and only implement pairs which I think is the most common case.

andreiltd avatar Oct 05 '21 08:10 andreiltd

Unfortunately the memory layout for tuples is not defined. While in practice e.g. a (u8, u8) is the same memory layout as [u8; 2] this is in no way guaranteed (and also the order in which it is stored in memory might be different) and can break in future Rust versions unless it is defined at some point.

sdroege avatar Oct 05 '21 08:10 sdroege

I see, then there is no way to implement that reliably for now. Thanks for the explanation.

andreiltd avatar Oct 05 '21 08:10 andreiltd

This could be fixed by submitting a Rust RFC that defines that the memory layout of homogeneous tuples is equivalent to the corresponding array. I don't think there's any practical reason to not define it like that other than nobody having spent time on that yet.

sdroege avatar Oct 05 '21 08:10 sdroege

https://github.com/rust-lang/unsafe-code-guidelines/issues/36

andreiltd avatar Oct 05 '21 13:10 andreiltd

The support for primitive arrays from https://github.com/sdroege/byte-slice-cast/pull/18 can kind of help here. Instead of working with e.g. (f32, f32) you could work with [f32; 2].

sdroege avatar Oct 19 '21 07:10 sdroege