Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/little-rivers-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 2 additions & 0 deletions .changeset/pink-stingrays-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 2 additions & 0 deletions .changeset/yellow-berries-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ authors = ["AsPulse <contact@aspulse.dev>"]
license = "MIT/Apache-2.0"
edition = "2021"
repository = "https://github.com/pulsuite/servify"

[profile.size-optimized]
inherits = "release"
opt-level = "s"
8 changes: 2 additions & 6 deletions servify/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
pub use servify_macro::{export, service};

pub trait ServifyExport {
type Request;
type Response;
}
pub mod processor;
pub mod serial;
56 changes: 56 additions & 0 deletions servify/src/processor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use tokio::sync::oneshot;

use crate::serial::ServifyMiddle;
use std::any::Any;

pub type ServifyMessage<T> = (
ServifyMiddle<<T as ServifyProcessor>::Kind, <T as ServifyProcessor>::Context>,
oneshot::Sender<Box<dyn Any + Send>>,
);

pub trait ServifyProcessor: Sized {
type Kind;
type Context;

fn serve(
&mut self,
middle: ServifyMiddle<Self::Kind, Self::Context>,
) -> impl std::future::Future<Output = Box<dyn Any + Send>>;

fn channel(self, buffer_size: usize) -> (ServifyService<Self>, ServifyAccess<Self>) {
let (tx, rx) = tokio::sync::mpsc::channel(buffer_size);
(
ServifyService {
processor: self,
rx,
},
ServifyAccess { tx },
)
}
}

pub struct ServifyService<T: ServifyProcessor + Sized> {
processor: T,
rx: tokio::sync::mpsc::Receiver<ServifyMessage<T>>,
}

impl<T: ServifyProcessor + Sized> ServifyService<T> {
pub async fn listen(mut self) {
while let Some((middle, tx)) = self.rx.recv().await {
let response = self.processor.serve(middle).await;
tx.send(response).unwrap();
}
}
}

pub struct ServifyAccess<T: ServifyProcessor + Sized> {
pub tx: tokio::sync::mpsc::Sender<ServifyMessage<T>>,
}

impl<T: ServifyProcessor + Sized> Clone for ServifyAccess<T> {
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
}
}
}
17 changes: 17 additions & 0 deletions servify/src/serial/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::any::Any;

pub struct ServifyMiddle<Kind, Context> {
pub ctx: Context,
pub request: ServifyRequest<Kind>,
}

pub struct ServifyRequest<Kind> {
pub payload: Box<dyn Any + Send>,
pub kind: Kind,
}

impl<Kind> ServifyRequest<Kind> {
pub fn with_ctx<Context>(self, ctx: Context) -> ServifyMiddle<Kind, Context> {
ServifyMiddle { ctx, request: self }
}
}
98 changes: 0 additions & 98 deletions servify/tests/expanded_1.rs

This file was deleted.

103 changes: 0 additions & 103 deletions servify/tests/expanded_2.rs

This file was deleted.

Loading