Skip to content

Commit 3c31794

Browse files
committed
Avoid metadata bloat by using trait FixedSizeArray
1 parent b98255c commit 3c31794

File tree

5 files changed

+40
-54
lines changed

5 files changed

+40
-54
lines changed

src/libcore/array.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,34 @@ use marker::{Copy, Sized};
2323
use option::Option;
2424
use slice::{Iter, IterMut, SliceExt};
2525

26+
/// Utility trait implemented only on arrays of fixed size
27+
///
28+
/// This trait can be used to implement other traits on fixed-size arrays
29+
/// without causing much metadata bloat.
30+
#[unstable(feature = "core")]
31+
pub trait FixedSizeArray<T> {
32+
/// Converts the array to immutable slice
33+
fn as_slice(&self) -> &[T];
34+
/// Converts the array to mutable slice
35+
fn as_mut_slice(&mut self) -> &mut [T];
36+
}
37+
2638
// macro for implementing n-ary tuple functions and operations
2739
macro_rules! array_impls {
2840
($($N:expr)+) => {
2941
$(
42+
#[unstable(feature = "core")]
43+
impl<T> FixedSizeArray<T> for [T; $N] {
44+
#[inline]
45+
fn as_slice(&self) -> &[T] {
46+
&self[..]
47+
}
48+
#[inline]
49+
fn as_mut_slice(&mut self) -> &mut [T] {
50+
&mut self[..]
51+
}
52+
}
53+
3054
#[stable(feature = "rust1", since = "1.0.0")]
3155
impl<T:Copy> Clone for [T; $N] {
3256
fn clone(&self) -> [T; $N] {

src/libcore/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ pub mod default;
129129
/* Core types and methods on primitives */
130130

131131
pub mod any;
132+
pub mod array;
132133
pub mod atomic;
133134
pub mod cell;
134135
pub mod char;
@@ -151,7 +152,6 @@ mod bool {
151152

152153
// note: does not need to be public
153154
mod tuple;
154-
mod array;
155155

156156
#[doc(hidden)]
157157
mod core {

src/libstd/ffi/c_str.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![unstable(feature = "std_misc")]
1212

1313
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
14+
use core::array::FixedSizeArray;
1415
use error::{Error, FromError};
1516
use fmt;
1617
use io;
@@ -450,22 +451,8 @@ impl IntoBytes for String {
450451
impl IntoBytes for Vec<u8> {
451452
fn into_bytes(self) -> Vec<u8> { self }
452453
}
453-
454-
macro_rules! array_impls {
455-
($($N: expr)+) => {
456-
$(
457-
impl<'a> IntoBytes for &'a [u8; $N] {
458-
fn into_bytes(self) -> Vec<u8> { self.to_vec() }
459-
}
460-
)+
461-
}
462-
}
463-
464-
array_impls! {
465-
0 1 2 3 4 5 6 7 8 9
466-
10 11 12 13 14 15 16 17 18 19
467-
20 21 22 23 24 25 26 27 28 29
468-
30 31 32
454+
impl<'a, T: FixedSizeArray<u8>> IntoBytes for &'a T {
455+
fn into_bytes(self) -> Vec<u8> { self.as_slice().to_vec() }
469456
}
470457

471458
#[cfg(test)]

src/libstd/io/cursor.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use prelude::v1::*;
1212
use io::prelude::*;
1313

14+
use core::array::FixedSizeArray;
1415
use cmp;
1516
use io::{self, SeekFrom, Error, ErrorKind};
1617
use iter::repeat;
@@ -72,7 +73,7 @@ macro_rules! seek {
7273
fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
7374
let pos = match style {
7475
SeekFrom::Start(n) => { self.pos = n; return Ok(n) }
75-
SeekFrom::End(n) => self.inner.len() as i64 + n,
76+
SeekFrom::End(n) => self.inner.as_slice().len() as i64 + n,
7677
SeekFrom::Current(n) => self.pos as i64 + n,
7778
};
7879

@@ -94,6 +95,7 @@ impl<'a> io::Seek for Cursor<&'a [u8]> { seek!(); }
9495
impl<'a> io::Seek for Cursor<&'a mut [u8]> { seek!(); }
9596
#[stable(feature = "rust1", since = "1.0.0")]
9697
impl io::Seek for Cursor<Vec<u8>> { seek!(); }
98+
impl<'a, T: FixedSizeArray<u8>> io::Seek for Cursor<&'a T> { seek!(); }
9799

98100
macro_rules! read {
99101
() => {
@@ -111,12 +113,13 @@ impl<'a> Read for Cursor<&'a [u8]> { read!(); }
111113
impl<'a> Read for Cursor<&'a mut [u8]> { read!(); }
112114
#[stable(feature = "rust1", since = "1.0.0")]
113115
impl Read for Cursor<Vec<u8>> { read!(); }
116+
impl<'a, T: FixedSizeArray<u8>> Read for Cursor<&'a T> { read!(); }
114117

115118
macro_rules! buffer {
116119
() => {
117120
fn fill_buf(&mut self) -> io::Result<&[u8]> {
118-
let amt = cmp::min(self.pos, self.inner.len() as u64);
119-
Ok(&self.inner[(amt as usize)..])
121+
let amt = cmp::min(self.pos, self.inner.as_slice().len() as u64);
122+
Ok(&self.inner.as_slice()[(amt as usize)..])
120123
}
121124
fn consume(&mut self, amt: usize) { self.pos += amt as u64; }
122125
}
@@ -128,23 +131,7 @@ impl<'a> BufRead for Cursor<&'a [u8]> { buffer!(); }
128131
impl<'a> BufRead for Cursor<&'a mut [u8]> { buffer!(); }
129132
#[stable(feature = "rust1", since = "1.0.0")]
130133
impl<'a> BufRead for Cursor<Vec<u8>> { buffer!(); }
131-
132-
macro_rules! array_impls {
133-
($($N: expr)+) => {
134-
$(
135-
impl<'a> io::Seek for Cursor<&'a [u8; $N]> { seek!(); }
136-
impl<'a> Read for Cursor<&'a [u8; $N]> { read!(); }
137-
impl<'a> BufRead for Cursor<&'a [u8; $N]> { buffer!(); }
138-
)+
139-
}
140-
}
141-
142-
array_impls! {
143-
0 1 2 3 4 5 6 7 8 9
144-
10 11 12 13 14 15 16 17 18 19
145-
20 21 22 23 24 25 26 27 28 29
146-
30 31 32
147-
}
134+
impl<'a, T: FixedSizeArray<u8>> BufRead for Cursor<&'a T> { buffer!(); }
148135

149136
#[stable(feature = "rust1", since = "1.0.0")]
150137
impl<'a> Write for Cursor<&'a mut [u8]> {

src/libstd/old_path/mod.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#![allow(deprecated)] // seriously this is all deprecated
6565
#![allow(unused_imports)]
6666

67+
use core::array::FixedSizeArray;
6768
use core::marker::Sized;
6869
use ffi::CString;
6970
use clone::Clone;
@@ -895,26 +896,13 @@ impl BytesContainer for [u8] {
895896
}
896897
}
897898

898-
macro_rules! array_impls {
899-
($($N: expr)+) => {
900-
$(
901-
impl BytesContainer for [u8; $N] {
902-
#[inline]
903-
fn container_as_bytes(&self) -> &[u8] {
904-
&self[..]
905-
}
906-
}
907-
)+
899+
impl<T: FixedSizeArray<u8>> BytesContainer for T {
900+
#[inline]
901+
fn container_as_bytes(&self) -> &[u8] {
902+
self.as_slice()
908903
}
909904
}
910905

911-
array_impls! {
912-
0 1 2 3 4 5 6 7 8 9
913-
10 11 12 13 14 15 16 17 18 19
914-
20 21 22 23 24 25 26 27 28 29
915-
30 31 32
916-
}
917-
918906
impl BytesContainer for Vec<u8> {
919907
#[inline]
920908
fn container_as_bytes(&self) -> &[u8] {

0 commit comments

Comments
 (0)