[][src]Enum spirit_tokio::runtime::Runtime

pub enum Runtime {
    ThreadPool(Box<dyn FnMut(&mut Builder) + Send>),
    CurrentThread(Box<dyn FnMut(&mut Builder) + Send>),
    Custom(Box<dyn FnMut(TokioBody) -> Result<(), Error> + Send>),
    // some variants omitted
}

An extension to initialize a tokio runtime as part of spirit.

The FutureInstaller in this crate (and as a result pipelines with Fragments like TcpListen, UdpListen) use this to make sure they have a runtime to handle the sockets on.

If you prefer to specify configuration of the runtime to use, instead of the default one, you can create an instance of this extension yourself and register it before registering any socket pipelines, which will take precedence and the sockets will use the one provided by you. You must register it using the with_singleton method.

Similarly, if all the pipelines are registered within the run method (or generally, after building is done), you need to install this manually before doing run.

Note that the provided closures are FnMut mostly because Box<FnOnce> doesn't work. They will be called just once, so you can use Option<T> inside and consume the value by take.unwrap().

Runtime configuration

You may have noticed the callbacks here don't have access to configuration. If you intend to configure eg. the number of threads from user configuration, use the ThreadPoolConfig instead.

Future compatibility

More variants may be added into the enum at any time. Such change will not be considered a breaking change.

Examples

extern crate failure;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate spirit;
extern crate spirit_tokio;
extern crate tokio;

use std::sync::Arc;

use failure::Error;
use spirit::prelude::*;
use spirit_tokio::{HandleListener, TcpListen};
use spirit_tokio::runtime::Runtime;
use tokio::prelude::*;

#[derive(Default, Deserialize)]
struct Config {
    #[serde(default)]
    listening_socket: Vec<TcpListen>,
}

impl Config {
    fn listener(&self) -> Vec<TcpListen> {
        self.listening_socket.clone()
    }
}

fn connection() -> impl Future<Item = (), Error = Error> {
    future::ok(()) // Just a dummy implementation
}

fn main() {
    Spirit::<Empty, Config>::new()
        // Uses the current thread runtime instead of the default threadpool. This'll create
        // smaller number of threads.
        .with_singleton(Runtime::CurrentThread(Box::new(|_| ())))
        .with(
            Pipeline::new("listener")
                .extract_cfg(Config::listener)
                .transform(HandleListener(|_conn, _cfg: &_| connection()))
        )
        .run(|spirit| {
            Ok(())
        });
}

Variants

ThreadPool(Box<dyn FnMut(&mut Builder) + Send>)

Use the threadpool runtime.

The threadpool runtime is the default (both in tokio and spirit).

This allows you to modify the builder prior to starting it, specifying custom options like number of threads.

CurrentThread(Box<dyn FnMut(&mut Builder) + Send>)

Use the current thread runtime.

If you prefer to run everything in a single thread, use this variant. The provided closure can modify the builder prior to starting it.

Custom(Box<dyn FnMut(TokioBody) -> Result<(), Error> + Send>)

Use completely custom runtime.

The provided closure should start the runtime and execute the provided future on it, blocking until the runtime becomes empty.

This allows combining arbitrary runtimes that are not directly supported by either tokio or spirit.

Trait Implementations

impl Default for Runtime[src]

impl<E> Extension<E> for Runtime where
    E: Extensible<Ok = E>,
    E::Config: DeserializeOwned + Send + Sync + 'static,
    E::Opts: StructOpt + Send + Sync + 'static, 
[src]

Auto Trait Implementations

impl Send for Runtime

impl !Sync for Runtime

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> IntoResult for T[src]

impl<B, F, R> Extension for F where
    F: FnOnce(B) -> R,
    R: IntoResult<B>, 
[src]

impl<T> Erased for T