Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow an inplace update to prevent a clone #116

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <william@blackhats.net.au>"]
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
Loading