Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for constant generics #204

Merged
merged 1 commit into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}