1
0
Fork
You've already forked as_variant
0
A simple Rust macro to convert enums to Options
  • Rust 100%
2026年05月13日 12:15:38 +02:00
src Prioritize pattern matching over path matching 2025年03月26日 16:34:17 +01:00
.gitignore Initial commit 2022年09月13日 17:35:45 +02:00
.rustfmt.toml Add support for expression-less syntax 2023年08月09日 13:28:37 +02:00
Cargo.toml Update repository URL 2026年05月13日 12:15:38 +02:00
LICENSE Initial commit 2022年09月13日 17:35:45 +02:00
README.md Add README.md 2022年09月13日 17:39:48 +02:00

as_variant

docs.rs

A simple Rust macro to convert enums with newtype variants to Options.

Basic example:

usestd::ops::Deref;useas_variant::as_variant;enum Value{Integer(i64),String(String),Array(Vec<Value>),}implValue{pubfn as_integer(&self)-> Option<i64>{as_variant!(self,Self::Integer).copied()}pubfn as_str(&self)-> Option<&str>{as_variant!(self,Self::String).map(Deref::deref)}pubfn as_array(&self)-> Option<&[Value]>{as_variant!(self,Self::Array).map(Deref::deref)}pubfn into_string(self)-> Option<String>{as_variant!(self,Self::String)}pubfn into_array(self)-> Option<Vec<Value>>{as_variant!(self,Self::Array)}}