tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
25.44k stars 2.3k forks source link

Implement Clone for AbortHandle #6621

Closed aschweig closed 1 month ago

aschweig commented 1 month ago

This is a small change so that AbortHandle implements Clone.

Motivation

The the module level docs indicate

Each task can only have one JoinHandle, but it can have more than one AbortHandle.

Cloning is convenient for AbortHandles given there can be more than one.

Solution

The proposed solution is similar to JoinHandle's abort_handle method:

impl Clone for AbortHandle {
    /// Returns a cloned `AbortHandle` that can be used to remotely abort this task.
    fn clone(&self) -> Self {
        self.raw.ref_inc();
        Self::new(self.raw)
    }
}