Use const Box::default in P::<[T]>::new

This commit is contained in:
Josh Stone 2022-04-11 11:44:13 -07:00
parent a2902ebe57
commit 2d5eda8fb0
2 changed files with 3 additions and 8 deletions

View file

@ -9,6 +9,8 @@
test(attr(deny(warnings)))
)]
#![feature(box_patterns)]
#![feature(const_default_impls)]
#![feature(const_trait_impl)]
#![feature(crate_visibility_modifier)]
#![feature(if_let_guard)]
#![feature(label_break_value)]

View file

@ -128,14 +128,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> {
impl<T> P<[T]> {
pub const fn new() -> P<[T]> {
// HACK(eddyb) bypass the lack of a `const fn` to create an empty `Box<[T]>`
// (as trait methods, `default` in this case, can't be `const fn` yet).
P {
ptr: unsafe {
use std::ptr::NonNull;
std::mem::transmute(NonNull::<[T; 0]>::dangling() as NonNull<[T]>)
},
}
P { ptr: Box::default() }
}
#[inline(never)]