diff --git a/CHANGELOG.md b/CHANGELOG.md index dfc96ff..b28146b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # Changelog -## [4.6.1] - 2022-9-27 +## [4.6.2] - 2022-9-27 - Make Flower uncloneable to avoid any kind of data races, added FlowerState as alternative. - Internal only: Replace `Option` with `TypeOpt` managing value of the sync (mtx) state. diff --git a/Cargo.toml b/Cargo.toml index b783e2f..6f5074c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "flowync" -version = "4.6.1" +version = "4.6.2" authors = ["Ar37-rs "] edition = "2021" description = "A simple utility for multithreading a/synchronization" diff --git a/src/lib.rs b/src/lib.rs index 2b6a376..82d6f1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ #![deny(unsafe_code)] use core::{ - clone::Clone, fmt::{self, Debug, Formatter}, future::Future, pin::Pin, @@ -9,14 +8,15 @@ use core::{ }; use std::{ error::Error, + mem, sync::{Condvar, Mutex}, }; use std::{sync::Arc, thread}; enum TypeOpt where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { Channel(S), Success(R), @@ -24,25 +24,20 @@ where None, } -impl Clone for TypeOpt +impl Default for TypeOpt where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { - fn clone(&self) -> Self { - match self { - Self::Channel(s) => Self::Channel(Clone::clone(s)), - Self::Success(r) => Self::Success(Clone::clone(r)), - Self::Error(e) => Self::Error(Clone::clone(e)), - Self::None => Self::None, - } + fn default() -> Self { + Self::None } } impl Debug for TypeOpt where - S: Send + Clone + Debug, - R: Send + Clone + Debug, + S: Send + Debug, + R: Send + Debug, { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { @@ -56,20 +51,18 @@ where impl TypeOpt where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { fn take(&mut self) -> Self { - let _take = self.clone(); - *self = TypeOpt::None; - _take + mem::take(self) } } struct InnerState where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { activated: AtomicBool, result_ready: AtomicBool, @@ -81,8 +74,8 @@ where impl Debug for InnerState where - S: Debug + Send + Clone, - R: Debug + Send + Clone, + S: Debug + Send, + R: Debug + Send, { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("InnerState") @@ -98,8 +91,8 @@ where impl Drop for InnerState where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { fn drop(&mut self) {} } @@ -107,8 +100,8 @@ where /// State of the flower pub struct FlowerState where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { state: Arc>, async_suspender: Arc<(Mutex>, AtomicBool)>, @@ -117,8 +110,8 @@ where impl Debug for FlowerState where - S: Send + Clone + Debug, - R: Send + Clone + Debug, + S: Send + Debug, + R: Send + Debug, { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("FlowerState") @@ -131,8 +124,8 @@ where impl FlowerState where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { /// Get ID of the flower. pub fn id(&self) -> usize { @@ -159,8 +152,8 @@ where impl Clone for FlowerState where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { fn clone(&self) -> Self { Self { @@ -173,8 +166,8 @@ where impl Drop for FlowerState where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { fn drop(&mut self) {} } @@ -199,8 +192,8 @@ impl Future for AsyncSuspender { /// A handle for the Flower pub struct Handle where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { state: Arc>, async_suspender: Arc<(Mutex>, AtomicBool)>, @@ -209,8 +202,8 @@ where impl Handle where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { /// Get ID of the flower. pub fn id(&self) -> usize { @@ -279,8 +272,8 @@ where impl Clone for Handle where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { fn clone(&self) -> Self { Self { @@ -293,8 +286,8 @@ where impl Drop for Handle where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { fn drop(&mut self) { if thread::panicking() && !self.state.result_ready.load(Ordering::Relaxed) { @@ -308,8 +301,8 @@ where impl Debug for Handle where - S: Debug + Send + Clone, - R: Debug + Send + Clone, + S: Debug + Send, + R: Debug + Send, { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("Handle") @@ -320,12 +313,12 @@ where } } -pub struct Extracted<'a, S: Send + Clone, R: Send + Clone>(&'a Flower); +pub struct Extracted<'a, S: Send, R: Send>(&'a Flower); impl Extracted<'_, S, R> where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { /// Try finalize result of the flower. pub fn finalize(self, f: impl FnOnce(Result)) { @@ -422,8 +415,8 @@ where /// ``` pub struct Flower where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { state: Arc>, async_suspender: Arc<(Mutex>, AtomicBool)>, @@ -432,8 +425,8 @@ where impl Flower where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { pub fn new(id: usize) -> Self { Self { @@ -566,8 +559,8 @@ where impl Debug for Flower where - S: Debug + Send + Clone, - R: Debug + Send + Clone, + S: Debug + Send, + R: Debug + Send, { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("Flower") @@ -580,8 +573,8 @@ where impl Drop for Flower where - S: Send + Clone, - R: Send + Clone, + S: Send, + R: Send, { fn drop(&mut self) { if thread::panicking() {