@@ -5,7 +5,7 @@ use core::fmt::Display;
55use std:: cmp:: Ordering ;
66use std:: hash:: { Hash , Hasher } ;
77
8- use num_traits:: NumCast ;
8+ use num_traits:: { NumCast , ToPrimitive } ;
99use paste:: paste;
1010use vortex_dtype:: half:: f16;
1111use vortex_dtype:: { NativePType , PType , ToBytes } ;
@@ -122,19 +122,7 @@ macro_rules! as_primitive {
122122 paste! {
123123 #[ doc = "Access PValue as `" $T "`, returning `None` if conversion is unsuccessful" ]
124124 pub fn [ <as_ $T>] ( self ) -> Option <$T> {
125- match self {
126- PValue :: U8 ( v) => <$T as NumCast >:: from( v) ,
127- PValue :: U16 ( v) => <$T as NumCast >:: from( v) ,
128- PValue :: U32 ( v) => <$T as NumCast >:: from( v) ,
129- PValue :: U64 ( v) => <$T as NumCast >:: from( v) ,
130- PValue :: I8 ( v) => <$T as NumCast >:: from( v) ,
131- PValue :: I16 ( v) => <$T as NumCast >:: from( v) ,
132- PValue :: I32 ( v) => <$T as NumCast >:: from( v) ,
133- PValue :: I64 ( v) => <$T as NumCast >:: from( v) ,
134- PValue :: F16 ( v) => <$T as NumCast >:: from( v) ,
135- PValue :: F32 ( v) => <$T as NumCast >:: from( v) ,
136- PValue :: F64 ( v) => <$T as NumCast >:: from( v) ,
137- }
125+ <$T>:: try_from( self ) . ok( )
138126 }
139127 }
140128 } ;
@@ -152,7 +140,7 @@ impl PValue {
152140 PType :: I16 => PValue :: I16 ( 0 ) ,
153141 PType :: I32 => PValue :: I32 ( 0 ) ,
154142 PType :: I64 => PValue :: I64 ( 0 ) ,
155- PType :: F16 => PValue :: F16 ( f16:: from_f32 ( 0.0 ) ) ,
143+ PType :: F16 => PValue :: F16 ( f16:: ZERO ) ,
156144 PType :: F32 => PValue :: F32 ( 0.0 ) ,
157145 PType :: F64 => PValue :: F64 ( 0.0 ) ,
158146 }
@@ -184,15 +172,15 @@ impl PValue {
184172 ///
185173 /// Panics if the conversion is not supported or would overflow.
186174 #[ inline]
187- pub fn as_primitive < T : NativePType > ( & self ) -> T {
188- self . as_primitive_opt :: < T > ( ) . vortex_expect ( "as_primitive" )
175+ pub fn cast < T : NativePType > ( & self ) -> T {
176+ self . cast_opt :: < T > ( ) . vortex_expect ( "as_primitive" )
189177 }
190178
191179 /// Converts this value to a specific native primitive type.
192180 ///
193181 /// Returns `None` if the conversion is not supported or would overflow.
194182 #[ inline]
195- pub fn as_primitive_opt < T : NativePType > ( & self ) -> Option < T > {
183+ pub fn cast_opt < T : NativePType > ( & self ) -> Option < T > {
196184 match * self {
197185 PValue :: U8 ( u) => T :: from_u8 ( u) ,
198186 PValue :: U16 ( u) => T :: from_u16 ( u) ,
@@ -297,16 +285,17 @@ macro_rules! int_pvalue {
297285
298286 fn try_from( value: PValue ) -> Result <Self , Self :: Error > {
299287 match value {
300- PValue :: U8 ( v ) => <$T as NumCast > :: from ( v ) ,
301- PValue :: U16 ( v ) => <$T as NumCast > :: from ( v ) ,
302- PValue :: U32 ( v ) => <$T as NumCast > :: from ( v ) ,
303- PValue :: U64 ( v ) => <$T as NumCast > :: from ( v ) ,
304- PValue :: I8 ( v ) => <$T as NumCast > :: from ( v ) ,
305- PValue :: I16 ( v ) => <$T as NumCast > :: from ( v ) ,
306- PValue :: I32 ( v ) => <$T as NumCast > :: from ( v ) ,
307- PValue :: I64 ( v ) => <$T as NumCast > :: from ( v ) ,
288+ PValue :: U8 ( _ )
289+ | PValue :: U16 ( _ )
290+ | PValue :: U32 ( _ )
291+ | PValue :: U64 ( _ )
292+ | PValue :: I8 ( _ )
293+ | PValue :: I16 ( _ )
294+ | PValue :: I32 ( _ )
295+ | PValue :: I64 ( _ ) => Some ( value ) ,
308296 _ => None ,
309297 }
298+ . and_then( |v| PValue :: cast_opt( & v) )
310299 . ok_or_else( || {
311300 vortex_err!( "Cannot read primitive value {:?} as {}" , value, PType :: $PT)
312301 } )
@@ -321,32 +310,29 @@ macro_rules! float_pvalue {
321310 type Error = VortexError ;
322311
323312 fn try_from( value: PValue ) -> Result <Self , Self :: Error > {
324- match value {
325- PValue :: U8 ( u) => <Self as NumCast >:: from( u) ,
326- PValue :: U16 ( u) => <Self as NumCast >:: from( u) ,
327- PValue :: U32 ( u) => <Self as NumCast >:: from( u) ,
328- PValue :: U64 ( u) => <Self as NumCast >:: from( u) ,
329- PValue :: I8 ( i) => <Self as NumCast >:: from( i) ,
330- PValue :: I16 ( i) => <Self as NumCast >:: from( i) ,
331- PValue :: I32 ( i) => <Self as NumCast >:: from( i) ,
332- PValue :: I64 ( i) => <Self as NumCast >:: from( i) ,
333- PValue :: F16 ( f) => <Self as NumCast >:: from( f) ,
334- PValue :: F32 ( f) => <Self as NumCast >:: from( f) ,
335- PValue :: F64 ( f) => <Self as NumCast >:: from( f) ,
336- }
337- . ok_or_else( || {
313+ value. cast_opt( ) . ok_or_else( || {
338314 vortex_err!( "Cannot read primitive value {:?} as {}" , value, PType :: $PT)
339315 } )
340316 }
341317 }
342318 } ;
343319}
344320
321+ impl TryFrom < PValue > for usize {
322+ type Error = VortexError ;
323+
324+ fn try_from ( value : PValue ) -> Result < Self , Self :: Error > {
325+ value
326+ . cast_opt :: < u64 > ( )
327+ . and_then ( |v| v. to_usize ( ) )
328+ . ok_or_else ( || vortex_err ! ( "Cannot read primitive value {:?} as usize" , value) )
329+ }
330+ }
331+
345332int_pvalue ! ( u8 , U8 ) ;
346333int_pvalue ! ( u16 , U16 ) ;
347334int_pvalue ! ( u32 , U32 ) ;
348335int_pvalue ! ( u64 , U64 ) ;
349- int_pvalue ! ( usize , U64 ) ;
350336int_pvalue ! ( i8 , I8 ) ;
351337int_pvalue ! ( i16 , I16 ) ;
352338int_pvalue ! ( i32 , I32 ) ;
@@ -532,8 +518,9 @@ mod test {
532518 use std:: cmp:: Ordering ;
533519 use std:: collections:: HashSet ;
534520
521+ use num_traits:: FromPrimitive ;
535522 use vortex_dtype:: half:: f16;
536- use vortex_dtype:: { FromPrimitiveOrF16 , NativePType , PType , ToBytes } ;
523+ use vortex_dtype:: { FromPrimitiveOrF16 , PType , ToBytes } ;
537524
538525 use crate :: PValue ;
539526 use crate :: pvalue:: CoercePValue ;
@@ -915,10 +902,9 @@ mod test {
915902
916903 #[ test]
917904 fn test_f16_nans_equal ( ) {
918- let nan = f16:: NAN ;
919- let nan2 = f16:: from_le_bytes ( [ 154 , 253 ] ) ;
920- assert ! ( nan2. is_nan( ) ) ;
921- let nan3 = f16:: from_f16 ( nan2) . unwrap ( ) ;
922- assert_eq ! ( nan2. to_bits( ) , nan3. to_bits( ) , ) ;
905+ let nan1 = f16:: from_le_bytes ( [ 154 , 253 ] ) ;
906+ assert ! ( nan1. is_nan( ) ) ;
907+ let nan3 = f16:: from_f16 ( nan1) . unwrap ( ) ;
908+ assert_eq ! ( nan1. to_bits( ) , nan3. to_bits( ) , ) ;
923909 }
924910}
0 commit comments