@@ -224,8 +224,28 @@ pub trait PkTranslate<P: MiniscriptKey, Q: MiniscriptKey> {
224
224
Fpk : FnMut ( & P ) -> Result < Q , E > ,
225
225
Fpkh : FnMut ( & P :: Hash ) -> Result < Q :: Hash , E > ,
226
226
Q : MiniscriptKey ;
227
+
228
+ /// Calls `translate_pk` with conversion functions that cannot fail
229
+ fn translate_pk_infallible < Fpk , Fpkh > (
230
+ & self ,
231
+ mut translatefpk : Fpk ,
232
+ mut translatefpkh : Fpkh ,
233
+ ) -> Self :: Output
234
+ where
235
+ Fpk : FnMut ( & P ) -> Q ,
236
+ Fpkh : FnMut ( & P :: Hash ) -> Q :: Hash ,
237
+ Q : MiniscriptKey ,
238
+ {
239
+ self . translate_pk :: < _ , _ , ( ) > ( |pk| Ok ( translatefpk ( pk) ) , |pkh| Ok ( translatefpkh ( pkh) ) )
240
+ . expect ( "infallible translation function" )
241
+ }
227
242
}
228
243
244
+ // There are some additional convenience functions we could add to `PkTranslate`
245
+ // for the common special cases where `P::Hash == Q::Hash`, or when `P == P::Hash`,
246
+ // but these are blocked on https://github.com/rust-lang/rust/issues/20041 which
247
+ // is unlikely to arrive in Rust very soon, as of Jan 2021. Should revisit this.
248
+
229
249
/// Script descriptor
230
250
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord ) ]
231
251
pub enum Descriptor < Pk : MiniscriptKey > {
@@ -548,11 +568,10 @@ where
548
568
impl Descriptor < DescriptorPublicKey > {
549
569
/// Derives all wildcard keys in the descriptor using the supplied `child_number`
550
570
pub fn derive ( & self , child_number : bip32:: ChildNumber ) -> Descriptor < DescriptorPublicKey > {
551
- self . translate_pk (
552
- |pk| Result :: Ok :: < DescriptorPublicKey , ( ) > ( pk. clone ( ) . derive ( child_number) ) ,
553
- |pk| Result :: Ok :: < DescriptorPublicKey , ( ) > ( pk. clone ( ) . derive ( child_number) ) ,
571
+ self . translate_pk_infallible (
572
+ |pk| pk. clone ( ) . derive ( child_number) ,
573
+ |pk| pk. clone ( ) . derive ( child_number) ,
554
574
)
555
- . expect ( "Translation fn can't fail." )
556
575
}
557
576
558
577
/// Parse a descriptor that may contain secret keys
0 commit comments