6
6
//! "abstract" is a reserved keyword in Rust.
7
7
8
8
use core:: { fmt, str} ;
9
- #[ cfg( feature = "std" ) ]
10
- use std:: error;
11
9
12
10
use bitcoin:: { absolute, relative} ;
13
11
@@ -50,32 +48,6 @@ pub enum Policy<Pk: MiniscriptKey> {
50
48
Thresh ( Threshold < Arc < Policy < Pk > > , 0 > ) ,
51
49
}
52
50
53
- /// Detailed error type for concrete policies.
54
- #[ derive( Copy , Clone , PartialEq , Eq , Debug , Hash ) ]
55
- pub enum PolicyError {
56
- /// Entailment max terminals exceeded.
57
- EntailmentMaxTerminals ,
58
- }
59
-
60
- impl fmt:: Display for PolicyError {
61
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
62
- match * self {
63
- PolicyError :: EntailmentMaxTerminals => {
64
- write ! ( f, "Policy entailment only supports {} terminals" , ENTAILMENT_MAX_TERMINALS )
65
- }
66
- }
67
- }
68
- }
69
-
70
- #[ cfg( feature = "std" ) ]
71
- impl error:: Error for PolicyError {
72
- fn cause ( & self ) -> Option < & dyn error:: Error > {
73
- match self {
74
- PolicyError :: EntailmentMaxTerminals => None ,
75
- }
76
- }
77
- }
78
-
79
51
impl < Pk : MiniscriptKey > ForEachKey < Pk > for Policy < Pk > {
80
52
fn for_each_key < ' a , F : FnMut ( & ' a Pk ) -> bool > ( & ' a self , mut pred : F ) -> bool {
81
53
self . pre_order_iter ( ) . all ( |policy| match policy {
@@ -164,17 +136,20 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
164
136
///
165
137
/// This implementation will run slowly for larger policies but should be
166
138
/// sufficient for most practical policies.
139
+ ///
140
+ /// Returns None for very large policies for which entailment cannot
141
+ /// be practically computed.
167
142
// This algorithm has a naive implementation. It is possible to optimize this
168
143
// by memoizing and maintaining a hashmap.
169
- pub fn entails ( self , other : Policy < Pk > ) -> Result < bool , PolicyError > {
144
+ pub fn entails ( self , other : Policy < Pk > ) -> Option < bool > {
170
145
if self . n_terminals ( ) > ENTAILMENT_MAX_TERMINALS {
171
- return Err ( PolicyError :: EntailmentMaxTerminals ) ;
146
+ return None ;
172
147
}
173
148
match ( self , other) {
174
- ( Policy :: Unsatisfiable , _) => Ok ( true ) ,
175
- ( Policy :: Trivial , Policy :: Trivial ) => Ok ( true ) ,
176
- ( Policy :: Trivial , _) => Ok ( false ) ,
177
- ( _, Policy :: Unsatisfiable ) => Ok ( false ) ,
149
+ ( Policy :: Unsatisfiable , _) => Some ( true ) ,
150
+ ( Policy :: Trivial , Policy :: Trivial ) => Some ( true ) ,
151
+ ( Policy :: Trivial , _) => Some ( false ) ,
152
+ ( _, Policy :: Unsatisfiable ) => Some ( false ) ,
178
153
( a, b) => {
179
154
let ( a_norm, b_norm) = ( a. normalized ( ) , b. normalized ( ) ) ;
180
155
let first_constraint = a_norm. first_constraint ( ) ;
@@ -186,7 +161,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
186
161
a_norm. satisfy_constraint ( & first_constraint, false ) ,
187
162
b_norm. satisfy_constraint ( & first_constraint, false ) ,
188
163
) ;
189
- Ok ( Policy :: entails ( a1, b1) ? && Policy :: entails ( a2, b2) ?)
164
+ Some ( Policy :: entails ( a1, b1) ? && Policy :: entails ( a2, b2) ?)
190
165
}
191
166
}
192
167
}
0 commit comments