Skip to content

Commit 057e6d3

Browse files
committedNov 25, 2018
Add TryFrom<&[T]> for [T; $N] where T: Copy
`TryFrom<&[T]> for &[T; $N]` (note *reference* to an array) already exists, but not needing to dereference makes type inference easier for example when using `u32::from_be_bytes`. Also add doc examples doing just that.

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
 

‎src/libcore/array.rs

+9
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ macro_rules! array_impls {
148148
}
149149
}
150150

151+
#[unstable(feature = "try_from", issue = "33417")]
152+
impl<'a, T> TryFrom<&'a [T]> for [T; $N] where T: Copy {
153+
type Error = TryFromSliceError;
154+
155+
fn try_from(slice: &[T]) -> Result<[T; $N], TryFromSliceError> {
156+
<&Self>::try_from(slice).map(|r| *r)
157+
}
158+
}
159+
151160
#[unstable(feature = "try_from", issue = "33417")]
152161
impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] {
153162
type Error = TryFromSliceError;

‎src/libcore/num/mod.rs

+78
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,19 @@ big endian.
19891989
```
19901990
let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");
19911991
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+
}
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
```
20092022
let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");
20102023
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+
}
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
});
20392065
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+
}
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
```
37203759
let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");
37213760
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+
}
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
```
37393791
let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");
37403792
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+
}
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
});
37693834
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+
}
37703848
```"),
37713849
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
37723850
#[rustc_const_unstable(feature = "const_int_conversion")]

0 commit comments

Comments
 (0)
Please sign in to comment.