Skip to main content

RcPredicate

Struct RcPredicate 

Source
pub struct RcPredicate<T> { /* private fields */ }
Expand description

An Rc-based predicate with single-threaded shared ownership.

This type is suitable for scenarios where the predicate needs to be reused in a single-threaded context. Composition methods borrow &self, allowing the original predicate to remain usable after composition.

§Examples

use qubit_function::{Predicate, RcPredicate};

let pred = RcPredicate::new(|x: &i32| *x > 0);
assert!(pred.test(&5));

// Original predicate remains usable after composition
let combined = pred.and(RcPredicate::new(|x| x % 2 == 0));
assert!(pred.test(&5));  // Still works

Implementations§

Source§

impl<T> RcPredicate<T>

Source

pub fn new<F>(f: F) -> Self
where F: Fn(&T) -> bool + 'static,

Creates a new predicate.

Wraps the provided closure in the appropriate smart pointer type for this predicate implementation.

Examples found in repository?
examples/predicates/predicate_fn_mut_demo.rs (line 47)
43fn demo_with_vec_retain() {
44    println!("2. Using Vec::retain");
45
46    // RcPredicate example
47    let pred = RcPredicate::new(|x: &i32| *x % 2 == 0);
48    let mut numbers = vec![1, 2, 3, 4, 5, 6];
49    println!("   Original data: {:?}", numbers);
50    numbers.retain(pred.to_fn());
51    println!("   Retained even numbers: {:?}", numbers);
52    assert_eq!(numbers, vec![2, 4, 6]);
53
54    // Original predicate is still available
55    assert!(pred.test(&10));
56    println!("   ✓ RcPredicate::to_fn() can be used in retain");
57    println!("   ✓ Original predicate is still available\n");
58}
59
60/// Demonstrates usage with generic functions that require FnMut
61fn demo_with_generic_function() {
62    println!("3. Using generic functions (requires FnMut)");
63
64    fn count_matching<F>(items: &[i32], mut predicate: F) -> usize
65    where
66        F: FnMut(&i32) -> bool,
67    {
68        items.iter().filter(|x| predicate(x)).count()
69    }
70
71    let pred = RcPredicate::new(|x: &i32| *x > 10);
72    let count1 = count_matching(&[5, 15, 8, 20], pred.to_fn());
73    println!("   First call: count = {}", count1);
74    assert_eq!(count1, 2);
75
76    // Original predicate can be reused
77    let count2 = count_matching(&[12, 3, 18], pred.to_fn());
78    println!("   Second call: count = {}", count2);
79    assert_eq!(count2, 2);
80
81    println!("   ✓ RcPredicate::to_fn() can be passed to generic functions requiring FnMut");
82    println!("   ✓ Original predicate can be converted and used multiple times\n");
83}
More examples
Hide additional examples
examples/predicates/predicate_set_name_demo.rs (line 66)
56fn demo_rc_predicate() {
57    println!("2. RcPredicate Naming Functionality");
58
59    // Using new_with_name
60    let pred1 = RcPredicate::new_with_name("greater_than_10", |x: &i32| *x > 10);
61    println!("   Using new_with_name:");
62    println!("     Name: {:?}", pred1.name());
63    println!("     Test 15: {}", pred1.test(&15));
64
65    // Using set_name
66    let mut pred2 = RcPredicate::new(|x: &i32| *x < 100);
67    println!("\n   Using set_name:");
68    println!("     Initial name: {:?}", pred2.name());
69    pred2.set_name("less_than_100");
70    println!("     Name after setting: {:?}", pred2.name());
71    println!("     Test 50: {}", pred2.test(&50));
72
73    // Name is preserved after cloning
74    let pred3 = pred2.clone();
75    println!("\n   Name preserved after cloning:");
76    println!("     Cloned name: {:?}", pred3.name());
77    println!("     Test 80: {}\n", pred3.test(&80));
78}
examples/predicates/predicate_demo.rs (line 111)
108fn rc_predicate_examples() {
109    println!("--- 3. RcPredicate Examples (Single-threaded Reuse) ---");
110
111    let is_positive = RcPredicate::new(|x: &i32| *x > 0);
112    let is_even = RcPredicate::new(|x: &i32| x % 2 == 0);
113
114    // Multiple compositions without consuming the original
115    let positive_and_even = is_positive.and(is_even.clone());
116    let positive_or_even = is_positive.or(is_even.clone());
117
118    println!("Original predicates still available:");
119    println!("  is_positive.test(&5) = {}", is_positive.test(&5));
120    println!("  is_even.test(&4) = {}", is_even.test(&4));
121
122    println!("Combined predicates:");
123    println!(
124        "  positive_and_even.test(&4) = {}",
125        positive_and_even.test(&4)
126    );
127    println!(
128        "  positive_or_even.test(&5) = {}",
129        positive_or_even.test(&5)
130    );
131
132    // Cloning
133    let cloned = is_positive.clone();
134    println!("Cloned predicate: {}", cloned.test(&10));
135}
136
137/// ArcPredicate examples - multi-threaded scenarios
138fn arc_predicate_examples() {
139    println!("--- 4. ArcPredicate Examples (Multi-threaded Scenarios) ---");
140
141    let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
142    let is_even = ArcPredicate::new(|x: &i32| x % 2 == 0);
143
144    // Create combined predicate
145    let combined = is_positive.and(is_even);
146
147    // Use in multiple threads
148    let handles: Vec<_> = (0..3)
149        .map(|i| {
150            let pred = combined.clone();
151            std::thread::spawn(move || {
152                let value = i * 2;
153                println!("  Thread {} testing {}: {}", i, value, pred.test(&value));
154            })
155        })
156        .collect();
157
158    for handle in handles {
159        handle.join().unwrap();
160    }
161
162    // Original predicates still usable
163    println!("Original predicates still available in main thread:");
164    println!("  is_positive.test(&5) = {}", is_positive.test(&5));
165}
166
167/// Logical composition examples
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
220
221/// Interior mutability examples
222fn interior_mutability_examples() {
223    println!("--- 6. Interior Mutability Examples ---");
224
225    // BoxPredicate with counter (RefCell)
226    println!("BoxPredicate with counter:");
227    let count = RefCell::new(0);
228    let pred = BoxPredicate::new(move |x: &i32| {
229        *count.borrow_mut() += 1;
230        *x > 0
231    });
232    println!("  Test 5: {}", pred.test(&5));
233    println!("  Test -3: {}", pred.test(&-3));
234    println!("  Test 10: {}", pred.test(&10));
235    // Note: count is moved into the closure, so we can't access it here
236
237    // RcPredicate with cache (RefCell + HashMap)
238    println!("\nRcPredicate with cache:");
239    let cache: RefCell<HashMap<i32, bool>> = RefCell::new(HashMap::new());
240    let expensive_pred = RcPredicate::new(move |x: &i32| {
241        let mut c = cache.borrow_mut();
242        *c.entry(*x).or_insert_with(|| {
243            println!("    Computing result for {} (expensive operation)", x);
244            *x > 0 && x % 2 == 0
245        })
246    });
247
248    println!("  First test 4:");
249    println!("    Result: {}", expensive_pred.test(&4));
250    println!("  Test 4 again (using cache):");
251    println!("    Result: {}", expensive_pred.test(&4));
252    println!("  Test 3:");
253    println!("    Result: {}", expensive_pred.test(&3));
254
255    // ArcPredicate with thread-safe counter (Mutex)
256    println!("\nArcPredicate with thread-safe counter:");
257    let counter = Arc::new(Mutex::new(0));
258    let pred = ArcPredicate::new({
259        let counter = Arc::clone(&counter);
260        move |x: &i32| {
261            let mut c = counter.lock().unwrap();
262            *c += 1;
263            *x > 0
264        }
265    });
266
267    let pred_clone = pred.clone();
268    let counter_clone = Arc::clone(&counter);
269
270    let handle = std::thread::spawn(move || {
271        pred_clone.test(&5);
272        pred_clone.test(&10);
273    });
274
275    pred.test(&3);
276    handle.join().unwrap();
277
278    println!("  Total call count: {}", counter_clone.lock().unwrap());
279}
280
281/// Practical use cases
282fn practical_use_cases() {
283    println!("--- 7. Practical Use Cases ---");
284
285    // Validation rules
286    println!("Scenario 1: Form Validation");
287    struct User {
288        name: String,
289        age: i32,
290        email: String,
291    }
292
293    let name_valid =
294        RcPredicate::new_with_name("name_not_empty", |user: &User| !user.name.is_empty());
295
296    let age_valid = RcPredicate::new_with_name("age_between_18_120", |user: &User| {
297        user.age >= 18 && user.age <= 120
298    });
299
300    let email_valid =
301        RcPredicate::new_with_name("email_contains_at", |user: &User| user.email.contains('@'));
302
303    let all_valid = name_valid.and(age_valid.clone()).and(email_valid.clone());
304
305    let user1 = User {
306        name: "Alice".to_string(),
307        age: 25,
308        email: "alice@example.com".to_string(),
309    };
310
311    let user2 = User {
312        name: "".to_string(),
313        age: 25,
314        email: "bob@example.com".to_string(),
315    };
316
317    println!("  user1 validation: {}", all_valid.test(&user1));
318    println!("  user2 validation: {}", all_valid.test(&user2));
319
320    // Filter pipeline
321    println!("\nScenario 2: Data Filtering Pipeline");
322    let numbers: Vec<i32> = (-10..=10).collect();
323
324    let positive = |x: &i32| *x > 0;
325    let even = |x: &i32| x % 2 == 0;
326    let less_than_eight = |x: &i32| *x < 8;
327
328    let filtered: Vec<i32> = numbers
329        .iter()
330        .filter(|x| positive.test(x))
331        .filter(|x| even.test(x))
332        .filter(|x| less_than_eight.test(x))
333        .copied()
334        .collect();
335
336    println!("  Filtered numbers: {:?}", filtered);
337
338    // Strategy pattern
339    println!("\nScenario 3: Strategy Pattern");
340    let mut strategies: HashMap<&str, RcPredicate<i32>> = HashMap::new();
341    strategies.insert("positive", RcPredicate::new(|x: &i32| *x > 0));
342    strategies.insert("negative", RcPredicate::new(|x: &i32| *x < 0));
343    strategies.insert("even", RcPredicate::new(|x: &i32| x % 2 == 0));
344
345    let test_value = 4;
346    for (name, pred) in strategies.iter() {
347        println!(
348            "  {} strategy test {}: {}",
349            name,
350            test_value,
351            pred.test(&test_value)
352        );
353    }
354}
Source

