From 60a97632bc19001166cc90304d2dcdd18c5aa53c Mon Sep 17 00:00:00 2001 From: bluss Date: Mon, 26 Sep 2016 15:42:02 +0200 Subject: [PATCH] Support generic-array as arrayvec backend --- .travis.yml | 3 ++- Cargo.toml | 5 +++++ src/array.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 8 ++++++++ tests/generic_array.rs | 23 +++++++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/generic_array.rs diff --git a/.travis.yml b/.travis.yml index 5356510f..bf91f076 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ matrix: - rust: 1.2.0 - rust: stable env: + - FEATURES="use_generic_array" - NODEFAULT=1 - rust: beta - rust: nightly @@ -15,7 +16,7 @@ matrix: - NODROP_FEATURES='use_needs_drop' - rust: nightly env: - - FEATURES='use_union' + - FEATURES='use_union use_generic_array' - NODROP_FEATURES='use_union' branches: only: diff --git a/Cargo.toml b/Cargo.toml index a6e51562..e8ddca4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,12 @@ version = "0.1.8" path = "nodrop" default-features = false +[dependencies.generic-array] +version = "0.5.1" +optional = true + [features] default = ["std"] std = ["odds/std", "nodrop/std"] use_union = ["nodrop/use_union"] +use_generic_array = ["generic-array"] diff --git a/src/array.rs b/src/array.rs index e5ce1cc7..31150b48 100644 --- a/src/array.rs +++ b/src/array.rs @@ -19,6 +19,24 @@ pub trait Index : PartialEq + Copy { fn from(usize) -> Self; } +#[cfg(feature = "use_generic_array")] +unsafe impl Array for ::generic_array::GenericArray + where U: ::generic_array::ArrayLength +{ + type Item = T; + type Index = usize; + fn as_ptr(&self) -> *const Self::Item { + (**self).as_ptr() + } + fn as_mut_ptr(&mut self) -> *mut Self::Item { + (**self).as_mut_ptr() + } + fn capacity() -> usize { + U::to_usize() + } + +} + impl Index for u8 { #[inline(always)] fn to_usize(self) -> usize { self as usize } @@ -33,6 +51,13 @@ impl Index for u16 { fn from(ix: usize) -> Self { ix as u16 } } +impl Index for usize { + #[inline(always)] + fn to_usize(self) -> usize { self } + #[inline(always)] + fn from(ix: usize) -> Self { ix } +} + macro_rules! fix_array_impl { ($index_type:ty, $len:expr ) => ( unsafe impl Array for [T; $len] { diff --git a/src/lib.rs b/src/lib.rs index 1bb33ae9..dafc0a58 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,10 +13,18 @@ //! - Requires Rust nightly channel //! - Use the unstable feature untagged unions for the internal implementation, //! which has reduced space overhead +//! +//! - `use_generic_array` +//! - Optional +//! - Depend on generic-array and allow using it just like a fixed +//! size array for ArrayVec storage. #![cfg_attr(not(feature="std"), no_std)] extern crate odds; extern crate nodrop; +#[cfg(feature = "use_generic_array")] +extern crate generic_array; + #[cfg(not(feature="std"))] extern crate core as std; diff --git a/tests/generic_array.rs b/tests/generic_array.rs new file mode 100644 index 00000000..3857f1eb --- /dev/null +++ b/tests/generic_array.rs @@ -0,0 +1,23 @@ +#![cfg(feature = "use_generic_array")] + +extern crate arrayvec; +#[macro_use] +extern crate generic_array; + +use arrayvec::ArrayVec; + +use generic_array::GenericArray; + +use generic_array::typenum::U41; + +#[test] +fn test_simple() { + let mut vec: ArrayVec> = ArrayVec::new(); + + assert_eq!(vec.len(), 0); + assert_eq!(vec.capacity(), 41); + vec.extend(0..20); + assert_eq!(vec.len(), 20); + assert_eq!(&vec[..5], &[0, 1, 2, 3, 4]); +} +