Skip to content

Commit e723d80

Browse files
committed
refactor: Parse byte array instead of byte slice
Update TupleVisitor{32,33} to pass its owned byte array to the parsing function instead of a mere byte slice. This gives us more flexibility inside the parsing function.
1 parent 0ecd2a2 commit e723d80

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/key.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,10 @@ impl<'de> serde::Deserialize<'de> for SecretKey {
401401
"a hex string representing 32 byte SecretKey",
402402
))
403403
} else {
404-
let visitor = super::serde_util::Tuple32Visitor::new(
405-
"raw 32 bytes SecretKey",
406-
SecretKey::from_slice,
407-
);
404+
let visitor =
405+
super::serde_util::Tuple32Visitor::new("raw 32 bytes SecretKey", |bytes| {
406+
SecretKey::from_byte_array(&bytes)
407+
});
408408
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
409409
}
410410
}
@@ -790,10 +790,10 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
790790
"an ASCII hex string representing a public key",
791791
))
792792
} else {
793-
let visitor = super::serde_util::Tuple33Visitor::new(
794-
"33 bytes compressed public key",
795-
PublicKey::from_slice,
796-
);
793+
let visitor =
794+
super::serde_util::Tuple33Visitor::new("33 bytes compressed public key", |bytes| {
795+
PublicKey::from_byte_array_compressed(&bytes)
796+
});
797797
d.deserialize_tuple(constants::PUBLIC_KEY_SIZE, visitor)
798798
}
799799
}
@@ -1117,7 +1117,7 @@ impl<'de> serde::Deserialize<'de> for Keypair {
11171117
let ctx = Secp256k1::signing_only();
11181118

11191119
#[allow(clippy::needless_borrow)]
1120-
Keypair::from_seckey_slice(&ctx, data)
1120+
Keypair::from_seckey_slice(&ctx, &data)
11211121
});
11221122
d.deserialize_tuple(constants::SECRET_KEY_SIZE, visitor)
11231123
}
@@ -1597,7 +1597,7 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
15971597
} else {
15981598
let visitor = super::serde_util::Tuple32Visitor::new(
15991599
"raw 32 bytes schnorr public key",
1600-
XOnlyPublicKey::from_slice,
1600+
|bytes| XOnlyPublicKey::from_byte_array(&bytes),
16011601
);
16021602
d.deserialize_tuple(constants::SCHNORR_PUBLIC_KEY_SIZE, visitor)
16031603
}

src/serde_util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ macro_rules! impl_tuple_visitor {
7474

7575
impl<F, T, E> $thing<F>
7676
where
77-
F: FnOnce(&[u8]) -> Result<T, E>,
77+
F: FnOnce([u8; $len]) -> Result<T, E>,
7878
E: fmt::Display,
7979
{
8080
pub fn new(expectation: &'static str, parse_fn: F) -> Self {
@@ -84,7 +84,7 @@ macro_rules! impl_tuple_visitor {
8484

8585
impl<'de, F, T, E> de::Visitor<'de> for $thing<F>
8686
where
87-
F: FnOnce(&[u8]) -> Result<T, E>,
87+
F: FnOnce([u8; $len]) -> Result<T, E>,
8888
E: fmt::Display,
8989
{
9090
type Value = T;
@@ -106,7 +106,7 @@ macro_rules! impl_tuple_visitor {
106106
return Err(de::Error::invalid_length(i, &self));
107107
}
108108
}
109-
(self.parse_fn)(&bytes).map_err(de::Error::custom)
109+
(self.parse_fn)(bytes).map_err(de::Error::custom)
110110
}
111111
}
112112
};

0 commit comments

Comments
 (0)