feat: remove closed flag from ChannelState

This commit is contained in:
Callum Leslie 2025-09-15 11:38:00 +01:00
parent 32b7aa65e6
commit ca4825552f
Signed by: cleslie
GPG key ID: D382C4AFEECEAA90
5 changed files with 16 additions and 36 deletions

View file

@ -1,5 +1,4 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use crate::error::Error;
use crate::sync::traits::{ChannelReceiver, ChannelSender};
@ -25,7 +24,7 @@ where
{
pub(crate) request_rx: SR,
pub(crate) response_tx: ST,
pub(crate) state: Arc<Mutex<ChannelState<T>>>,
pub(crate) state: ChannelState<T>,
pub(crate) _phantom: std::marker::PhantomData<T>,
}
@ -38,10 +37,7 @@ where
/// Set a fixed value
pub fn set_static(&self, value: T) -> Result<(), Error> {
let mut state = self.state.lock().map_err(|_| Error::InternalError)?;
if state.closed {
return Err(Error::ChannelClosed);
}
state.source = ValueSource::Static(value);
*state = ValueSource::Static(value);
Ok(())
}
@ -51,18 +47,14 @@ where
F: Fn() -> T + Send + Sync + 'static,
{
let mut state = self.state.lock().map_err(|_| Error::InternalError)?;
if state.closed {
return Err(Error::ChannelClosed);
}
state.source = ValueSource::Dynamic(Box::new(closure));
*state = ValueSource::Dynamic(Box::new(closure));
Ok(())
}
/// Close the channel
pub fn close(&self) -> Result<(), Error> {
let mut state = self.state.lock().map_err(|_| Error::InternalError)?;
state.closed = true;
state.source = ValueSource::None;
*state = ValueSource::Cleared;
Ok(())
}
@ -80,8 +72,7 @@ where
Ok(Request::Close) => {
// Close channel
let mut state = self.state.lock().map_err(|_| Error::InternalError)?;
state.closed = true;
state.source = ValueSource::None;
*state = ValueSource::Cleared;
break;
}
Err(_) => {
@ -95,11 +86,8 @@ where
fn handle_get_value(&self) -> Result<Response<T>, Error> {
let state = self.state.lock().map_err(|_| Error::InternalError)?;
if state.closed {
return Ok(Response::Closed);
}
match &state.source {
match &*state {
ValueSource::Static(value) => Ok(Response::Value(value.clone())),
ValueSource::Dynamic(closure) => {
let value = self.execute_closure_safely(closure);
@ -108,7 +96,8 @@ where
Err(_) => Ok(Response::NoSource), // Closure execution failed
}
}
ValueSource::None => Ok(Response::NoSource),
ValueSource::None => Ok(Response::NoSource), // No source was ever set
ValueSource::Cleared => Ok(Response::Closed), // Channel was closed (source was set then cleared)
}
}

View file

@ -64,10 +64,7 @@ impl<T> CrossbeamSuck<T> {
let (request_tx, request_rx) = CrossbeamChannel::create_request_channel();
let (response_tx, response_rx) = CrossbeamChannel::create_response_channel::<T>();
let state = std::sync::Arc::new(std::sync::Mutex::new(crate::types::ChannelState {
source: crate::types::ValueSource::None,
closed: false,
}));
let state = std::sync::Arc::new(std::sync::Mutex::new(crate::types::ValueSource::None));
let sucker = crate::Sucker {
request_tx,

View file

@ -64,10 +64,7 @@ impl<T> FlumeSuck<T> {
let (request_tx, request_rx) = FlumeChannel::create_request_channel();
let (response_tx, response_rx) = FlumeChannel::create_response_channel::<T>();
let state = std::sync::Arc::new(std::sync::Mutex::new(crate::types::ChannelState {
source: crate::types::ValueSource::None,
closed: false,
}));
let state = std::sync::Arc::new(std::sync::Mutex::new(crate::types::ValueSource::None));
let sucker = crate::Sucker {
request_tx,

View file

@ -61,10 +61,7 @@ impl<T> StdSuck<T> {
let (request_tx, request_rx) = StdChannel::create_request_channel();
let (response_tx, response_rx) = StdChannel::create_response_channel::<T>();
let state = std::sync::Arc::new(std::sync::Mutex::new(crate::types::ChannelState {
source: crate::types::ValueSource::None,
closed: false,
}));
let state = std::sync::Arc::new(std::sync::Mutex::new(crate::types::ValueSource::None));
let sucker = crate::Sucker {
request_tx,

View file

@ -1,3 +1,5 @@
use std::sync::{Arc, Mutex};
/// Request messages sent from consumer to producer
pub enum Request {
GetValue,
@ -15,11 +17,9 @@ pub enum Response<T> {
pub(crate) enum ValueSource<T> {
Static(T),
Dynamic(Box<dyn Fn() -> T + Send + Sync + 'static>),
None,
None, // Never set
Cleared, // Was set but cleared (closed)
}
/// Internal channel state shared between producer and consumer
pub(crate) struct ChannelState<T> {
pub(crate) source: ValueSource<T>,
pub(crate) closed: bool,
}
pub(crate) type ChannelState<T> = Arc<Mutex<ValueSource<T>>>;