pub fn new_with_name<F>(name: &str, f: F) -> Self
where F: Fn(&T) -> bool + 'static,

Creates a new named predicate.

Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.

Examples found in repository?
examples/predicates/predicate_set_name_demo.rs (line 60)
56fn demo_rc_predicate() {
57    println!("2. RcPredicate Naming Functionality");
58
59    // Using new_with_name
60    let pred1 = RcPredicate::new_with_name("greater_than_10", |x: &i32| *x > 10);
61    println!("   Using new_with_name:");
62    println!("     Name: {:?}", pred1.name());
63    println!("     Test 15: {}", pred1.test(&15));
64
65    // Using set_name
66    let mut pred2 = RcPredicate::new(|x: &i32| *x < 100);
67    println!("\n   Using set_name:");
68    println!("     Initial name: {:?}", pred2.name());
69    pred2.set_name("less_than_100");
70    println!("     Name after setting: {:?}", pred2.name());
71    println!("     Test 50: {}", pred2.test(&50));
72
73    // Name is preserved after cloning
74    let pred3 = pred2.clone();
75    println!("\n   Name preserved after cloning:");
76    println!("     Cloned name: {:?}", pred3.name());
77    println!("     Test 80: {}\n", pred3.test(&80));
78}
More examples
Hide additional examples
examples/predicates/predicate_demo.rs (line 171)
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
220
221/// Interior mutability examples
222fn interior_mutability_examples() {
223    println!("--- 6. Interior Mutability Examples ---");
224
225    // BoxPredicate with counter (RefCell)
226    println!("BoxPredicate with counter:");
227    let count = RefCell::new(0);
228    let pred = BoxPredicate::new(move |x: &i32| {
229        *count.borrow_mut() += 1;
230        *x > 0
231    });
232    println!("  Test 5: {}", pred.test(&5));
233    println!("  Test -3: {}", pred.test(&-3));
234    println!("  Test 10: {}", pred.test(&10));
235    // Note: count is moved into the closure, so we can't access it here
236
237    // RcPredicate with cache (RefCell + HashMap)
238    println!("\nRcPredicate with cache:");
239    let cache: RefCell<HashMap<i32, bool>> = RefCell::new(HashMap::new());
240    let expensive_pred = RcPredicate::new(move |x: &i32| {
241        let mut c = cache.borrow_mut();
242        *c.entry(*x).or_insert_with(|| {
243            println!("    Computing result for {} (expensive operation)", x);
244            *x > 0 && x % 2 == 0
245        })
246    });
247
248    println!("  First test 4:");
249    println!("    Result: {}", expensive_pred.test(&4));
250    println!("  Test 4 again (using cache):");
251    println!("    Result: {}", expensive_pred.test(&4));
252    println!("  Test 3:");
253    println!("    Result: {}", expensive_pred.test(&3));
254
255    // ArcPredicate with thread-safe counter (Mutex)
256    println!("\nArcPredicate with thread-safe counter:");
257    let counter = Arc::new(Mutex::new(0));
258    let pred = ArcPredicate::new({
259        let counter = Arc::clone(&counter);
260        move |x: &i32| {
261            let mut c = counter.lock().unwrap();
262            *c += 1;
263            *x > 0
264        }
265    });
266
267    let pred_clone = pred.clone();
268    let counter_clone = Arc::clone(&counter);
269
270    let handle = std::thread::spawn(move || {
271        pred_clone.test(&5);
272        pred_clone.test(&10);
273    });
274
275    pred.test(&3);
276    handle.join().unwrap();
277
278    println!("  Total call count: {}", counter_clone.lock().unwrap());
279}
280
281/// Practical use cases
282fn practical_use_cases() {
283    println!("--- 7. Practical Use Cases ---");
284
285    // Validation rules
286    println!("Scenario 1: Form Validation");
287    struct User {
288        name: String,
289        age: i32,
290        email: String,
291    }
292
293    let name_valid =
294        RcPredicate::new_with_name("name_not_empty", |user: &User| !user.name.is_empty());
295
296    let age_valid = RcPredicate::new_with_name("age_between_18_120", |user: &User| {
297        user.age >= 18 && user.age <= 120
298    });
299
300    let email_valid =
301        RcPredicate::new_with_name("email_contains_at", |user: &User| user.email.contains('@'));
302
303    let all_valid = name_valid.and(age_valid.clone()).and(email_valid.clone());
304
305    let user1 = User {
306        name: "Alice".to_string(),
307        age: 25,
308        email: "alice@example.com".to_string(),
309    };
310
311    let user2 = User {
312        name: "".to_string(),
313        age: 25,
314        email: "bob@example.com".to_string(),
315    };
316
317    println!("  user1 validation: {}", all_valid.test(&user1));
318    println!("  user2 validation: {}", all_valid.test(&user2));
319
320    // Filter pipeline
321    println!("\nScenario 2: Data Filtering Pipeline");
322    let numbers: Vec<i32> = (-10..=10).collect();
323
324    let positive = |x: &i32| *x > 0;
325    let even = |x: &i32| x % 2 == 0;
326    let less_than_eight = |x: &i32| *x < 8;
327
328    let filtered: Vec<i32> = numbers
329        .iter()
330        .filter(|x| positive.test(x))
331        .filter(|x| even.test(x))
332        .filter(|x| less_than_eight.test(x))
333        .copied()
334        .collect();
335
336    println!("  Filtered numbers: {:?}", filtered);
337
338    // Strategy pattern
339    println!("\nScenario 3: Strategy Pattern");
340    let mut strategies: HashMap<&str, RcPredicate<i32>> = HashMap::new();
341    strategies.insert("positive", RcPredicate::new(|x: &i32| *x > 0));
342    strategies.insert("negative", RcPredicate::new(|x: &i32| *x < 0));
343    strategies.insert("even", RcPredicate::new(|x: &i32| x % 2 == 0));
344
345    let test_value = 4;
346    for (name, pred) in strategies.iter() {
347        println!(
348            "  {} strategy test {}: {}",
349            name,
350            test_value,
351            pred.test(&test_value)
352        );
353    }
354}
Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where F: Fn(&T) -> bool + 'static,

