From abe6949c8263915c579687080b29f505da9f4e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Tue, 26 Oct 2021 14:07:31 +0200 Subject: [PATCH 1/3] Wait condition and/or/not combinators MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Teo Klestrup Röijezon --- kube-runtime/src/wait.rs | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/kube-runtime/src/wait.rs b/kube-runtime/src/wait.rs index 20fca6b93..bff3fdabc 100644 --- a/kube-runtime/src/wait.rs +++ b/kube-runtime/src/wait.rs @@ -87,6 +87,27 @@ where /// ``` pub trait Condition { fn matches_object(&self, obj: Option<&K>) -> bool; + + fn not(self) -> conditions::Not + where + Self: Sized, + { + conditions::Not(self) + } + + fn and>(self, other: Other) -> conditions::And + where + Self: Sized, + { + conditions::And(self, other) + } + + fn or>(self, other: Other) -> conditions::Or + where + Self: Sized, + { + conditions::Or(self, other) + } } impl) -> bool> Condition for F { @@ -134,6 +155,35 @@ pub mod conditions { false } } + + pub struct Not(pub(super) A); + impl, K> Condition for Not { + fn matches_object(&self, obj: Option<&K>) -> bool { + !self.0.matches_object(obj) + } + } + + pub struct And(pub(super) A, pub(super) B); + impl Condition for And + where + A: Condition, + B: Condition, + { + fn matches_object(&self, obj: Option<&K>) -> bool { + self.0.matches_object(obj) && self.1.matches_object(obj) + } + } + + pub struct Or(pub(super) A, pub(super) B); + impl Condition for Or + where + A: Condition, + B: Condition, + { + fn matches_object(&self, obj: Option<&K>) -> bool { + self.0.matches_object(obj) || self.1.matches_object(obj) + } + } } /// Utilities for deleting objects From 2c8b0ce85299d3ff449508327abab650163c918a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Tue, 26 Oct 2021 14:26:34 +0200 Subject: [PATCH 2/3] Combinator docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Teo Klestrup Röijezon --- kube-runtime/src/wait.rs | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/kube-runtime/src/wait.rs b/kube-runtime/src/wait.rs index bff3fdabc..24826b99e 100644 --- a/kube-runtime/src/wait.rs +++ b/kube-runtime/src/wait.rs @@ -88,6 +88,16 @@ where pub trait Condition { fn matches_object(&self, obj: Option<&K>) -> bool; + /// Returns a `Condition` that holds if `self` does not + /// + /// # Usage + /// + /// ```rust + /// # use kube_runtime::wait::Condition; + /// let condition: fn(Option<&()>) -> bool = |_| true; + /// assert!(condition.matches_object(None)); + /// assert!(!condition.not().matches_object(None)); + /// ``` fn not(self) -> conditions::Not where Self: Sized, @@ -95,6 +105,19 @@ pub trait Condition { conditions::Not(self) } + /// Returns a `Condition` that holds if `self` and `other` both do + /// + /// # Usage + /// + /// ```rust + /// # use kube_runtime::wait::Condition; + /// let cond_false: fn(Option<&()>) -> bool = |_| false; + /// let cond_true: fn(Option<&()>) -> bool = |_| true; + /// assert!(!cond_false.and(cond_false).matches_object(None)); + /// assert!(!cond_false.and(cond_true).matches_object(None)); + /// assert!(!cond_true.and(cond_false).matches_object(None)); + /// assert!(cond_true.and(cond_true).matches_object(None)); + /// ``` fn and>(self, other: Other) -> conditions::And where Self: Sized, @@ -102,6 +125,19 @@ pub trait Condition { conditions::And(self, other) } + /// Returns a `Condition` that holds if either `self` or `other` does + /// + /// # Usage + /// + /// ```rust + /// # use kube_runtime::wait::Condition; + /// let cond_false: fn(Option<&()>) -> bool = |_| false; + /// let cond_true: fn(Option<&()>) -> bool = |_| true; + /// assert!(!cond_false.or(cond_false).matches_object(None)); + /// assert!(cond_false.or(cond_true).matches_object(None)); + /// assert!(cond_true.or(cond_false).matches_object(None)); + /// assert!(cond_true.or(cond_true).matches_object(None)); + /// ``` fn or>(self, other: Other) -> conditions::Or where Self: Sized, @@ -156,6 +192,8 @@ pub mod conditions { } } + /// See [`Condition::not`] + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Not(pub(super) A); impl, K> Condition for Not { fn matches_object(&self, obj: Option<&K>) -> bool { @@ -163,6 +201,8 @@ pub mod conditions { } } + /// See [`Condition::and`] + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct And(pub(super) A, pub(super) B); impl Condition for And where @@ -174,6 +214,8 @@ pub mod conditions { } } + /// See [`Condition::or`] + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Or(pub(super) A, pub(super) B); impl Condition for Or where From 187f1d0c2559de73ef34edb5b5a0865827512b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Tue, 26 Oct 2021 14:31:53 +0200 Subject: [PATCH 3/3] Changelog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Teo Klestrup Röijezon --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 369b33a57..15aa894d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ UNRELEASED =================== * see https://github.com/kube-rs/kube-rs/compare/0.63.0...master + * `kube::runtime::wait::Condition` added boolean combinators (`not`/`and`/`or`) - #678 0.63.0 / 2021-10-26 ===================