Merge rust-bitcoin/rust-secp256k1#658: Serialized signture improvements

62c839c9e0 Implement conversion traits (Martin Habovstiak)
dc3eab7aaa Implement `Borrow<[u8]>`, `PartialEq<[u8]>`, `Hash` (Martin Habovstiak)
7dac91d973 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 62c839c9e0

Tree-SHA512: 724a08af7dc915e166e3efcdc4be681a53ae14a55d9cbd62dd4c5240fa8c0f13110498d03ebb0edc1d56f969901f978aa33bae9df19376957ff7f51698ed9535
This commit is contained in:
Andrew Poelstra 2023-10-31 15:06:43 +00:00
commit fe2905d8e3
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 45 additions and 0 deletions

View File

@ -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;
@ -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];
@ -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.
///
@ -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() }
@ -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 }
}