Skip to content

Commit 5c7c5bb

Browse files
committed
Auto merge of #28088 - tbu-:pr_fixed_size_array, r=alexcrichton
2 parents 7e3eabc + 4d2709d commit 5c7c5bb

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

src/libcore/array.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use default::Default;
2626
use fmt;
2727
use hash::{Hash, self};
2828
use iter::IntoIterator;
29-
use marker::{Copy, Sized};
29+
use marker::{Copy, Sized, Unsize};
3030
use option::Option;
3131
use slice::{Iter, IterMut, SliceExt};
3232

@@ -41,21 +41,21 @@ pub trait FixedSizeArray<T> {
4141
fn as_mut_slice(&mut self) -> &mut [T];
4242
}
4343

44+
impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
45+
#[inline]
46+
fn as_slice(&self) -> &[T] {
47+
self
48+
}
49+
#[inline]
50+
fn as_mut_slice(&mut self) -> &mut [T] {
51+
self
52+
}
53+
}
54+
4455
// macro for implementing n-ary tuple functions and operations
4556
macro_rules! array_impls {
4657
($($N:expr)+) => {
4758
$(
48-
impl<T> FixedSizeArray<T> for [T; $N] {
49-
#[inline]
50-
fn as_slice(&self) -> &[T] {
51-
&self[..]
52-
}
53-
#[inline]
54-
fn as_mut_slice(&mut self) -> &mut [T] {
55-
&mut self[..]
56-
}
57-
}
58-
5959
impl<T> AsRef<[T]> for [T; $N] {
6060
#[inline]
6161
fn as_ref(&self) -> &[T] {

src/libcoretest/array.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
use core::array::FixedSizeArray;
11+
12+
#[test]
13+
fn fixed_size_array() {
14+
let mut array = [0; 64];
15+
let mut zero_sized = [(); 64];
16+
let mut empty_array = [0; 0];
17+
let mut empty_zero_sized = [(); 0];
18+
19+
assert_eq!(FixedSizeArray::as_slice(&array).len(), 64);
20+
assert_eq!(FixedSizeArray::as_slice(&zero_sized).len(), 64);
21+
assert_eq!(FixedSizeArray::as_slice(&empty_array).len(), 0);
22+
assert_eq!(FixedSizeArray::as_slice(&empty_zero_sized).len(), 0);
23+
24+
assert_eq!(FixedSizeArray::as_mut_slice(&mut array).len(), 64);
25+
assert_eq!(FixedSizeArray::as_mut_slice(&mut zero_sized).len(), 64);
26+
assert_eq!(FixedSizeArray::as_mut_slice(&mut empty_array).len(), 0);
27+
assert_eq!(FixedSizeArray::as_mut_slice(&mut empty_zero_sized).len(), 0);
28+
}

src/libcoretest/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
#![feature(const_fn)]
1616
#![feature(core)]
1717
#![feature(core_float)]
18+
#![feature(dec2flt)]
19+
#![feature(decode_utf16)]
20+
#![feature(fixed_size_array)]
1821
#![feature(float_extras)]
1922
#![feature(float_from_str_radix)]
2023
#![feature(flt2dec)]
21-
#![feature(dec2flt)]
22-
#![feature(decode_utf16)]
2324
#![feature(fmt_radix)]
2425
#![feature(iter_arith)]
2526
#![feature(iter_arith)]
@@ -48,6 +49,7 @@ extern crate rustc_unicode;
4849
extern crate rand;
4950

5051
mod any;
52+
mod array;
5153
mod atomic;
5254
mod cell;
5355
mod char;

0 commit comments

Comments
 (0)