Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait combinators #678

Merged
merged 4 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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