Skip to content

Commit

Permalink
Auto merge of #204 - c410-f3r:const__generics, r=mbrubeck
Browse files Browse the repository at this point in the history
Add support for constant generics

No breaking changes and no internal hacks. This PR only adds a feature to switch between implementations of the `Array` trait.

I personally think that this approach is pretty reasonable and should be at least considered for discussion.
  • Loading branch information
bors-servo committed Mar 18, 2020
2 parents 02cdf24 + 70e967e commit 9cdf8fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -12,6 +12,7 @@ readme = "README.md"
documentation = "https://docs.rs/smallvec/"

[features]
const_generics = []
write = []
union = []
specialization = []
Expand Down
16 changes: 16 additions & 0 deletions lib.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -1667,6 +1669,13 @@ impl<'a> Drop for SetLenOnDrop<'a> {
}
}

#[cfg(feature = "const_generics")]
unsafe impl<T, const N: usize> Array for [T; N] {
type Item = T;
fn size() -> usize { N }
}

#[cfg(not(feature = "const_generics"))]
macro_rules! impl_array(
($($size:expr),+) => {
$(
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 9cdf8fb

Please sign in to comment.