Skip to content

Commit a3aa0d9

Browse files
committed
Merge #728: Feature gate the Keypair::FromStr impl
d600a6c Feature gate the Keypair::FromStr impl (Tobin C. Harding) Pull request description: Currently we are panicing if neither `global-context` or `alloc` features are enabled. We do not need to do so, we can just disable the whole impl of `FromStr`. This was pulled out of #699. ACKs for top commit: apoelstra: ACK d600a6c successfully ran local tests Kixunil: ACK d600a6c Tree-SHA512: 940bec95ce732b4bc482e23da114cb03b767780f93777621c9d0985d1288e36756bdf6f050172eac00f89b6f39aa0efdb30cc77425b6f87505659c8c012981ca
2 parents fb188dd + d600a6c commit a3aa0d9

File tree

1 file changed

+2
-15
lines changed

1 file changed

+2
-15
lines changed

src/key.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ impl<'a> From<&'a Keypair> for PublicKey {
10001000
fn from(pair: &'a Keypair) -> Self { PublicKey::from_keypair(pair) }
10011001
}
10021002

1003+
#[cfg(any(feature = "global-context", feature = "alloc"))]
10031004
impl str::FromStr for Keypair {
10041005
type Err = Error;
10051006

@@ -1011,9 +1012,6 @@ impl str::FromStr for Keypair {
10111012
#[cfg(all(not(feature = "global-context"), feature = "alloc"))]
10121013
let ctx = Secp256k1::signing_only();
10131014

1014-
#[cfg(not(any(feature = "global-context", feature = "alloc")))]
1015-
let ctx: Secp256k1<crate::SignOnlyPreallocated> = panic!("The previous implementation was panicking too, please enable the global-context feature of rust-secp256k1");
1016-
10171015
#[allow(clippy::needless_borrow)]
10181016
Keypair::from_seckey_str(&ctx, s)
10191017
}
@@ -1040,7 +1038,7 @@ impl serde::Serialize for Keypair {
10401038

10411039
#[cfg(feature = "serde")]
10421040
#[allow(unused_variables)] // For `data` under some feature combinations (the unconditional panic below).
1043-
#[allow(unreachable_code)] // For `Keypair::from_seckey_slice` after unconditional panic.
1041+
#[cfg(all(feature = "serde", any(feature = "global-context", feature = "alloc")))]
10441042
impl<'de> serde::Deserialize<'de> for Keypair {
10451043
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
10461044
if d.is_human_readable() {
@@ -1055,9 +1053,6 @@ impl<'de> serde::Deserialize<'de> for Keypair {
10551053
#[cfg(all(not(feature = "global-context"), feature = "alloc"))]
10561054
let ctx = Secp256k1::signing_only();
10571055

1058-
#[cfg(not(any(feature = "global-context", feature = "alloc")))]
1059-
let ctx: Secp256k1<crate::SignOnlyPreallocated> = panic!("cannot deserialize key pair without a context (please enable either the global-context or alloc feature)");
1060-
10611056
#[allow(clippy::needless_borrow)]
10621057
Keypair::from_seckey_slice(&ctx, data)
10631058
});
@@ -2423,14 +2418,6 @@ mod test {
24232418
.collect::<Vec<_>>();
24242419
serde_test::assert_tokens(&keypair.compact(), &tokens);
24252420
}
2426-
2427-
#[test]
2428-
#[should_panic(expected = "The previous implementation was panicking too")]
2429-
#[cfg(not(any(feature = "alloc", feature = "global-context")))]
2430-
fn test_parse_keypair_no_alloc_panic() {
2431-
let key_hex = "4242424242424242424242424242424242424242424242424242424242424242";
2432-
let _: Keypair = key_hex.parse().expect("We shouldn't even get this far");
2433-
}
24342421
}
24352422

24362423
#[cfg(bench)]

0 commit comments

Comments
 (0)