Skip to content

Commit fe2905d

Browse files
committed
Merge #658: Serialized signture improvements
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
2 parents 902150c + 62c839c commit fe2905d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/ecdsa/serialized_signature.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//! unable to run on platforms without allocator. We implement a special type to encapsulate
88
//! serialized signatures and since it's a bit more complicated it has its own module.
99
10+
use core::borrow::Borrow;
11+
use core::convert::TryFrom;
1012
use core::{fmt, ops};
1113

1214
pub use into_iter::IntoIter;
@@ -41,11 +43,30 @@ impl PartialEq for SerializedSignature {
4143
fn eq(&self, other: &SerializedSignature) -> bool { **self == **other }
4244
}
4345

46+
impl PartialEq<[u8]> for SerializedSignature {
47+
#[inline]
48+
fn eq(&self, other: &[u8]) -> bool { **self == *other }
49+
}
50+
51+
impl PartialEq<SerializedSignature> for [u8] {
52+
#[inline]
53+
fn eq(&self, other: &SerializedSignature) -> bool { *self == **other }
54+
}
55+
56+
impl core::hash::Hash for SerializedSignature {
57+
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { (**self).hash(state) }
58+
}
59+
4460
impl AsRef<[u8]> for SerializedSignature {
4561
#[inline]
4662
fn as_ref(&self) -> &[u8] { self }
4763
}
4864

65+
impl Borrow<[u8]> for SerializedSignature {
66+
#[inline]
67+
fn borrow(&self) -> &[u8] { self }
68+
}
69+
4970
impl ops::Deref for SerializedSignature {
5071
type Target = [u8];
5172

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

95+
impl From<Signature> for SerializedSignature {
96+
fn from(value: Signature) -> Self { Self::from_signature(&value) }
97+
}
98+
99+
impl<'a> From<&'a Signature> for SerializedSignature {
100+
fn from(value: &'a Signature) -> Self { Self::from_signature(value) }
101+
}
102+
103+
impl TryFrom<SerializedSignature> for Signature {
104+
type Error = Error;
105+
106+
fn try_from(value: SerializedSignature) -> Result<Self, Self::Error> { value.to_signature() }
107+
}
108+
109+
impl<'a> TryFrom<&'a SerializedSignature> for Signature {
110+
type Error = Error;
111+
112+
fn try_from(value: &'a SerializedSignature) -> Result<Self, Self::Error> {
113+
value.to_signature()
114+
}
115+
}
116+
74117
impl SerializedSignature {
75118
/// Creates `SerializedSignature` from data and length.
76119
///
@@ -84,6 +127,7 @@ impl SerializedSignature {
84127
}
85128

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

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

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

0 commit comments

Comments
 (0)