Derive Macro Bind

Source
#[derive(Bind)]
{
    // Attributes available to this derive:
    #[binding]
}
Expand description

Derive a custom implementation of [hyperactor::message::Bind] trait for a struct or enum. This macro is normally used in tandem with [fn derive_unbind] to make the applied struct or enum castable.

Specifically, the derived implementation iterates through fields annotated with #[binding(include)] based on their order of declaration in the struct or enum. These fields’ types must implement Bind trait as well. During the iteration, parameters from bindings are bound to these fields.

§Example

This macro supports named and unamed structs and enums. Below are examples of the supported types:

#[derive(Bind, Unbind)]
struct MyNamedStruct {
    field0: u64,
    field1: MyReply,
    #[binding(include)]
nnnn     field2: PortRef<MyReply>,
    field3: bool,
    #[binding(include)]
    field4: hyperactor::PortRef<u64>,
}

#[derive(Bind, Unbind)]
struct MyUnamedStruct(
    u64,
    MyReply,
    #[binding(include)] hyperactor::PortRef<MyReply>,
    bool,
    #[binding(include)] PortRef<u64>,
);

#[derive(Bind, Unbind)]
enum MyEnum {
    Unit,
    NoopTuple(u64, bool),
    NoopStruct {
        field0: u64,
        field1: bool,
    },
    Tuple(
        u64,
        MyReply,
        #[binding(include)] PortRef<MyReply>,
        bool,
        #[binding(include)] hyperactor::PortRef<u64>,
    ),
    Struct {
        field0: u64,
        field1: MyReply,
        #[binding(include)]
        field2: PortRef<MyReply>,
        field3: bool,
        #[binding(include)]
        field4: hyperactor::PortRef<u64>,
    },
}

The following shows what derived Bind`` and Unbind`` implementations for MyNamedStruct will look like. The implementations of other types are similar, and thus are not shown here.

impl Bind for MyNamedStruct {
fn bind(&mut self, bindings: &mut Bindings) -> anyhow::Result<()> {
    Bind::bind(&mut self.field2, bindings)?;
    Bind::bind(&mut self.field4, bindings)?;
    Ok(())
}

impl Unbind for MyNamedStruct {
    fn unbind(&self, bindings: &mut Bindings) -> anyhow::Result<()> {
        Unbind::unbind(&self.field2, bindings)?;
        Unbind::unbind(&self.field4, bindings)?;
        Ok(())
    }
}