oneshot
oneshot copied to clipboard
add a is_closed or similar to Sender
trafficstars
hi,
i'm using oneshot to get the result of a task out of a thread-pool and into the async world. We want to know if the Receiver was dropped before working on a task, and don't run that task if its result would be discarded. Currently we do this by wrapping both ends of the channel, but it seems this feature would be rather easy to implement in oneshot itself, and could serve others as well.
what would you think of adding a is_closed to cover such use case?
/// Returns true if the associated [Receiver] handle has been dropped.
///
/// If `true` is returned, a call to `send` will always result in an error.
pub fn is_closed(&self) -> bool {
// SAFETY: The channel exists on the heap for the entire duration of this method and we
// only ever acquire shared references to it. Note that if the receiver disconnects it
// does not free the channel.
let channel = unsafe { self.channel_ptr.as_ref() };
channel.state.load(Relaxed) == DISCONNECTED
}
This is being implemented in #35
Merged as part of #35