Skip to content

Commit

Permalink
Make Vec and BTreeMap constructors const fns
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Jan 29, 2025
1 parent 94214fa commit 3d70d06
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ pub struct FrozenBTreeMap<K, V> {
// safety: UnsafeCell implies !Sync

impl<K: Clone + Ord, V: StableDeref> FrozenBTreeMap<K, V> {
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
map: UnsafeCell::new(Default::default()),
map: UnsafeCell::new(BTreeMap::new()),
in_use: Cell::new(false),
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,10 @@ impl<T> FrozenVec<T> {
}

impl<T: StableDeref> FrozenVec<T> {
pub fn new() -> Self {
Default::default()
pub const fn new() -> Self {
Self {
vec: RwLock::new(Vec::new()),
}
}

// these should never return &T
Expand Down Expand Up @@ -703,11 +705,7 @@ impl<T: Copy> Default for LockFreeFrozenVec<T> {
/// Creates an empty `LockFreeFrozenVec` that does not allocate
/// any heap allocations until the first time data is pushed to it.
fn default() -> Self {
Self {
data: Self::null(),
len: AtomicUsize::new(0),
locked: AtomicBool::new(false),
}
Self::new()
}
}

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

pub fn new() -> Self {
Default::default()
pub const fn new() -> Self {
Self {
data: Self::null(),
len: AtomicUsize::new(0),
locked: AtomicBool::new(false),
}
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct FrozenVec<T> {

impl<T> FrozenVec<T> {
/// Constructs a new, empty vector.
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
vec: UnsafeCell::new(Default::default()),
vec: UnsafeCell::new(Vec::new()),
}
}
}
Expand Down

0 comments on commit 3d70d06

Please sign in to comment.