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

§Author

Haixing Hu

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

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.