Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(xcvm): define helper proto::define_conversion macro #4112

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions code/xcvm/lib/core/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ impl core::fmt::Display for DecodeError {
}
}
}

/// Defines conversions between protocol buffer message `$pb` and Rust type
/// `$ty`.
///
/// Specifically, implements `TryFrom<$pb> for $ty` and `From<$ty> for $pb`.
/// That is, conversion from protocol message is fallible while conversion to
/// protocol message isn’t. The error for the `TryFrom` conversion is `()`.
macro_rules! define_conversion {
(($pb_name:ident: $pb:ty) -> { $($from_pb:tt)* }
($ty_name:ident: $ty:ty) -> { $($from_ty:tt)* }) => {
impl TryFrom<$pb> for $ty {
type Error = ();
fn try_from($pb_name: $pb) -> Result<Self, Self::Error> {
$($from_pb)*
}
}

impl From<$ty> for $pb {
fn from($ty_name: $ty) -> $pb {
$($from_ty)*
}
}
}
}

use define_conversion;
Loading
Loading