Creates a new named predicate with an optional name.

Wraps the provided closure and assigns it an optional name.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this predicate.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Examples found in repository?
examples/predicates/predicate_set_name_demo.rs (line 62)
56fn demo_rc_predicate() {
57    println!("2. RcPredicate Naming Functionality");
58
59    // Using new_with_name
60    let pred1 = RcPredicate::new_with_name("greater_than_10", |x: &i32| *x > 10);
61    println!("   Using new_with_name:");
62    println!("     Name: {:?}", pred1.name());
63    println!("     Test 15: {}", pred1.test(&15));
64
65    // Using set_name
66    let mut pred2 = RcPredicate::new(|x: &i32| *x < 100);
67    println!("\n   Using set_name:");
68    println!("     Initial name: {:?}", pred2.name());
69    pred2.set_name("less_than_100");
70    println!("     Name after setting: {:?}", pred2.name());
71    println!("     Test 50: {}", pred2.test(&50));
72
73    // Name is preserved after cloning
74    let pred3 = pred2.clone();
75    println!("\n   Name preserved after cloning:");
76    println!("     Cloned name: {:?}", pred3.name());
77    println!("     Test 80: {}\n", pred3.test(&80));
78}
More examples
Hide additional examples
examples/predicates/predicate_demo.rs (line 177)
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
examples/predicates/always_predicate_demo.rs (line 49)
17fn main() {
18    println!("=== BoxPredicate always_true/always_false Demo ===\n");
19
20    // BoxPredicate::always_true
21    let always_true: BoxPredicate<i32> = BoxPredicate::always_true();
22    println!("BoxPredicate::always_true():");
23    println!("  test(&42): {}", always_true.test(&42));
24    println!("  test(&-1): {}", always_true.test(&-1));
25    println!("  test(&0): {}", always_true.test(&0));
26    println!("  name: {:?}", always_true.name());
27
28    // BoxPredicate::always_false
29    let always_false: BoxPredicate<i32> = BoxPredicate::always_false();
30    println!("\nBoxPredicate::always_false():");
31    println!("  test(&42): {}", always_false.test(&42));
32    println!("  test(&-1): {}", always_false.test(&-1));
33    println!("  test(&0): {}", always_false.test(&0));
34    println!("  name: {:?}", always_false.name());
35
36    println!("\n=== RcPredicate always_true/always_false Demo ===\n");
37
38    // RcPredicate::always_true
39    let rc_always_true: RcPredicate<String> = RcPredicate::always_true();
40    println!("RcPredicate::always_true():");
41    println!(
42        "  test(&\"hello\"): {}",
43        rc_always_true.test(&"hello".to_string())
44    );
45    println!(
46        "  test(&\"world\"): {}",
47        rc_always_true.test(&"world".to_string())
48    );
49    println!("  name: {:?}", rc_always_true.name());
50
51    // RcPredicate::always_false
52    let rc_always_false: RcPredicate<String> = RcPredicate::always_false();
53    println!("\nRcPredicate::always_false():");
54    println!(
55        "  test(&\"hello\"): {}",
56        rc_always_false.test(&"hello".to_string())
57    );
58    println!(
59        "  test(&\"world\"): {}",
60        rc_always_false.test(&"world".to_string())
61    );
62    println!("  name: {:?}", rc_always_false.name());
63
64    // Can be cloned and reused
65    let rc_clone = rc_always_true.clone();
66    println!("\nAfter cloning, still usable:");
67    println!(
68        "  Original: test(&\"test\"): {}",
69        rc_always_true.test(&"test".to_string())
70    );
71    println!(
72        "  Clone: test(&\"test\"): {}",
73        rc_clone.test(&"test".to_string())
74    );
75
76    println!("\n=== ArcPredicate always_true/always_false Demo ===\n");
77
78    // ArcPredicate::always_true
79    let arc_always_true: ArcPredicate<i32> = ArcPredicate::always_true();
80    println!("ArcPredicate::always_true():");
81    println!("  test(&100): {}", arc_always_true.test(&100));
82    println!("  test(&-100): {}", arc_always_true.test(&-100));
83    println!("  name: {:?}", arc_always_true.name());
84
85    // ArcPredicate::always_false
86    let arc_always_false: ArcPredicate<i32> = ArcPredicate::always_false();
87    println!("\nArcPredicate::always_false():");
88    println!("  test(&100): {}", arc_always_false.test(&100));
89    println!("  test(&-100): {}", arc_always_false.test(&-100));
90    println!("  name: {:?}", arc_always_false.name());
91
92    println!("\n=== Combining with other predicates ===\n");
93
94    // Combining with always_true (AND)
95    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
96    let combined_and_true = is_positive.and(BoxPredicate::always_true());
97    println!("is_positive AND always_true:");
98    println!(
99        "  test(&5): {} (equivalent to is_positive)",
100        combined_and_true.test(&5)
101    );
102    println!(
103        "  test(&-3): {} (equivalent to is_positive)",
104        combined_and_true.test(&-3)
105    );
106
107    // Combining with always_false (AND)
108    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
109    let combined_and_false = is_positive.and(BoxPredicate::always_false());
110    println!("\nis_positive AND always_false:");
111    println!("  test(&5): {} (always false)", combined_and_false.test(&5));
112    println!(
113        "  test(&-3): {} (always false)",
114        combined_and_false.test(&-3)
115    );
116
117    // Combining with always_true (OR)
118    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
119    let combined_or_true = is_positive.or(BoxPredicate::always_true());
120    println!("\nis_positive OR always_true:");
121    println!("  test(&5): {} (always true)", combined_or_true.test(&5));
122    println!("  test(&-3): {} (always true)", combined_or_true.test(&-3));
123
124    // Combining with always_false (OR)
125    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
126    let combined_or_false = is_positive.or(BoxPredicate::always_false());
127    println!("\nis_positive OR always_false:");
128    println!(
129        "  test(&5): {} (equivalent to is_positive)",
130        combined_or_false.test(&5)
131    );
132    println!(
133        "  test(&-3): {} (equivalent to is_positive)",
134        combined_or_false.test(&-3)
135    );
136
137    println!("\n=== Practical scenarios: Default pass/reject filters ===\n");
138
139    // Scenario 1: Default pass-all filter
140    let numbers = vec![1, 2, 3, 4, 5];
141    let pass_all = BoxPredicate::<i32>::always_true();
142    let filtered: Vec<_> = numbers.iter().copied().filter(pass_all.into_fn()).collect();
143    println!("Default pass all elements: {:?} -> {:?}", numbers, filtered);
144
145    // Scenario 2: Default reject-all filter
146    let numbers = vec![1, 2, 3, 4, 5];
147    let reject_all = BoxPredicate::<i32>::always_false();
148    let filtered: Vec<_> = numbers
149        .iter()
150        .copied()
151        .filter(reject_all.into_fn())
152        .collect();
153    println!(
154        "Default reject all elements: {:?} -> {:?}",
155        numbers, filtered
156    );
157
158    // Scenario 3: Configurable filter
159    fn configurable_filter(enable_filter: bool) -> BoxPredicate<i32> {
160        if enable_filter {
161            BoxPredicate::new(|x: &i32| *x > 3)
162        } else {
163            BoxPredicate::always_true()
164        }
165    }
166
167    let numbers = vec![1, 2, 3, 4, 5];
168
169    let filter_enabled = configurable_filter(true);
170    let filtered: Vec<_> = numbers
171        .iter()
172        .copied()
173        .filter(filter_enabled.into_fn())
174        .collect();
175    println!("\nFilter enabled: {:?} -> {:?}", numbers, filtered);
176
177    let filter_disabled = configurable_filter(false);
178    let filtered: Vec<_> = numbers
179        .iter()
180        .copied()
181        .filter(filter_disabled.into_fn())
182        .collect();
183    println!("Filter disabled: {:?} -> {:?}", numbers, filtered);
184}
Source

