Skip to content

Commit

Permalink
Allow an inplace update to prevent a clone
Browse files Browse the repository at this point in the history
  • Loading branch information
Firstyear committed May 30, 2024
1 parent c3d91cd commit c12d7f2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "concread"
version = "0.5.0"
version = "0.5.1"
authors = ["William Brown <[email protected]>"]
edition = "2021"
description = "Concurrently Readable Data-Structures for Rust"
Expand Down
6 changes: 6 additions & 0 deletions src/cowcell/asynch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ where
self.work.as_mut().expect("can not fail")
}

/// Update the inner value with a new one. This function exists to prevent a clone
/// in the case where you take a read transaction and would otherwise use `mem::swap`
pub fn replace(&mut self, value: T) {
self.work = Some(value);
}

/// Commit the changes made in this write transactions to the `CowCell`.
/// This will consume the transaction so no further changes can be made
/// after this is called. Not calling this in a block, is equivalent to
Expand Down
7 changes: 6 additions & 1 deletion src/cowcell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ where
/// Access a mutable pointer of the data in the `CowCell`. This data is only
/// visible to the write transaction object in this thread, until you call
/// `commit()`.
#[inline(always)]
pub fn get_mut(&mut self) -> &mut T {
if self.work.is_none() {
let mut data: Option<T> = Some((*self.read).clone());
Expand All @@ -190,6 +189,12 @@ where
self.work.as_mut().expect("can not fail")
}

/// Update the inner value with a new one. This function exists to prevent a clone
/// in the case where you take a read transaction and would otherwise use `mem::swap`
pub fn replace(&mut self, value: T) {
self.work = Some(value);
}

/// Commit the changes made in this write transactions to the `CowCell`.
/// This will consume the transaction so no further changes can be made
/// after this is called. Not calling this in a block, is equivalent to
Expand Down

0 comments on commit c12d7f2

Please sign in to comment.