pub struct ContainerBuilder { /* private fields */ }Expand description
Builds a Container from definitions, aliases, parameters, and compile passes.
§Examples
use std::sync::Arc;
use serenade_di::{ContainerBuilder, ServiceDefinition};
let mut builder = ContainerBuilder::new();
builder
.register(ServiceDefinition::new("greeting"), |_| {
Ok(Box::new(String::from("hello")))
})
.expect("register");
let container = builder.compile().expect("compile");
let greeting = container.get_as::<String>("greeting").expect("resolve");
assert_eq!(greeting.as_str(), "hello");
assert!(Arc::strong_count(&greeting) >= 1);Implementations§
Source§impl ContainerBuilder
impl ContainerBuilder
Sourcepub fn new() -> ContainerBuilder
pub fn new() -> ContainerBuilder
Creates an empty builder.
Sourcepub const fn parameters(&self) -> &ParameterBag
pub const fn parameters(&self) -> &ParameterBag
Shared parameter bag.
Sourcepub const fn parameters_mut(&mut self) -> &mut ParameterBag
pub const fn parameters_mut(&mut self) -> &mut ParameterBag
Mutable parameter bag.
Sourcepub fn register(
&mut self,
definition: ServiceDefinition,
factory: impl Fn(&Container) -> Result<Box<dyn Any + Sync + Send>, DiError> + Send + Sync + 'static,
) -> Result<(), DiError>
pub fn register( &mut self, definition: ServiceDefinition, factory: impl Fn(&Container) -> Result<Box<dyn Any + Sync + Send>, DiError> + Send + Sync + 'static, ) -> Result<(), DiError>
Registers a service factory under definition.id().
§Errors
Returns DiError::DuplicateService when the id already exists as a service or alias.
Sourcepub fn set_alias(
&mut self,
alias: impl Into<String>,
target: impl Into<String>,
) -> Result<(), DiError>
pub fn set_alias( &mut self, alias: impl Into<String>, target: impl Into<String>, ) -> Result<(), DiError>
Adds an alias from alias to target.
§Errors
Returns DiError::DuplicateService when alias is already used.
Sourcepub fn add_compile_pass(&mut self, pass: impl CompilePass + 'static)
pub fn add_compile_pass(&mut self, pass: impl CompilePass + 'static)
Appends a compile pass. Passes run in registration order during Self::compile.
Sourcepub fn definitions(&self) -> Vec<&ServiceDefinition>
pub fn definitions(&self) -> Vec<&ServiceDefinition>
Returns registered service definitions.
Trait Implementations§
Source§impl Default for ContainerBuilder
impl Default for ContainerBuilder
Source§fn default() -> ContainerBuilder
fn default() -> ContainerBuilder
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for ContainerBuilder
impl !UnwindSafe for ContainerBuilder
impl Forget for ContainerBuilder
impl Freeze for ContainerBuilder
impl Move for ContainerBuilder
impl Send for ContainerBuilder
impl Sync for ContainerBuilder
impl Unpin for ContainerBuilder
impl UnsafeUnpin for ContainerBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SerializableKey for T
impl<T> SerializableKey for T
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Borrows the value.
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Takes the value.