Skip to content

Commit

Permalink
feat: support BoxService (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWhiteWu authored Feb 9, 2023
1 parent 858832b commit 965578a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 48 deletions.
82 changes: 41 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion motore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "motore"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
description = """
Motore is a library of modular and reusable components for building robust
Expand Down
77 changes: 73 additions & 4 deletions motore/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,85 @@ pub trait UnaryService<Request> {
fn call(&self, req: Request) -> Self::Future<'_>;
}

/// A [`Send`] + [`Sync`] boxed [`Service`].
///
/// [`BoxService`] turns a service into a trait object, allowing the
/// response future type to be dynamic, and allowing the service to be cloned.
pub struct BoxService<Cx, T, U, E> {
raw: *mut (),
vtable: ServiceVtable<Cx, T, U, E>,
}

impl<Cx, T, U, E> BoxService<Cx, T, U, E> {
/// Create a new `BoxService`.
pub fn new<S>(s: S) -> Self
where
S: Service<Cx, T, Response = U, Error = E> + Send + Sync + 'static,
T: 'static,
for<'cx> S::Future<'cx>: Send,
{
let raw = Box::into_raw(Box::new(s)) as *mut ();
BoxService {
raw,
vtable: ServiceVtable {
call: call::<Cx, T, S>,
drop: drop::<S>,
},
}
}
}

impl<Cx, T, U, E> Drop for BoxService<Cx, T, U, E> {
fn drop(&mut self) {
unsafe { (self.vtable.drop)(self.raw) };
}
}

impl<Cx, T, U, E> fmt::Debug for BoxService<Cx, T, U, E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("BoxService").finish()
}
}

impl<Cx, T, U, E> Service<Cx, T> for BoxService<Cx, T, U, E> {
type Response = U;

type Error = E;

type Future<'cx> = BoxFuture<'cx, Result<U, E>>
where
Self: 'cx;

fn call<'cx, 's>(&'s self, cx: &'cx mut Cx, req: T) -> Self::Future<'cx>
where
's: 'cx,
{
unsafe { (self.vtable.call)(self.raw, cx, req) }
}
}

/// # Safety
///
/// The contained `Service` must be `Send` and `Sync` required by the bounds of `new` and `clone`.
unsafe impl<Cx, T, U, E> Send for BoxService<Cx, T, U, E> {}

unsafe impl<Cx, T, U, E> Sync for BoxService<Cx, T, U, E> {}

struct ServiceVtable<Cx, T, U, E> {
call: unsafe fn(raw: *mut (), cx: &mut Cx, req: T) -> BoxFuture<'_, Result<U, E>>,
drop: unsafe fn(raw: *mut ()),
}

/// A [`Clone`] + [`Send`] + [`Sync`] boxed [`Service`].
///
/// [`BoxCloneService`] turns a service into a trait object, allowing the
/// response future type to be dynamic, and allowing the service to be cloned.
///
/// This is similar to [`BoxService`](super::BoxService) except the resulting
/// This is similar to [`BoxService`](BoxService) except the resulting
/// service implements [`Clone`].
pub struct BoxCloneService<Cx, T, U, E> {
raw: *mut (),
vtable: ServiceVtable<Cx, T, U, E>,
vtable: CloneServiceVtable<Cx, T, U, E>,
}

impl<Cx, T, U, E> BoxCloneService<Cx, T, U, E> {
Expand All @@ -141,7 +210,7 @@ impl<Cx, T, U, E> BoxCloneService<Cx, T, U, E> {
let raw = Box::into_raw(Box::new(s)) as *mut ();
BoxCloneService {
raw,
vtable: ServiceVtable {
vtable: CloneServiceVtable {
call: call::<Cx, T, S>,
clone: clone::<Cx, T, S>,
drop: drop::<S>,
Expand Down Expand Up @@ -192,7 +261,7 @@ unsafe impl<Cx, T, U, E> Send for BoxCloneService<Cx, T, U, E> {}

unsafe impl<Cx, T, U, E> Sync for BoxCloneService<Cx, T, U, E> {}

struct ServiceVtable<Cx, T, U, E> {
struct CloneServiceVtable<Cx, T, U, E> {
call: unsafe fn(raw: *mut (), cx: &mut Cx, req: T) -> BoxFuture<'_, Result<U, E>>,
clone: unsafe fn(raw: *mut ()) -> BoxCloneService<Cx, T, U, E>,
drop: unsafe fn(raw: *mut ()),
Expand Down
4 changes: 2 additions & 2 deletions motore/src/service/service_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod tests {
struct MotoreContext;

async fn handle(cx: &mut MotoreContext, request: String) -> Result<String, Infallible> {
println!("{:?}, {:?}", cx, request);
println!("{cx:?}, {request:?}");
Ok::<_, Infallible>(request.to_uppercase())
}

Expand All @@ -121,7 +121,7 @@ mod tests {
assert_eq!(
"ServiceFn { f: motore::service::service_fn::tests::debug_impl_ok::handle }"
.to_string(),
format!("{:?}", uppercase_service),
format!("{uppercase_service:?}"),
);
}
}

0 comments on commit 965578a

Please sign in to comment.