pub fn set_name(&mut self, name: &str)

Sets the name of this predicate.

§Parameters
  • name - The name to set for this predicate
Examples found in repository?
examples/predicates/predicate_set_name_demo.rs (line 69)
56fn demo_rc_predicate() {
57    println!("2. RcPredicate Naming Functionality");
58
59    // Using new_with_name
60    let pred1 = RcPredicate::new_with_name("greater_than_10", |x: &i32| *x > 10);
61    println!("   Using new_with_name:");
62    println!("     Name: {:?}", pred1.name());
63    println!("     Test 15: {}", pred1.test(&15));
64
65    // Using set_name
66    let mut pred2 = RcPredicate::new(|x: &i32| *x < 100);
67    println!("\n   Using set_name:");
68    println!("     Initial name: {:?}", pred2.name());
69    pred2.set_name("less_than_100");
70    println!("     Name after setting: {:?}", pred2.name());
71    println!("     Test 50: {}", pred2.test(&50));
72
73    // Name is preserved after cloning
74    let pred3 = pred2.clone();
75    println!("\n   Name preserved after cloning:");
76    println!("     Cloned name: {:?}", pred3.name());
77    println!("     Test 80: {}\n", pred3.test(&80));
78}
Source

pub fn clear_name(&mut self)

Clears the name of this predicate.

Source

pub fn always_true() -> Self

Creates a predicate that always returns true.

§Returns

A new RcPredicate that always returns true.

