Crates.io | as_variant |
lib.rs | as_variant |
version | 1.2.0 |
source | src |
created_at | 2022-09-13 15:35:54.769714 |
updated_at | 2023-08-09 11:28:45.096988 |
description | A simple macro to convert enums with newtype variants to `Option`s |
homepage | |
repository | https://github.com/jplatte/as_variant |
max_upload_size | |
id | 664689 |
size | 23,785 |
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)
}
}