brug

Crates.iobrug
lib.rsbrug
version0.3.0-alpha0
sourcesrc
created_at2023-07-28 11:25:13.445213
updated_at2024-05-10 14:55:14.943384
descriptionenum command generation for implementation blocks
homepage
repositoryhttps://github.com/cijber/brug/
max_upload_size
id928439
size7,402
eater (the-eater)

documentation

README

Brug

It's a bridge!

Brug allows you to transform function calls for a implementation to be turned into RPC like enum's, and offers both generation of facade traits and performer traits

An example speaks louder than 2 words:

use brug::{Performer, tokio::OneShot};

struct MyStruct;

#[brug::performer]
impl MyStruct {
  fn add(a: usize, b: usize) -> usize {
    a + b
  }
}

async fn main() {
  let (s, r) = OneShot::pair();
  let command = MyStructCommand::Add(1, 2, s);
  let mut my = MyStruct;

  my.perform(command).await;
  assert_eq!(r.receive().await.expect("command got dropped before processed"), 3);
}

// The attribute on MyStruct expands to the following:
pub enum MyStructCommand<T: ::brug::Transport> {
  Add(usize, usize, T::Sender<usize>),
}

#[::brug::async_trait]
impl ::brug::Performer<MyStructCommand> for MyStruct {
  async fn perform(&mut self, command: MyStructCommand) {
    match command {
      MyStructCommand::Add(a, b, resp) => {
        ::brug::Sender::send(resp, self.add(a, b)).await;
      }
    }
  }
}

#[::brug::async_trait]
pub trait MyStructFacade<T: ::brug::Transport> {
  async fn add(&self, a: usize, b: usize) -> usize {
    let (s, r) = T::pair();
    self.handle(MyStructCommand::Add(a, b, s)).await;
    return r.receive().await.expect("add didn't return a value");
  }

  async fn handle(&self, command: MyStructCommand<T>);
}

#[::brug::async_trait]
pub trait MyStructFacadeMut<T: ::brug::Transport> {
  async fn add(&mut self, a: usize, b: usize) -> usize {
    let (s, r) = T::pair();
    self.handle(MyStructCommand::Add(a, b, s)).await;
    return r.receive().await.expect("add didn't return a value");
  }

  async fn handle(&mut self, command: MyStructCommand<T>);
}

#[::brug::async_trait]
impl<T: ::brug::Transport, F: MyStructFacade<T> + Send + Sync> MyStructFacadeMut<T> for F {
  async fn handle(&mut self, command: MyStructCommand<T>) {
    MyStructFacadeMut::handle(self, command).await;
  }
}

The Command enum allows you to use an RPC pattern for a given struct, and the Facade's allow you to create an object that functions like the given struct, but is actually using said RPC pattern

Commit count: 14

cargo fmt