derive_more
derive_more copied to clipboard
Complete enum derives with getters which return Option<&T>
I'm submitting this as an issue to gather feedback; I'm happy to do the work on an implementation if this is a desired feature.
Currently I can use a combination of IsVariant and Unwrap to build an Option<T> for a specific variant of an enum.
It would be nice to be able to derive a getter which combines this functionality. For instance:
#[derive(GetVariant)]
enum MyEnum<A, B> {
VarA(A),
VarB(B),
}
Becomes:
enum MyEnum<A, B> {
VarA(A),
VarB(B),
}
impl <A, B> MyEnum<A, B> {
fn var_a(&self) -> Option<&A> {
match self {
Self::VarA(var_a) => Some(var_a),
_ => None,
}
}
fn var_b(&self) -> Option<&B> {
match self {
Self::VarB(var_b) => Some(var_b),
_ => None,
}
}
}
This somewhat mimics the implementation of .ok() and .err() on std's Result<T, E>, but those return Option<T> rather than Option<&T>. In that instance, this distinction doesn't matter as first convert to a Result<&T, &E> with .as_ref().