Skip to main content

wire

Macro wire 

Source
macro_rules! wire {
    (
        $(#[$meta:meta])*
        $vis:vis struct $name:ident {
            $( $(#[$field_meta:meta])* $field_vis:vis $field:ident : $ty:ty ),* $(,)?
        }
    ) => { ... };
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident $( < $( $gen:ident $( = $default:ty )? ),+ > )? {
            $(
                $(#[$variant_meta:meta])*
                $variant:ident
                $( ( $($part:ty),+ $(,)? ) )?
                $( { $( $(#[$member_meta:meta])* $member:ident : $member_ty:ty ),* $(,)? } )?
                $( = $number:literal )?
            ),* $(,)?
        }
        $($kind:tt)*
    ) => { ... };
    (@number $at:expr) => { ... };
    (@number $at:expr, $number:literal) => { ... };
    (@kind $name:ident $gens:tt [$( $(#[$variant_meta:meta])* $variant:ident )*]) => { ... };
    (@kind $name:ident [] [$( $(#[$variant_meta:meta])* $variant:ident )*] enum Self;) => { ... };
    (@kind $name:ident [$( $gen:ident ),*] [$( $(#[$variant_meta:meta])* $variant:ident )*]
        $(#[$kind_meta:meta])* $kind_vis:vis enum $kind:ident;
    ) => { ... };
    (@kinds $name:ident $kind:ident [$( $variant:ident )*]) => { ... };
    (@position $self:ident $kind:ident [$( $arms:tt )*] $at:expr;) => { ... };
    (@position $self:ident $kind:ident [$( $arms:tt )*] $at:expr; $variant:ident $( $rest:ident )*) => { ... };
    (@enum $name:ident $gens:tt [$out:ident $head:ident $catalog:ident $ty:ident $parts:ident] [$at:expr]
        [$($shape:tt)*] [$($write:tt)*] [$($open:tt)*] [$($value:tt)*] [$($of:tt)*]
        $variant:ident $( = $number:literal )?; $($rest:tt)*
    ) => { ... };
    (@enum $name:ident $gens:tt $context:tt $at:tt $shape:tt $write:tt $open:tt $value:tt $of:tt
        $variant:ident ( $($part:ty),+ ) $( = $number:literal )?; $($rest:tt)*
    ) => { ... };
    (@positional $state:tt $variant:tt [$($named:tt)*] [$part:ty $(, $more:ty)*]
        $($rest:tt)*
    ) => { ... };
    (@positional
        [$name:ident $gens:tt [$out:ident $head:ident $catalog:ident $ty:ident $parts:ident] [$at:expr]
            [$($shape:tt)*] [$($write:tt)*] [$($open:tt)*] [$($value:tt)*] [$($of:tt)*]]
        [$variant:ident [$($number:literal)?]] [$( ($each:ident : $each_ty:ty) )+] []
        $($rest:tt)*
    ) => { ... };
    (@enum $name:ident $gens:tt [$out:ident $head:ident $catalog:ident $ty:ident $parts:ident] [$at:expr]
        [$($shape:tt)*] [$($write:tt)*] [$($open:tt)*] [$($value:tt)*] [$($of:tt)*]
        $variant:ident { $( $member:ident : $member_ty:ty ),* } $( = $number:literal )?;
        $($rest:tt)*
    ) => { ... };
    (@enum $name:ident [$($gen:ident),*] [$out:ident $head:ident $catalog:ident $ty:ident $parts:ident] [$at:expr]
        [$($shape:tt)*] [$($write:tt)*] [$($open:tt)*] [$($value:tt)*] [$($of:tt)*]
    ) => { ... };
}
Expand description

Declares a record or an enum once: the type itself, its Wire codec and its Shape. Doc comments and attributes pass through.

use flow_abi::wire::{Keyed, Wire};

flow_abi::wire! {
    /// What a request asks for.
    #[derive(Clone, Debug, PartialEq)]
    pub enum Request {
        /// What the server is doing.
        Status,
        /// Move, by three flags.
        Move(bool, String, bool),
        /// Stop, and why.
        Stop { reason: String } = 5,
    }
    /// Which request one is.
    pub enum RequestKind;
}

let stop = Request::Stop { reason: "done".to_owned() };
assert_eq!(stop.bytes(), [5, 4, b'd', b'o', b'n', b'e']);
assert_eq!(Request::decode(&stop.bytes()), Ok(stop.clone()));
assert_eq!(stop.payload_bytes(), [4, b'd', b'o', b'n', b'e']);
assert_eq!(Request::decode_payload(5, &stop.payload_bytes()), Ok(stop));
let moved = Request::Move(true, "a".to_owned(), false);
assert_eq!(moved.bytes(), [1, 1, 1, b'a', 0]);
assert_eq!(Request::decode(&moved.bytes()), Ok(moved));
assert_eq!(RequestKind::of(5), Some(RequestKind::Stop));
assert_eq!(RequestKind::Status.number(), 0);

A constructor’s number is its position unless given after =. An enum may take type parameters, each a part’s type, and may name after it a fieldless companion enum of its constructors’ kinds, which for an enum with type parameters needs each to have a default; an enum whose constructors carry nothing names itself, enum Self;.

A declaration that could not read back what it writes does not compile: a Rest before the last part of a record, a named payload or a positional one,

ⓘ
flow_abi::wire! {
    pub enum Message {
        Case(flow_abi::wire::Rest, String),
    }
}

and two constructors of one number.

ⓘ
flow_abi::wire! {
    pub enum Message {
        A = 5,
        B = 5,
    }
}