Răsfoiți Sursa

add docstring for StoppableTask

x 3 ani în urmă
părinte
comite
4c94fa251c
1 a modificat fișierele cu 21 adăugiri și 0 ștergeri
  1. 21 0
      src/system/stoppable_task.rs

+ 21 - 0
src/system/stoppable_task.rs

@@ -28,21 +28,42 @@ pub type StoppableTaskPtr = Arc<StoppableTask>;
 
 #[derive(Debug)]
 pub struct StoppableTask {
+    // NOTE: we could send the error code from stop() instead of having it specified in start()
+    // but then that would introduce lifetimes to the entire struct.
     stop_send: channel::Sender<()>,
     stop_recv: channel::Receiver<()>,
 }
 
+/// A task that can be prematurely stopped at any time.
+///
+/// ```rust
+///     let task = StoppableTask::new();
+///     task.clone().start(
+///         my_method(),
+///         |result| self_.handle_stop(result),
+///         Error::MyStopError,
+///         executor,
+///     );
+/// ```
+///
+/// Then at any time we can call `task.stop()` to close the task.
 impl StoppableTask {
     pub fn new() -> Arc<Self> {
         let (stop_send, stop_recv) = channel::bounded(1);
         Arc::new(Self { stop_send, stop_recv })
     }
 
+    /// Stops the task
     pub async fn stop(&self) {
         // Ignore any errors from this send
         let _ = self.stop_send.send(()).await;
     }
 
+    /// Starts the task.
+    ///
+    /// * `main` is a function of the type `async fn foo() -> ()`
+    /// * `stop_handler` is a function of the type `async fn handle_stop(result: Result<()>) -> ()`
+    /// * `stop_value` is the Error code passed to `stop_handler` when `task.stop()` is called
     pub fn start<'a, MainFut, StopFut, StopFn, Error>(
         self: Arc<Self>,
         main: MainFut,