Skip to main content

wire_impl

Macro wire_impl 

Source
macro_rules! wire_impl {
    (
        impl $name:ident [$( $gen:ident = $index:literal ),*] $( where [$( $pred:tt )*] )? record {
            $( $field:tt $flow:literal : $ty:ty ),* $(,)?
        } phantom [$( $phantom:tt ),*] $( notes $notes:expr )?
    ) => { ... };
    (
        impl $name:ident [$( $gen:ident = $index:literal ),*] $( where [$( $pred:tt )*] )? enum {
            $(
                $variant:ident = $number:literal $kind:ident {
                    $( $member:tt ($bind:ident) $member_flow:literal : $member_ty:ty ),* $(,)?
                }
            ),* $(,)?
        } $( notes $notes:expr )?
    ) => { ... };
    (
        impl $name:ident [$( $gen:ident = $index:literal ),*] $( where [$( $pred:tt )*] )? newtype {
            $field:tt : $ty:ty
        } phantom [$( $phantom:tt ),*] $( notes $notes:expr )?
    ) => { ... };
    (@notes) => { ... };
    (@notes $notes:expr) => { ... };
    (@form $name:ident []) => { ... };
    (@form $name:ident [$( $gen:ident = $index:literal ),+]) => { ... };
    (@parts Empty []) => { ... };
    (@parts Positional [$( $flow:literal : $ty:ty ),+]) => { ... };
    (@parts Named [$( $flow:literal : $ty:ty ),*]) => { ... };
}
Expand description

Gives a record, an enum or a newtype defined elsewhere its Shape and its Shaped mapping, from its field list: #[derive(Flow)] expands to it. The type’s parameters come with their positions, and any bounds its definition puts on them in brackets after where; each field, constructor part and constructor field with its Flow name, and each part with the name its value is bound to; and a constructor with its number, which is its position, and whether its payload is Empty, Positional or Named. Fields of PhantomData are listed after phantom, and what the declaration’s text says beside its shape after notes.

use flow_abi::wire::{Declared, Layout, Shaped, WireForm};

pub struct Pair<T> {
    pub first: T,
    pub rest: Vec<T>,
}
pub enum Turn {
    Stop,
    Move(bool),
    Say { text: String },
}

flow_abi::wire_impl! {
    impl Pair [T = 0] record {
        first "first": T,
        rest "rest": Vec<T>,
    } phantom []
}
flow_abi::wire_impl! {
    impl Turn [] enum {
        Stop = 0 Empty {},
        Move = 1 Positional { 0 (flag) "": bool },
        Say = 2 Named { text (text) "text": String },
    }
}

assert!(matches!(<Pair<bool> as Shaped>::FORM, WireForm::Applied(..)));
assert!(matches!(<Turn as Declared>::shape().layout, Layout::Enum(ctors) if ctors.len() == 3));