Skip to content

Commit

Permalink
ecdsa: add RecoveryId::from_byte (#531)
Browse files Browse the repository at this point in the history
Adds a `const fn` by-value constructor
  • Loading branch information
tarcieri authored Sep 7, 2022
1 parent 923f8a1 commit eab0ed6
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ecdsa/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ impl RecoveryId {
(self.0 & 1) != 0
}

/// Convert a `u8` into a [`RecoveryId`].
pub const fn from_byte(byte: u8) -> Option<Self> {
if byte <= Self::MAX {
Some(Self(byte))
} else {
None
}
}

/// Convert this [`RecoveryId`] into a `u8`.
pub const fn to_byte(self) -> u8 {
self.0
Expand All @@ -50,11 +59,7 @@ impl TryFrom<u8> for RecoveryId {
type Error = Error;

fn try_from(byte: u8) -> Result<Self> {
if byte <= Self::MAX {
Ok(Self(byte))
} else {
Err(Error::new())
}
Self::from_byte(byte).ok_or_else(Error::new)
}
}

Expand Down

0 comments on commit eab0ed6

Please sign in to comment.