From c12d7f2ab11133302892a48dd3b47b67cb790952 Mon Sep 17 00:00:00 2001 From: William Brown Date: Thu, 30 May 2024 15:35:34 +1000 Subject: [PATCH] Allow an inplace update to prevent a clone --- Cargo.toml | 2 +- src/cowcell/asynch.rs | 6 ++++++ src/cowcell/mod.rs | 7 ++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a4d6e0e..ccaafbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "concread" -version = "0.5.0" +version = "0.5.1" authors = ["William Brown "] edition = "2021" description = "Concurrently Readable Data-Structures for Rust" diff --git a/src/cowcell/asynch.rs b/src/cowcell/asynch.rs index bfa3504..de3794f 100644 --- a/src/cowcell/asynch.rs +++ b/src/cowcell/asynch.rs @@ -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 diff --git a/src/cowcell/mod.rs b/src/cowcell/mod.rs index 125d1d0..7cd451e 100644 --- a/src/cowcell/mod.rs +++ b/src/cowcell/mod.rs @@ -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 = Some((*self.read).clone()); @@ -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