use const trick
This commit is contained in:
parent
0212e0230a
commit
e5280e452f
2 changed files with 7 additions and 24 deletions
|
@ -54,28 +54,18 @@ pub struct RawVec<T, A: Alloc = Global> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A: Alloc> RawVec<T, A> {
|
impl<T, A: Alloc> RawVec<T, A> {
|
||||||
// FIXME: this should be made `const` when `if` statements are allowed
|
|
||||||
/// Like `new` but parameterized over the choice of allocator for
|
/// Like `new` but parameterized over the choice of allocator for
|
||||||
/// the returned RawVec.
|
/// the returned RawVec.
|
||||||
pub fn new_in(a: A) -> Self {
|
pub const fn new_in(a: A) -> Self {
|
||||||
// !0 is usize::MAX. This branch should be stripped at compile time.
|
// !0 is usize::MAX. This branch should be stripped at compile time.
|
||||||
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
|
// FIXME(mark-i-m): use this line when `if`s are allowed in `const`
|
||||||
|
//let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
|
||||||
|
|
||||||
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
|
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
|
||||||
RawVec {
|
RawVec {
|
||||||
ptr: Unique::empty(),
|
ptr: Unique::empty(),
|
||||||
cap,
|
// FIXME(mark-i-m): use `cap` when ifs are allowed in const
|
||||||
a,
|
cap: [0, !0][(mem::size_of::<T>() != 0) as usize],
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: this should removed when `new_in` can be made `const`
|
|
||||||
/// Like `empty` but parametrized over the choice of allocator for the returned `RawVec`.
|
|
||||||
pub const fn empty_in(a: A) -> Self {
|
|
||||||
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
|
|
||||||
RawVec {
|
|
||||||
ptr: Unique::empty(),
|
|
||||||
cap: 0,
|
|
||||||
a,
|
a,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,17 +122,10 @@ impl<T> RawVec<T, Global> {
|
||||||
/// RawVec with capacity 0. If T has 0 size, then it makes a
|
/// RawVec with capacity 0. If T has 0 size, then it makes a
|
||||||
/// RawVec with capacity `usize::MAX`. Useful for implementing
|
/// RawVec with capacity `usize::MAX`. Useful for implementing
|
||||||
/// delayed allocation.
|
/// delayed allocation.
|
||||||
pub fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self::new_in(Global)
|
Self::new_in(Global)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: this should removed when `new` can be made `const`
|
|
||||||
/// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
|
|
||||||
/// allocating.
|
|
||||||
pub const fn empty() -> Self {
|
|
||||||
Self::empty_in(Global)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a RawVec (on the system heap) with exactly the
|
/// Creates a RawVec (on the system heap) with exactly the
|
||||||
/// capacity and alignment requirements for a `[T; cap]`. This is
|
/// capacity and alignment requirements for a `[T; cap]`. This is
|
||||||
/// equivalent to calling RawVec::new when `cap` is 0 or T is
|
/// equivalent to calling RawVec::new when `cap` is 0 or T is
|
||||||
|
|
|
@ -325,7 +325,7 @@ impl<T> Vec<T> {
|
||||||
#[rustc_const_unstable(feature = "const_vec_new")]
|
#[rustc_const_unstable(feature = "const_vec_new")]
|
||||||
pub const fn new() -> Vec<T> {
|
pub const fn new() -> Vec<T> {
|
||||||
Vec {
|
Vec {
|
||||||
buf: RawVec::empty(),
|
buf: RawVec::new(),
|
||||||
len: 0,
|
len: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue