@@ -115,6 +115,18 @@ impl_int_conversion!(PodU64, u64);
115
115
pub struct PodI64 ( [ u8 ; 8 ] ) ;
116
116
impl_int_conversion ! ( PodI64 , i64 ) ;
117
117
118
+ /// `u128` type that can be used in Pods
119
+ #[ cfg_attr(
120
+ feature = "borsh" ,
121
+ derive( BorshDeserialize , BorshSerialize , BorshSchema )
122
+ ) ]
123
+ #[ cfg_attr( feature = "serde-traits" , derive( Serialize , Deserialize ) ) ]
124
+ #[ cfg_attr( feature = "serde-traits" , serde( from = "u128" , into = "u128" ) ) ]
125
+ #[ derive( Clone , Copy , Debug , Default , PartialEq , Pod , Zeroable ) ]
126
+ #[ repr( transparent) ]
127
+ pub struct PodU128 ( pub [ u8 ; 16 ] ) ;
128
+ impl_int_conversion ! ( PodU128 , u128 ) ;
129
+
118
130
#[ cfg( test) ]
119
131
mod tests {
120
132
use { super :: * , crate :: bytemuck:: pod_from_bytes} ;
@@ -230,4 +242,28 @@ mod tests {
230
242
let deserialized = serde_json:: from_str :: < PodI64 > ( & serialized) . unwrap ( ) ;
231
243
assert_eq ! ( pod_i64, deserialized) ;
232
244
}
245
+
246
+ #[ test]
247
+ fn test_pod_u128 ( ) {
248
+ assert ! ( pod_from_bytes:: <PodU128 >( & [ ] ) . is_err( ) ) ;
249
+ assert_eq ! (
250
+ 1u128 ,
251
+ u128 :: from(
252
+ * pod_from_bytes:: <PodU128 >( & [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] )
253
+ . unwrap( )
254
+ )
255
+ ) ;
256
+ }
257
+
258
+ #[ cfg( feature = "serde-traits" ) ]
259
+ #[ test]
260
+ fn test_pod_u128_serde ( ) {
261
+ let pod_u128: PodU128 = u128:: MAX . into ( ) ;
262
+
263
+ let serialized = serde_json:: to_string ( & pod_u128) . unwrap ( ) ;
264
+ assert_eq ! ( & serialized, "340282366920938463463374607431768211455" ) ;
265
+
266
+ let deserialized = serde_json:: from_str :: < PodU128 > ( & serialized) . unwrap ( ) ;
267
+ assert_eq ! ( pod_u128, deserialized) ;
268
+ }
233
269
}
0 commit comments