Basic derives for Parity

This commit is contained in:
sanket1729 2022-01-06 22:47:26 +05:30
parent 7059192de9
commit 837be22e09
1 changed files with 34 additions and 0 deletions

View File

@ -910,6 +910,7 @@ impl XOnlyPublicKey {
}
/// Opaque type used to hold the parity passed between FFI function calls.
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
pub struct Parity(i32);
impl From<i32> for Parity {
@ -924,6 +925,39 @@ impl From<Parity> for i32 {
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl ::serde::Serialize for Parity {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_i32(self.0)
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> ::serde::Deserialize<'de> for Parity {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
struct I32Visitor;
impl<'de> ::serde::de::Visitor<'de> for I32Visitor
{
type Value = Parity;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Expecting a 4 byte int i32")
}
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where E: ::serde::de::Error
{
Ok(Parity::from(v))
}
}
d.deserialize_i32(I32Visitor)
}
}
impl CPtr for XOnlyPublicKey {
type Target = ffi::XOnlyPublicKey;
fn as_c_ptr(&self) -> *const Self::Target {