@@ -1989,6 +1989,19 @@ big endian.
1989
1989
```
1990
1990
let value = " , stringify!( $SelfT) , "::from_be_bytes(" , $be_bytes, ");
1991
1991
assert_eq!(value, " , $swap_op, ");
1992
+ ```
1993
+
1994
+ When starting from a slice rather than an array, fallible conversion APIs can be used:
1995
+
1996
+ ```
1997
+ #![feature(try_from)]
1998
+ use std::convert::TryInto;
1999
+
2000
+ fn read_be_" , stringify!( $SelfT) , "(input: &mut &[u8]) -> " , stringify!( $SelfT) , " {
2001
+ let (int_bytes, rest) = input.split_at(std::mem::size_of::<" , stringify!( $SelfT) , ">());
2002
+ *input = rest;
2003
+ " , stringify!( $SelfT) , "::from_be_bytes(int_bytes.try_into().unwrap())
2004
+ }
1992
2005
```" ) ,
1993
2006
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
1994
2007
#[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -2008,6 +2021,19 @@ little endian.
2008
2021
```
2009
2022
let value = " , stringify!( $SelfT) , "::from_le_bytes(" , $le_bytes, ");
2010
2023
assert_eq!(value, " , $swap_op, ");
2024
+ ```
2025
+
2026
+ When starting from a slice rather than an array, fallible conversion APIs can be used:
2027
+
2028
+ ```
2029
+ #![feature(try_from)]
2030
+ use std::convert::TryInto;
2031
+
2032
+ fn read_be_" , stringify!( $SelfT) , "(input: &mut &[u8]) -> " , stringify!( $SelfT) , " {
2033
+ let (int_bytes, rest) = input.split_at(std::mem::size_of::<" , stringify!( $SelfT) , ">());
2034
+ *input = rest;
2035
+ " , stringify!( $SelfT) , "::from_be_bytes(int_bytes.try_into().unwrap())
2036
+ }
2011
2037
```" ) ,
2012
2038
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
2013
2039
#[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -2037,6 +2063,19 @@ let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"bi
2037
2063
" , $le_bytes, "
2038
2064
});
2039
2065
assert_eq!(value, " , $swap_op, ");
2066
+ ```
2067
+
2068
+ When starting from a slice rather than an array, fallible conversion APIs can be used:
2069
+
2070
+ ```
2071
+ #![feature(try_from)]
2072
+ use std::convert::TryInto;
2073
+
2074
+ fn read_be_" , stringify!( $SelfT) , "(input: &mut &[u8]) -> " , stringify!( $SelfT) , " {
2075
+ let (int_bytes, rest) = input.split_at(std::mem::size_of::<" , stringify!( $SelfT) , ">());
2076
+ *input = rest;
2077
+ " , stringify!( $SelfT) , "::from_be_bytes(int_bytes.try_into().unwrap())
2078
+ }
2040
2079
```" ) ,
2041
2080
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
2042
2081
#[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -3719,6 +3758,19 @@ big endian.
3719
3758
```
3720
3759
let value = " , stringify!( $SelfT) , "::from_be_bytes(" , $be_bytes, ");
3721
3760
assert_eq!(value, " , $swap_op, ");
3761
+ ```
3762
+
3763
+ When starting from a slice rather than an array, fallible conversion APIs can be used:
3764
+
3765
+ ```
3766
+ #![feature(try_from)]
3767
+ use std::convert::TryInto;
3768
+
3769
+ fn read_be_" , stringify!( $SelfT) , "(input: &mut &[u8]) -> " , stringify!( $SelfT) , " {
3770
+ let (int_bytes, rest) = input.split_at(std::mem::size_of::<" , stringify!( $SelfT) , ">());
3771
+ *input = rest;
3772
+ " , stringify!( $SelfT) , "::from_be_bytes(int_bytes.try_into().unwrap())
3773
+ }
3722
3774
```" ) ,
3723
3775
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
3724
3776
#[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -3738,6 +3790,19 @@ little endian.
3738
3790
```
3739
3791
let value = " , stringify!( $SelfT) , "::from_le_bytes(" , $le_bytes, ");
3740
3792
assert_eq!(value, " , $swap_op, ");
3793
+ ```
3794
+
3795
+ When starting from a slice rather than an array, fallible conversion APIs can be used:
3796
+
3797
+ ```
3798
+ #![feature(try_from)]
3799
+ use std::convert::TryInto;
3800
+
3801
+ fn read_be_" , stringify!( $SelfT) , "(input: &mut &[u8]) -> " , stringify!( $SelfT) , " {
3802
+ let (int_bytes, rest) = input.split_at(std::mem::size_of::<" , stringify!( $SelfT) , ">());
3803
+ *input = rest;
3804
+ " , stringify!( $SelfT) , "::from_be_bytes(int_bytes.try_into().unwrap())
3805
+ }
3741
3806
```" ) ,
3742
3807
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
3743
3808
#[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -3767,6 +3832,19 @@ let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"bi
3767
3832
" , $le_bytes, "
3768
3833
});
3769
3834
assert_eq!(value, " , $swap_op, ");
3835
+ ```
3836
+
3837
+ When starting from a slice rather than an array, fallible conversion APIs can be used:
3838
+
3839
+ ```
3840
+ #![feature(try_from)]
3841
+ use std::convert::TryInto;
3842
+
3843
+ fn read_be_" , stringify!( $SelfT) , "(input: &mut &[u8]) -> " , stringify!( $SelfT) , " {
3844
+ let (int_bytes, rest) = input.split_at(std::mem::size_of::<" , stringify!( $SelfT) , ">());
3845
+ *input = rest;
3846
+ " , stringify!( $SelfT) , "::from_be_bytes(int_bytes.try_into().unwrap())
3847
+ }
3770
3848
```" ) ,
3771
3849
#[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
3772
3850
#[ rustc_const_unstable( feature = "const_int_conversion" ) ]
0 commit comments