Skip to content

Commit 8973eab

Browse files
committed
limit feature const_new to arrays
1 parent 11e34ee commit 8973eab

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

src/lib.rs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
//! When this feature is enabled, `SmallVec` works with any arrays of any size, not just a fixed
4949
//! list of sizes.
5050
//!
51+
//! ### `const_new`
52+
//!
53+
//! **This feature requires Rust 1.51.**
54+
//!
55+
//! This feature exposes the functions [`SmallVec::new_const`], [`SmallVec::from_const`], and [`smallvec_inline`] which enables the `SmallVec` to be initialized from a const context.
56+
//! For details, see the
57+
//! [Rust Reference](https://doc.rust-lang.org/reference/const_eval.html#const-functions).
58+
//!
5159
//! ### `specialization`
5260
//!
5361
//! **This feature is unstable and requires a nightly build of the Rust toolchain.**
@@ -67,23 +75,12 @@
6775
//! [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch).
6876
//!
6977
//! Tracking issue: [rust-lang/rust#34761](https://github.com/rust-lang/rust/issues/34761)
70-
//!
71-
//! ### `const_new`
72-
//!
73-
//! **This feature is unstable and requires a nightly build of the Rust toolchain.**
74-
//!
75-
//! This feature exposes the functions [`SmallVec::new_const`] and [`SmallVec::from_const`] which enables the `SmallVec` to be initialized from a const context.
76-
//! For details, see the
77-
//! [Rust Reference](https://doc.rust-lang.org/reference/const_eval.html#const-functions).
78-
//!
79-
//! Tracking issue: [rust-lang/rust#57563](https://github.com/rust-lang/rust/issues/57563)
8078
8179
#![no_std]
8280
#![cfg_attr(docsrs, feature(doc_cfg))]
8381
#![cfg_attr(feature = "specialization", allow(incomplete_features))]
8482
#![cfg_attr(feature = "specialization", feature(specialization))]
8583
#![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
86-
#![cfg_attr(feature = "const_new", feature(const_fn_trait_bound))]
8784
#![deny(missing_docs)]
8885

8986
#[doc(hidden)]
@@ -413,10 +410,10 @@ union SmallVecData<A: Array> {
413410
}
414411

415412
#[cfg(all(feature = "union", feature = "const_new"))]
416-
impl<A: Array> SmallVecData<A> {
413+
impl<T, const N: usize> SmallVecData<[T; N]> {
417414
#[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
418415
#[inline]
419-
const fn from_const(inline: MaybeUninit<A>) -> SmallVecData<A> {
416+
const fn from_const(inline: MaybeUninit<[T; N]>) -> Self {
420417
SmallVecData {
421418
inline: core::mem::ManuallyDrop::new(inline),
422419
}
@@ -464,10 +461,10 @@ enum SmallVecData<A: Array> {
464461
}
465462

466463
#[cfg(all(not(feature = "union"), feature = "const_new"))]
467-
impl<A: Array> SmallVecData<A> {
464+
impl<T, const N: usize> SmallVecData<[T; N]> {
468465
#[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
469466
#[inline]
470-
const fn from_const(inline: MaybeUninit<A>) -> SmallVecData<A> {
467+
const fn from_const(inline: MaybeUninit<[T; N]>) -> Self {
471468
SmallVecData::Inline(inline)
472469
}
473470
}
@@ -1408,27 +1405,6 @@ impl<A: Array> SmallVec<A> {
14081405
}
14091406
}
14101407

1411-
#[cfg(feature = "const_new")]
1412-
impl<A: Array> SmallVec<A> {
1413-
/// Construct an empty vector. This is currently gated behind the feature `const_new`.
1414-
///
1415-
/// # Safety
1416-
/// No size validation is attempted for this function.
1417-
/// Invalid custom implementations of [`Array`] normally panics during [`new`].
1418-
/// `new_const` will still initialize which may cause undefined behavior (such as segmentation errors) when used with invalid implementations.
1419-
///
1420-
/// [`Array`]: crate::Array
1421-
/// [`new`]: crate::SmallVec::new
1422-
#[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
1423-
#[inline]
1424-
pub const unsafe fn new_const() -> SmallVec<A> {
1425-
SmallVec {
1426-
capacity: 0,
1427-
data: SmallVecData::from_const(MaybeUninit::uninit()),
1428-
}
1429-
}
1430-
}
1431-
14321408
impl<A: Array> SmallVec<A>
14331409
where
14341410
A::Item: Copy,
@@ -2055,11 +2031,24 @@ impl<'a> Drop for SetLenOnDrop<'a> {
20552031

20562032
#[cfg(feature = "const_new")]
20572033
impl<T, const N: usize> SmallVec<[T; N]> {
2034+
/// Construct an empty vector.
2035+
///
2036+
/// This is a `const` version of [`SmallVec::new`] that is enabled by the feature `const_new`, with the limitation that it only works for arrays.
2037+
#[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
2038+
#[inline]
2039+
pub const fn new_const() -> Self {
2040+
SmallVec {
2041+
capacity: 0,
2042+
data: SmallVecData::from_const(MaybeUninit::uninit()),
2043+
}
2044+
}
2045+
20582046
/// The array passed as an argument is moved to be an inline version of `SmallVec`.
2047+
///
20592048
/// This is a `const` version of [`SmallVec::from_buf`] that is enabled by the feature `const_new`, with the limitation that it only works for arrays.
20602049
#[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
20612050
#[inline]
2062-
pub const fn from_const(items: [T; N]) -> SmallVec<[T; N]> {
2051+
pub const fn from_const(items: [T; N]) -> Self {
20632052
SmallVec {
20642053
capacity: N,
20652054
data: SmallVecData::from_const(MaybeUninit::new(items)),

src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ fn const_new() {
921921
}
922922
#[cfg(feature = "const_new")]
923923
const fn const_new_inner() -> SmallVec<[i32; 4]> {
924-
unsafe { SmallVec::<[i32; 4]>::new_const() }
924+
SmallVec::<[i32; 4]>::new_const()
925925
}
926926
#[cfg(feature = "const_new")]
927927
const fn const_new_inline_sized() -> SmallVec<[i32; 4]> {

0 commit comments

Comments
 (0)