diff --git a/Cargo.toml b/Cargo.toml index 674f99f..16806d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,9 @@ grab_spare_slice = [] # https://github.com/rust-lang/rust/issues/54279 nightly_slice_partition_dedup = [] +# use const generics for arrays +nightly_const_generics = [] + [badges] appveyor = { repository = "Lokathor/tinyvec" } travis-ci = { repository = "Lokathor/tinyvec" } diff --git a/src/array.rs b/src/array.rs index dcc441f..840e433 100644 --- a/src/array.rs +++ b/src/array.rs @@ -34,6 +34,21 @@ pub trait Array { fn as_slice_mut(&mut self) -> &mut [Self::Item]; } +#[cfg(feature = "nightly_const_generics")] +impl Array for [T; N] { + type Item = T; + const CAPACITY: usize = N; + #[inline(always)] + fn as_slice(&self) -> &[T] { + &*self + } + #[inline(always)] + fn as_slice_mut(&mut self) -> &mut [T] { + &mut *self + } +} + +#[cfg(not(feature = "nightly_const_generics"))] macro_rules! impl_array_for_len { ($($len:expr),+ $(,)?) => { $(impl Array for [T; $len] { @@ -51,6 +66,7 @@ macro_rules! impl_array_for_len { } } +#[cfg(not(feature = "nightly_const_generics"))] impl_array_for_len! { 0, /* The oft-forgotten 0-length array! */ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, diff --git a/src/lib.rs b/src/lib.rs index 2100b1e..b47da38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,10 @@ feature = "nightly_slice_partition_dedup", feature(slice_partition_dedup) )] +#![cfg_attr( + feature = "nightly_const_generics", + feature(const_generics) +)] #![warn(clippy::missing_inline_in_public_items)] #![warn(clippy::must_use_candidate)] #![warn(missing_docs)]