Skip to content

Commit 53ef88a

Browse files
authored
Merge pull request #63 from luser/function-unsized
Use T: ?Sized everywhere for FnPredicate so slices and str work
2 parents a5a2dd4 + 56592ba commit 53ef88a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/function.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use Predicate;
1919
pub struct FnPredicate<F, T>
2020
where
2121
F: Fn(&T) -> bool,
22+
T: ?Sized,
2223
{
2324
function: F,
2425
name: &'static str,
@@ -28,6 +29,7 @@ where
2829
impl<F, T> FnPredicate<F, T>
2930
where
3031
F: Fn(&T) -> bool,
32+
T: ?Sized,
3133
{
3234
/// Provide a descriptive name for this function.
3335
///
@@ -54,6 +56,7 @@ where
5456
impl<F, T> Predicate<T> for FnPredicate<F, T>
5557
where
5658
F: Fn(&T) -> bool,
59+
T: ?Sized,
5760
{
5861
fn eval(&self, variable: &T) -> bool {
5962
(self.function)(variable)
@@ -63,6 +66,7 @@ where
6366
impl<F, T> fmt::Display for FnPredicate<F, T>
6467
where
6568
F: Fn(&T) -> bool,
69+
T: ?Sized,
6670
{
6771
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6872
write!(f, "{}(var)", self.name)
@@ -94,10 +98,18 @@ where
9498
pub fn function<F, T>(function: F) -> FnPredicate<F, T>
9599
where
96100
F: Fn(&T) -> bool,
101+
T: ?Sized,
97102
{
98103
FnPredicate {
99104
function,
100105
name: "fn",
101106
_phantom: PhantomData,
102107
}
103108
}
109+
110+
#[test]
111+
fn str_function() {
112+
let f = function(|x: &str| x == "hello");
113+
assert!(f.eval(&"hello"));
114+
assert!(!f.eval(&"goodbye"));
115+
}

0 commit comments

Comments
 (0)