Skip to content

Commit 9726c64

Browse files
authored
Basic support to generic array (Lokathor#204)
1 parent 659e6c1 commit 9726c64

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ tinyvec_macros = { version = "0.1", optional = true }
1616
serde = { version = "1.0", optional = true, default-features = false }
1717
# Provides derived `Arbitrary` implementations
1818
arbitrary = { version = "1", optional = true }
19+
# Implements the trait `Array` for `GenericArray` struct.
20+
generic-array = { version = "1.1.1", optional = true, default-features = false }
1921

2022
[features]
2123
default = []

src/array.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ pub trait Array {
4141
fn default() -> Self;
4242
}
4343

44+
#[cfg(all(feature = "generic-array", not(feature = "rustc_1_55")))]
45+
core::compile_error!("generic-array requires `rustc_1_55` feature");
46+
4447
#[cfg(feature = "rustc_1_55")]
4548
mod const_generic_impl;
4649

4750
#[cfg(not(feature = "rustc_1_55"))]
4851
mod generated_impl;
52+
53+
#[cfg(feature = "generic-array")]
54+
mod generic_array_impl;

src/array/generic_array_impl.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use core::default;
2+
3+
use super::Array;
4+
use generic_array::{ArrayLength, GenericArray};
5+
6+
impl<T: Default, N: ArrayLength> Array for GenericArray<T, N> {
7+
type Item = T;
8+
const CAPACITY: usize = N::USIZE;
9+
10+
#[inline(always)]
11+
#[must_use]
12+
fn as_slice(&self) -> &[T] {
13+
&*self
14+
}
15+
16+
#[inline(always)]
17+
#[must_use]
18+
fn as_slice_mut(&mut self) -> &mut [T] {
19+
&mut *self
20+
}
21+
22+
#[inline(always)]
23+
fn default() -> Self {
24+
<Self as Default>::default()
25+
}
26+
}

0 commit comments

Comments
 (0)