Skip to content

Commit

Permalink
Merge pull request #678 from teozkr/feature/wait-combinators
Browse files Browse the repository at this point in the history
Wait combinators
  • Loading branch information
nightkr authored Oct 26, 2021
2 parents 73c9148 + b8691d4 commit 4eb11f7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
===================
Expand Down
92 changes: 92 additions & 0 deletions kube-runtime/src/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,63 @@ where
/// ```
pub trait Condition<K> {
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<Self>
where
Self: Sized,
{
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<Other: Condition<K>>(self, other: Other) -> conditions::And<Self, Other>
where
Self: Sized,
{
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<Other: Condition<K>>(self, other: Other) -> conditions::Or<Self, Other>
where
Self: Sized,
{
conditions::Or(self, other)
}
}

impl<K, F: Fn(Option<&K>) -> bool> Condition<K> for F {
Expand Down Expand Up @@ -134,6 +191,41 @@ pub mod conditions {
false
}
}

/// See [`Condition::not`]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Not<A>(pub(super) A);
impl<A: Condition<K>, K> Condition<K> for Not<A> {
fn matches_object(&self, obj: Option<&K>) -> bool {
!self.0.matches_object(obj)
}
}

/// See [`Condition::and`]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct And<A, B>(pub(super) A, pub(super) B);
impl<A, B, K> Condition<K> for And<A, B>
where
A: Condition<K>,
B: Condition<K>,
{
fn matches_object(&self, obj: Option<&K>) -> bool {
self.0.matches_object(obj) && self.1.matches_object(obj)
}
}

/// See [`Condition::or`]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Or<A, B>(pub(super) A, pub(super) B);
impl<A, B, K> Condition<K> for Or<A, B>
where
A: Condition<K>,
B: Condition<K>,
{
fn matches_object(&self, obj: Option<&K>) -> bool {
self.0.matches_object(obj) || self.1.matches_object(obj)
}
}
}

/// Utilities for deleting objects
Expand Down

0 comments on commit 4eb11f7

Please sign in to comment.