[pbs-devel] [PATCH proxmox 08/18] api-macro: add more standard Maybe methods

Wolfgang Bumiller w.bumiller at proxmox.com
Fri Dec 18 12:25:56 CET 2020


Note that any methods added there should be oriented around
`Option`.

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 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 e74e04b..87e6414 100644
--- a/proxmox-api-macro/src/util.rs
+++ b/proxmox-api-macro/src/util.rs
@@ -584,6 +584,12 @@ pub enum Maybe<T> {
     None,
 }
 
+impl<T> Default for Maybe<T> {
+    fn default() -> Self {
+        Maybe::None
+    }
+}
+
 impl<T> Maybe<T> {
     pub fn as_ref(&self) -> Maybe<&T> {
         match self {
@@ -600,6 +606,13 @@ impl<T> Maybe<T> {
         }
     }
 
+    pub fn ok(self) -> Option<T> {
+        match self {
+            Maybe::Explicit(v) | Maybe::Derived(v) => Some(v),
+            Maybe::None => None,
+        }
+    }
+
     pub fn ok_or_else<E, F>(self, other: F) -> Result<T, E>
     where
         F: FnOnce() -> E,
@@ -613,6 +626,14 @@ impl<T> Maybe<T> {
     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<T> Into<Option<T>> for Maybe<T> {
-- 
2.20.1






More information about the pbs-devel mailing list