@@ -2,10 +2,10 @@ use std::fmt;
22use std:: sync:: Arc ;
33
44use either:: Either ;
5- use hex_conservative :: DisplayHex ;
5+ use miniscript :: bitcoin :: hex :: DisplayHex ;
66use miniscript:: iter:: { Tree , TreeLike } ;
77use simplicity:: types:: Final as SimType ;
8- use simplicity:: { BitCollector , Value as SimValue } ;
8+ use simplicity:: { BitCollector , Value as SimValue , ValueRef } ;
99
1010use crate :: array:: { BTreeSlice , Combiner , Partition , Unfolder } ;
1111use crate :: error:: { Error , RichError , WithSpan } ;
@@ -623,7 +623,7 @@ impl Value {
623623
624624 /// Create a value from the given `hexadecimal` string and type.
625625 pub fn parse_hexadecimal ( hexadecimal : & Hexadecimal , ty : & ResolvedType ) -> Result < Self , Error > {
626- use hex_conservative :: FromHex ;
626+ use miniscript :: bitcoin :: hex :: FromHex ;
627627
628628 let expected_byte_len = match ty. as_inner ( ) {
629629 TypeInner :: UInt ( int) => int. byte_width ( ) ,
@@ -730,11 +730,11 @@ impl Value {
730730 pub fn reconstruct ( value : & StructuralValue , ty : & ResolvedType ) -> Option < Self > {
731731 let mut output = vec ! [ ] ;
732732 for data in value. destruct ( ty) . post_order_iter ( ) {
733+ let size = data. node . n_children ( ) ;
733734 let ( value, ty) = match data. node {
734735 Destructor :: Ok { value, ty } => ( value, ty) ,
735736 Destructor :: WrongType => return None ,
736737 } ;
737- let size = data. node . n_children ( ) ;
738738 match ty. as_inner ( ) {
739739 TypeInner :: Boolean => {
740740 let bit = destruct:: as_bit ( value) ?;
@@ -877,10 +877,10 @@ impl TreeLike for StructuralValue {
877877 fn as_node ( & self ) -> Tree < Self > {
878878 use simplicity:: dag:: { Dag , DagLike } ;
879879
880- match ( & self . 0 ) . as_dag_node ( ) {
880+ match self . 0 . as_ref ( ) . as_dag_node ( ) {
881881 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 ( ) ) ) ,
884884 }
885885 }
886886}
@@ -1059,7 +1059,7 @@ impl StructuralValue {
10591059
10601060 fn destruct < ' a > ( & ' a self , ty : & ' a ResolvedType ) -> Destructor < ' a > {
10611061 Destructor :: Ok {
1062- value : self . as_ref ( ) ,
1062+ value : self . 0 . as_ref ( ) ,
10631063 ty,
10641064 }
10651065 }
@@ -1091,30 +1091,30 @@ impl StructuralValue {
10911091/// The leaf values (Boolean, unsigned integer, empty tuple, empty array) are not checked.
10921092/// Extraction of actual Simfony values (Boolean, unsigned integer, ...)
10931093/// 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 ) ]
10951095enum Destructor < ' a > {
10961096 Ok {
1097- value : & ' a SimValue ,
1097+ value : ValueRef < ' a > ,
10981098 ty : & ' a ResolvedType ,
10991099 } ,
11001100 WrongType ,
11011101}
11021102
11031103impl < ' a > Destructor < ' a > {
11041104 /// 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 {
11061106 Self :: Ok { value, ty }
11071107 }
11081108
1109- const fn new_pair ( ( value, ty) : ( & ' a SimValue , & ' a ResolvedType ) ) -> Self {
1109+ const fn new_pair ( ( value, ty) : ( ValueRef < ' a > , & ' a ResolvedType ) ) -> Self {
11101110 Self :: Ok { value, ty }
11111111 }
11121112}
11131113
11141114impl TreeLike for Destructor < ' _ > {
11151115 fn as_node ( & self ) -> Tree < Self > {
11161116 let ( value, ty) = match self {
1117- Self :: Ok { value, ty } => ( value, ty) ,
1117+ Self :: Ok { value, ty } => ( value. clone ( ) , ty) ,
11181118 Self :: WrongType => return Tree :: Nullary ,
11191119 } ;
11201120 match ty. as_inner ( ) {
@@ -1166,8 +1166,9 @@ impl TreeLike for Destructor<'_> {
11661166/// Functions for destructing Simplicity values alongside Simfony types.
11671167mod destruct {
11681168 use super :: * ;
1169+ use simplicity:: ValueRef ;
11691170
1170- pub fn as_bit ( value : & SimValue ) -> Option < bool > {
1171+ pub fn as_bit ( value : ValueRef ) -> Option < bool > {
11711172 match value. as_left ( ) {
11721173 Some ( unit) if unit. is_unit ( ) => Some ( false ) ,
11731174 _ => match value. as_right ( ) {
@@ -1177,10 +1178,10 @@ mod destruct {
11771178 }
11781179 }
11791180
1180- pub fn as_integer ( value : & SimValue , ty : UIntType ) -> Option < UIntValue > {
1181+ pub fn as_integer ( value : ValueRef , ty : UIntType ) -> Option < UIntValue > {
11811182 let bit_len = ty. bit_width ( ) . get ( ) ;
11821183 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 ( ) ) ?;
11841185 let ( bytes, written_bits) = bit_values. into_iter ( ) . filter_map ( as_bit) . collect_bits ( ) ;
11851186 if bit_len != written_bits {
11861187 return None ;
@@ -1196,31 +1197,31 @@ mod destruct {
11961197 }
11971198 }
11981199
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 ( ) )
12011202 }
12021203
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 ( ) )
12051206 }
12061207
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) {
12091210 Some ( Some ( folded) ) => as_array ( folded, size) ,
12101211 Some ( None ) => Some ( vec ! [ ] ) ,
12111212 None => None ,
12121213 } ;
1213- Combiner :: new ( value, bound) . unfold ( as_block, SimValue :: as_product)
1214+ Combiner :: new ( value, bound) . unfold ( as_block, |v| v . as_product ( ) )
12141215 }
12151216
1216- pub fn as_either ( value : & SimValue ) -> Option < Either < & SimValue , & SimValue > > {
1217+ pub fn as_either ( value : ValueRef ) -> Option < Either < ValueRef , ValueRef > > {
12171218 match value. as_left ( ) {
12181219 Some ( inner) => Some ( Either :: Left ( inner) ) ,
12191220 None => value. as_right ( ) . map ( Either :: Right ) ,
12201221 }
12211222 }
12221223
1223- pub fn as_option ( value : & SimValue ) -> Option < Option < & SimValue > > {
1224+ pub fn as_option ( value : ValueRef ) -> Option < Option < ValueRef > > {
12241225 match value. as_left ( ) {
12251226 Some ( inner) if inner. is_unit ( ) => Some ( None ) ,
12261227 _ => value. as_right ( ) . map ( Some ) ,
0 commit comments