@@ -63,44 +63,25 @@ pub struct Symbol {
63
63
// Value conversions
64
64
//
65
65
66
- // From primitive numbers to Value
67
- impl From < i64 > for Value {
68
- fn from ( n : i64 ) -> Value {
69
- Number :: Int ( n) . into ( )
70
- }
71
- }
72
- impl From < f64 > for Value {
73
- fn from ( n : f64 ) -> Value {
74
- Number :: Float ( n) . into ( )
75
- }
76
- }
77
- impl From < u32 > for Value {
78
- fn from ( num : u32 ) -> Value {
79
- ( num as i64 ) . into ( )
80
- }
81
- }
82
- impl From < i32 > for Value {
83
- fn from ( num : i32 ) -> Value {
84
- ( num as i64 ) . into ( )
85
- }
86
- }
87
- impl From < usize > for Value {
88
- fn from ( num : usize ) -> Value {
89
- // TODO this should use TryFrom
90
- Number :: Int ( num. try_into ( ) . unwrap ( ) ) . into ( )
91
- }
92
- }
66
+ // From primitive types to Value
67
+ impl_simple_to_value ! ( i64 , n, Number :: Int ( n) ) ;
68
+ impl_simple_to_value ! ( f64 , n, Number :: Float ( n) ) ;
69
+ impl_simple_to_value ! ( u32 , n, n as i64 ) ;
70
+ impl_simple_to_value ! ( i32 , n, n as i64 ) ;
71
+ impl_simple_to_value ! ( usize , n, i64 :: try_from( n) . unwrap( ) ) ; // XXX should not panic
72
+ impl_simple_to_value ! ( & str , s, s. to_string( ) ) ;
73
+
93
74
impl TryFrom < u64 > for Value {
94
75
type Error = Error ;
95
76
fn try_from ( num : u64 ) -> Result < Self > {
96
77
Ok ( Value :: Number ( Number :: Int ( num. try_into ( ) ?) ) )
97
78
}
98
79
}
99
80
100
- // From NativeFunction/UserFunction to Value
101
- impl < T : Into < Function > > From < T > for Value {
81
+ // From any Into<Array> type to Value
82
+ impl < T : Into < Array > > From < T > for Value {
102
83
fn from ( f : T ) -> Self {
103
- Value :: Function ( f. into ( ) )
84
+ Value :: Array ( f. into ( ) )
104
85
}
105
86
}
106
87
@@ -109,7 +90,6 @@ impl_from_variant!(bool, Value, Bool);
109
90
impl_from_variant ! ( Number , Value ) ;
110
91
impl_from_variant ! ( String , Value ) ;
111
92
impl_from_variant ! ( Vec <u8 >, Value , Bytes ) ;
112
- impl_from_variant ! ( Array , Value ) ;
113
93
impl_from_variant ! ( Symbol , Value ) ;
114
94
impl_from_variant ! ( Policy , Value ) ;
115
95
impl_from_variant ! ( Descriptor , Value ) ;
@@ -123,12 +103,6 @@ impl_from_variant!(TaprootSpendInfo, Value, TapInfo);
123
103
impl_from_variant ! ( WshScript , Value ) ;
124
104
impl_from_variant ! ( Psbt , Value ) ;
125
105
126
- impl From < & str > for Value {
127
- fn from ( s : & str ) -> Self {
128
- Value :: String ( s. to_string ( ) )
129
- }
130
- }
131
-
132
106
// From Value to the underlying enum inner type
133
107
// Simple extraction of the enum variant, with no specialized type coercion logic
134
108
impl_simple_into_variant ! ( bool , Bool , into_bool, NotBool ) ;
@@ -138,6 +112,7 @@ impl_simple_into_variant!(Function, Function, into_fn, NotFn);
138
112
impl_simple_into_variant ! ( String , String , into_string, NotString ) ;
139
113
impl_simple_into_variant ! ( WshScript , WshScript , into_wsh_script, NotWshScript ) ;
140
114
115
+
141
116
// From Value to f64 primitive, with auto-coercion for integers
142
117
impl TryFrom < Value > for f64 {
143
118
type Error = Error ;
@@ -310,23 +285,6 @@ impl_delegate_array_conv!((A, ), A: FromValue);
310
285
impl_delegate_array_conv ! ( ( A , B ) , A : FromValue , B : FromValue ) ;
311
286
impl_delegate_array_conv ! ( ( A , B , C ) , A : FromValue , B : FromValue , C : FromValue ) ;
312
287
313
- // Generic conversion from sets into a Value
314
- impl < A : Into < Value > , B : Into < Value > > From < ( A , B ) > for Value {
315
- fn from ( value : ( A , B ) ) -> Self {
316
- Value :: Array ( value. into ( ) )
317
- }
318
- }
319
- impl < T : Into < Value > > From < Vec < T > > for Value {
320
- fn from ( value : Vec < T > ) -> Self {
321
- Value :: Array ( value. into ( ) )
322
- }
323
- }
324
- impl < K : Into < Value > , V : Into < Value > > From < BTreeMap < K , V > > for Value {
325
- fn from ( value : BTreeMap < K , V > ) -> Self {
326
- Value :: Array ( value. into ( ) )
327
- }
328
- }
329
-
330
288
//
331
289
// Value impl
332
290
//
@@ -421,8 +379,15 @@ impl Value {
421
379
pub fn array ( elements : Vec < Value > ) -> Self {
422
380
Value :: Array ( Array ( elements) )
423
381
}
424
- pub fn array_of ( array : impl Into < Array > ) -> Self {
425
- Value :: Array ( array. into ( ) )
382
+
383
+ pub fn function ( func : impl Into < Function > ) -> Self {
384
+ Value :: Function ( func. into ( ) )
385
+ }
386
+ }
387
+
388
+ impl < V : Into < Value > > std:: iter:: FromIterator < V > for Value {
389
+ fn from_iter < T : IntoIterator < Item = V > > ( iter : T ) -> Self {
390
+ Array :: from_iter ( iter) . into ( )
426
391
}
427
392
}
428
393
0 commit comments