@@ -5,7 +5,7 @@ use either::Either;
5
5
use hex_conservative:: DisplayHex ;
6
6
use miniscript:: iter:: { Tree , TreeLike } ;
7
7
use simplicity:: types:: Final as SimType ;
8
- use simplicity:: { BitCollector , Value as SimValue } ;
8
+ use simplicity:: { BitCollector , Value as SimValue , ValueRef } ;
9
9
10
10
use crate :: array:: { BTreeSlice , Combiner , Partition , Unfolder } ;
11
11
use crate :: error:: { Error , RichError , WithSpan } ;
@@ -730,11 +730,11 @@ impl Value {
730
730
pub fn reconstruct ( value : & StructuralValue , ty : & ResolvedType ) -> Option < Self > {
731
731
let mut output = vec ! [ ] ;
732
732
for data in value. destruct ( ty) . post_order_iter ( ) {
733
+ let size = data. node . n_children ( ) ;
733
734
let ( value, ty) = match data. node {
734
735
Destructor :: Ok { value, ty } => ( value, ty) ,
735
736
Destructor :: WrongType => return None ,
736
737
} ;
737
- let size = data. node . n_children ( ) ;
738
738
match ty. as_inner ( ) {
739
739
TypeInner :: Boolean => {
740
740
let bit = destruct:: as_bit ( value) ?;
@@ -877,10 +877,10 @@ impl TreeLike for StructuralValue {
877
877
fn as_node ( & self ) -> Tree < Self > {
878
878
use simplicity:: dag:: { Dag , DagLike } ;
879
879
880
- match ( & self . 0 ) . as_dag_node ( ) {
880
+ match self . 0 . as_ref ( ) . as_dag_node ( ) {
881
881
Dag :: Nullary => Tree :: Nullary ,
882
- Dag :: Unary ( l) => Tree :: Unary ( Self ( l. shallow_clone ( ) ) ) ,
883
- Dag :: Binary ( l, r) => Tree :: Binary ( Self ( l. shallow_clone ( ) ) , Self ( r. shallow_clone ( ) ) ) ,
882
+ Dag :: Unary ( l) => Tree :: Unary ( Self ( l. to_value ( ) ) ) ,
883
+ Dag :: Binary ( l, r) => Tree :: Binary ( Self ( l. to_value ( ) ) , Self ( r. to_value ( ) ) ) ,
884
884
}
885
885
}
886
886
}
@@ -1059,7 +1059,7 @@ impl StructuralValue {
1059
1059
1060
1060
fn destruct < ' a > ( & ' a self , ty : & ' a ResolvedType ) -> Destructor < ' a > {
1061
1061
Destructor :: Ok {
1062
- value : self . as_ref ( ) ,
1062
+ value : self . 0 . as_ref ( ) ,
1063
1063
ty,
1064
1064
}
1065
1065
}
@@ -1091,30 +1091,30 @@ impl StructuralValue {
1091
1091
/// The leaf values (Boolean, unsigned integer, empty tuple, empty array) are not checked.
1092
1092
/// Extraction of actual Simfony values (Boolean, unsigned integer, ...)
1093
1093
/// from the leaf Simplicity values may fail, in which case the entire tree is, again, ill-typed.
1094
- #[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
1094
+ #[ derive( Clone , Debug ) ]
1095
1095
enum Destructor < ' a > {
1096
1096
Ok {
1097
- value : & ' a SimValue ,
1097
+ value : ValueRef < ' a > ,
1098
1098
ty : & ' a ResolvedType ,
1099
1099
} ,
1100
1100
WrongType ,
1101
1101
}
1102
1102
1103
1103
impl < ' a > Destructor < ' a > {
1104
1104
/// Create a destructor for the given Simplicity `value` and the given Simfony type.
1105
- pub const fn new ( value : & ' a SimValue , ty : & ' a ResolvedType ) -> Self {
1105
+ pub const fn new ( value : ValueRef < ' a > , ty : & ' a ResolvedType ) -> Self {
1106
1106
Self :: Ok { value, ty }
1107
1107
}
1108
1108
1109
- const fn new_pair ( ( value, ty) : ( & ' a SimValue , & ' a ResolvedType ) ) -> Self {
1109
+ const fn new_pair ( ( value, ty) : ( ValueRef < ' a > , & ' a ResolvedType ) ) -> Self {
1110
1110
Self :: Ok { value, ty }
1111
1111
}
1112
1112
}
1113
1113
1114
1114
impl TreeLike for Destructor < ' _ > {
1115
1115
fn as_node ( & self ) -> Tree < Self > {
1116
1116
let ( value, ty) = match self {
1117
- Self :: Ok { value, ty } => ( value, ty) ,
1117
+ Self :: Ok { value, ty } => ( value. clone ( ) , ty) ,
1118
1118
Self :: WrongType => return Tree :: Nullary ,
1119
1119
} ;
1120
1120
match ty. as_inner ( ) {
@@ -1166,8 +1166,9 @@ impl TreeLike for Destructor<'_> {
1166
1166
/// Functions for destructing Simplicity values alongside Simfony types.
1167
1167
mod destruct {
1168
1168
use super :: * ;
1169
+ use simplicity:: ValueRef ;
1169
1170
1170
- pub fn as_bit ( value : & SimValue ) -> Option < bool > {
1171
+ pub fn as_bit ( value : ValueRef ) -> Option < bool > {
1171
1172
match value. as_left ( ) {
1172
1173
Some ( unit) if unit. is_unit ( ) => Some ( false ) ,
1173
1174
_ => match value. as_right ( ) {
@@ -1177,10 +1178,10 @@ mod destruct {
1177
1178
}
1178
1179
}
1179
1180
1180
- pub fn as_integer ( value : & SimValue , ty : UIntType ) -> Option < UIntValue > {
1181
+ pub fn as_integer ( value : ValueRef , ty : UIntType ) -> Option < UIntValue > {
1181
1182
let bit_len = ty. bit_width ( ) . get ( ) ;
1182
1183
let unfolder = Unfolder :: new ( value, bit_len) ;
1183
- let bit_values = unfolder. unfold ( SimValue :: as_product) ?;
1184
+ let bit_values = unfolder. unfold ( |v| v . as_product ( ) ) ?;
1184
1185
let ( bytes, written_bits) = bit_values. into_iter ( ) . filter_map ( as_bit) . collect_bits ( ) ;
1185
1186
if bit_len != written_bits {
1186
1187
return None ;
@@ -1196,31 +1197,31 @@ mod destruct {
1196
1197
}
1197
1198
}
1198
1199
1199
- pub fn as_tuple ( value : & SimValue , size : usize ) -> Option < Vec < & SimValue > > {
1200
- Unfolder :: new ( value, size) . unfold ( SimValue :: as_product)
1200
+ pub fn as_tuple ( value : ValueRef , size : usize ) -> Option < Vec < ValueRef > > {
1201
+ Unfolder :: new ( value, size) . unfold ( |v| v . as_product ( ) )
1201
1202
}
1202
1203
1203
- pub fn as_array ( value : & SimValue , size : usize ) -> Option < Vec < & SimValue > > {
1204
- Unfolder :: new ( value, size) . unfold ( SimValue :: as_product)
1204
+ pub fn as_array ( value : ValueRef , size : usize ) -> Option < Vec < ValueRef > > {
1205
+ Unfolder :: new ( value, size) . unfold ( |v| v . as_product ( ) )
1205
1206
}
1206
1207
1207
- pub fn as_list < ' a > ( value : & ' a SimValue , bound : NonZeroPow2Usize ) -> Option < Vec < & ' a SimValue > > {
1208
- let as_block = |value : & ' a SimValue , size : usize | match as_option ( value) {
1208
+ pub fn as_list < ' a > ( value : ValueRef < ' a > , bound : NonZeroPow2Usize ) -> Option < Vec < ValueRef < ' a > > > {
1209
+ let as_block = |value : ValueRef < ' a > , size : usize | match as_option ( value) {
1209
1210
Some ( Some ( folded) ) => as_array ( folded, size) ,
1210
1211
Some ( None ) => Some ( vec ! [ ] ) ,
1211
1212
None => None ,
1212
1213
} ;
1213
- Combiner :: new ( value, bound) . unfold ( as_block, SimValue :: as_product)
1214
+ Combiner :: new ( value, bound) . unfold ( as_block, |v| v . as_product ( ) )
1214
1215
}
1215
1216
1216
- pub fn as_either ( value : & SimValue ) -> Option < Either < & SimValue , & SimValue > > {
1217
+ pub fn as_either ( value : ValueRef ) -> Option < Either < ValueRef , ValueRef > > {
1217
1218
match value. as_left ( ) {
1218
1219
Some ( inner) => Some ( Either :: Left ( inner) ) ,
1219
1220
None => value. as_right ( ) . map ( Either :: Right ) ,
1220
1221
}
1221
1222
}
1222
1223
1223
- pub fn as_option ( value : & SimValue ) -> Option < Option < & SimValue > > {
1224
+ pub fn as_option ( value : ValueRef ) -> Option < Option < ValueRef > > {
1224
1225
match value. as_left ( ) {
1225
1226
Some ( inner) if inner. is_unit ( ) => Some ( None ) ,
1226
1227
_ => value. as_right ( ) . map ( Some ) ,
0 commit comments