From 1b42f992ede54326d3ed2dd002c148d400c19b4a Mon Sep 17 00:00:00 2001 From: root Date: Fri, 22 May 2015 13:26:01 +0200 Subject: [PATCH] Move Array trait to separate file Also remove the ::new() method on the trait --- src/array.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 51 +++++++++------------------------------------------ 2 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 src/array.rs diff --git a/src/array.rs b/src/array.rs new file mode 100644 index 0000000..5dfbb24 --- /dev/null +++ b/src/array.rs @@ -0,0 +1,39 @@ + +/// Trait for fixed size arrays. +pub unsafe trait Array { + /// The array's element type + type Item; + #[doc(hidden)] + fn as_ptr(&self) -> *const Self::Item; + #[doc(hidden)] + fn as_mut_ptr(&mut self) -> *mut Self::Item; + #[doc(hidden)] + fn capacity() -> usize; +} + +macro_rules! fix_array_impl { + ($len:expr ) => ( + unsafe impl Array for [T; $len] { + type Item = T; + #[inline(always)] + fn as_ptr(&self) -> *const T { self as *const _ as *const _ } + #[inline(always)] + fn as_mut_ptr(&mut self) -> *mut T { self as *mut _ as *mut _} + #[inline(always)] + fn capacity() -> usize { $len } + } + ) +} + +macro_rules! fix_array_impl_recursive { + () => (); + ($len:expr, $($more:expr,)*) => ( + fix_array_impl!($len); + fix_array_impl_recursive!($($more,)*); + ); +} + +fix_array_impl_recursive!(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, 40, 48, 56, 64, 72, 96, 128, 160, 192, 224,); + diff --git a/src/lib.rs b/src/lib.rs index 6559632..a100ad8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,51 +16,18 @@ use std::borrow::{Borrow, BorrowMut}; use std::hash::{Hash, Hasher}; use std::fmt; -/// Trait for fixed size arrays. -pub unsafe trait Array { - /// The array's element type - type Item; - #[doc(hidden)] - unsafe fn new() -> Self; - #[doc(hidden)] - fn as_ptr(&self) -> *const Self::Item; - #[doc(hidden)] - fn as_mut_ptr(&mut self) -> *mut Self::Item; - #[doc(hidden)] - fn capacity() -> usize; -} +mod array; +pub use array::Array; -macro_rules! fix_array_impl { - ($len:expr ) => ( - unsafe impl Array for [T; $len] { - type Item = T; - /// Note: Returnin an uninitialized value here only works - /// if we can be sure the data is never used. The nullable pointer - /// inside enum optimization conflicts with this this for example, - /// so we need to be extra careful. See `Flag` enum. - unsafe fn new() -> [T; $len] { mem::uninitialized() } - #[inline(always)] - fn as_ptr(&self) -> *const T { self as *const _ as *const _ } - #[inline(always)] - fn as_mut_ptr(&mut self) -> *mut T { self as *mut _ as *mut _} - #[inline(always)] - fn capacity() -> usize { $len } - } - ) -} -macro_rules! fix_array_impl_recursive { - () => (); - ($len:expr, $($more:expr,)*) => ( - fix_array_impl!($len); - fix_array_impl_recursive!($($more,)*); - ); +unsafe fn new_array() -> A { + // Note: Returning an uninitialized value here only works + // if we can be sure the data is never used. The nullable pointer + // inside enum optimization conflicts with this this for example, + // so we need to be extra careful. See `Flag` enum. + mem::uninitialized() } -fix_array_impl_recursive!(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, 40, 48, 56, 64, 72, 96, 128, 160, 192, 224,); - /// A vector with a fixed capacity. /// /// The **ArrayVec** is a vector backed by a fixed size array and keeps track of @@ -103,7 +70,7 @@ impl ArrayVec { /// ``` pub fn new() -> ArrayVec { unsafe { - ArrayVec { xs: NoDrop::new(Array::new()), len: 0 } + ArrayVec { xs: NoDrop::new(new_array()), len: 0 } } }