Examples found in repository?
examples/predicates/always_predicate_demo.rs (line 39)
17fn main() {
18    println!("=== BoxPredicate always_true/always_false Demo ===\n");
19
20    // BoxPredicate::always_true
21    let always_true: BoxPredicate<i32> = BoxPredicate::always_true();
22    println!("BoxPredicate::always_true():");
23    println!("  test(&42): {}", always_true.test(&42));
24    println!("  test(&-1): {}", always_true.test(&-1));
25    println!("  test(&0): {}", always_true.test(&0));
26    println!("  name: {:?}", always_true.name());
27
28    // BoxPredicate::always_false
29    let always_false: BoxPredicate<i32> = BoxPredicate::always_false();
30    println!("\nBoxPredicate::always_false():");
31    println!("  test(&42): {}", always_false.test(&42));
32    println!("  test(&-1): {}", always_false.test(&-1));
33    println!("  test(&0): {}", always_false.test(&0));
34    println!("  name: {:?}", always_false.name());
35
36    println!("\n=== RcPredicate always_true/always_false Demo ===\n");
37
38    // RcPredicate::always_true
39    let rc_always_true: RcPredicate<String> = RcPredicate::always_true();
40    println!("RcPredicate::always_true():");
41    println!(
42        "  test(&\"hello\"): {}",
43        rc_always_true.test(&"hello".to_string())
44    );
45    println!(
46        "  test(&\"world\"): {}",
47        rc_always_true.test(&"world".to_string())
48    );
49    println!("  name: {:?}", rc_always_true.name());
50
51    // RcPredicate::always_false
52    let rc_always_false: RcPredicate<String> = RcPredicate::always_false();
53    println!("\nRcPredicate::always_false():");
54    println!(
55        "  test(&\"hello\"): {}",
56        rc_always_false.test(&"hello".to_string())
57    );
58    println!(
59        "  test(&\"world\"): {}",
60        rc_always_false.test(&"world".to_string())
61    );
62    println!("  name: {:?}", rc_always_false.name());
63
64    // Can be cloned and reused
65    let rc_clone = rc_always_true.clone();
66    println!("\nAfter cloning, still usable:");
67    println!(
68        "  Original: test(&\"test\"): {}",
69        rc_always_true.test(&"test".to_string())
70    );
71    println!(
72        "  Clone: test(&\"test\"): {}",
73        rc_clone.test(&"test".to_string())
74    );
75
76    println!("\n=== ArcPredicate always_true/always_false Demo ===\n");
77
78    // ArcPredicate::always_true
79    let arc_always_true: ArcPredicate<i32> = ArcPredicate::always_true();
80    println!("ArcPredicate::always_true():");
81    println!("  test(&100): {}", arc_always_true.test(&100));
82    println!("  test(&-100): {}", arc_always_true.test(&-100));
83    println!("  name: {:?}", arc_always_true.name());
84
85    // ArcPredicate::always_false
86    let arc_always_false: ArcPredicate<i32> = ArcPredicate::always_false();
87    println!("\nArcPredicate::always_false():");
88    println!("  test(&100): {}", arc_always_false.test(&100));
89    println!("  test(&-100): {}", arc_always_false.test(&-100));
90    println!("  name: {:?}", arc_always_false.name());
91
92    println!("\n=== Combining with other predicates ===\n");
93
94    // Combining with always_true (AND)
95    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
96    let combined_and_true = is_positive.and(BoxPredicate::always_true());
97    println!("is_positive AND always_true:");
98    println!(
99        "  test(&5): {} (equivalent to is_positive)",
100        combined_and_true.test(&5)
101    );
102    println!(
103        "  test(&-3): {} (equivalent to is_positive)",
104        combined_and_true.test(&-3)
105    );
106
107    // Combining with always_false (AND)
108    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
109    let combined_and_false = is_positive.and(BoxPredicate::always_false());
110    println!("\nis_positive AND always_false:");
111    println!("  test(&5): {} (always false)", combined_and_false.test(&5));
112    println!(
113        "  test(&-3): {} (always false)",
114        combined_and_false.test(&-3)
115    );
116
117    // Combining with always_true (OR)
118    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
119    let combined_or_true = is_positive.or(BoxPredicate::always_true());
120    println!("\nis_positive OR always_true:");
121    println!("  test(&5): {} (always true)", combined_or_true.test(&5));
122    println!("  test(&-3): {} (always true)", combined_or_true.test(&-3));
123
124    // Combining with always_false (OR)
125    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
126    let combined_or_false = is_positive.or(BoxPredicate::always_false());
127    println!("\nis_positive OR always_false:");
128    println!(
129        "  test(&5): {} (equivalent to is_positive)",
130        combined_or_false.test(&5)
131    );
132    println!(
133        "  test(&-3): {} (equivalent to is_positive)",
134        combined_or_false.test(&-3)
135    );
136
137    println!("\n=== Practical scenarios: Default pass/reject filters ===\n");
138
139    // Scenario 1: Default pass-all filter
140    let numbers = vec![1, 2, 3, 4, 5];
141    let pass_all = BoxPredicate::<i32>::always_true();
142    let filtered: Vec<_> = numbers.iter().copied().filter(pass_all.into_fn()).collect();
143    println!("Default pass all elements: {:?} -> {:?}", numbers, filtered);
144
145    // Scenario 2: Default reject-all filter
146    let numbers = vec![1, 2, 3, 4, 5];
147    let reject_all = BoxPredicate::<i32>::always_false();
148    let filtered: Vec<_> = numbers
149        .iter()
150        .copied()
151        .filter(reject_all.into_fn())
152        .collect();
153    println!(
154        "Default reject all elements: {:?} -> {:?}",
155        numbers, filtered
156    );
157
158    // Scenario 3: Configurable filter
159    fn configurable_filter(enable_filter: bool) -> BoxPredicate<i32> {
160        if enable_filter {
161            BoxPredicate::new(|x: &i32| *x > 3)
162        } else {
163            BoxPredicate::always_true()
164        }
165    }
166
167    let numbers = vec![1, 2, 3, 4, 5];
168
169    let filter_enabled = configurable_filter(true);
170    let filtered: Vec<_> = numbers
171        .iter()
172        .copied()
173        .filter(filter_enabled.into_fn())
174        .collect();
175    println!("\nFilter enabled: {:?} -> {:?}", numbers, filtered);
176
177    let filter_disabled = configurable_filter(false);
178    let filtered: Vec<_> = numbers
179        .iter()
180        .copied()
181        .filter(filter_disabled.into_fn())
182        .collect();
183    println!("Filter disabled: {:?} -> {:?}", numbers, filtered);
184}
Source

pub fn always_false() -> Self

Creates a predicate that always returns false.

§Returns

A new RcPredicate that always returns false.

