Skip to content

Commit 202b301

Browse files
committed
Avoid metadata bloat by using trait FixedSizeArray
1 parent 2c6df97 commit 202b301

File tree

5 files changed

+40
-54
lines changed

5 files changed

+40
-54
lines changed

src/libcore/array.rs

+24
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

+1-1
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

+3-16
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;
@@ -446,22 +447,8 @@ impl IntoBytes for String {
446447
impl IntoBytes for Vec<u8> {
447448
fn into_bytes(self) -> Vec<u8> { self }
448449
}
449-
450-
macro_rules! array_impls {
451-
($($N: expr)+) => {
452-
$(
453-
impl<'a> IntoBytes for &'a [u8; $N] {
454-
fn into_bytes(self) -> Vec<u8> { self.to_vec() }
455-
}
456-
)+
457-
}
458-
}
459-
460-
array_impls! {
461-
0 1 2 3 4 5 6 7 8 9
462-
10 11 12 13 14 15 16 17 18 19
463-
20 21 22 23 24 25 26 27 28 29
464-
30 31 32
450+
impl<'a, T: FixedSizeArray<u8>> IntoBytes for &'a T {
451+
fn into_bytes(self) -> Vec<u8> { self.as_slice().to_vec() }
465452
}
466453

467454
#[cfg(test)]

src/libstd/io/cursor.rs

+7-20
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

+5-17
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;
@@ -893,26 +894,13 @@ impl BytesContainer for [u8] {
893894
}
894895
}
895896

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

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

0 commit comments

Comments
 (0)