Trait pattern_3::needle::DoubleEndedConsumer
source · pub unsafe trait DoubleEndedConsumer<A: Hay + ?Sized>: ReverseConsumer<A> { }
Expand description
A consumer which can be searched from both end with consistent results.
It is used to support the following standard algorithm:
The trim
function is implemented by calling
trim_start
and trim_end
together.
This trait encodes the fact that we can call these two functions in any order.
Examples
The consumer of a character implements DoubleEndedConsumer
, while that of
a string does not. trim
is implemented only for a DoubleEndedConsumer
.
extern crate pattern_3;
use pattern_3::ext::{trim_start, trim_end, trim};
// for a `char`, we get the same trim result no matter which function is called first.
let trim_start_first = trim_end(trim_start("xyxyx", 'x'), 'x');
let trim_end_first = trim_start(trim_end("xyxyx", 'x'), 'x');
let trim_together = trim("xyxyx", 'x');
assert_eq!(trim_start_first, "yxy");
assert_eq!(trim_end_first, "yxy");
assert_eq!(trim_together, "yxy");
// this property does not exist for a `&str` in general.
let trim_start_first = trim_end(trim_start("xyxyx", "xyx"), "xyx");
let trim_end_first = trim_start(trim_end("xyxyx", "xyx"), "xyx");
// let trim_together = trim("xyxyx", 'x'); // cannot be defined
assert_eq!(trim_start_first, "yx");
assert_eq!(trim_end_first, "xy");
// assert_eq!(trim_together, /*????*/); // cannot be defined