Examples found in repository?
examples/predicates/always_predicate_demo.rs (line 52)
17fn main() {
18    println!("=== BoxPredicate always_true/always_false Demo ===\n");
19
20    // BoxPredicate::always_true
21    let always_true: BoxPredicate<i32> = BoxPredicate::always_true();
22    println!("BoxPredicate::always_true():");
23    println!("  test(&42): {}", always_true.test(&42));
24    println!("  test(&-1): {}", always_true.test(&-1));
25    println!("  test(&0): {}", always_true.test(&0));
26    println!("  name: {:?}", always_true.name());
27
28    // BoxPredicate::always_false
29    let always_false: BoxPredicate<i32> = BoxPredicate::always_false();
30    println!("\nBoxPredicate::always_false():");
31    println!("  test(&42): {}", always_false.test(&42));
32    println!("  test(&-1): {}", always_false.test(&-1));
33    println!("  test(&0): {}", always_false.test(&0));
34    println!("  name: {:?}", always_false.name());
35
36    println!("\n=== RcPredicate always_true/always_false Demo ===\n");
37
38    // RcPredicate::always_true
39    let rc_always_true: RcPredicate<String> = RcPredicate::always_true();
40    println!("RcPredicate::always_true():");
41    println!(
42        "  test(&\"hello\"): {}",
43        rc_always_true.test(&"hello".to_string())
44    );
45    println!(
46        "  test(&\"world\"): {}",
47        rc_always_true.test(&"world".to_string())
48    );
49    println!("  name: {:?}", rc_always_true.name());
50
51    // RcPredicate::always_false
52    let rc_always_false: RcPredicate<String> = RcPredicate::always_false();
53    println!("\nRcPredicate::always_false():");
54    println!(
55        "  test(&\"hello\"): {}",
56        rc_always_false.test(&"hello".to_string())
57    );
58    println!(
59        "  test(&\"world\"): {}",
60        rc_always_false.test(&"world".to_string())
61    );
62    println!("  name: {:?}", rc_always_false.name());
63
64    // Can be cloned and reused
65    let rc_clone = rc_always_true.clone();
66    println!("\nAfter cloning, still usable:");
67    println!(
68        "  Original: test(&\"test\"): {}",
69        rc_always_true.test(&"test".to_string())
70    );
71    println!(
72        "  Clone: test(&\"test\"): {}",
73        rc_clone.test(&"test".to_string())
74    );
75
76    println!("\n=== ArcPredicate always_true/always_false Demo ===\n");
77
78    // ArcPredicate::always_true
79    let arc_always_true: ArcPredicate<i32> = ArcPredicate::always_true();
80    println!("ArcPredicate::always_true():");
81    println!("  test(&100): {}", arc_always_true.test(&100));
82    println!("  test(&-100): {}", arc_always_true.test(&-100));
83    println!("  name: {:?}", arc_always_true.name());
84
85    // ArcPredicate::always_false
86    let arc_always_false: ArcPredicate<i32> = ArcPredicate::always_false();
87    println!("\nArcPredicate::always_false():");
88    println!("  test(&100): {}", arc_always_false.test(&100));
89    println!("  test(&-100): {}", arc_always_false.test(&-100));
90    println!("  name: {:?}", arc_always_false.name());
91
92    println!("\n=== Combining with other predicates ===\n");
93
94    // Combining with always_true (AND)
95    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
96    let combined_and_true = is_positive.and(BoxPredicate::always_true());
97    println!("is_positive AND always_true:");
98    println!(
99        "  test(&5): {} (equivalent to is_positive)",
100        combined_and_true.test(&5)
101    );
102    println!(
103        "  test(&-3): {} (equivalent to is_positive)",
104        combined_and_true.test(&-3)
105    );
106
107    // Combining with always_false (AND)
108    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
109    let combined_and_false = is_positive.and(BoxPredicate::always_false());
110    println!("\nis_positive AND always_false:");
111    println!("  test(&5): {} (always false)", combined_and_false.test(&5));
112    println!(
113        "  test(&-3): {} (always false)",
114        combined_and_false.test(&-3)
115    );
116
117    // Combining with always_true (OR)
118    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
119    let combined_or_true = is_positive.or(BoxPredicate::always_true());
120    println!("\nis_positive OR always_true:");
121    println!("  test(&5): {} (always true)", combined_or_true.test(&5));
122    println!("  test(&-3): {} (always true)", combined_or_true.test(&-3));
123
124    // Combining with always_false (OR)
125    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
126    let combined_or_false = is_positive.or(BoxPredicate::always_false());
127    println!("\nis_positive OR always_false:");
128    println!(
129        "  test(&5): {} (equivalent to is_positive)",
130        combined_or_false.test(&5)
131    );
132    println!(
133        "  test(&-3): {} (equivalent to is_positive)",
134        combined_or_false.test(&-3)
135    );
136
137    println!("\n=== Practical scenarios: Default pass/reject filters ===\n");
138
139    // Scenario 1: Default pass-all filter
140    let numbers = vec![1, 2, 3, 4, 5];
141    let pass_all = BoxPredicate::<i32>::always_true();
142    let filtered: Vec<_> = numbers.iter().copied().filter(pass_all.into_fn()).collect();
143    println!("Default pass all elements: {:?} -> {:?}", numbers, filtered);
144
145    // Scenario 2: Default reject-all filter
146    let numbers = vec![1, 2, 3, 4, 5];
147    let reject_all = BoxPredicate::<i32>::always_false();
148    let filtered: Vec<_> = numbers
149        .iter()
150        .copied()
151        .filter(reject_all.into_fn())
152        .collect();
153    println!(
154        "Default reject all elements: {:?} -> {:?}",
155        numbers, filtered
156    );
157
158    // Scenario 3: Configurable filter
159    fn configurable_filter(enable_filter: bool) -> BoxPredicate<i32> {
160        if enable_filter {
161            BoxPredicate::new(|x: &i32| *x > 3)
162        } else {
163            BoxPredicate::always_true()
164        }
165    }
166
167    let numbers = vec![1, 2, 3, 4, 5];
168
169    let filter_enabled = configurable_filter(true);
170    let filtered: Vec<_> = numbers
171        .iter()
172        .copied()
173        .filter(filter_enabled.into_fn())
174        .collect();
175    println!("\nFilter enabled: {:?} -> {:?}", numbers, filtered);
176
177    let filter_disabled = configurable_filter(false);
178    let filtered: Vec<_> = numbers
179        .iter()
180        .copied()
181        .filter(filter_disabled.into_fn())
182        .collect();
183    println!("Filter disabled: {:?} -> {:?}", numbers, filtered);
184}
Source

pub fn and<P>(&self, other: P) -> RcPredicate<T>
where T: 'static, P: Predicate<T> + 'static,

Returns a predicate that represents the logical AND of this predicate and another.

This method consumes self due to single-ownership semantics.

§Parameters
  • other - The other predicate to combine with.
§Returns

A new predicate representing the logical AND.

