Skip to content

Commit 9cdf8fb

Browse files
author
bors-servo
authored
Auto merge of #204 - c410-f3r:const__generics, r=mbrubeck
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.
2 parents 02cdf24 + 70e967e commit 9cdf8fb

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ readme = "README.md"
1212
documentation = "https://docs.rs/smallvec/"
1313

1414
[features]
15+
const_generics = []
1516
write = []
1617
union = []
1718
specialization = []

lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#![cfg_attr(feature = "union", feature(untagged_unions))]
3030
#![cfg_attr(feature = "specialization", feature(specialization))]
3131
#![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
32+
#![cfg_attr(feature = "const_generics", allow(incomplete_features))]
33+
#![cfg_attr(feature = "const_generics", feature(const_generics))]
3234
#![deny(missing_docs)]
3335

3436
#[doc(hidden)]
@@ -1667,6 +1669,13 @@ impl<'a> Drop for SetLenOnDrop<'a> {
16671669
}
16681670
}
16691671

1672+
#[cfg(feature = "const_generics")]
1673+
unsafe impl<T, const N: usize> Array for [T; N] {
1674+
type Item = T;
1675+
fn size() -> usize { N }
1676+
}
1677+
1678+
#[cfg(not(feature = "const_generics"))]
16701679
macro_rules! impl_array(
16711680
($($size:expr),+) => {
16721681
$(
@@ -1678,6 +1687,7 @@ macro_rules! impl_array(
16781687
}
16791688
);
16801689

1690+
#[cfg(not(feature = "const_generics"))]
16811691
impl_array!(
16821692
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, 0x40, 0x60, 0x80,
16831693
0x100, 0x200, 0x400, 0x600, 0x800, 0x1000, 0x2000, 0x4000, 0x6000, 0x8000, 0x10000, 0x20000,
@@ -2533,4 +2543,10 @@ mod tests {
25332543
assert_eq!(v.capacity(), 4);
25342544
assert_eq!(v[..], [0, 1, 2]);
25352545
}
2546+
2547+
#[cfg(feature = "const_generics")]
2548+
#[test]
2549+
fn const_generics() {
2550+
let _v = SmallVec::<[i32; 987]>::default();
2551+
}
25362552
}

0 commit comments

Comments
 (0)