Macro d3d12::weak_com_inheritance_chain

source ·
macro_rules! weak_com_inheritance_chain {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident {
            $first_variant:ident($first_type:ty), $first_from_name:ident, $first_as_name:ident, $first_unwrap_name:ident $(;)?
            $($variant:ident($type:ty), $from_name:ident, $as_name:ident, $unwrap_name:ident);* $(;)?
        }
    ) => { ... };
    (
        @recursion_logic,
        $vis:vis,
        $(,)? $($prev_variant:ident),* $(,)?;
        $this_variant:ident($this_type:ty), $this_from_name:ident, $this_as_name:ident, $this_unwrap_name:ident $(;)?
        $($next_variant:ident($next_type:ty), $next_from_name:ident, $next_as_name:ident, $next_unwrap_name:ident);*
    ) => { ... };
    (
        @recursion_logic,
        $vis:vis,
        $($prev_variant:ident),*;
    ) => { ... };
    (
        @render_members,
        $vis:vis,
        $from_name:ident, $as_name:ident, $unwrap_name:ident;
        $($prev_variant:ident),*;
        $variant:ident($type:ty);
        $($next_variant:ident),*;
    ) => { ... };
}
Expand description

Macro that allows generation of an easy to use enum for dealing with many different possible versions of a COM object.

Give the variants so that parents come before children. This often manifests as going up in order (1 -> 2 -> 3). This is vital for safety.

Three function names need to be attached to each variant. The examples are given for the MyComObject1 variant below:

  • the from function (ComPtr<actual::ComObject1> -> Self)
  • the as function (&self -> Option<ComPtr<actual::ComObject1>>)
  • the unwrap function (&self -> ComPtr<actual::ComObject1> panicking on failure to cast)
weak_com_inheritance_chain! {
    pub enum MyComObject {
        MyComObject(actual::ComObject), from_my_com_object, as_my_com_object, my_com_object; // First variant doesn't use "unwrap" as it can never fail
        MyComObject1(actual::ComObject1), from_my_com_object1, as_my_com_object1, unwrap_my_com_object1;
        MyComObject2(actual::ComObject2), from_my_com_object2, as_my_com_object2, unwrap_my_com_object2;
    }
}