Skip to content

Commit

Permalink
merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain Jiang committed Nov 9, 2023
1 parent b6201ab commit 6926ae5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion service-async/examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async fn main() {
// to make it more flexible, we can even make the factory a boxed type.
// so we can insert different layers and get a same type.
#[allow(unused_assignments)]
let mut fac: BoxedMakeService<usize, (), _, _> = FactoryStack::new(config)
let mut fac: BoxedMakeService<_, _> = FactoryStack::new(config)
.push(SvcAFactory::layer())
.push(SvcBFactory::layer())
.push_boxed_service()
Expand Down
14 changes: 14 additions & 0 deletions service-async/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ where
_marker: PhantomData<Req>,
}

unsafe impl<F: Send, Req> Send for BoxServiceFactory<F, Req>
where
F: MakeService,
F::Service: Service<Req>,
{
}

unsafe impl<F: Sync, Req> Sync for BoxServiceFactory<F, Req>
where
F: MakeService,
F::Service: Service<Req>,
{
}

impl<F, Req> BoxServiceFactory<F, Req>
where
F: MakeService,
Expand Down
21 changes: 17 additions & 4 deletions service-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,34 @@ pub trait MakeService {
}
}

impl<T: MakeService> MakeService for &T {
impl<T: MakeService + ?Sized> MakeService for &T {
type Service = T::Service;
type Error = T::Error;
fn make_via_ref(&self, old: Option<&Self::Service>) -> Result<Self::Service, Self::Error> {
(*self).make_via_ref(old)
}
}

impl<T: MakeService> MakeService for Arc<T> {
impl<T: MakeService + ?Sized> MakeService for Arc<T> {
type Service = T::Service;
type Error = T::Error;
fn make_via_ref(&self, old: Option<&Self::Service>) -> Result<Self::Service, Self::Error> {
self.as_ref().make_via_ref(old)
}
}

pub type BoxedMakeService<Req, Resp, SE, ME> =
Box<dyn MakeService<Service = BoxedService<Req, Resp, SE>, Error = ME>>;
impl<T: MakeService + ?Sized> MakeService for Box<T> {
type Service = T::Service;
type Error = T::Error;
fn make_via_ref(&self, old: Option<&Self::Service>) -> Result<Self::Service, Self::Error> {
self.as_ref().make_via_ref(old)
}
}

pub type BoxedMakeService<S, E> =
Box<dyn MakeService<Service = S, Error = E> + Send + Sync + 'static>;
pub type ArcMakeService<S, E> =
Arc<dyn MakeService<Service = S, Error = E> + Send + Sync + 'static>;
pub type BoxedMakeBoxedService<Req, Resp, SE, ME> =
BoxedMakeService<BoxedService<Req, Resp, SE>, ME>;
pub type ArcMakeBoxedService<Req, Resp, SE, ME> = ArcMakeService<BoxedService<Req, Resp, SE>, ME>;
25 changes: 19 additions & 6 deletions service-async/src/stack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{boxed::BoxServiceFactory, BoxedMakeService, BoxedService};
use std::sync::Arc;

use super::{layer::FactoryLayer, MakeService, MapTargetService, Service};
use super::{
boxed::BoxServiceFactory, layer::FactoryLayer, ArcMakeService, BoxedMakeService, MakeService,
MapTargetService, Service,
};

pub struct FactoryStack<C, S> {
config: C,
Expand Down Expand Up @@ -63,18 +66,28 @@ impl<C, F> FactoryStack<C, F> {

/// Push a new factory wrapper to get a fixed type factory.
#[inline]
pub fn push_boxed_factory<Req, Resp, SE, ME>(
self,
) -> FactoryStack<C, BoxedMakeService<Req, Resp, SE, ME>>
pub fn push_boxed_factory(self) -> FactoryStack<C, BoxedMakeService<F::Service, F::Error>>
where
F: MakeService<Service = BoxedService<Req, Resp, SE>, Error = ME> + 'static,
F: MakeService + Send + Sync + 'static,
{
FactoryStack {
config: self.config,
inner: Box::new(self.inner),
}
}

/// Push a new factory wrapper to get a fixed type factory.
#[inline]
pub fn push_arc_factory(self) -> FactoryStack<C, ArcMakeService<F::Service, F::Error>>
where
F: MakeService + Send + Sync + 'static,
{
FactoryStack {
config: self.config,
inner: Arc::new(self.inner),
}
}

/// Check if the stack is a factory of Service<R>.
#[inline]
pub fn check_make_svc<R>(self) -> Self
Expand Down

0 comments on commit 6926ae5

Please sign in to comment.