Examples found in repository?
examples/predicates/predicate_demo.rs (line 115)
108fn rc_predicate_examples() {
109    println!("--- 3. RcPredicate Examples (Single-threaded Reuse) ---");
110
111    let is_positive = RcPredicate::new(|x: &i32| *x > 0);
112    let is_even = RcPredicate::new(|x: &i32| x % 2 == 0);
113
114    // Multiple compositions without consuming the original
115    let positive_and_even = is_positive.and(is_even.clone());
116    let positive_or_even = is_positive.or(is_even.clone());
117
118    println!("Original predicates still available:");
119    println!("  is_positive.test(&5) = {}", is_positive.test(&5));
120    println!("  is_even.test(&4) = {}", is_even.test(&4));
121
122    println!("Combined predicates:");
123    println!(
124        "  positive_and_even.test(&4) = {}",
125        positive_and_even.test(&4)
126    );
127    println!(
128        "  positive_or_even.test(&5) = {}",
129        positive_or_even.test(&5)
130    );
131
132    // Cloning
133    let cloned = is_positive.clone();
134    println!("Cloned predicate: {}", cloned.test(&10));
135}
136
137/// ArcPredicate examples - multi-threaded scenarios
138fn arc_predicate_examples() {
139    println!("--- 4. ArcPredicate Examples (Multi-threaded Scenarios) ---");
140
141    let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
142    let is_even = ArcPredicate::new(|x: &i32| x % 2 == 0);
143
144    // Create combined predicate
145    let combined = is_positive.and(is_even);
146
147    // Use in multiple threads
148    let handles: Vec<_> = (0..3)
149        .map(|i| {
150            let pred = combined.clone();
151            std::thread::spawn(move || {
152                let value = i * 2;
153                println!("  Thread {} testing {}: {}", i, value, pred.test(&value));
154            })
155        })
156        .collect();
157
158    for handle in handles {
159        handle.join().unwrap();
160    }
161
162    // Original predicates still usable
163    println!("Original predicates still available in main thread:");
164    println!("  is_positive.test(&5) = {}", is_positive.test(&5));
165}
166
167/// Logical composition examples
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
220
221/// Interior mutability examples
222fn interior_mutability_examples() {
223    println!("--- 6. Interior Mutability Examples ---");
224
225    // BoxPredicate with counter (RefCell)
226    println!("BoxPredicate with counter:");
227    let count = RefCell::new(0);
228    let pred = BoxPredicate::new(move |x: &i32| {
229        *count.borrow_mut() += 1;
230        *x > 0
231    });
232    println!("  Test 5: {}", pred.test(&5));
233    println!("  Test -3: {}", pred.test(&-3));
234    println!("  Test 10: {}", pred.test(&10));
235    // Note: count is moved into the closure, so we can't access it here
236
237    // RcPredicate with cache (RefCell + HashMap)
238    println!("\nRcPredicate with cache:");
239    let cache: RefCell<HashMap<i32, bool>> = RefCell::new(HashMap::new());
240    let expensive_pred = RcPredicate::new(move |x: &i32| {
241        let mut c = cache.borrow_mut();
242        *c.entry(*x).or_insert_with(|| {
243            println!("    Computing result for {} (expensive operation)", x);
244            *x > 0 && x % 2 == 0
245        })
246    });
247
248    println!("  First test 4:");
249    println!("    Result: {}", expensive_pred.test(&4));
250    println!("  Test 4 again (using cache):");
251    println!("    Result: {}", expensive_pred.test(&4));
252    println!("  Test 3:");
253    println!("    Result: {}", expensive_pred.test(&3));
254
255    // ArcPredicate with thread-safe counter (Mutex)
256    println!("\nArcPredicate with thread-safe counter:");
257    let counter = Arc::new(Mutex::new(0));
258    let pred = ArcPredicate::new({
259        let counter = Arc::clone(&counter);
260        move |x: &i32| {
261            let mut c = counter.lock().unwrap();
262            *c += 1;
263            *x > 0
264        }
265    });
266
267    let pred_clone = pred.clone();
268    let counter_clone = Arc::clone(&counter);
269
270    let handle = std::thread::spawn(move || {
271        pred_clone.test(&5);
272        pred_clone.test(&10);
273    });
274
275    pred.test(&3);
276    handle.join().unwrap();
277
278    println!("  Total call count: {}", counter_clone.lock().unwrap());
279}
280
281/// Practical use cases
282fn practical_use_cases() {
283    println!("--- 7. Practical Use Cases ---");
284
285    // Validation rules
286    println!("Scenario 1: Form Validation");
287    struct User {
288        name: String,
289        age: i32,
290        email: String,
291    }
292
293    let name_valid =
294        RcPredicate::new_with_name("name_not_empty", |user: &User| !user.name.is_empty());
295
296    let age_valid = RcPredicate::new_with_name("age_between_18_120", |user: &User| {
297        user.age >= 18 && user.age <= 120
298    });
299
300    let email_valid =
301        RcPredicate::new_with_name("email_contains_at", |user: &User| user.email.contains('@'));
302
303    let all_valid = name_valid.and(age_valid.clone()).and(email_valid.clone());
304
305    let user1 = User {
306        name: "Alice".to_string(),
307        age: 25,
308        email: "alice@example.com".to_string(),
309    };
310
311    let user2 = User {
312        name: "".to_string(),
313        age: 25,
314        email: "bob@example.com".to_string(),
315    };
316
317    println!("  user1 validation: {}", all_valid.test(&user1));
318    println!("  user2 validation: {}", all_valid.test(&user2));
319
320    // Filter pipeline
321    println!("\nScenario 2: Data Filtering Pipeline");
322    let numbers: Vec<i32> = (-10..=10).collect();
323
324    let positive = |x: &i32| *x > 0;
325    let even = |x: &i32| x % 2 == 0;
326    let less_than_eight = |x: &i32| *x < 8;
327
328    let filtered: Vec<i32> = numbers
329        .iter()
330        .filter(|x| positive.test(x))
331        .filter(|x| even.test(x))
332        .filter(|x| less_than_eight.test(x))
333        .copied()
334        .collect();
335
336    println!("  Filtered numbers: {:?}", filtered);
337
338    // Strategy pattern
339    println!("\nScenario 3: Strategy Pattern");
340    let mut strategies: HashMap<&str, RcPredicate<i32>> = HashMap::new();
341    strategies.insert("positive", RcPredicate::new(|x: &i32| *x > 0));
342    strategies.insert("negative", RcPredicate::new(|x: &i32| *x < 0));
343    strategies.insert("even", RcPredicate::new(|x: &i32| x % 2 == 0));
344
345    let test_value = 4;
346    for (name, pred) in strategies.iter() {
347        println!(
348            "  {} strategy test {}: {}",
349            name,
350            test_value,
351            pred.test(&test_value)
352        );
353    }
354}
Source

pub fn or<P>(&self, other: P) -> RcPredicate<T>
where T: 'static, P: Predicate<T> + 'static,

Returns a predicate that represents the logical OR of this predicate and another.

This method consumes self due to single-ownership semantics.

§Parameters
  • other - The other predicate to combine with.
§Returns

A new predicate representing the logical OR.

Examples found in repository?
examples/predicates/predicate_demo.rs (line 116)
108fn rc_predicate_examples() {
109    println!("--- 3. RcPredicate Examples (Single-threaded Reuse) ---");
110
111    let is_positive = RcPredicate::new(|x: &i32| *x > 0);
112    let is_even = RcPredicate::new(|x: &i32| x % 2 == 0);
113
114    // Multiple compositions without consuming the original
115    let positive_and_even = is_positive.and(is_even.clone());
116    let positive_or_even = is_positive.or(is_even.clone());
117
118    println!("Original predicates still available:");
119    println!("  is_positive.test(&5) = {}", is_positive.test(&5));
120    println!("  is_even.test(&4) = {}", is_even.test(&4));
121
122    println!("Combined predicates:");
123    println!(
124        "  positive_and_even.test(&4) = {}",
125        positive_and_even.test(&4)
126    );
127    println!(
128        "  positive_or_even.test(&5) = {}",
129        positive_or_even.test(&5)
130    );
131
132    // Cloning
133    let cloned = is_positive.clone();
134    println!("Cloned predicate: {}", cloned.test(&10));
135}
136
137/// ArcPredicate examples - multi-threaded scenarios
138fn arc_predicate_examples() {
139    println!("--- 4. ArcPredicate Examples (Multi-threaded Scenarios) ---");
140
141    let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
142    let is_even = ArcPredicate::new(|x: &i32| x % 2 == 0);
143
144    // Create combined predicate
145    let combined = is_positive.and(is_even);
146
147    // Use in multiple threads
148    let handles: Vec<_> = (0..3)
149        .map(|i| {
150            let pred = combined.clone();
151            std::thread::spawn(move || {
152                let value = i * 2;
153                println!("  Thread {} testing {}: {}", i, value, pred.test(&value));
154            })
155        })
156        .collect();
157
158    for handle in handles {
159        handle.join().unwrap();
160    }
161
162    // Original predicates still usable
163    println!("Original predicates still available in main thread:");
164    println!("  is_positive.test(&5) = {}", is_positive.test(&5));
165}
166
167/// Logical composition examples
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
Source

