Struct jupiter::platform::Platform

source ·
pub struct Platform { /* private fields */ }
Expand description

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§

source§

impl Platform

source

pub fn new() -> Arc<Self>

Creates a new platform instance..

source

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

Registers a new component.

§Examples

struct Service {
    value: i32
}

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

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

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);
source

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

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>();
source

pub fn is_running(&self) -> bool

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

source

pub fn terminate(&self)

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more