@@ -703,6 +703,46 @@ macro_rules! decnum_tryinto_primitive_uint {
703
703
} } ;
704
704
}
705
705
706
+ /// Refer to the comments on [`Context<Decimal<N>>::try_into_i32()`], which also apply to
707
+ /// this trait.
708
+ impl < const N : usize > TryFrom < Decimal < N > > for i8 {
709
+ type Error = TryFromDecimalError ;
710
+ fn try_from ( n : Decimal < N > ) -> Result < i8 , Self :: Error > {
711
+ let mut cx = Context :: < Decimal < N > > :: default ( ) ;
712
+ cx. try_into_i8 ( n)
713
+ }
714
+ }
715
+
716
+ /// Refer to the comments on [`Context<Decimal<N>>::try_into_i32()`], which also apply to
717
+ /// this trait.
718
+ impl < const N : usize > TryFrom < Decimal < N > > for u8 {
719
+ type Error = TryFromDecimalError ;
720
+ fn try_from ( n : Decimal < N > ) -> Result < u8 , Self :: Error > {
721
+ let mut cx = Context :: < Decimal < N > > :: default ( ) ;
722
+ cx. try_into_u8 ( n)
723
+ }
724
+ }
725
+
726
+ /// Refer to the comments on [`Context<Decimal<N>>::try_into_i32()`], which also apply to
727
+ /// this trait.
728
+ impl < const N : usize > TryFrom < Decimal < N > > for i16 {
729
+ type Error = TryFromDecimalError ;
730
+ fn try_from ( n : Decimal < N > ) -> Result < i16 , Self :: Error > {
731
+ let mut cx = Context :: < Decimal < N > > :: default ( ) ;
732
+ cx. try_into_i16 ( n)
733
+ }
734
+ }
735
+
736
+ /// Refer to the comments on [`Context<Decimal<N>>::try_into_i32()`], which also apply to
737
+ /// this trait.
738
+ impl < const N : usize > TryFrom < Decimal < N > > for u16 {
739
+ type Error = TryFromDecimalError ;
740
+ fn try_from ( n : Decimal < N > ) -> Result < u16 , Self :: Error > {
741
+ let mut cx = Context :: < Decimal < N > > :: default ( ) ;
742
+ cx. try_into_u16 ( n)
743
+ }
744
+ }
745
+
706
746
/// Refer to the comments on [`Context<Decimal<N>>::try_into_i32()`], which also apply to
707
747
/// this trait.
708
748
impl < const N : usize > TryFrom < Decimal < N > > for i32 {
@@ -856,6 +896,30 @@ impl<const N: usize> From<f64> for Decimal<N> {
856
896
}
857
897
}
858
898
899
+ impl < const N : usize > From < i8 > for Decimal < N > {
900
+ fn from ( n : i8 ) -> Decimal < N > {
901
+ Decimal :: < N > :: from ( i32:: from ( n) )
902
+ }
903
+ }
904
+
905
+ impl < const N : usize > From < u8 > for Decimal < N > {
906
+ fn from ( n : u8 ) -> Decimal < N > {
907
+ Decimal :: < N > :: from ( u32:: from ( n) )
908
+ }
909
+ }
910
+
911
+ impl < const N : usize > From < i16 > for Decimal < N > {
912
+ fn from ( n : i16 ) -> Decimal < N > {
913
+ Decimal :: < N > :: from ( i32:: from ( n) )
914
+ }
915
+ }
916
+
917
+ impl < const N : usize > From < u16 > for Decimal < N > {
918
+ fn from ( n : u16 ) -> Decimal < N > {
919
+ Decimal :: < N > :: from ( u32:: from ( n) )
920
+ }
921
+ }
922
+
859
923
impl < const N : usize > From < i32 > for Decimal < N > {
860
924
fn from ( n : i32 ) -> Decimal < N > {
861
925
let mut d = Decimal :: default ( ) ;
@@ -1284,6 +1348,42 @@ impl<const N: usize> Context<Decimal<N>> {
1284
1348
decimal_from_unsigned_int ! ( self , n)
1285
1349
}
1286
1350
1351
+ /// Attempts to convert `d` to `u8` or fails if not possible.
1352
+ ///
1353
+ /// Refer to the comments on [`Self::try_into_i32()`], which also apply to this
1354
+ /// function.
1355
+ pub fn try_into_u8 ( & mut self , d : Decimal < N > ) -> Result < u8 , TryFromDecimalError > {
1356
+ let i = self . try_into_u32 ( d) ?;
1357
+ u8:: try_from ( i) . map_err ( |_| TryFromDecimalError )
1358
+ }
1359
+
1360
+ /// Attempts to convert `d` to `i8` or fails if not possible.
1361
+ ///
1362
+ /// Refer to the comments on [`Self::try_into_i32()`], which also apply to this
1363
+ /// function.
1364
+ pub fn try_into_i8 ( & mut self , d : Decimal < N > ) -> Result < i8 , TryFromDecimalError > {
1365
+ let i = self . try_into_i32 ( d) ?;
1366
+ i8:: try_from ( i) . map_err ( |_| TryFromDecimalError )
1367
+ }
1368
+
1369
+ /// Attempts to convert `d` to `u16` or fails if not possible.
1370
+ ///
1371
+ /// Refer to the comments on [`Self::try_into_i32()`], which also apply to this
1372
+ /// function.
1373
+ pub fn try_into_u16 ( & mut self , d : Decimal < N > ) -> Result < u16 , TryFromDecimalError > {
1374
+ let i = self . try_into_u32 ( d) ?;
1375
+ u16:: try_from ( i) . map_err ( |_| TryFromDecimalError )
1376
+ }
1377
+
1378
+ /// Attempts to convert `d` to `i16` or fails if not possible.
1379
+ ///
1380
+ /// Refer to the comments on [`Self::try_into_i32()`], which also apply to this
1381
+ /// function.
1382
+ pub fn try_into_i16 ( & mut self , d : Decimal < N > ) -> Result < i16 , TryFromDecimalError > {
1383
+ let i = self . try_into_i32 ( d) ?;
1384
+ i16:: try_from ( i) . map_err ( |_| TryFromDecimalError )
1385
+ }
1386
+
1287
1387
/// Attempts to convert `d` to `i32` or fails if not possible. Note that
1288
1388
/// when returning an error, `self`'s [`context::Status`] is set to
1289
1389
/// `invalid_operation` in addition to using Rust's `Err` return value.
0 commit comments