|
67 | 67 | //! [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch).
|
68 | 68 | //!
|
69 | 69 | //! 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 function [`SmallVec::const_new`] which is a `const fn` so the `SmallVec` may be used 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) |
70 | 80 |
|
71 | 81 | #![no_std]
|
72 | 82 | #![cfg_attr(feature = "specialization", allow(incomplete_features))]
|
73 | 83 | #![cfg_attr(feature = "specialization", feature(specialization))]
|
74 | 84 | #![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
|
| 85 | +#![cfg_attr(feature = "const_new", feature(const_fn_trait_bound))] |
75 | 86 | #![deny(missing_docs)]
|
76 | 87 |
|
77 | 88 | #[doc(hidden)]
|
@@ -353,6 +364,16 @@ union SmallVecData<A: Array> {
|
353 | 364 | heap: (*mut A::Item, usize),
|
354 | 365 | }
|
355 | 366 |
|
| 367 | +#[cfg(all(feature = "union", feature = "const_new"))] |
| 368 | +impl<A: Array> SmallVecData<A> { |
| 369 | + #[inline] |
| 370 | + const fn from_const(inline: MaybeUninit<A>) -> SmallVecData<A> { |
| 371 | + SmallVecData { |
| 372 | + inline: core::mem::ManuallyDrop::new(inline), |
| 373 | + } |
| 374 | + } |
| 375 | +} |
| 376 | + |
356 | 377 | #[cfg(feature = "union")]
|
357 | 378 | impl<A: Array> SmallVecData<A> {
|
358 | 379 | #[inline]
|
@@ -393,6 +414,14 @@ enum SmallVecData<A: Array> {
|
393 | 414 | Heap((*mut A::Item, usize)),
|
394 | 415 | }
|
395 | 416 |
|
| 417 | +#[cfg(all(not(feature = "union"), feature = "const_new"))] |
| 418 | +impl<A: Array> SmallVecData<A> { |
| 419 | + #[inline] |
| 420 | + const fn from_const(inline: MaybeUninit<A>) -> SmallVecData<A> { |
| 421 | + SmallVecData::Inline(inline) |
| 422 | + } |
| 423 | +} |
| 424 | + |
396 | 425 | #[cfg(not(feature = "union"))]
|
397 | 426 | impl<A: Array> SmallVecData<A> {
|
398 | 427 | #[inline]
|
@@ -1329,6 +1358,26 @@ impl<A: Array> SmallVec<A> {
|
1329 | 1358 | }
|
1330 | 1359 | }
|
1331 | 1360 |
|
| 1361 | +#[cfg(feature = "const_new")] |
| 1362 | +impl<A: Array> SmallVec<A> { |
| 1363 | + /// Construct an empty vector. |
| 1364 | + /// |
| 1365 | + /// # Safety |
| 1366 | + /// No size validation is attempted for this function. |
| 1367 | + /// Invalid custom implementations of [`Array`] normally panics during [`new`]. |
| 1368 | + /// `new_const` will still initialize which may cause undefined behavior (such as segmentation errors) when used with invalid implementations. |
| 1369 | + /// |
| 1370 | + /// [`Array`]: crate::Array |
| 1371 | + /// [`new`]: crate::SmallVec::new |
| 1372 | + #[inline] |
| 1373 | + pub const unsafe fn new_const() -> SmallVec<A> { |
| 1374 | + SmallVec { |
| 1375 | + capacity: 0, |
| 1376 | + data: SmallVecData::from_const(MaybeUninit::uninit()), |
| 1377 | + } |
| 1378 | + } |
| 1379 | +} |
| 1380 | + |
1332 | 1381 | impl<A: Array> SmallVec<A>
|
1333 | 1382 | where
|
1334 | 1383 | A::Item: Copy,
|
|
0 commit comments