oneshot icon indicating copy to clipboard operation
oneshot copied to clipboard

add a is_closed or similar to Sender

Open trinity-1686a opened this issue 1 year ago • 1 comments
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
    }

trinity-1686a avatar May 07 '24 08:05 trinity-1686a

This is being implemented in #35

faern avatar May 08 '24 20:05 faern

Merged as part of #35

faern avatar May 23 '24 22:05 faern