mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-07 15:36:08 +00:00
api-macro: add more standard Maybe methods
Note that any methods added there should be oriented around `Option`. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
6b5bf36989
commit
357b3016d5
@ -584,6 +584,12 @@ pub enum Maybe<T> {
|
|||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Default for Maybe<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Maybe::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> Maybe<T> {
|
impl<T> Maybe<T> {
|
||||||
pub fn as_ref(&self) -> Maybe<&T> {
|
pub fn as_ref(&self) -> Maybe<&T> {
|
||||||
match self {
|
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>
|
pub fn ok_or_else<E, F>(self, other: F) -> Result<T, E>
|
||||||
where
|
where
|
||||||
F: FnOnce() -> E,
|
F: FnOnce() -> E,
|
||||||
@ -613,6 +626,14 @@ impl<T> Maybe<T> {
|
|||||||
pub fn is_none(&self) -> bool {
|
pub fn is_none(&self) -> bool {
|
||||||
matches!(self, Maybe::None)
|
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> {
|
impl<T> Into<Option<T>> for Maybe<T> {
|
||||||
|
Loading…
Reference in New Issue
Block a user