Skip to content

Commit 837be22

Browse files
committed
Basic derives for Parity
1 parent 7059192 commit 837be22

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/key.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ impl XOnlyPublicKey {
910910
}
911911

912912
/// Opaque type used to hold the parity passed between FFI function calls.
913+
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
913914
pub struct Parity(i32);
914915

915916
impl From<i32> for Parity {
@@ -924,6 +925,39 @@ impl From<Parity> for i32 {
924925
}
925926
}
926927

928+
#[cfg(feature = "serde")]
929+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
930+
impl ::serde::Serialize for Parity {
931+
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
932+
s.serialize_i32(self.0)
933+
}
934+
}
935+
936+
#[cfg(feature = "serde")]
937+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
938+
impl<'de> ::serde::Deserialize<'de> for Parity {
939+
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
940+
struct I32Visitor;
941+
942+
impl<'de> ::serde::de::Visitor<'de> for I32Visitor
943+
{
944+
type Value = Parity;
945+
946+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
947+
formatter.write_str("Expecting a 4 byte int i32")
948+
}
949+
950+
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
951+
where E: ::serde::de::Error
952+
{
953+
Ok(Parity::from(v))
954+
}
955+
}
956+
957+
d.deserialize_i32(I32Visitor)
958+
}
959+
}
960+
927961
impl CPtr for XOnlyPublicKey {
928962
type Target = ffi::XOnlyPublicKey;
929963
fn as_c_ptr(&self) -> *const Self::Target {

0 commit comments

Comments
 (0)