|
| 1 | +use crate::{FutureObj, LocalFutureObj}; |
| 2 | +use core::fmt; |
| 3 | +use std::{future::Future, pin::Pin}; |
| 4 | + |
| 5 | +/// The `Spawn` trait allows for pushing futures onto an executor that will |
| 6 | +/// run them to completion. |
| 7 | +pub trait Spawn { |
| 8 | + type Handle; |
| 9 | + /// An error that occurred during spawning. |
| 10 | + type Error; |
| 11 | + |
| 12 | + /// Spawns a future that will be run to completion. |
| 13 | + /// |
| 14 | + /// # Errors |
| 15 | + /// |
| 16 | + /// The executor may be unable to spawn tasks. Spawn errors should |
| 17 | + /// represent relatively rare scenarios, such as the executor |
| 18 | + /// having been shut down so that it is no longer able to accept |
| 19 | + /// tasks. |
| 20 | + fn spawn_boxed( |
| 21 | + &self, |
| 22 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 23 | + ) -> Result<Self::Handle, Self::Error>; |
| 24 | + |
| 25 | + /// Determines whether the executor is able to spawn new tasks. |
| 26 | + /// |
| 27 | + /// This method will return `Ok` when the executor is *likely* |
| 28 | + /// (but not guaranteed) to accept a subsequent spawn attempt. |
| 29 | + /// Likewise, an `Err` return means that `spawn` is likely, but |
| 30 | + /// not guaranteed, to yield an error. |
| 31 | + #[inline] |
| 32 | + fn status(&self) -> Result<(), Self::Error> { |
| 33 | + Ok(()) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// The `LocalSpawn` is similar to [`Spawn`], but allows spawning futures |
| 38 | +/// that don't implement `Send`. |
| 39 | +pub trait LocalSpawn { |
| 40 | + type Handle; |
| 41 | + /// An error that occurred during spawning. |
| 42 | + type Error; |
| 43 | + |
| 44 | + /// Spawns a future that will be run to completion. |
| 45 | + /// |
| 46 | + /// # Errors |
| 47 | + /// |
| 48 | + /// The executor may be unable to spawn tasks. Spawn errors should |
| 49 | + /// represent relatively rare scenarios, such as the executor |
| 50 | + /// having been shut down so that it is no longer able to accept |
| 51 | + /// tasks. |
| 52 | + fn spawn_local_boxed( |
| 53 | + &self, |
| 54 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 55 | + ) -> Result<Self::Handle, Self::Error>; |
| 56 | + |
| 57 | + /// Determines whether the executor is able to spawn new tasks. |
| 58 | + /// |
| 59 | + /// This method will return `Ok` when the executor is *likely* |
| 60 | + /// (but not guaranteed) to accept a subsequent spawn attempt. |
| 61 | + /// Likewise, an `Err` return means that `spawn` is likely, but |
| 62 | + /// not guaranteed, to yield an error. |
| 63 | + #[inline] |
| 64 | + fn status_local(&self) -> Result<(), Self::Error> { |
| 65 | + Ok(()) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<Sp: ?Sized + Spawn> Spawn for &Sp { |
| 70 | + fn spawn_boxed( |
| 71 | + &self, |
| 72 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 73 | + ) -> Result<Self::Handle, Self::Error> { |
| 74 | + Sp::spawn_obj(self, future) |
| 75 | + } |
| 76 | + |
| 77 | + fn status(&self) -> Result<(), Self::Error> { |
| 78 | + Sp::status(self) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<Sp: ?Sized + Spawn> Spawn for &mut Sp { |
| 83 | + fn spawn_boxed( |
| 84 | + &self, |
| 85 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 86 | + ) -> Result<Self::Handle, Self::Error> { |
| 87 | + Sp::spawn_obj(self, future) |
| 88 | + } |
| 89 | + |
| 90 | + fn status(&self) -> Result<(), Self::Error> { |
| 91 | + Sp::status(self) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<Sp: ?Sized + LocalSpawn> LocalSpawn for &Sp { |
| 96 | + fn spawn_local_boxed( |
| 97 | + &self, |
| 98 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 99 | + ) -> Result<Self::Handle, Self::Error> { |
| 100 | + Sp::spawn_local_boxed(self, future) |
| 101 | + } |
| 102 | + |
| 103 | + fn status_local(&self) -> Result<(), Self::Error> { |
| 104 | + Sp::status_local(self) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<Sp: ?Sized + LocalSpawn> LocalSpawn for &mut Sp { |
| 109 | + fn spawn_local_boxed( |
| 110 | + &self, |
| 111 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 112 | + ) -> Result<Self::Handle, Self::Error> { |
| 113 | + Sp::spawn_local_boxed(self, future) |
| 114 | + } |
| 115 | + |
| 116 | + fn status_local(&self) -> Result<(), Self::Error> { |
| 117 | + Sp::status_local(self) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(feature = "alloc")] |
| 122 | +mod if_alloc { |
| 123 | + use super::*; |
| 124 | + use alloc::{boxed::Box, rc::Rc}; |
| 125 | + |
| 126 | + impl<Sp: ?Sized + Spawn> Spawn for Box<Sp> { |
| 127 | + fn spawn_boxed( |
| 128 | + &self, |
| 129 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 130 | + ) -> Result<Self::Handle, Self::Error> { |
| 131 | + (**self).spawn_obj(future) |
| 132 | + } |
| 133 | + |
| 134 | + fn status(&self) -> Result<(), Self::Error> { |
| 135 | + (**self).status() |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + impl<Sp: ?Sized + LocalSpawn> LocalSpawn for Box<Sp> { |
| 140 | + fn spawn_local_boxed( |
| 141 | + &self, |
| 142 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 143 | + ) -> Result<Self::Handle, Self::Error> { |
| 144 | + (**self).spawn_local_boxed(future) |
| 145 | + } |
| 146 | + |
| 147 | + fn status_local(&self) -> Result<(), Self::Error> { |
| 148 | + (**self).status_local() |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + impl<Sp: ?Sized + Spawn> Spawn for Rc<Sp> { |
| 153 | + fn spawn_boxed( |
| 154 | + &self, |
| 155 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 156 | + ) -> Result<Self::Handle, Self::Error> { |
| 157 | + (**self).spawn_obj(future) |
| 158 | + } |
| 159 | + |
| 160 | + fn status(&self) -> Result<(), Self::Error> { |
| 161 | + (**self).status() |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + impl<Sp: ?Sized + LocalSpawn> LocalSpawn for Rc<Sp> { |
| 166 | + fn spawn_local_boxed( |
| 167 | + &self, |
| 168 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 169 | + ) -> Result<Self::Handle, Self::Error> { |
| 170 | + (**self).spawn_local_boxed(future) |
| 171 | + } |
| 172 | + |
| 173 | + fn status_local(&self) -> Result<(), Self::Error> { |
| 174 | + (**self).status_local() |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + cfg_target_has_atomic! { |
| 179 | + use alloc::sync::Arc; |
| 180 | + |
| 181 | + impl<Sp: ?Sized + Spawn> Spawn for Arc<Sp> { |
| 182 | + fn spawn_boxed( |
| 183 | + &self, |
| 184 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 185 | + ) -> Result<Self::Handle, Self::Error> { |
| 186 | + (**self).spawn_obj(future) |
| 187 | + } |
| 188 | + |
| 189 | + fn status(&self) -> Result<(), Self::Error> { |
| 190 | + (**self).status() |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + impl<Sp: ?Sized + LocalSpawn> LocalSpawn for Arc<Sp> { |
| 195 | + fn spawn_local_boxed( |
| 196 | + &self, |
| 197 | + future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>, |
| 198 | + ) -> Result<Self::Handle, Self::Error> { |
| 199 | + (**self).spawn_local_boxed(future) |
| 200 | + } |
| 201 | + |
| 202 | + fn status_local(&self) -> Result<(), Self::Error> { |
| 203 | + (**self).status_local() |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments