From 70e967edaaa6801cc79c497442d58e861ffd8f75 Mon Sep 17 00:00:00 2001 From: Caio Date: Tue, 17 Mar 2020 17:37:31 -0300 Subject: [PATCH] Add support for constant generics --- Cargo.toml | 1 + lib.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index b09d63f..b5884fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ readme = "README.md" documentation = "https://docs.rs/smallvec/" [features] +const_generics = [] write = [] union = [] specialization = [] diff --git a/lib.rs b/lib.rs index d43700a..d3e6a71 100644 --- a/lib.rs +++ b/lib.rs @@ -29,6 +29,8 @@ #![cfg_attr(feature = "union", feature(untagged_unions))] #![cfg_attr(feature = "specialization", feature(specialization))] #![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))] +#![cfg_attr(feature = "const_generics", allow(incomplete_features))] +#![cfg_attr(feature = "const_generics", feature(const_generics))] #![deny(missing_docs)] #[doc(hidden)] @@ -1667,6 +1669,13 @@ impl<'a> Drop for SetLenOnDrop<'a> { } } +#[cfg(feature = "const_generics")] +unsafe impl Array for [T; N] { + type Item = T; + fn size() -> usize { N } +} + +#[cfg(not(feature = "const_generics"))] macro_rules! impl_array( ($($size:expr),+) => { $( @@ -1678,6 +1687,7 @@ macro_rules! impl_array( } ); +#[cfg(not(feature = "const_generics"))] impl_array!( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, 0x40, 0x60, 0x80, 0x100, 0x200, 0x400, 0x600, 0x800, 0x1000, 0x2000, 0x4000, 0x6000, 0x8000, 0x10000, 0x20000, @@ -2533,4 +2543,10 @@ mod tests { assert_eq!(v.capacity(), 4); assert_eq!(v[..], [0, 1, 2]); } + + #[cfg(feature = "const_generics")] + #[test] + fn const_generics() { + let _v = SmallVec::<[i32; 987]>::default(); + } }