Skip to main content

one_of

Macro one_of 

Source
macro_rules! one_of {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident as $shown:literal {
            $( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
        }
    ) => { ... };
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident {
            $( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
        }
    ) => { ... };
    (@impl $shown:expr;
        $(#[$meta:meta])*
        $vis:vis enum $name:ident {
            $( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+
        }
    ) => { ... };
}
Expand description

Defines an enum for use as a OneOf union argument.

Each variant holds one FromValue type; at the call site OneOf<Name> tries the variants top-to-bottom and builds the first whose type matches the runtime value, so put the most specific types first.

ⓘ
one_of! {
    /// A CLI key: a positional index or a flag name.
    pub enum ArgKey {
        Index(i64),
        Name(XurStr),
    }
}
// fn arg(&self, key: OneOf<ArgKey>) -> ... { match key.0 { ArgKey::Index(i) => .., ArgKey::Name(n) => .. } }

Docs show a union with more than a few options by its name; give it a friendlier one with as: pub enum TypeNameArg as "TypeName" { .. }.