4 stable releases
1.2.0 | Aug 9, 2023 |
---|---|
1.1.0 | Sep 14, 2022 |
1.0.1 | Sep 13, 2022 |
#57 in No standard library
6,897 downloads per month
Used in 36 crates
(10 directly)
8KB
as_variant
A simple Rust macro to convert enums with newtype variants to Option
s.
Basic example:
use std::ops::Deref;
use as_variant::as_variant;
enum Value {
Integer(i64),
String(String),
Array(Vec<Value>),
}
impl Value {
pub fn as_integer(&self) -> Option<i64> {
as_variant!(self, Self::Integer).copied()
}
pub fn as_str(&self) -> Option<&str> {
as_variant!(self, Self::String).map(Deref::deref)
}
pub fn as_array(&self) -> Option<&[Value]> {
as_variant!(self, Self::Array).map(Deref::deref)
}
pub fn into_string(self) -> Option<String> {
as_variant!(self, Self::String)
}
pub fn into_array(self) -> Option<Vec<Value>> {
as_variant!(self, Self::Array)
}
}
lib.rs
:
Provides a simple macro to convert enums with newtype variants to Option
s.