Skip to content

Commit e4a593d

Browse files
authored
ctutils: impl From<u8> for Choice (#1309)
This is the main way `subtle` constructs `Choice` so it's important for backwards compatibility. It seems like if we had `from_u8_lsb`, we could avoid the panic condition by always masking the input `u8`. We don't yet, but this commit also leaves a comment to consider deprecating both this `From<u8>` impl and `Choice::new` in the future.
1 parent 2041dad commit e4a593d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

ctutils/src/choice.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl Choice {
4949
///
5050
/// # Panics
5151
/// - in `debug` builds, panics if the value is anything other than `0` or `1`.
52+
// TODO(tarcieri): deprecate this in favor of non-panicking constructors?
5253
#[inline]
5354
pub const fn new(value: u8) -> Self {
5455
// Compare to what should be the non-secret upper bits of the value, which should always be
@@ -388,6 +389,17 @@ impl CtSelect for Choice {
388389
}
389390
}
390391

392+
/// Create a new [`Choice`] from the given `u8` value, which MUST be either `0` or `1`.
393+
///
394+
/// # Panics
395+
/// - in `debug` builds, panics if the value is anything other than `0` or `1`.
396+
// TODO(tarcieri): deprecate this in favor of non-panicking constructors?
397+
impl From<u8> for Choice {
398+
fn from(value: u8) -> Self {
399+
Choice::new(value)
400+
}
401+
}
402+
391403
impl From<Choice> for u8 {
392404
fn from(choice: Choice) -> u8 {
393405
choice.to_u8()

0 commit comments

Comments
 (0)