[WIP] ctutils: automatically use optimized 1-byte CtEq#1369
Conversation
c4536aa to
62bb328
Compare
As an alternative to the `BytesCtEq` approach to using the optimized impl of `CmovEq` for `[u8]`, this implementation automatically uses it for all 1-byte types when the `CtEq` impl for `[T]` is invoked (or anything that calls it, like the impls on `[T; N]`, `Box<[T]>`, and `Vec<T>`. To ensure we're not casting from a slice of a type containing uninitialized memory to `[u8]`, this bounds all such `CtEq` impls on a newly introduced `unsafe trait NoUninit`, which is currently not exposed in the public API except through the bounds. The trait has been impl'd for all of the types we impl other traits for in this crate.
62bb328 to
bcc517f
Compare
| #[cfg(feature = "alloc")] | ||
| unsafe impl<T: NoUninit> NoUninit for Box<T> {} | ||
| #[cfg(feature = "alloc")] | ||
| unsafe impl<T: NoUninit> NoUninit for Vec<T> {} |
There was a problem hiding this comment.
This impl is not sound, vec does not have layout guarantees and there could be padding under -Zrandomize-layout for example
| unsafe impl<T: NoUninit, const N: usize> NoUninit for [T; N] {} | ||
|
|
||
| #[cfg(feature = "alloc")] | ||
| unsafe impl<T: NoUninit> NoUninit for Box<T> {} |
There was a problem hiding this comment.
For Box it should be fine since I think it's guaranteed that it's just a pointer, but you don't even need the T: NoUninit bound here :). the implicit T: Sized matters though since fat pointer layout isn't guaranteed
|
I would probably recommend copying the impls from |
|
I'm going to go a completely different direction with this.
Since this approach restricts the types that can be used with the impl anyway, we can just delegate to |
|
In case anyone is curious, here's the alternative approach which avoids (any additional) unsafe code entirely: #1376 Instead of having a generic implementation that tries to use Perhaps in a future with specialization, we could eventually add the generic impls back, but in the meantime they have been retained as generic free functions users can use to write their own trait impls for concrete types. |
As an alternative to the
BytesCtEqapproach to using the optimized impl ofCmovEqfor[u8], this implementation automatically uses it for all 1-byte types when theCtEqimpl for[T]is invoked (or anything that calls it, like the impls on[T; N],Box<[T]>, andVec<T>.To ensure we're not casting from a slice of a type containing uninitialized memory to
[u8], this bounds all suchCtEqimpls on a newly introducedunsafe trait NoUninit, which is currently not exposed in the public API except through the bounds. The trait has been impl'd for all of the types we impl other traits for in this crate.