pub trait Runnable {
fn run(&self);
}
pub trait Callable<T> {
fn call(&self) -> T;
}
#[cfg(test)]
mod tests {
use super::Runnable;
use super::Callable;
struct MyRunnable;
impl Runnable for MyRunnable {
fn run(&self) {}
}
struct MyCallable;
impl Callable<i32> for MyCallable {
fn call(&self) -> i32 {
1337
}
}
struct StructWithClosure<'life> {
closure_field: &'life Fn(),
}
struct StructTwoWithClosure<'life> {
closure_field: &'life Fn() -> i32,
}
#[test]
fn runnable_can_run() {
MyRunnable.run();
}
#[test]
fn runnable_passed_as_closure() {
let my_struct = StructWithClosure { closure_field: &|| MyRunnable.run() };
assert_eq!((my_struct.closure_field)(), ());
}
#[test]
fn callable_can_be_called() {
assert_eq!(MyCallable.call(), 1337);
}
#[test]
fn callable_passed_as_closure() {
let struct_two = StructTwoWithClosure { closure_field: &|| -> i32 { MyCallable.call() } };
assert_eq!((struct_two.closure_field)(), 1337);
}
}