Struct apollo_framework::platform::Platform[][src]

pub struct Platform { /* fields omitted */ }

Provides a container to keep all central services in a single place.

Examples

Building and accessing components:


struct Service {}

#[tokio::main]
async fn main() {
    let platform = Platform::new();
    platform.register(Arc::new(Service {}));
    assert_eq!(platform.find::<Service>().is_some(), true);
}

Checking the central “is running” active..


struct Service {}

#[tokio::main]
async fn main() {
    let platform = Platform::new();
    platform.register(Arc::new(Service {}));

    // By default the platform is running...
    assert_eq!(platform.is_running(), true);

    // once terminated...
    platform.terminate();

    // all services are evicted so that their Dropped handlers are executed
    assert_eq!(platform.find::<Service>().is_some(), false);

    // the platform is considered halted...
    assert_eq!(platform.is_running(), false);
}

Implementations

impl Platform[src]

pub fn new() -> Arc<Self>[src]

Creates a new platform instance..

pub fn register<T>(&self, service: Arc<T>) where
    T: Any + Send + Sync
[src]

Registers a new component.

Examples


struct Service {
    value: i32
}

let platform = Platform::new();
platform.register::<Service>(Arc::new(Service { value: 42 }));

pub fn find<T>(&self) -> Option<Arc<T>> where
    T: Any + Send + Sync
[src]

Tries to resolve a previously registered service.

Note, if one knows for certain, that a service will be present, Platform::require can be used.

Examples


struct Service {
    value: i32
}

struct UnknownService;

let platform = Platform::new();
platform.register::<Service>(Arc::new(Service { value: 42 }));

// A lookup for a known service yields a result..
assert_eq!(platform.find::<Service>().unwrap().value, 42);

// A lookup for an unknown service returns None...
assert_eq!(platform.find::<UnknownService>().is_none(), true);

pub fn require<T>(&self) -> Arc<T> where
    T: Any + Send + Sync
[src]

Resolve a previously registered service.

Note, if the framework is already shutting down, all services are evicted. Therefor this might panic even if it worked before Platform::terminate was invoked.

Panics

Panics if the requested service isn’t available.

Examples


struct Service {
    value: i32
}

let platform = Platform::new();
platform.register::<Service>(Arc::new(Service { value: 42 }));

// A lookup for a known service yields a result..
assert_eq!(platform.require::<Service>().value, 42);

Requiring a service which is unknown will panic:


struct UnknownService;

let platform = Platform::new();

// This will panic...
platform.require::<UnknownService>();

pub fn is_running(&self) -> bool[src]

Determines if the platform is still running or if Platform::terminate has already been called.

pub fn terminate(&self)[src]

Terminates the platform.

This will immediately release all services (so that the Dropped handlers run eventually). It will also toggle the is_running() flag to false.

Auto Trait Implementations

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

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

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

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

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

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.