Tester

Trait Tester 

Source
pub trait Tester {
    // Required method
    fn test(&self) -> bool;

    // Provided methods
    fn into_box(self) -> BoxTester
       where Self: Sized + 'static { ... }
    fn into_rc(self) -> RcTester
       where Self: Sized + 'static { ... }
    fn into_arc(self) -> ArcTester
       where Self: Sized + Send + Sync + 'static { ... }
    fn into_fn(self) -> impl Fn() -> bool
       where Self: Sized + 'static { ... }
    fn to_box(&self) -> BoxTester
       where Self: Clone + 'static { ... }
    fn to_rc(&self) -> RcTester
       where Self: Clone + 'static { ... }
    fn to_arc(&self) -> ArcTester
       where Self: Clone + Send + Sync + 'static { ... }
    fn to_fn(&self) -> impl Fn() -> bool
       where Self: Clone + 'static { ... }
}
Expand description

Tests whether a state or condition holds

Tester is a functional abstraction for testing states or conditions. It accepts no parameters and returns a boolean value indicating the test result of some state or condition.

§Core Characteristics

  • No input parameters: Captures context through closures
  • Returns boolean: Indicates test results
  • Uses &self: Does not modify its own state, only reads external state
  • Repeatable calls: The same Tester can call test() multiple times

§Use Cases

  • State checking: Check system or service status
  • Condition waiting: Repeatedly check until conditions are met
  • Health monitoring: Check system health status
  • Precondition validation: Verify conditions before operations

§Design Philosophy

Tester’s responsibility is “test judgment”, not “state management”. State management is the caller’s responsibility. Tester only reads state and returns judgment results.

§Examples

use prism3_function::{BoxTester, Tester};
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};

// State managed externally
let ready = Arc::new(AtomicBool::new(false));
let ready_clone = Arc::clone(&ready);

// Tester only responsible for reading state
let tester = BoxTester::new(move || {
    ready_clone.load(Ordering::Acquire)
});

// Can be called multiple times
assert!(!tester.test());
ready.store(true, Ordering::Release);
assert!(tester.test());

§Author

Hu Haixing

Required Methods§

Source

fn test(&self) -> bool

Executes the test and returns the test result

This method can be called multiple times without modifying the Tester’s own state.

§Return Value

Returns true if the condition holds, otherwise returns false

§Examples
use prism3_function::{BoxTester, Tester};

let tester = BoxTester::new(|| true);
assert!(tester.test());

Provided Methods§

Source

fn into_box(self) -> BoxTester
where Self: Sized + 'static,

Converts this tester to BoxTester

§Return Value

A BoxTester that wraps this tester

§Examples
use prism3_function::{Tester, BoxTester};

let closure = || true;
let boxed: BoxTester = closure.into_box();
Source

fn into_rc(self) -> RcTester
where Self: Sized + 'static,

Converts this tester to RcTester

§Return Value

A RcTester that wraps this tester

§Examples
use prism3_function::{Tester, RcTester};

let closure = || true;
let rc: RcTester = closure.into_rc();
Source

fn into_arc(self) -> ArcTester
where Self: Sized + Send + Sync + 'static,

Converts this tester to ArcTester

§Return Value

An ArcTester that wraps this tester

§Examples
use prism3_function::{Tester, ArcTester};

let closure = || true;
let arc: ArcTester = closure.into_arc();
Source

fn into_fn(self) -> impl Fn() -> bool
where Self: Sized + 'static,

Converts this tester to a boxed function pointer

§Return Value

A Box<dyn Fn() -> bool> that wraps this tester

§Examples
use prism3_function::Tester;

let closure = || true;
let func = closure.into_fn();
assert!(func());
Source

fn to_box(&self) -> BoxTester
where Self: Clone + 'static,

Clones and converts this tester to BoxTester

§Return Value

A BoxTester that wraps a clone of this tester

§Examples
use prism3_function::{Tester, BoxTester, ArcTester};

let arc = ArcTester::new(|| true);
let boxed: BoxTester = arc.to_box();
// arc is still available
Source

fn to_rc(&self) -> RcTester
where Self: Clone + 'static,

Clones and converts this tester to RcTester

§Return Value

A RcTester that wraps a clone of this tester

§Examples
use prism3_function::{Tester, RcTester, ArcTester};

let arc = ArcTester::new(|| true);
let rc: RcTester = arc.to_rc();
// arc is still available
Source

fn to_arc(&self) -> ArcTester
where Self: Clone + Send + Sync + 'static,

Clones and converts this tester to ArcTester

§Return Value

An ArcTester that wraps a clone of this tester

§Examples
use prism3_function::{Tester, ArcTester, RcTester};

let rc = RcTester::new(|| true);
// Note: This will panic for RcTester as it's not Send + Sync
// let arc: ArcTester = rc.to_arc();
Source

fn to_fn(&self) -> impl Fn() -> bool
where Self: Clone + 'static,

Clones and converts this tester to a boxed function pointer

§Return Value

A Box<dyn Fn() -> bool> that wraps a clone of this tester

§Examples
use prism3_function::{Tester, ArcTester};

let arc = ArcTester::new(|| true);
let func = arc.to_fn();
// arc is still available
assert!(func());

Implementors§

Source§

impl Tester for ArcTester

Source§

impl Tester for BoxTester

Source§

impl Tester for RcTester

Source§

impl<F> Tester for F
where F: Fn() -> bool,