Skip to content

Commit

Permalink
Merge #658: Serialized signture improvements
Browse files Browse the repository at this point in the history
62c839c Implement conversion traits (Martin Habovstiak)
dc3eab7 Implement `Borrow<[u8]>`, `PartialEq<[u8]>`, `Hash` (Martin Habovstiak)
7dac91d Deprecate `capacity` and `is_empty` (Martin Habovstiak)

Pull request description:

  This deprecates methods returning constants and impls a few traits.

ACKs for top commit:
  apoelstra:
    ACK 62c839c

Tree-SHA512: 724a08af7dc915e166e3efcdc4be681a53ae14a55d9cbd62dd4c5240fa8c0f13110498d03ebb0edc1d56f969901f978aa33bae9df19376957ff7f51698ed9535
  • Loading branch information
apoelstra committed Oct 31, 2023
2 parents 902150c + 62c839c commit fe2905d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/ecdsa/serialized_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//! unable to run on platforms without allocator. We implement a special type to encapsulate
//! 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;
Expand Down Expand Up @@ -41,11 +43,30 @@ impl PartialEq for SerializedSignature {
fn eq(&self, other: &SerializedSignature) -> bool { **self == **other }
}

impl PartialEq<[u8]> for SerializedSignature {
#[inline]
fn eq(&self, other: &[u8]) -> bool { **self == *other }
}

impl PartialEq<SerializedSignature> for [u8] {
#[inline]
fn eq(&self, other: &SerializedSignature) -> bool { *self == **other }
}

impl core::hash::Hash for SerializedSignature {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { (**self).hash(state) }
}

impl AsRef<[u8]> for SerializedSignature {
#[inline]
fn as_ref(&self) -> &[u8] { self }
}

impl Borrow<[u8]> for SerializedSignature {
#[inline]
fn borrow(&self) -> &[u8] { self }
}

impl ops::Deref for SerializedSignature {
type Target = [u8];

Expand All @@ -71,6 +92,28 @@ impl<'a> IntoIterator for &'a SerializedSignature {
fn into_iter(self) -> Self::IntoIter { self.iter() }
}

impl From<Signature> 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<SerializedSignature> for Signature {
type Error = Error;

fn try_from(value: SerializedSignature) -> Result<Self, Self::Error> { value.to_signature() }
}

impl<'a> TryFrom<&'a SerializedSignature> for Signature {
type Error = Error;

fn try_from(value: &'a SerializedSignature) -> Result<Self, Self::Error> {
value.to_signature()
}
}

impl SerializedSignature {
/// Creates `SerializedSignature` from data and length.
///
Expand All @@ -84,6 +127,7 @@ impl SerializedSignature {
}

/// Get the capacity of the underlying data buffer.
#[deprecated = "This always returns 72"]
#[inline]
pub fn capacity(&self) -> usize { self.data.len() }

Expand All @@ -106,6 +150,7 @@ impl SerializedSignature {
pub fn from_signature(sig: &Signature) -> SerializedSignature { sig.serialize_der() }

/// Check if the space is zero.
#[deprecated = "This always returns false"]
#[inline]
pub fn is_empty(&self) -> bool { self.len() == 0 }
}
Expand Down

0 comments on commit fe2905d

Please sign in to comment.