Skip to content

Support generic-array as arrayvec backend #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ matrix:
- rust: 1.2.0
- rust: stable
env:
- FEATURES="use_generic_array"
- NODEFAULT=1
- rust: beta
- rust: nightly
Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
25 changes: 25 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ pub trait Index : PartialEq + Copy {
fn from(usize) -> Self;
}

#[cfg(feature = "use_generic_array")]
unsafe impl<T, U> Array for ::generic_array::GenericArray<T, U>
where U: ::generic_array::ArrayLength<T>
{
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 }
Expand All @@ -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<T> Array for [T; $len] {
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 23 additions & 0 deletions tests/generic_array.rs
Original file line number Diff line number Diff line change
@@ -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<GenericArray<i32, U41>> = 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]);
}