Skip to content

Commit

Permalink
Basic support to generic array (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohann authored Jan 29, 2025
1 parent 659e6c1 commit 9726c64
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tinyvec_macros = { version = "0.1", optional = true }
serde = { version = "1.0", optional = true, default-features = false }
# Provides derived `Arbitrary` implementations
arbitrary = { version = "1", optional = true }
# Implements the trait `Array` for `GenericArray` struct.
generic-array = { version = "1.1.1", optional = true, default-features = false }

[features]
default = []
Expand Down
6 changes: 6 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ pub trait Array {
fn default() -> Self;
}

#[cfg(all(feature = "generic-array", not(feature = "rustc_1_55")))]
core::compile_error!("generic-array requires `rustc_1_55` feature");

#[cfg(feature = "rustc_1_55")]
mod const_generic_impl;

#[cfg(not(feature = "rustc_1_55"))]
mod generated_impl;

#[cfg(feature = "generic-array")]
mod generic_array_impl;
26 changes: 26 additions & 0 deletions src/array/generic_array_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use core::default;

use super::Array;
use generic_array::{ArrayLength, GenericArray};

impl<T: Default, N: ArrayLength> Array for GenericArray<T, N> {
type Item = T;
const CAPACITY: usize = N::USIZE;

#[inline(always)]
#[must_use]
fn as_slice(&self) -> &[T] {
&*self
}

#[inline(always)]
#[must_use]
fn as_slice_mut(&mut self) -> &mut [T] {
&mut *self
}

#[inline(always)]
fn default() -> Self {
<Self as Default>::default()
}
}

0 comments on commit 9726c64

Please sign in to comment.