|
48 | 48 | //! When this feature is enabled, `SmallVec` works with any arrays of any size, not just a fixed
|
49 | 49 | //! list of sizes.
|
50 | 50 | //!
|
| 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 | +//! |
51 | 59 | //! ### `specialization`
|
52 | 60 | //!
|
53 | 61 | //! **This feature is unstable and requires a nightly build of the Rust toolchain.**
|
|
67 | 75 | //! [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch).
|
68 | 76 | //!
|
69 | 77 | //! 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) |
80 | 78 |
|
81 | 79 | #![no_std]
|
82 | 80 | #![cfg_attr(docsrs, feature(doc_cfg))]
|
83 | 81 | #![cfg_attr(feature = "specialization", allow(incomplete_features))]
|
84 | 82 | #![cfg_attr(feature = "specialization", feature(specialization))]
|
85 | 83 | #![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
|
86 |
| -#![cfg_attr(feature = "const_new", feature(const_fn_trait_bound))] |
87 | 84 | #![deny(missing_docs)]
|
88 | 85 |
|
89 | 86 | #[doc(hidden)]
|
@@ -413,10 +410,10 @@ union SmallVecData<A: Array> {
|
413 | 410 | }
|
414 | 411 |
|
415 | 412 | #[cfg(all(feature = "union", feature = "const_new"))]
|
416 |
| -impl<A: Array> SmallVecData<A> { |
| 413 | +impl<T, const N: usize> SmallVecData<[T; N]> { |
417 | 414 | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
|
418 | 415 | #[inline]
|
419 |
| - const fn from_const(inline: MaybeUninit<A>) -> SmallVecData<A> { |
| 416 | + const fn from_const(inline: MaybeUninit<[T; N]>) -> Self { |
420 | 417 | SmallVecData {
|
421 | 418 | inline: core::mem::ManuallyDrop::new(inline),
|
422 | 419 | }
|
@@ -464,10 +461,10 @@ enum SmallVecData<A: Array> {
|
464 | 461 | }
|
465 | 462 |
|
466 | 463 | #[cfg(all(not(feature = "union"), feature = "const_new"))]
|
467 |
| -impl<A: Array> SmallVecData<A> { |
| 464 | +impl<T, const N: usize> SmallVecData<[T; N]> { |
468 | 465 | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
|
469 | 466 | #[inline]
|
470 |
| - const fn from_const(inline: MaybeUninit<A>) -> SmallVecData<A> { |
| 467 | + const fn from_const(inline: MaybeUninit<[T; N]>) -> Self { |
471 | 468 | SmallVecData::Inline(inline)
|
472 | 469 | }
|
473 | 470 | }
|
@@ -1408,27 +1405,6 @@ impl<A: Array> SmallVec<A> {
|
1408 | 1405 | }
|
1409 | 1406 | }
|
1410 | 1407 |
|
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 |
| - |
1432 | 1408 | impl<A: Array> SmallVec<A>
|
1433 | 1409 | where
|
1434 | 1410 | A::Item: Copy,
|
@@ -2055,11 +2031,24 @@ impl<'a> Drop for SetLenOnDrop<'a> {
|
2055 | 2031 |
|
2056 | 2032 | #[cfg(feature = "const_new")]
|
2057 | 2033 | 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 | + |
2058 | 2046 | /// The array passed as an argument is moved to be an inline version of `SmallVec`.
|
| 2047 | + /// |
2059 | 2048 | /// 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.
|
2060 | 2049 | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))]
|
2061 | 2050 | #[inline]
|
2062 |
| - pub const fn from_const(items: [T; N]) -> SmallVec<[T; N]> { |
| 2051 | + pub const fn from_const(items: [T; N]) -> Self { |
2063 | 2052 | SmallVec {
|
2064 | 2053 | capacity: N,
|
2065 | 2054 | data: SmallVecData::from_const(MaybeUninit::new(items)),
|
|
0 commit comments