Skip to main content

Application

Struct Application 

Source
pub struct Application { /* private fields */ }
Expand description

The application: a Container with a boot lifecycle, an environment, and a filesystem layout.

Implementations§

Source§

impl Application

Source

pub fn new(base_path: impl Into<PathBuf>) -> Self

A new application rooted at base_path, in the production environment until told otherwise.

Source

pub fn with_environment(self, environment: impl Into<String>) -> Self

Builder form of set_environment.

Source

pub fn container(&self) -> &Arc<Container> ⓘ

The shared container. Hand this to anything that needs to resolve services without holding the whole application.

Source

pub fn register(&self, provider: impl ServiceProvider) -> Result<()>

Register a provider, running its register pass immediately.

Booting is deliberately not done here: a provider’s boot may resolve services that a later-registered provider binds, so booting has to wait until every provider has registered. Register everything, then call boot.

Source

pub fn register_arc(&self, provider: Arc<dyn ServiceProvider>) -> Result<()>

register for a provider that is already shared.

Source

pub fn provider_names(&self) -> Vec<&'static str>

The names of every registered provider, in registration order.

Source

pub async fn boot(&self) -> Result<()>

Boot every provider that has not booted yet, running the booting hooks first and the booted hooks after.

Idempotent, and safe to call again after registering more providers — only the new ones boot.

Source

pub fn is_booted(&self) -> bool

Whether boot has completed at least once.

Source

pub fn booting(&self, hook: impl Fn(&Application) + Send + Sync + 'static)

Run hook immediately before providers boot.

Ignored if the application has already booted — “before boot” has passed, and silently running it late would be worse than not running it.

Source

pub fn booted(&self, hook: impl Fn(&Application) + Send + Sync + 'static)

Run hook once the application has booted — or immediately, if it already has. The immediate call is what makes this usable from code that cannot know whether boot has happened yet.

Source

pub fn terminating(&self, hook: impl Fn(&Application) + Send + Sync + 'static)

Run hook when terminate is called — after a response has been sent, for work that should not delay it.

Source

pub fn terminate(&self)

Run the terminating hooks. The HTTP kernel calls this after flushing a response; a console command calls it before exiting.

Source

pub fn environment(&self) -> String

The current environment name (local, testing, production, …).

Source

pub fn set_environment(&self, environment: impl Into<String>)

Set the environment name.

Source

pub fn environment_is(&self, names: &[&str]) -> bool

Whether the environment is any of names.

Source

pub fn is_local(&self) -> bool

Whether the environment is local.

Source

pub fn is_testing(&self) -> bool

Whether the environment is testing.

Source

pub fn is_production(&self) -> bool

Whether the environment is production.

Source

pub fn base_path(&self) -> &Path

The application root.

Source

pub fn path(&self, relative: impl AsRef<Path>) -> PathBuf

A path relative to the application root.

Source

pub fn config_path(&self) -> PathBuf

<base>/config.

Source

pub fn storage_path(&self) -> PathBuf

<base>/storage.

Source

pub fn resource_path(&self) -> PathBuf

<base>/resources — where views live.

Source

pub fn database_path(&self) -> PathBuf

<base>/database.

Methods from Deref<Target = Container>§

Source

pub fn bind<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Bind a factory that runs on every resolution.

Source

pub fn singleton<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Bind a factory that runs once; every resolution afterwards hands back the same Arc.

Source

pub fn scoped<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Like singleton, but flush_scoped drops the memoised instance so the next resolution rebuilds it.

This is the binding a long-running worker wants for per-job state: the queue worker flushes scoped bindings between jobs so one job cannot leak state into the next.

Source

pub fn instance<T: Send + Sync + 'static>(&self, value: T)

Register an already-built value. The container never constructs it.

Source

pub fn instance_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)

Register an already-built value that is already behind an Arc — useful when the caller must keep a handle to it too.

Source

pub fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>

Resolve T, building it if necessary.

Fails when T was never bound, when its factory fails, or when resolving it re-enters itself (a dependency cycle).

Source

pub fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>

Resolve T, or None if it is unbound or its factory failed.

Source

pub fn expect_resolve<T: Send + Sync + 'static>(&self) -> Arc<T> ⓘ

Resolve T, panicking with the resolution error if it is not available. For bootstrap code and facades, where an unbound service is a configuration bug rather than a runtime condition.

Source

pub fn bound<T: 'static>(&self) -> bool

Whether anything is bound for T.

Source

pub fn forget<T: 'static>(&self)

Drop the binding for T, if any.

Source

pub fn flush_scoped(&self)

Drop the memoised instance of every scoped binding. The bindings survive; only the built values are discarded.

Source

pub fn len(&self) -> usize

How many types are bound.

Source

pub fn is_empty(&self) -> bool

Whether nothing is bound.

Trait Implementations§

Source§

impl Debug for Application

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Application

Source§

type Target = Container

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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