From 670da82553e9025a737f771f27b7254b05bf0fd5 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Thu, 24 Feb 2022 16:27:38 +0100 Subject: [PATCH] Implemented `TryFrom<{u8, i32}>` for `Parity` --- src/key.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/key.rs b/src/key.rs index ad552dafc..4945893c3 100644 --- a/src/key.rs +++ b/src/key.rs @@ -19,6 +19,7 @@ #[cfg(any(test, feature = "rand"))] use rand::Rng; use core::{fmt, ptr, str}; +use core::convert::TryFrom; use core::ops::BitXor; use super::{from_hex, Secp256k1}; @@ -1236,6 +1237,24 @@ impl Parity { } } +/// `Even` for `0`, `Odd` for `1`, error for anything else +impl TryFrom for Parity { + type Error = InvalidParityValue; + + fn try_from(parity: i32) -> Result { + Self::from_i32(parity) + } +} + +/// `Even` for `0`, `Odd` for `1`, error for anything else +impl TryFrom for Parity { + type Error = InvalidParityValue; + + fn try_from(parity: u8) -> Result { + Self::from_u8(parity) + } +} + /// The conversion returns `0` for even parity and `1` for odd. impl From for i32 { fn from(parity: Parity) -> i32 {