Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Jan 16, 2026

The Bytes* traits exist because we don't have specialization1 and the generic implementation of these traits for [T] and [T; N] will operate a byte-at-a-time on byte slices and byte arrays, when efficient implementations from the cmov crate can instead perform the same operations a word-at-a-time.

Adds != 1 const size assertions for T in the impls of CtAssign, CtEq for [T] and [T; N] as well as the CtSelect impl for [T; N] which cause a compile-time error if they're requested to be used with 1-byte values:

error[E0080]: evaluation panicked: use `BytesCtEq::bytes_ct_eq` when working with byte-sized values
  --> ctutils/src/traits/ct_eq.rs:81:13
   |
81 | /             assert!(
82 | |                 size_of::<T>() != 1,
83 | |                 "use `BytesCtEq::bytes_ct_eq` when working with byte-sized values"
84 | |             );
   | |_____________^ evaluation of `<[u8] as traits::ct_eq::CtEq>::ct_eq::{constant#0}` failed here

Each of them has been configured with a message that steers the user instead towards the appropriate BytesCt* trait to use instead.

The `Bytes*` traits exist because we don't have specialization[1] and
the generic implementation of these traits for `[T]` and `[T; N]` will
operate a byte-at-a-time on byte slices and byte arrays, when efficient
implementations from the `cmov` crate can instead perform the same
operations a word-at-a-time.

Adds != 1 const size assertions for `T` in the impls of `CtAssign`,
`CtEq` for `[T]` and `[T; N]` as well as the `CtSelect` impl for
`[T; N]` which cause a compile-time error if they're requested to be
used with 1-byte values:

    error[E0080]: evaluation panicked: use `BytesCtEq::bytes_ct_eq` when working with byte-sized values
      --> ctutils/src/traits/ct_eq.rs:81:13
       |
    81 | /             assert!(
    82 | |                 size_of::<T>() != 1,
    83 | |                 "use `BytesCtEq::bytes_ct_eq` when working with byte-sized values"
    84 | |             );
       | |_____________^ evaluation of `<[u8] as traits::ct_eq::CtEq>::ct_eq::{constant#0}` failed here

Each of them has been configured with a message that steers the user
instead towards the appropriate `BytesCt*` trait to use instead.

[1]: https://rust-lang.github.io/rfcs/1210-impl-specialization.html
@tarcieri tarcieri force-pushed the ctutils/enforce-bytes-trait-usage branch from 514bdc2 to 6dc3949 Compare January 16, 2026 22:49
@tarcieri tarcieri merged commit 2fc06e6 into master Jan 16, 2026
12 checks passed
@tarcieri tarcieri deleted the ctutils/enforce-bytes-trait-usage branch January 16, 2026 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants