diff --git a/ecdsa/src/recovery.rs b/ecdsa/src/recovery.rs index 0069dbc8..c923bbad 100644 --- a/ecdsa/src/recovery.rs +++ b/ecdsa/src/recovery.rs @@ -40,6 +40,15 @@ impl RecoveryId { (self.0 & 1) != 0 } + /// Convert a `u8` into a [`RecoveryId`]. + pub const fn from_byte(byte: u8) -> Option { + if byte <= Self::MAX { + Some(Self(byte)) + } else { + None + } + } + /// Convert this [`RecoveryId`] into a `u8`. pub const fn to_byte(self) -> u8 { self.0 @@ -50,11 +59,7 @@ impl TryFrom for RecoveryId { type Error = Error; fn try_from(byte: u8) -> Result { - if byte <= Self::MAX { - Ok(Self(byte)) - } else { - Err(Error::new()) - } + Self::from_byte(byte).ok_or_else(Error::new) } }