Skip to content

Commit 3d2dcb4

Browse files
authored
Merge pull request #41 from bluss/generic-array
Support generic-array as arrayvec backend
2 parents 82ab295 + 60a9763 commit 3d2dcb4

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ matrix:
55
- rust: 1.2.0
66
- rust: stable
77
env:
8+
- FEATURES="use_generic_array"
89
- NODEFAULT=1
910
- rust: beta
1011
- rust: nightly
@@ -15,7 +16,7 @@ matrix:
1516
- NODROP_FEATURES='use_needs_drop'
1617
- rust: nightly
1718
env:
18-
- FEATURES='use_union'
19+
- FEATURES='use_union use_generic_array'
1920
- NODROP_FEATURES='use_union'
2021
branches:
2122
only:

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ version = "0.1.8"
1919
path = "nodrop"
2020
default-features = false
2121

22+
[dependencies.generic-array]
23+
version = "0.5.1"
24+
optional = true
25+
2226
[features]
2327
default = ["std"]
2428
std = ["odds/std", "nodrop/std"]
2529
use_union = ["nodrop/use_union"]
30+
use_generic_array = ["generic-array"]

src/array.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ pub trait Index : PartialEq + Copy {
1919
fn from(usize) -> Self;
2020
}
2121

22+
#[cfg(feature = "use_generic_array")]
23+
unsafe impl<T, U> Array for ::generic_array::GenericArray<T, U>
24+
where U: ::generic_array::ArrayLength<T>
25+
{
26+
type Item = T;
27+
type Index = usize;
28+
fn as_ptr(&self) -> *const Self::Item {
29+
(**self).as_ptr()
30+
}
31+
fn as_mut_ptr(&mut self) -> *mut Self::Item {
32+
(**self).as_mut_ptr()
33+
}
34+
fn capacity() -> usize {
35+
U::to_usize()
36+
}
37+
38+
}
39+
2240
impl Index for u8 {
2341
#[inline(always)]
2442
fn to_usize(self) -> usize { self as usize }
@@ -33,6 +51,13 @@ impl Index for u16 {
3351
fn from(ix: usize) -> Self { ix as u16 }
3452
}
3553

54+
impl Index for usize {
55+
#[inline(always)]
56+
fn to_usize(self) -> usize { self }
57+
#[inline(always)]
58+
fn from(ix: usize) -> Self { ix }
59+
}
60+
3661
macro_rules! fix_array_impl {
3762
($index_type:ty, $len:expr ) => (
3863
unsafe impl<T> Array for [T; $len] {

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313
//! - Requires Rust nightly channel
1414
//! - Use the unstable feature untagged unions for the internal implementation,
1515
//! which has reduced space overhead
16+
//!
17+
//! - `use_generic_array`
18+
//! - Optional
19+
//! - Depend on generic-array and allow using it just like a fixed
20+
//! size array for ArrayVec storage.
1621
#![cfg_attr(not(feature="std"), no_std)]
1722
extern crate odds;
1823
extern crate nodrop;
1924

25+
#[cfg(feature = "use_generic_array")]
26+
extern crate generic_array;
27+
2028
#[cfg(not(feature="std"))]
2129
extern crate core as std;
2230

tests/generic_array.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![cfg(feature = "use_generic_array")]
2+
3+
extern crate arrayvec;
4+
#[macro_use]
5+
extern crate generic_array;
6+
7+
use arrayvec::ArrayVec;
8+
9+
use generic_array::GenericArray;
10+
11+
use generic_array::typenum::U41;
12+
13+
#[test]
14+
fn test_simple() {
15+
let mut vec: ArrayVec<GenericArray<i32, U41>> = ArrayVec::new();
16+
17+
assert_eq!(vec.len(), 0);
18+
assert_eq!(vec.capacity(), 41);
19+
vec.extend(0..20);
20+
assert_eq!(vec.len(), 20);
21+
assert_eq!(&vec[..5], &[0, 1, 2, 3, 4]);
22+
}
23+

0 commit comments

Comments
 (0)