Skip to content

Commit 3d70d06

Browse files
committed
Make Vec and BTreeMap constructors const fns
1 parent 94214fa commit 3d70d06

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ pub struct FrozenBTreeMap<K, V> {
311311
// safety: UnsafeCell implies !Sync
312312

313313
impl<K: Clone + Ord, V: StableDeref> FrozenBTreeMap<K, V> {
314-
pub fn new() -> Self {
314+
pub const fn new() -> Self {
315315
Self {
316-
map: UnsafeCell::new(Default::default()),
316+
map: UnsafeCell::new(BTreeMap::new()),
317317
in_use: Cell::new(false),
318318
}
319319
}

src/sync.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,10 @@ impl<T> FrozenVec<T> {
473473
}
474474

475475
impl<T: StableDeref> FrozenVec<T> {
476-
pub fn new() -> Self {
477-
Default::default()
476+
pub const fn new() -> Self {
477+
Self {
478+
vec: RwLock::new(Vec::new()),
479+
}
478480
}
479481

480482
// these should never return &T
@@ -703,11 +705,7 @@ impl<T: Copy> Default for LockFreeFrozenVec<T> {
703705
/// Creates an empty `LockFreeFrozenVec` that does not allocate
704706
/// any heap allocations until the first time data is pushed to it.
705707
fn default() -> Self {
706-
Self {
707-
data: Self::null(),
708-
len: AtomicUsize::new(0),
709-
locked: AtomicBool::new(false),
710-
}
708+
Self::new()
711709
}
712710
}
713711

@@ -716,8 +714,12 @@ impl<T: Copy> LockFreeFrozenVec<T> {
716714
[const { AtomicPtr::new(std::ptr::null_mut()) }; NUM_BUFFERS]
717715
}
718716

719-
pub fn new() -> Self {
720-
Default::default()
717+
pub const fn new() -> Self {
718+
Self {
719+
data: Self::null(),
720+
len: AtomicUsize::new(0),
721+
locked: AtomicBool::new(false),
722+
}
721723
}
722724

723725
/// Obtains a write lock that ensures other writing threads
@@ -1007,7 +1009,7 @@ fn test_non_lockfree() {
10071009
pub struct FrozenBTreeMap<K, V>(RwLock<BTreeMap<K, V>>);
10081010

10091011
impl<K: Clone + Ord, V: StableDeref> FrozenBTreeMap<K, V> {
1010-
pub fn new() -> Self {
1012+
pub const fn new() -> Self {
10111013
Self(RwLock::new(BTreeMap::new()))
10121014
}
10131015

src/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pub struct FrozenVec<T> {
1717

1818
impl<T> FrozenVec<T> {
1919
/// Constructs a new, empty vector.
20-
pub fn new() -> Self {
20+
pub const fn new() -> Self {
2121
Self {
22-
vec: UnsafeCell::new(Default::default()),
22+
vec: UnsafeCell::new(Vec::new()),
2323
}
2424
}
2525
}

0 commit comments

Comments
 (0)