From 357b3016d526333a7a59338f6e185baca4e35c9a Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 18 Dec 2020 12:25:56 +0100 Subject: [PATCH] api-macro: add more standard Maybe methods Note that any methods added there should be oriented around `Option`. Signed-off-by: Wolfgang Bumiller --- proxmox-api-macro/src/util.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/proxmox-api-macro/src/util.rs b/proxmox-api-macro/src/util.rs index e74e04b4..87e6414a 100644 --- a/proxmox-api-macro/src/util.rs +++ b/proxmox-api-macro/src/util.rs @@ -584,6 +584,12 @@ pub enum Maybe { None, } +impl Default for Maybe { + fn default() -> Self { + Maybe::None + } +} + impl Maybe { pub fn as_ref(&self) -> Maybe<&T> { match self { @@ -600,6 +606,13 @@ impl Maybe { } } + pub fn ok(self) -> Option { + match self { + Maybe::Explicit(v) | Maybe::Derived(v) => Some(v), + Maybe::None => None, + } + } + pub fn ok_or_else(self, other: F) -> Result where F: FnOnce() -> E, @@ -613,6 +626,14 @@ impl Maybe { pub fn is_none(&self) -> bool { matches!(self, Maybe::None) } + + pub fn is_explicit(&self) -> bool { + matches!(self, Maybe::Explicit(_)) + } + + pub fn take(&mut self) -> Self { + std::mem::take(self) + } } impl Into> for Maybe {