@@ -196,11 +196,11 @@ pub enum Value {
196
196
197
197
#[ cfg( feature = "with-bigdecimal" ) ]
198
198
#[ cfg_attr( docsrs, doc( cfg( feature = "with-bigdecimal" ) ) ) ]
199
- BigDecimal ( Option < BigDecimal > ) ,
199
+ BigDecimal ( Option < Box < BigDecimal > > ) ,
200
200
201
201
#[ cfg( feature = "postgres-array" ) ]
202
202
#[ cfg_attr( docsrs, doc( cfg( feature = "postgres-array" ) ) ) ]
203
- Array ( ArrayType , Option < Vec < Value > > ) ,
203
+ Array ( ArrayType , Option < Box < Vec < Value > > > ) ,
204
204
205
205
#[ cfg( feature = "postgres-vector" ) ]
206
206
#[ cfg_attr( docsrs, doc( cfg( feature = "postgres-vector" ) ) ) ]
@@ -215,6 +215,19 @@ pub enum Value {
215
215
MacAddress ( Option < MacAddress > ) ,
216
216
}
217
217
218
+ /// This test is to check if the size of [`Value`] exceeds the limit.
219
+ /// If the size exceeds the limit, you should box the variant.
220
+ /// Previously, the size was 24. We bumped it to 32 such that `String`
221
+ /// can be unboxed.
222
+ pub const VALUE_SIZE : usize = check_value_size ( ) ;
223
+
224
+ const fn check_value_size ( ) -> usize {
225
+ if std:: mem:: size_of :: < Value > ( ) > 32 {
226
+ panic ! ( "the size of Value shouldn't be greater than 32 bytes" )
227
+ }
228
+ std:: mem:: size_of :: < Value > ( )
229
+ }
230
+
218
231
impl std:: fmt:: Display for Value {
219
232
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
220
233
write ! ( f, "{}" , CommonSqlQueryBuilder . value_to_string( self ) )
@@ -543,6 +556,45 @@ macro_rules! type_to_value {
543
556
} ;
544
557
}
545
558
559
+ #[ cfg( feature = "with-bigdecimal" ) ]
560
+ macro_rules! type_to_box_value {
561
+ ( $type: ty, $name: ident, $col_type: expr ) => {
562
+ impl From <$type> for Value {
563
+ fn from( x: $type) -> Value {
564
+ Value :: $name( Some ( Box :: new( x) ) )
565
+ }
566
+ }
567
+
568
+ impl Nullable for $type {
569
+ fn null( ) -> Value {
570
+ Value :: $name( None )
571
+ }
572
+ }
573
+
574
+ impl ValueType for $type {
575
+ fn try_from( v: Value ) -> Result <Self , ValueTypeErr > {
576
+ match v {
577
+ Value :: $name( Some ( x) ) => Ok ( * x) ,
578
+ _ => Err ( ValueTypeErr ) ,
579
+ }
580
+ }
581
+
582
+ fn type_name( ) -> String {
583
+ stringify!( $type) . to_owned( )
584
+ }
585
+
586
+ fn array_type( ) -> ArrayType {
587
+ ArrayType :: $name
588
+ }
589
+
590
+ fn column_type( ) -> ColumnType {
591
+ use ColumnType :: * ;
592
+ $col_type
593
+ }
594
+ }
595
+ } ;
596
+ }
597
+
546
598
type_to_value ! ( bool , Bool , Boolean ) ;
547
599
type_to_value ! ( i8 , TinyInt , TinyInteger ) ;
548
600
type_to_value ! ( i16 , SmallInt , SmallInteger ) ;
@@ -839,7 +891,7 @@ mod with_rust_decimal {
839
891
mod with_bigdecimal {
840
892
use super :: * ;
841
893
842
- type_to_value ! ( BigDecimal , BigDecimal , Decimal ( None ) ) ;
894
+ type_to_box_value ! ( BigDecimal , BigDecimal , Decimal ( None ) ) ;
843
895
}
844
896
845
897
#[ cfg( feature = "with-uuid" ) ]
@@ -996,7 +1048,7 @@ pub mod with_array {
996
1048
fn from ( x : Vec < T > ) -> Value {
997
1049
Value :: Array (
998
1050
T :: array_type ( ) ,
999
- Some ( x. into_iter ( ) . map ( |e| e. into ( ) ) . collect ( ) ) ,
1051
+ Some ( Box :: new ( x. into_iter ( ) . map ( |e| e. into ( ) ) . collect ( ) ) ) ,
1000
1052
)
1001
1053
}
1002
1054
}
@@ -1636,12 +1688,14 @@ pub fn sea_value_to_json_value(value: &Value) -> Json {
1636
1688
#[ cfg( feature = "with-bigdecimal" ) ]
1637
1689
Value :: BigDecimal ( Some ( v) ) => {
1638
1690
use bigdecimal:: ToPrimitive ;
1639
- v. to_f64 ( ) . unwrap ( ) . into ( )
1691
+ v. as_ref ( ) . to_f64 ( ) . unwrap ( ) . into ( )
1640
1692
}
1641
1693
#[ cfg( feature = "with-uuid" ) ]
1642
1694
Value :: Uuid ( Some ( v) ) => Json :: String ( v. to_string ( ) ) ,
1643
1695
#[ cfg( feature = "postgres-array" ) ]
1644
- Value :: Array ( _, Some ( v) ) => Json :: Array ( v. iter ( ) . map ( sea_value_to_json_value) . collect ( ) ) ,
1696
+ Value :: Array ( _, Some ( v) ) => {
1697
+ Json :: Array ( v. as_ref ( ) . iter ( ) . map ( sea_value_to_json_value) . collect ( ) )
1698
+ }
1645
1699
#[ cfg( feature = "postgres-vector" ) ]
1646
1700
Value :: Vector ( Some ( v) ) => Json :: Array ( v. as_slice ( ) . iter ( ) . map ( |& v| v. into ( ) ) . collect ( ) ) ,
1647
1701
#[ cfg( feature = "with-ipnetwork" ) ]
@@ -2409,6 +2463,7 @@ mod hashable_value {
2409
2463
Value :: Int ( Some ( 1 ) ) ,
2410
2464
Value :: Int ( Some ( 2 ) )
2411
2465
] )
2466
+ . into( )
2412
2467
)
2413
2468
)
2414
2469
) ;
@@ -2423,6 +2478,7 @@ mod hashable_value {
2423
2478
Value :: Float ( Some ( 1.0 ) ) ,
2424
2479
Value :: Float ( Some ( 2.0 ) )
2425
2480
] )
2481
+ . into( )
2426
2482
)
2427
2483
)
2428
2484
) ;
0 commit comments