From 7cbc6d0962b73b447640aa5ae8514f7376a348a1 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 19 Jul 2019 14:08:55 +0200 Subject: [PATCH] api: add a meta module to help with macro impls Signed-off-by: Wolfgang Bumiller --- proxmox-api/src/api_type.rs | 2 ++ proxmox-api/src/lib.rs | 2 ++ proxmox-api/src/meta.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 proxmox-api/src/meta.rs diff --git a/proxmox-api/src/api_type.rs b/proxmox-api/src/api_type.rs index f3b95c11..17676549 100644 --- a/proxmox-api/src/api_type.rs +++ b/proxmox-api/src/api_type.rs @@ -186,6 +186,7 @@ pub trait ApiType { Self::type_info() } + #[inline] fn should_skip_serialization(&self) -> bool { false } @@ -239,6 +240,7 @@ impl ApiType for Option { */ } + #[inline] fn should_skip_serialization(&self) -> bool { self.is_none() } diff --git a/proxmox-api/src/lib.rs b/proxmox-api/src/lib.rs index 21bcbba0..ee75872f 100644 --- a/proxmox-api/src/lib.rs +++ b/proxmox-api/src/lib.rs @@ -23,6 +23,8 @@ pub use router::*; pub mod cli; +pub mod meta; + /// Return type of an API method. pub type ApiOutput = Result, Error>; diff --git a/proxmox-api/src/meta.rs b/proxmox-api/src/meta.rs new file mode 100644 index 00000000..8021028d --- /dev/null +++ b/proxmox-api/src/meta.rs @@ -0,0 +1,28 @@ +//! Type related meta information, mostly used by the macro code. + +use crate::ApiType; + +/// Helper trait for entries with a `default` value in their api type definition. +pub trait OrDefault { + type Output; + + fn or_default(&self, def: &'static Self::Output) -> &Self::Output; + fn set(&mut self, value: Self::Output); +} + +impl OrDefault for Option +where + T: ApiType, +{ + type Output = T; + + #[inline] + fn or_default(&self, def: &'static Self::Output) -> &Self::Output { + self.as_ref().unwrap_or(def) + } + + #[inline] + fn set(&mut self, value: Self::Output) { + *self = Some(value); + } +}