function

Function function 

Source
pub fn function<F, T>(function: F) -> FnPredicate<F, T>
where F: Fn(&T) -> bool, T: ?Sized,
Expand description

Creates a new predicate that wraps over the given function. The returned type implements Predicate and therefore has all combinators available to it.

ยงExamples

use predicates::prelude::*;

struct Example {
    string: String,
    number: i32,
}

let string_check = predicate::function(|x: &Example| x.string == "hello");
let number_check = predicate::function(|x: &Example| x.number == 42);
let predicate_fn = string_check.and(number_check);
let good_example = Example { string: "hello".into(), number: 42 };
assert_eq!(true, predicate_fn.eval(&good_example));
let bad_example = Example { string: "goodbye".into(), number: 0 };
assert_eq!(false, predicate_fn.eval(&bad_example));
Examples found in repository?
examples/generic_method.rs (line 63)
38fn main()
39{
40    let mut mock_foo = MockFoo::new();
41
42    unsafe {
43        mock_foo.expect_bar::<u16>().returning(|_me, num| {
44            println!("bar was called with {num}");
45
46            "Hello".to_string()
47        })
48    }
49    .times(3);
50
51    unsafe {
52        mock_foo.expect_bar::<&str>().returning(|_me, num| {
53            println!("bar was called with {num}");
54
55            128u8
56        });
57    }
58
59    unsafe {
60        mock_foo
61            .expect_abc::<&str, f64>()
62            .with(
63                function(|thing_a: &&str| thing_a.starts_with("Good morning")),
64                is_close(7.13081),
65            )
66            .returning(|_me, _thing_a, _thing_b| {
67                println!("abc was called");
68            })
69    }
70    .times(1);
71
72    assert_eq!(mock_foo.bar::<u16>(123), "Hello".to_string());
73    assert_eq!(mock_foo.bar::<u16>(123), "Hello".to_string());
74    assert_eq!(mock_foo.bar::<u16>(123), "Hello".to_string());
75
76    // Would panic
77    // mock_foo.bar::<String>(123);
78
79    assert_eq!(mock_foo.bar::<&str>(456), 128);
80
81    mock_foo.abc(
82        concat!(
83            "Good morning, and in case I don't see ya, good afternoon,",
84            " good evening, and good night!"
85        ),
86        7.13081f64,
87    );
88}