pub use std::sync::Arc;
pub use ulo_macros::{__enhancer_specs, __handler};
use std::marker::PhantomData;
use crate::construct::ConstructError;
use crate::dependency::{Dependencies, DependencyLabel, FromContainer};
use crate::scope::{AllowedIn, Scope};
use crate::timer::BoxError;
use crate::transport::Transport;
use crate::transport::metadata::TransportMetadata;
pub fn field<S: FromContainer + AllowedIn<Sc>, Sc: Scope>(d: &mut Dependencies, name: &'static str) {
d.field::<S>(name);
}
pub fn param<S: FromContainer + AllowedIn<Sc>, Sc: Scope>(d: &mut Dependencies, name: &'static str) {
d.param::<S>(name);
}
#[derive(Clone, Copy, Debug)]
pub enum HandlerParam {
Named(&'static str),
At(usize),
}
pub fn handler_param(d: &mut Dependencies, param: HandlerParam, declare: impl FnOnce(&mut Dependencies)) {
let start = d.list.len();
declare(d);
let label = match param {
HandlerParam::Named(name) => DependencyLabel::Param(name),
HandlerParam::At(index) => DependencyLabel::Position(index),
};
for record in &mut d.list[start..] {
record.label = label;
}
}
pub const fn key_in(key: &str, keys: &[&str]) -> bool {
let mut i = 0;
while i < keys.len() {
if bytes_eq(key.as_bytes(), keys[i].as_bytes()) {
return true;
}
i += 1;
}
false
}
const fn bytes_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
pub struct Shared<T>(pub T);
pub struct MetaProbe<T, V: ?Sized> {
_t: PhantomData<fn() -> T>,
_v: PhantomData<fn() -> *const V>,
}
impl<T, V: ?Sized> MetaProbe<T, V> {
pub fn new(_value: &V) -> Self {
MetaProbe { _t: PhantomData, _v: PhantomData }
}
}
pub struct MetaMismatch<T, U> {
_t: PhantomData<fn() -> (T, U)>,
}
pub trait MetaSame {
fn check(&self);
}
impl<T: Transport, V: TransportMetadata<Transport = T>> MetaSame for &&MetaProbe<T, V> {
fn check(&self) {}
}
pub trait MetaOther<T, U> {
fn check(&self) -> MetaMismatch<T, U>;
}
impl<T: Transport, V: TransportMetadata> MetaOther<T, V::Transport> for &MetaProbe<T, V> {
fn check(&self) -> MetaMismatch<T, V::Transport> {
MetaMismatch { _t: PhantomData }
}
}
pub trait MetaAny {
fn check(&self);
}
impl<T, V: ?Sized> MetaAny for MetaProbe<T, V> {
fn check(&self) {}
}
#[diagnostic::on_unimplemented(
message = "a constructor returns `{T}` or `Result<{T}, E>`, not `{Self}`",
label = "returned by this constructor",
note = "the error type `E` must convert into `BoxError`, as any `std::error::Error + Send + Sync` does"
)]
pub trait IntoConstructed<T> {
fn into_constructed(self) -> Result<T, ConstructError>;
}
impl<T> IntoConstructed<T> for T {
fn into_constructed(self) -> Result<T, ConstructError> {
Ok(self)
}
}
impl<T, E: Into<BoxError>> IntoConstructed<T> for Result<T, E> {
fn into_constructed(self) -> Result<T, ConstructError> {
self.map_err(ConstructError::failed)
}
}
pub mod hooks {
use std::marker::PhantomData;
use crate::hooks::{
BeforeApplicationShutdown, Hooks, OnApplicationBootstrap, OnApplicationShutdown, OnModuleDestroy,
OnModuleInit,
};
pub struct Probe<T>(PhantomData<fn() -> T>);
impl<T> Probe<T> {
pub fn of(_hooks: &Hooks<T>) -> Self {
Probe(PhantomData)
}
}
pub trait RegisterInit<T> {
fn register_init(&self, h: &mut Hooks<T>);
}
impl<T: OnModuleInit> RegisterInit<T> for Probe<T> {
fn register_init(&self, h: &mut Hooks<T>) {
h.on_module_init();
}
}
pub trait NoInit<T> {
fn register_init(&self, _h: &mut Hooks<T>) {}
}
impl<T> NoInit<T> for &Probe<T> {}
pub trait RegisterBootstrap<T> {
fn register_bootstrap(&self, h: &mut Hooks<T>);
}
impl<T: OnApplicationBootstrap> RegisterBootstrap<T> for Probe<T> {
fn register_bootstrap(&self, h: &mut Hooks<T>) {
h.on_application_bootstrap();
}
}
pub trait NoBootstrap<T> {
fn register_bootstrap(&self, _h: &mut Hooks<T>) {}
}
impl<T> NoBootstrap<T> for &Probe<T> {}
pub trait RegisterDestroy<T> {
fn register_destroy(&self, h: &mut Hooks<T>);
}
impl<T: OnModuleDestroy> RegisterDestroy<T> for Probe<T> {
fn register_destroy(&self, h: &mut Hooks<T>) {
h.on_module_destroy();
}
}
pub trait NoDestroy<T> {
fn register_destroy(&self, _h: &mut Hooks<T>) {}
}
impl<T> NoDestroy<T> for &Probe<T> {}
pub trait RegisterBeforeShutdown<T> {
fn register_before_shutdown(&self, h: &mut Hooks<T>);
}
impl<T: BeforeApplicationShutdown> RegisterBeforeShutdown<T> for Probe<T> {
fn register_before_shutdown(&self, h: &mut Hooks<T>) {
h.before_application_shutdown();
}
}
pub trait NoBeforeShutdown<T> {
fn register_before_shutdown(&self, _h: &mut Hooks<T>) {}
}
impl<T> NoBeforeShutdown<T> for &Probe<T> {}
pub trait RegisterShutdown<T> {
fn register_shutdown(&self, h: &mut Hooks<T>);
}
impl<T: OnApplicationShutdown> RegisterShutdown<T> for Probe<T> {
fn register_shutdown(&self, h: &mut Hooks<T>) {
h.on_application_shutdown();
}
}
pub trait NoShutdown<T> {
fn register_shutdown(&self, _h: &mut Hooks<T>) {}
}
impl<T> NoShutdown<T> for &Probe<T> {}
}
pub mod factory {
use std::cell::Cell;
use std::marker::PhantomData;
use std::sync::Arc;
use crate::binding::contribute::Contribute;
use crate::binding::factory::Factory;
use crate::module::def::ModuleDef;
use crate::scope::{Auto, PerExecution, Singleton, Transient};
use crate::timer::BoxError;
pub struct Probe<F, Args> {
factory: Cell<Option<F>>,
_a: PhantomData<fn() -> Args>,
}
impl<F, Args> Probe<F, Args> {
pub fn new(factory: F) -> Self {
Probe { factory: Cell::new(Some(factory)), _a: PhantomData }
}
}
pub trait FallibleBinding {
type Built: Send + Sync + 'static;
#[track_caller]
fn bind<S: ProviderScope>(&self, m: &mut ModuleDef<'_>);
#[track_caller]
fn bind_as<K, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
S: ProviderScope,
C: Fn(Arc<Self::Built>) -> Arc<K> + Send + Sync + 'static;
}
impl<F, Args, T, E> FallibleBinding for Probe<F, Args>
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
{
type Built = T;
#[track_caller]
fn bind<S: ProviderScope>(&self, m: &mut ModuleDef<'_>) {
if let Some(factory) = self.factory.take() {
S::try_bind::<Args, F, T, E>(m, factory);
}
}
#[track_caller]
fn bind_as<K, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
S: ProviderScope,
C: Fn(Arc<Self::Built>) -> Arc<K> + Send + Sync + 'static,
{
if let Some(factory) = self.factory.take() {
S::try_bind_as::<K, Args, F, T, E, C>(m, factory, coerce);
}
}
}
pub trait PlainBinding {
type Built: Send + Sync + 'static;
#[track_caller]
fn bind<S: ProviderScope>(&self, m: &mut ModuleDef<'_>);
#[track_caller]
fn bind_as<K, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
S: ProviderScope,
C: Fn(Arc<Self::Built>) -> Arc<K> + Send + Sync + 'static;
}
impl<F, Args> PlainBinding for &Probe<F, Args>
where
F: Factory<Args>,
F::Output: Send + Sync + 'static,
{
type Built = <F as Factory<Args>>::Output;
#[track_caller]
fn bind<S: ProviderScope>(&self, m: &mut ModuleDef<'_>) {
if let Some(factory) = self.factory.take() {
S::bind::<Args, F>(m, factory);
}
}
#[track_caller]
fn bind_as<K, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
S: ProviderScope,
C: Fn(Arc<Self::Built>) -> Arc<K> + Send + Sync + 'static,
{
if let Some(factory) = self.factory.take() {
S::bind_as::<K, Args, F, C>(m, factory, coerce);
}
}
}
pub trait ProviderScope {
#[track_caller]
fn bind<Args, F>(m: &mut ModuleDef<'_>, factory: F)
where
F: Factory<Args>,
F::Output: Send + Sync + 'static;
#[track_caller]
fn try_bind<Args, F, T, E>(m: &mut ModuleDef<'_>, factory: F)
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static;
#[track_caller]
fn bind_as<K, Args, F, C>(m: &mut ModuleDef<'_>, factory: F, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
F: Factory<Args>,
F::Output: Send + Sync + 'static,
C: Fn(Arc<F::Output>) -> Arc<K> + Send + Sync + 'static;
#[track_caller]
fn try_bind_as<K, Args, F, T, E, C>(m: &mut ModuleDef<'_>, factory: F, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
C: Fn(Arc<T>) -> Arc<K> + Send + Sync + 'static;
}
macro_rules! provider_scope {
($($marker:ty => $plain:ident, $fallible:ident;)*) => {$(
impl ProviderScope for $marker {
#[track_caller]
fn bind<Args, F>(m: &mut ModuleDef<'_>, factory: F)
where
F: Factory<Args>,
F::Output: Send + Sync + 'static,
{
m.$plain::<Args, F>(factory);
}
#[track_caller]
fn try_bind<Args, F, T, E>(m: &mut ModuleDef<'_>, factory: F)
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
{
m.$fallible::<Args, F, T, E>(factory);
}
#[track_caller]
fn bind_as<K, Args, F, C>(m: &mut ModuleDef<'_>, factory: F, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
F: Factory<Args>,
F::Output: Send + Sync + 'static,
C: Fn(Arc<F::Output>) -> Arc<K> + Send + Sync + 'static,
{
m.$plain::<Args, F>(factory).also_as::<K>(coerce);
}
#[track_caller]
fn try_bind_as<K, Args, F, T, E, C>(m: &mut ModuleDef<'_>, factory: F, coerce: C)
where
K: ?Sized + Send + Sync + 'static,
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
C: Fn(Arc<T>) -> Arc<K> + Send + Sync + 'static,
{
m.$fallible::<Args, F, T, E>(factory).also_as::<K>(coerce);
}
}
)*};
}
provider_scope! {
Auto => with, try_with;
Singleton => singleton, try_singleton;
PerExecution => execution, try_execution;
Transient => transient, try_transient;
}
pub trait FallibleContribution {
type Built: Send + Sync + 'static;
#[track_caller]
fn contribute_with<U, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
S: ContributionScope,
C: Fn(Arc<Self::Built>) -> Arc<U> + Send + Sync + 'static;
}
impl<F, Args, T, E> FallibleContribution for Probe<F, Args>
where
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
{
type Built = T;
#[track_caller]
fn contribute_with<U, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
S: ContributionScope,
C: Fn(Arc<Self::Built>) -> Arc<U> + Send + Sync + 'static,
{
if let Some(factory) = self.factory.take() {
S::try_contribute::<U, Args, F, T, E, C>(m.contribute::<U>(), factory, coerce);
}
}
}
pub trait PlainContribution {
type Built: Send + Sync + 'static;
#[track_caller]
fn contribute_with<U, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
S: ContributionScope,
C: Fn(Arc<Self::Built>) -> Arc<U> + Send + Sync + 'static;
}
impl<F, Args> PlainContribution for &Probe<F, Args>
where
F: Factory<Args>,
F::Output: Send + Sync + 'static,
{
type Built = <F as Factory<Args>>::Output;
#[track_caller]
fn contribute_with<U, S, C>(&self, m: &mut ModuleDef<'_>, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
S: ContributionScope,
C: Fn(Arc<Self::Built>) -> Arc<U> + Send + Sync + 'static,
{
if let Some(factory) = self.factory.take() {
S::contribute::<U, Args, F, C>(m.contribute::<U>(), factory, coerce);
}
}
}
pub trait ContributionScope {
#[track_caller]
fn contribute<U, Args, F, C>(into: Contribute<'_, U>, factory: F, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
F: Factory<Args>,
F::Output: Send + Sync + 'static,
C: Fn(Arc<F::Output>) -> Arc<U> + Send + Sync + 'static;
#[track_caller]
fn try_contribute<U, Args, F, T, E, C>(into: Contribute<'_, U>, factory: F, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
C: Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static;
}
macro_rules! contribution_scope {
($($marker:ty => $plain:ident, $fallible:ident;)*) => {$(
impl ContributionScope for $marker {
#[track_caller]
fn contribute<U, Args, F, C>(into: Contribute<'_, U>, factory: F, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
F: Factory<Args>,
F::Output: Send + Sync + 'static,
C: Fn(Arc<F::Output>) -> Arc<U> + Send + Sync + 'static,
{
into.$plain::<Args, F>(factory, coerce);
}
#[track_caller]
fn try_contribute<U, Args, F, T, E, C>(into: Contribute<'_, U>, factory: F, coerce: C)
where
U: ?Sized + Send + Sync + 'static,
F: Factory<Args, Output = Result<T, E>>,
T: Send + Sync + 'static,
E: Into<BoxError> + Send + 'static,
C: Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
{
into.$fallible::<Args, F, T, E>(factory, coerce);
}
}
)*};
}
contribution_scope! {
Auto => with, try_with;
Singleton => singleton, try_singleton;
PerExecution => execution, try_execution;
Transient => transient, try_transient;
}
}