diff --git a/src/macros.rs b/src/macros.rs index 1d50f6571be4b313e50a9805c37d2cf393500373..e27cb8c2d16d10bef8c5165b96b8d6d091bde3fe 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -21,8 +21,9 @@ macro_rules! dbg_code { } // Given a function name, return type and variant, will generate the all-so -// common `union_value.as_variant()` method. -macro_rules! union_cast_method_impl { +// common `union_value.as_variant()` method. The return value is the reference +// to the embedded union type. +macro_rules! union_cast_to_ref_method_impl { ($func_name:ident, $ret_type:ty, $variant:path) => { fn $func_name(&self) -> &$ret_type { match self { @@ -31,4 +32,18 @@ macro_rules! union_cast_method_impl { } } } +} + +// Another union cast, but now returning a copy of the value +macro_rules! union_cast_to_value_method_impl { + ($func_name:ident, $ret_type:ty, $variant:path) => { + impl Value { + pub(crate) fn $func_name(&self) -> $ret_type { + match self { + $variant(v) => *v, + _ => unreachable!(), + } + } + } + } } \ No newline at end of file