pub fn not(&self) -> RcPredicate<T>
where T: 'static,

Returns a predicate that represents the logical negation of this predicate.

This method consumes self due to single-ownership semantics.

§Returns

A new predicate representing the logical negation.

Examples found in repository?
examples/predicates/predicate_demo.rs (line 188)
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
Source

pub fn nand<P>(&self, other: P) -> RcPredicate<T>
where T: 'static, P: Predicate<T> + 'static,

Returns a predicate that represents the logical NAND (NOT AND) of this predicate and another.

NAND returns true unless both predicates are true. Equivalent to !(self AND other).

This method consumes self due to single-ownership semantics.

§Parameters
  • other - The other predicate to combine with.
§Returns

A new predicate representing the logical NAND.

Examples found in repository?
examples/predicates/predicate_demo.rs (line 194)
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
Source

pub fn xor<P>(&self, other: P) -> RcPredicate<T>
where T: 'static, P: Predicate<T> + 'static,

Returns a predicate that represents the logical XOR (exclusive OR) of this predicate and another.

XOR returns true if exactly one of the predicates is true.

This method consumes self due to single-ownership semantics.

§Parameters
  • other - The other predicate to combine with.
§Returns

A new predicate representing the logical XOR.

Examples found in repository?
examples/predicates/predicate_demo.rs (line 200)
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}
Source

pub fn nor<P>(&self, other: P) -> RcPredicate<T>
where T: 'static, P: Predicate<T> + 'static,

Returns a predicate that represents the logical NOR (NOT OR) of this predicate and another.

NOR returns true only when both predicates are false. Equivalent to !(self OR other).

This method consumes self due to single-ownership semantics.

§Parameters
  • other - The other predicate to combine with.
§Returns

A new predicate representing the logical NOR.

Examples found in repository?
examples/predicates/predicate_demo.rs (line 207)
168fn logical_composition_examples() {
169    println!("--- 5. Logical Composition Examples ---");
170
171    let positive = RcPredicate::new_with_name("positive", |x: &i32| *x > 0);
172    let even = RcPredicate::new_with_name("even", |x: &i32| x % 2 == 0);
173    let less_than_ten = RcPredicate::new_with_name("less_than_ten", |x: &i32| *x < 10);
174
175    // AND composition
176    let positive_and_even = positive.and(even.clone());
177    println!("positive AND even: name={:?}", positive_and_even.name());
178    println!("  Test 4: {}", positive_and_even.test(&4));
179    println!("  Test 5: {}", positive_and_even.test(&5));
180
181    // OR composition
182    let positive_or_even = positive.or(even.clone());
183    println!("positive OR even: name={:?}", positive_or_even.name());
184    println!("  Test -2: {}", positive_or_even.test(&-2));
185    println!("  Test 5: {}", positive_or_even.test(&5));
186
187    // NOT composition
188    let not_positive = positive.not();
189    println!("NOT positive: name={:?}", not_positive.name());
190    println!("  Test 5: {}", not_positive.test(&5));
191    println!("  Test -3: {}", not_positive.test(&-3));
192
193    // NAND composition
194    let nand = positive.nand(even.clone());
195    println!("positive NAND even: name={:?}", nand.name());
196    println!("  Test 3: {}", nand.test(&3)); // true NAND false = true
197    println!("  Test 4: {}", nand.test(&4)); // true NAND true = false
198
199    // XOR composition
200    let xor = positive.xor(even.clone());
201    println!("positive XOR even: name={:?}", xor.name());
202    println!("  Test 3: {}", xor.test(&3)); // true XOR false = true
203    println!("  Test 4: {}", xor.test(&4)); // true XOR true = false
204    println!("  Test -2: {}", xor.test(&-2)); // false XOR true = true
205
206    // NOR composition
207    let nor = positive.nor(even.clone());
208    println!("positive NOR even: name={:?}", nor.name());
209    println!("  Test -3: {}", nor.test(&-3)); // false NOR false = true
210    println!("  Test 3: {}", nor.test(&3)); // true NOR false = false
211    println!("  Test -2: {}", nor.test(&-2)); // false NOR true = false
212    println!("  Test 4: {}", nor.test(&4)); // true NOR true = false
213
214    // Complex composition
215    let complex = positive.and(even.clone()).and(less_than_ten.clone());
216    println!("Complex composition: name={:?}", complex.name());
217    println!("  Test 4: {}", complex.test(&4));
218    println!("  Test 12: {}", complex.test(&12));
219}

Trait Implementations§

Source§

impl<T> Clone for RcPredicate<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for RcPredicate<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for RcPredicate<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Predicate<T> for RcPredicate<T>

Source§

fn test(&self, value: &T) -> bool

Tests whether the given value satisfies this predicate. Read more
Source§

fn into_box(self) -> BoxPredicate<T>
where Self: 'static,

Converts this predicate into a BoxPredicate. Read more
Source§

fn into_rc(self) -> RcPredicate<T>

Converts this predicate into an RcPredicate. Read more
Source§

fn into_fn(self) -> impl Fn(&T) -> bool

Converts this predicate into a closure that can be used directly with standard library methods. Read more
Source§

fn to_box(&self) -> BoxPredicate<T>
where Self: 'static,

Converts a reference to this predicate into a BoxPredicate. Read more
Source§

fn to_rc(&self) -> RcPredicate<T>

Converts a reference to this predicate into an RcPredicate. Read more
Source§

fn to_fn(&self) -> impl Fn(&T) -> bool

Converts a reference to this predicate into a closure that can be used directly with standard library methods. Read more
Source§

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

Converts this predicate into an ArcPredicate. Read more
Source§

fn to_arc(&self) -> ArcPredicate<T>
where Self: Clone + Sized + Send + Sync + 'static,

Converts a reference to this predicate into an ArcPredicate. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RcPredicate<T>

§

impl<T> !RefUnwindSafe for RcPredicate<T>

§

impl<T> !Send for RcPredicate<T>

§

impl<T> !Sync for RcPredicate<T>

§

impl<T> Unpin for RcPredicate<T>

§

impl<T> UnsafeUnpin for RcPredicate<T>

§

impl<T> !UnwindSafe for RcPredicate<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.