Skip to content

Commit

Permalink
Merge pull request #1 from yumemi-inc/feat/you-dont-need-resolver
Browse files Browse the repository at this point in the history
feat: Get rid of Resolver struct
  • Loading branch information
siketyan authored Feb 28, 2023
2 parents 97a8a60 + 492649a commit 9bd20cf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.1.0"
edition = "2021"
rust-version = "1.67.1"
license = "MIT"
readme = "../README.md"
repository = "https://github.com/yumemi-inc/ruice.git"
authors = [
"Naoki Ikeguchi <n_ikeguchi@yumemi.co.jp>",
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.1.0"
edition = "2021"
rust-version = "1.67.1"
license = "MIT"
readme = "../README.md"
repository = "https://github.com/yumemi-inc/ruice.git"
authors = [
"Naoki Ikeguchi <n_ikeguchi@yumemi.co.jp>",
Expand Down
8 changes: 3 additions & 5 deletions core/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::sync::Arc;

use async_trait::async_trait;

use crate::{
AsyncResolve, AsyncResolver, AsyncServices, Resolve, Resolver, ServiceContainer, Services,
};
use crate::{AsyncResolve, AsyncResolver, AsyncServices, Resolve, ServiceContainer, Services};

pub struct Bound<Interface>
where
Expand Down Expand Up @@ -100,7 +98,7 @@ pub trait BindServices: Services {
where
Interface: ?Sized + Send + Sync + 'static,
{
self.put(Resolver::new(Bound::from(service)));
self.put(Bound::from(service));
}

fn bind_by<Interface, F>(&mut self, f: F)
Expand All @@ -109,7 +107,7 @@ pub trait BindServices: Services {
F: (Fn(&Self) -> Option<Arc<Interface>>) + Send + Sync + 'static,
Self: 'static,
{
self.put(Resolver::new(BindBy::from(f)))
self.put(BindBy::from(f))
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/construct.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::marker::PhantomData;
use std::sync::Arc;

use crate::{Resolve, Resolver, ServiceContainer, Services};
use crate::{Resolve, ServiceContainer, Services};

pub trait Construct<S = Self, C = ServiceContainer>: Send + Sync {
fn construct(container: &C) -> Option<S>;
Expand Down Expand Up @@ -39,7 +39,7 @@ pub trait ConstructServices: Services {
where
S: Construct<S, Self> + 'static,
{
self.put(Resolver::<S, Self>::new(Constructor::<S>::new()));
self.put(Constructor::<S>::new());
}
}

Expand Down
19 changes: 11 additions & 8 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
fn resolve(&self, container: &C) -> Option<Arc<S>>;
}

pub struct Resolver<S, C = ServiceContainer>
struct Resolver<S, C = ServiceContainer>
where
S: ?Sized,
{
Expand All @@ -52,7 +52,7 @@ impl<S, C> Resolver<S, C>
where
S: ?Sized,
{
pub fn new<R>(resolve: R) -> Self
fn new<R>(resolve: R) -> Self
where
R: Resolve<S, C> + 'static,
{
Expand All @@ -61,7 +61,7 @@ where
}
}

pub fn as_inner(&self) -> &dyn Resolve<S, C> {
fn as_inner(&self) -> &dyn Resolve<S, C> {
self.resolve.as_ref()
}
}
Expand Down Expand Up @@ -104,16 +104,17 @@ pub trait Services: Sized + Send + Sync {
where
S: ?Sized + Send + Sync + 'static;

fn put<S>(&mut self, resolver: Resolver<S, Self>)
fn put<S, R>(&mut self, resolver: R)
where
S: ?Sized + Send + Sync + 'static;
S: ?Sized + Send + Sync + 'static,
R: Resolve<S, Self> + 'static;

fn replace<S, F>(&mut self, f: F)
where
S: Send + Sync + 'static,
F: FnOnce(Option<&S>) -> S,
{
self.put(Resolver::new(Singleton::new(f(self.get::<S>().as_deref()))));
self.put(Singleton::new(f(self.get::<S>().as_deref())));
}
}

Expand Down Expand Up @@ -146,11 +147,13 @@ impl Services for ServiceContainer {
.and_then(|r| r.as_inner().resolve(self))
}

fn put<S>(&mut self, resolver: Resolver<S>)
fn put<S, R>(&mut self, resolver: R)
where
S: ?Sized + Send + Sync + 'static,
R: Resolve<S, Self> + 'static,
{
self.services.insert(TypeId::of::<S>(), Arc::new(resolver));
self.services
.insert(TypeId::of::<S>(), Arc::new(Resolver::new(resolver)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/singleton.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::{Resolve, Resolver, Services};
use crate::{Resolve, Services};

pub struct Singleton<S> {
service: Arc<S>,
Expand Down Expand Up @@ -28,7 +28,7 @@ pub trait SingletonServices: Services {
where
S: Send + Sync + 'static,
{
self.put(Resolver::new(Singleton::new(service)));
self.put(Singleton::new(service));
}
}

Expand Down

0 comments on commit 9bd20cf

Please sign in to comment.