about summary refs log tree commit diff
path: root/src/libcore/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-10-31 23:06:37 +0000
committerbors <bors@rust-lang.org>2017-10-31 23:06:37 +0000
commitf3b900cc3b122c7e9eb78ca28bec18df68791b08 (patch)
tree0fa55ce4def4412af633d90d87e52c18fe195fc9 /src/libcore/tests
parent8b22e70b2de5152db3b0c53cfa16eb96b0b9e40e (diff)
parent4c853adce9103b8bc84cd6b0026bcdc2eed7da31 (diff)
Auto merge of #44764 - nvzqz:master, r=alexcrichton
Implement TryFrom<&[T]> for &[T; N]

There are many cases where a buffer with a static compile-time size is preferred over a slice with a dynamic size. This allows for performing a checked conversion from `&[T]` to `&[T; N]`. This may also lead to compile-time optimizations involving `[T; N]` such as loop unrolling.

This is my first PR to Rust, so I'm not sure if discussion of this change should happen here or does it need its own RFC? I figured these changes would be a subset of #33417.
Diffstat (limited to 'src/libcore/tests')
-rw-r--r--src/libcore/tests/array.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libcore/tests/array.rs b/src/libcore/tests/array.rs
index 6af031dee58..6278d5e23e0 100644
--- a/src/libcore/tests/array.rs
+++ b/src/libcore/tests/array.rs
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 use core::array::FixedSizeArray;
+use core::convert::TryFrom;
 
 #[test]
 fn fixed_size_array() {
@@ -26,3 +27,25 @@ fn fixed_size_array() {
     assert_eq!(FixedSizeArray::as_mut_slice(&mut empty_array).len(), 0);
     assert_eq!(FixedSizeArray::as_mut_slice(&mut empty_zero_sized).len(), 0);
 }
+
+#[test]
+fn array_try_from() {
+    macro_rules! test {
+        ($($N:expr)+) => {
+            $({
+                type Array = [u8; $N];
+                let array: Array = [0; $N];
+                let slice: &[u8] = &array[..];
+
+                let result = <&Array>::try_from(slice);
+                assert_eq!(&array, result.unwrap());
+            })+
+        }
+    }
+    test! {
+         0  1  2  3  4  5  6  7  8  9
+        10 11 12 13 14 15 16 17 18 19
+        20 21 22 23 24 25 26 27 28 29
+        30 31 32
+    }
+}