Skip to content

Commit

Permalink
chore: run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandros committed Oct 16, 2024
1 parent 45d30be commit e957d39
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 107 deletions.
27 changes: 16 additions & 11 deletions ferrunix-core/src/dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! All possible dependencies for injected types.
//!
//! The following dependency types are available, as of right now:
//! * [`Transient`]: Dependencies that are created from scratch when requested.
//! * [`Transient`]: Dependencies that are created from scratch when
//! requested.
//! * [`Singleton`]: Dependencies that are created once for every registry.
//!
//! All dependency types implement the [`Dep`] trait, and can get access to the inner type via
//! `.get`.
//! All dependency types implement the [`Dep`] trait, and can get access to the
//! inner type via `.get`.
//!
//! # Examples
//! ```
Expand Down Expand Up @@ -40,7 +41,8 @@ mod private {
pub trait Sealed {}
}

/// Trait to specify a dependency. Every possible dependency type is implementing this trait.
/// Trait to specify a dependency. Every possible dependency type is
/// implementing this trait.
///
/// Current implementors:
/// * [`Transient`]
Expand Down Expand Up @@ -106,9 +108,10 @@ impl<T: Registerable> Dep for Transient<T> {
/// This function panics if the `T` isn't registered.
fn new(registry: &Registry) -> Self {
Self {
inner: registry
.get_transient::<T>()
.expect("transient dependency must only be constructed if it's fulfillable"),
inner: registry.get_transient::<T>().expect(
"transient dependency must only be constructed if it's \
fulfillable",
),
}
}

Expand All @@ -120,7 +123,8 @@ impl<T: Registerable> Dep for Transient<T> {

/// Singleton dependencies.
///
/// This dependency is created only once for the specified registry. It's created lazily on-demand.
/// This dependency is created only once for the specified registry. It's
/// created lazily on-demand.
#[repr(transparent)]
pub struct Singleton<T> {
/// The resolved type.
Expand Down Expand Up @@ -173,9 +177,10 @@ impl<T: Registerable> Dep for Singleton<T> {
/// This function panics if the `T` isn't registered.
fn new(registry: &Registry) -> Self {
Self {
inner: registry
.get_singleton::<T>()
.expect("singleton dependency must only be constructed if it's fulfillable"),
inner: registry.get_singleton::<T>().expect(
"singleton dependency must only be constructed if it's \
fulfillable",
),
}
}

Expand Down
32 changes: 18 additions & 14 deletions ferrunix-core/src/dependency_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Implementation of [`DepBuilder`] for tuples to be used with [`Registry::with_deps`].
//! Implementation of [`DepBuilder`] for tuples to be used with
//! [`Registry::with_deps`].
use std::any::TypeId;

Expand All @@ -7,28 +8,31 @@ use crate::Registry;

/// Required for sealing the trait. *Must not be public*.
pub(crate) mod private {
/// This token is used to seal the [`DepBuilder`] trait from downstream crates.
/// This token is used to seal the [`DepBuilder`] trait from downstream
/// crates.
#[allow(missing_debug_implementations)]
#[derive(Clone, Copy)]
pub struct SealToken;
}

/// The [`DepBuilder`] trait is the key to specify a variable amount of dependencies in the
/// [`Registry::with_deps`] call from [`Registry`].
/// The [`DepBuilder`] trait is the key to specify a variable amount of
/// dependencies in the [`Registry::with_deps`] call from [`Registry`].
///
/// The trait is implemented by the `DepBuilderImpl!` macro for 0-ary, to 10-ary tuples (e.g.,
/// `(T1,)`, `(T1, T2)`, etc.), which allows these tuples to be passed as a single type parameter
/// into [`Registry::with_deps`].
/// The trait is implemented by the `DepBuilderImpl!` macro for 0-ary, to 10-ary
/// tuples (e.g., `(T1,)`, `(T1, T2)`, etc.), which allows these tuples to be
/// passed as a single type parameter into [`Registry::with_deps`].
///
/// This trait is sealed, meaning it cannot be implemented or called by any downstream crates.
/// This trait is sealed, meaning it cannot be implemented or called by any
/// downstream crates.
pub trait DepBuilder<R> {
/// When implemented, this should validate that all dependencies which are part of `Self` exist
/// to construct the type `R`. If the dependencies cannot be fulfilled, `None` must be
/// returned.
/// When implemented, this should validate that all dependencies which are
/// part of `Self` exist to construct the type `R`. If the dependencies
/// cannot be fulfilled, `None` must be returned.
///
/// If the dependencies can be fulfilled, they must be constructed as an N-ary tuple (same
/// length and types as `Self`) and passed as the argument to `ctor`. `ctor` is a user provided
/// constructor for the type `R`.
/// If the dependencies can be fulfilled, they must be constructed as an
/// N-ary tuple (same length and types as `Self`) and passed as the
/// argument to `ctor`. `ctor` is a user provided constructor for the
/// type `R`.
///
/// An implementation for tuples is provided by `DepBuilderImpl!`.
///
Expand Down
4 changes: 2 additions & 2 deletions ferrunix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! [`ferrunix`]: https://crates.io/crates/ferrunix
//! [`ferrunix-core`]: https://crates.io/crates/ferrunix-core
#[doc(hidden)]
pub mod types;
pub mod dependencies;
pub mod dependency_builder;
pub mod error;
#[doc(hidden)]
pub mod registration;
pub mod registry;
#[doc(hidden)]
pub mod types;

// Public re-exports for easier access.
// These are the main types users use for interacting with ferrunix.
Expand Down
13 changes: 7 additions & 6 deletions ferrunix-core/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::{types::OnceCell, Registry};

/// The global, `'static` default [`Registry`]. It's constructed and accessible via
/// [`Registry::global`].
/// The global, `'static` default [`Registry`]. It's constructed and accessible
/// via [`Registry::global`].
#[cfg(feature = "multithread")]
pub(crate) static DEFAULT_REGISTRY: OnceCell<Registry> = OnceCell::new();

Expand All @@ -29,11 +29,12 @@ pub struct RegistrationFunc(pub(crate) fn(&Registry));
impl RegistrationFunc {
/// Create a new [`RegistrationFunc`] from a `register` function.
///
/// The `register` function gets passed a [`Registry`], which it must use to register one or
/// more types.
/// The `register` function gets passed a [`Registry`], which it must use to
/// register one or more types.
///
/// The function must not have any side-effects. It may be called concurrently, in parallel,
/// and from any thread (not only the main thread).
/// The function must not have any side-effects. It may be called
/// concurrently, in parallel, and from any thread (not only the main
/// thread).
///
/// # Example
/// ```no_run
Expand Down
86 changes: 48 additions & 38 deletions ferrunix-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ pub struct Registry {
}

impl Registry {
/// Create a new, empty, registry. This registry contains no pre-registered types.
/// Create a new, empty, registry. This registry contains no pre-registered
/// types.
///
/// Types that are auto-registered are also not included in this registry.
///
/// To get access to the auto-registered types (types that are annotated by the derive macro),
/// the global registry [`Registry::global`] needs to be used.
/// To get access to the auto-registered types (types that are annotated by
/// the derive macro), the global registry [`Registry::global`] needs to
/// be used.
#[must_use]
pub fn empty() -> Self {
Self {
Expand All @@ -43,8 +45,8 @@ impl Registry {

/// Create an empty registry, and add all autoregistered types into it.
///
/// This is the constructor for the global registry that can be acquired with
/// [`Registry::global`].
/// This is the constructor for the global registry that can be acquired
/// with [`Registry::global`].
#[must_use]
pub fn autoregistered() -> Self {
let registry = Self::empty();
Expand All @@ -61,8 +63,8 @@ impl Registry {
/// [`Registry::with_deps`].
///
/// # Parameters
/// * `ctor`: A constructor function returning the newly constructed `T`. This constructor
/// will be called for every `T` that is requested.
/// * `ctor`: A constructor function returning the newly constructed `T`.
/// This constructor will be called for every `T` that is requested.
pub fn transient<T>(&self, ctor: fn() -> T)
where
T: Registerable,
Expand All @@ -85,8 +87,9 @@ impl Registry {
/// [`Registry::with_deps`].
///
/// # Parameters
/// * `ctor`: A constructor function returning the newly constructed `T`. This constructor
/// will be called once, lazily, when the first instance of `T` is requested.
/// * `ctor`: A constructor function returning the newly constructed `T`.
/// This constructor will be called once, lazily, when the first
/// instance of `T` is requested.
pub fn singleton<T>(&self, ctor: fn() -> T)
where
T: Registerable,
Expand Down Expand Up @@ -127,8 +130,8 @@ impl Registry {

/// Retrieves the singleton `T` from this registry.
///
/// Returns `None` if `T` wasn't registered or failed to construct. The singleton is a
/// ref-counted pointer object (either `Arc` or `Rc`).
/// Returns `None` if `T` wasn't registered or failed to construct. The
/// singleton is a ref-counted pointer object (either `Arc` or `Rc`).
pub fn get_singleton<T>(&self) -> Option<Ref<T>>
where
T: Registerable,
Expand Down Expand Up @@ -159,22 +162,23 @@ impl Registry {

/// Check whether all registered types have the required dependencies.
///
/// Returns true if for all registered types all of it's dependencies can be constructed, false
/// otherwise.
/// Returns true if for all registered types all of it's dependencies can be
/// constructed, false otherwise.
///
/// This is a potentially expensive call since it needs to go through the entire dependency
/// tree for each registered type.
/// This is a potentially expensive call since it needs to go through the
/// entire dependency tree for each registered type.
///
/// Nontheless, it's recommended to call this before using the [`Registry`].
pub fn validate_all(&self) -> bool {
let lock = self.validation.read();
lock.iter().all(|(_, validator)| (validator)(self))
}

/// Check whether the type `T` is registered in this registry, and all dependencies of the type
/// `T` are also registered.
/// Check whether the type `T` is registered in this registry, and all
/// dependencies of the type `T` are also registered.
///
/// Returns true if the type and it's dependencies can be constructed, false otherwise.
/// Returns true if the type and it's dependencies can be constructed, false
/// otherwise.
pub fn validate<T>(&self) -> bool
where
T: Registerable,
Expand All @@ -186,17 +190,17 @@ impl Registry {

/// Access the global registry.
///
/// This registry contains the types that are marked for auto-registration via the derive
/// macro.
/// This registry contains the types that are marked for auto-registration
/// via the derive macro.
#[cfg(feature = "multithread")]
pub fn global() -> &'static Self {
DEFAULT_REGISTRY.get_or_init(Self::autoregistered)
}

/// Access the global registry.
///
/// This registry contains the types that are marked for auto-registration via the derive
/// macro.
/// This registry contains the types that are marked for auto-registration
/// via the derive macro.
#[cfg(not(feature = "multithread"))]
pub fn global() -> std::rc::Rc<Self> {
DEFAULT_REGISTRY.with(|val| {
Expand All @@ -206,8 +210,8 @@ impl Registry {
})
}

/// Reset the global registry, removing all previously registered types, and re-running the
/// auto-registration routines.
/// Reset the global registry, removing all previously registered types, and
/// re-running the auto-registration routines.
///
/// # Safety
/// Ensure that no other thread is currently using [`Registry::global()`].
Expand Down Expand Up @@ -235,7 +239,8 @@ impl std::fmt::Debug for Registry {
}
}

/// A builder for objects with dependencies. This can be created by using [`Registry::with_deps`].
/// A builder for objects with dependencies. This can be created by using
/// [`Registry::with_deps`].
#[allow(clippy::single_char_lifetime_names)]
pub struct Builder<'a, T, Deps> {
registry: &'a Registry,
Expand All @@ -248,13 +253,14 @@ where
Deps: DepBuilder<T> + 'static,
T: Registerable,
{
/// Register a new transient object, with dependencies specified in `.with_deps`.
/// Register a new transient object, with dependencies specified in
/// `.with_deps`.
///
/// The `ctor` parameter is a constructor function returning the newly constructed `T`. The constructor
/// accepts a single argument `Deps` (a tuple implementing
/// [`crate::dependency_builder::DepBuilder`]). It's best to destructure the tuple to
/// accept each dependency separately. This constructor will be called for every `T` that is
/// requested.
/// The `ctor` parameter is a constructor function returning the newly
/// constructed `T`. The constructor accepts a single argument `Deps` (a
/// tuple implementing [`crate::dependency_builder::DepBuilder`]). It's
/// best to destructure the tuple to accept each dependency separately.
/// This constructor will be called for every `T` that is requested.
///
/// # Example
/// ```no_run
Expand All @@ -271,7 +277,8 @@ where
/// });
/// ```
///
/// For single dependencies, the destructured tuple needs to end with a comma: `(dep,)`.
/// For single dependencies, the destructured tuple needs to end with a
/// comma: `(dep,)`.
pub fn transient(&self, ctor: fn(Deps) -> T) {
self.registry.objects.write().insert(
TypeId::of::<T>(),
Expand Down Expand Up @@ -304,12 +311,14 @@ where
);
}

/// Register a new singleton object, with dependencies specified in `.with_deps`.
/// Register a new singleton object, with dependencies specified in
/// `.with_deps`.
///
/// The `ctor` parameter is a constructor function returning the newly constructed `T`. The
/// constructor accepts a single argument `Deps` (a tuple implementing
/// [`crate::dependency_builder::DepBuilder`]). It's best to destructure the tuple to accept
/// each dependency separately. This constructor will be called once, lazily, when the first
/// The `ctor` parameter is a constructor function returning the newly
/// constructed `T`. The constructor accepts a single argument `Deps` (a
/// tuple implementing [`crate::dependency_builder::DepBuilder`]). It's
/// best to destructure the tuple to accept each dependency separately.
/// This constructor will be called once, lazily, when the first
/// instance of `T` is requested.
///
/// # Example
Expand All @@ -327,7 +336,8 @@ where
/// });
/// ```
///
/// For single dependencies, the destructured tuple needs to end with a comma: `(dep,)`.
/// For single dependencies, the destructured tuple needs to end with a
/// comma: `(dep,)`.
pub fn singleton(&self, ctor: fn(Deps) -> T) {
let getter = Box::new(
move |this: &Registry, cell: &SingletonCell| -> Option<RefAny> {
Expand Down
Loading

0 comments on commit e957d39

Please sign in to comment.