Struct corona::Coroutine
[−]
[src]
pub struct Coroutine { /* fields omitted */ }
A builder of coroutines.
This struct is the main entry point and a way to start coroutines of various kinds. It allows both starting them with default parameters and configuring them with the builder pattern.
Methods
impl Coroutine
[src]
fn new(handle: Handle) -> Self
[src]
Starts building a coroutine.
This constructor produces a new builder for coroutines. The builder can then be used to specify configuration of the coroutines.
It is possible to spawn multiple coroutines from the same builder.
Parameters
handle
: The coroutines need a reactor core to run on and schedule their control switches. This is the handle to the reactor core to be used.
Examples
use corona::Coroutine; use tokio_core::reactor::Core; let core = Core::new().unwrap(); let builder = Coroutine::new(core.handle()); let coroutine = builder.spawn(|| { });
fn stack_size(&mut self, size: usize)
[src]
Configures the stack size used for coroutines.
Coroutines spawned from this builder will get stack of this size. The default is something small, so if you use recursion, you might want to use this.
Note that the size must be a valid stack size. This is platform dependente, but usually must be multiple of a page size. That usually means a multiple of 4096.
If an invalid size is set, attemts to spawn coroutines will fail with an error.
Parameters
size
: The stack size to use for newly spawned coroutines.
fn with_defaults<R, Task>(handle: Handle, task: Task) -> CoroutineResult<R> where
R: 'static,
Task: FnOnce() -> R + 'static,
[src]
R: 'static,
Task: FnOnce() -> R + 'static,
Spawns a coroutine directly.
This constructor spawns a coroutine with default parameters without the inconvenience of handling a builder. It is equivalent to spawning it with an unconfigured builder.
Unlike the spawn
, this one can't fail, since the default parameters
of the builder are expected to always work (if they don't, file a bug).
Parameters
handle
: Handle to the reactor the coroutine will use to suspend its execution and wait for events.task
: The closure to run inside the coroutine.
Returns
A future that'll resolve once the coroutine completes, with the result of the task
, or
with an error explaining why the coroutine failed.
Examples
use corona::Coroutine; use tokio_core::reactor::Core; let mut core = Core::new().unwrap(); let coroutine = Coroutine::with_defaults(core.handle(), || { }); core.run(coroutine).unwrap();
fn spawn<R, Task>(&self, task: Task) -> Result<CoroutineResult<R>, StackError> where
R: 'static,
Task: FnOnce() -> R + 'static,
[src]
R: 'static,
Task: FnOnce() -> R + 'static,
Spawns a coroutine with configuration from the builder.
This spawns a new coroutine with the values previously set in the builder.
Parameters
task
: The closure to run inside the coroutine.
Returns
A future that'll resolve once the coroutine terminates and will yield the result of
task
, or an error explaining why the coroutine failed.
This returns a StackError
if the configured stack size is invalid.
Examples
use corona::Coroutine; use tokio_core::reactor::Core; let mut core = Core::new().unwrap(); let mut builder = Coroutine::new(core.handle()); builder.stack_size(40960); let coroutine = builder.spawn(|| { }).unwrap(); core.run(coroutine).unwrap();
fn wait<I, E, Fut>(fut: Fut) -> Result<Result<I, E>, Dropped> where
Fut: Future<Item = I, Error = E>,
[src]
Fut: Future<Item = I, Error = E>,
Waits for completion of a future.
This suspends the execution of the current coroutine until the provided future is completed, possibly switching to other coroutines in the meantime.
This is the low-level implementation of the waiting. It is expected user code will use the
interface in prelude
instead.
Parameters
fut
: The future to wait on.
Returns
Ok(result)
with the result the future resolved to.Err(Dropped)
when the reactor was dropped before the future had a chance to resolve.
Panics
If called outside of a coroutine (there's nothing to suspend).