ctrlgen-rs

A fork of vi/trait-enumizer that attempts to be easier to use, while sacrificing a bit of flexibility.

Documentation to come. See docs of trait-enumizer, and examples here for now.

Differences to trait-enumizer:

Most of the efforts here could probably be merged into trait-enumizer, but i was in a hurry, so i implemented the features i needed instead.

Example

```rust,ignore struct Service> { counter: T, }

[ctrlgen::ctrlgen(pub ServiceMsg,

returnval = TokioRetval,
proxy = ServiceProxy

)] impl Service { pub fn increment_by(&mut self, arg: i32) -> i32 { self.counter = arg; self.counter } } ```

This will generate the following code:

```rust,ignore pub enum ServiceMsg where TokioRetval: ::ctrlgen::Returnval, { IncrementBy { arg: i32, ret: ::Sender, }, }

impl ::ctrlgen::CallMut for ServiceMsg where TokioRetval: ::ctrlgen::Returnval, { type Output = core::result::Result<(), ::SendError>; fn callmut(self, this: &mut Service) -> Self::Output { match self { Self::IncrementBy { arg, ret } => { ::send(ret, this.incrementby(arg)) } } } }

pub struct ServiceProxy> { sender: Sender, }

impl> ServiceProxy where TokioRetval: ::ctrlgen::Returnval, { pub fn new(sender: Sender) -> Self { Self { sender } } pub fn increment_by(&self, arg: i32) -> ::RecvResult { let ret = ::create(); let msg = ServiceMsg::IncrementBy { arg, ret: ret.0 }; self.sender.send(msg); ::recv(ret.1) } } ```

Returnval

By setting the returnval = <Trait> parameter, you configure the channel over which return values are sent. <Trait> must implement ctrlgen::Returnval.

Example Implementation:

```rust,ignore pub struct FailedToSendRetVal; pub struct TokioRetval; impl Returnval for TokioRetval { type Sender = promise::Sender; type Receiver = promise::Promise; type SendError = FailedToSendRetVal;

type RecvResult<T> = promise::Promise<T>;

fn create<T>() -> (Self::Sender<T>, Self::Receiver<T>) {
    promise::Promise::channel()
}

fn recv<T>(rx: Self::Receiver<T>) -> Self::RecvResult<T> {
    rx
}

fn send<T>(tx: Self::Sender<T>, msg: T) -> core::result::Result<(), Self::SendError> {
    tx.send(msg).map_err(|_| FailedToSendRetVal)
}

} ```