From c49be7b254867d8dcc88a93b16f1f76697274c2a Mon Sep 17 00:00:00 2001 From: James McGlashan Date: Sat, 18 Jan 2020 00:00:00 +0000 Subject: [PATCH] add nightly_const_generics flag --- Cargo.toml | 3 +++ src/array.rs | 16 ++++++++++++++++ src/lib.rs | 4 ++++ 3 files changed, 23 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 8cd927a..ee615cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,9 @@ alloc = [] # 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)]