@@ -1989,6 +1989,19 @@ big endian.
19891989```
19901990let value = " , stringify!( $SelfT) , "::from_be_bytes(" , $be_bytes, ");
19911991assert_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+ }
19922005```" ) ,
19932006 #[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
19942007 #[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -2008,6 +2021,19 @@ little endian.
20082021```
20092022let value = " , stringify!( $SelfT) , "::from_le_bytes(" , $le_bytes, ");
20102023assert_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+ }
20112037```" ) ,
20122038 #[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
20132039 #[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -2037,6 +2063,19 @@ let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"bi
20372063 " , $le_bytes, "
20382064 });
20392065assert_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+ }
20402079```" ) ,
20412080 #[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
20422081 #[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -3719,6 +3758,19 @@ big endian.
37193758```
37203759let value = " , stringify!( $SelfT) , "::from_be_bytes(" , $be_bytes, ");
37213760assert_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+ }
37223774```" ) ,
37233775 #[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
37243776 #[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -3738,6 +3790,19 @@ little endian.
37383790```
37393791let value = " , stringify!( $SelfT) , "::from_le_bytes(" , $le_bytes, ");
37403792assert_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+ }
37413806```" ) ,
37423807 #[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
37433808 #[ rustc_const_unstable( feature = "const_int_conversion" ) ]
@@ -3767,6 +3832,19 @@ let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"bi
37673832 " , $le_bytes, "
37683833 });
37693834assert_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+ }
37703848```" ) ,
37713849 #[ stable( feature = "int_to_from_bytes" , since = "1.32.0" ) ]
37723850 #[ rustc_const_unstable( feature = "const_int_conversion" ) ]
0 commit comments