From 62c839c9e03b5cacb7da34aecff6eefc29f8f293 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 31 Oct 2023 14:32:09 +0100 Subject: [PATCH] Implement conversion traits Converting signature to serialized signature and back is natural, so the conversion traits should be implemented. --- src/ecdsa/serialized_signature.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/ecdsa/serialized_signature.rs b/src/ecdsa/serialized_signature.rs index 1aba65f..bfd035f 100644 --- a/src/ecdsa/serialized_signature.rs +++ b/src/ecdsa/serialized_signature.rs @@ -8,6 +8,7 @@ //! serialized signatures and since it's a bit more complicated it has its own module. use core::borrow::Borrow; +use core::convert::TryFrom; use core::{fmt, ops}; pub use into_iter::IntoIter; @@ -91,6 +92,28 @@ impl<'a> IntoIterator for &'a SerializedSignature { fn into_iter(self) -> Self::IntoIter { self.iter() } } +impl From for SerializedSignature { + fn from(value: Signature) -> Self { Self::from_signature(&value) } +} + +impl<'a> From<&'a Signature> for SerializedSignature { + fn from(value: &'a Signature) -> Self { Self::from_signature(value) } +} + +impl TryFrom for Signature { + type Error = Error; + + fn try_from(value: SerializedSignature) -> Result { value.to_signature() } +} + +impl<'a> TryFrom<&'a SerializedSignature> for Signature { + type Error = Error; + + fn try_from(value: &'a SerializedSignature) -> Result { + value.to_signature() + } +} + impl SerializedSignature { /// Creates `SerializedSignature` from data and length. ///