函数返回一个闭包不在我的过滤器内工作

时间:2016-09-16 23:19:14

标签: rust closures trait-objects

如果不使用闭包,我无法编译。我试图让函数apply首先返回正确的闭包。

#![feature(conservative_impl_trait)]
#![allow(dead_code)]

fn accumulate<'a>(tuples: &[(&'a str, &Fn(i32) -> bool)], i: i32) {

    // this works
    let _ = tuples.iter().filter(|t| apply(second, i)(t));

    // this doesn't
    //let f = apply(second, i);
    //let _ = tuples.iter().filter(f);

    //this works as well

    let f  = |t: &&(_,_)| apply(second, i)(t);
    let _ = tuples.iter().filter(f);
}

fn apply<A, B, C, F, G>(mut f: F, a: A) -> impl FnMut(B) -> C
         where F: FnMut(B) -> G,
               G: FnMut(A) -> C,
               A: Clone
{
    move |b| f(b)(a.clone())
}


fn second<A, B: ?Sized>(&(_, ref second): &(A, B)) -> &B {
    second
}

fn main()  {}

我可以做些什么让apply像我想要的那样工作?

1 个答案:

答案 0 :(得分:12)

首先,我要说这个问题与使用impl Trait语法无关。我将闭包转换为命名结构并获得相同的结果。

所以,让我们看一下你想要工作的代码:

let f = apply(second, i);
let _ = tuples.iter().filter(f);

编译器对此有何评价?

error[E0277]: the trait bound `for<'r> impl std::ops::FnMut<(&(_, _),)>: std::ops::FnMut<(&'r &(&str, &std::ops::Fn(i32) -> bool),)>` is not satisfied
  --> <anon>:11:27
   |
11 |     let _ = tuples.iter().filter(f);
   |                           ^^^^^^ trait `for<'r> impl std::ops::FnMut<(&(_, _),)>: std::ops::FnMut<(&'r &(&str, &std::ops::Fn(i32) -> bool),)>` not satisfied

error[E0277]: the trait bound `for<'r> impl std::ops::FnMut<(&(_, _),)>: std::ops::FnOnce<(&'r &(&str, &std::ops::Fn(i32) -> bool),)>` is not satisfied
  --> <anon>:11:27
   |
11 |     let _ = tuples.iter().filter(f);
   |                           ^^^^^^ trait `for<'r> impl std::ops::FnMut<(&(_, _),)>: std::ops::FnOnce<(&'r &(&str, &std::ops::Fn(i32) -> bool),)>` not satisfied

好的,所以我们有X型,它需要实现特征Y,但它没有。但让我们仔细看看:

for<'r> impl
std::ops::FnMut<(&(_, _),)>:
std::ops::FnMut<(&'r &(_, _),)>
啊哈哈! filter期望一个函数接受对元组的引用的引用,而我们传入的函数接受对元组的引用。 filter传递对引用的引用,因为tuples.iter()遍历引用,filter传递对引用的引用。

好的,让我们更改second的定义以接受对引用的引用:

fn second<'a, A, B: ?Sized>(&&(_, ref second): &&'a (A, B)) -> &'a B {
    second
}

编译器仍然不满意:

error[E0277]: the trait bound `for<'r> impl std::ops::FnMut<(&&(_, _),)>: std::ops::FnMut<(&'r &(&str, &std::ops::Fn(i32) -> bool),)>` is not satisfied
  --> <anon>:11:27
   |
11 |     let _ = tuples.iter().filter(f);
   |                           ^^^^^^ trait `for<'r> impl std::ops::FnMut<(&&(_, _),)>: std::ops::FnMut<(&'r &(&str, &std::ops::Fn(i32) -> bool),)>` not satisfied

error[E0271]: type mismatch resolving `for<'r> <impl std::ops::FnMut<(&&(_, _),)> as std::ops::FnOnce<(&'r &(&str, &std::ops::Fn(i32) -> bool),)>>::Output == bool`
  --> <anon>:11:27
   |
11 |     let _ = tuples.iter().filter(f);
   |                           ^^^^^^ expected bound lifetime parameter , found concrete lifetime
   |
   = note: concrete lifetime that was found is lifetime '_#24r

expected bound lifetime parameter , found concrete lifetime ......这是什么意思?

f的类型是实现FnMut(&'c &'b (&'a str, &Fn(i32) -> bool)) -> bool的某种类型。在致电applyB == &'c &'b (&'a str, &Fn(i32) -> bool)C == bool时。请注意,此处B 一个固定类型; 'c代表一个固定生命周期,称为具体生命周期

让我们来看看filter的签名:

fn filter<P>(self, predicate: P) -> Filter<Self, P> where
    Self: Sized, P: FnMut(&Self::Item) -> bool,

此处,P必须实施FnMut(&Self::Item) -> bool。实际上,这种语法是for<'r> FnMut(&'r Self::Item) -> bool的简写。这里。 'r是绑定生存期参数。

所以,问题是我们实现FnMut(&'c &'b (&'a str, &Fn(i32) -> bool)) -> bool的函数实现for<'r> FnMut(&'r Self::Item) -> bool。我们需要一个实现for<'c> FnMut(&'c &'b (&'a str, &Fn(i32) -> bool)) -> bool的函数。目前,唯一的方法是写apply,如下所示:

fn apply<A, B, C, F, G>(mut f: F, a: A) -> impl FnMut(&B) -> C
         where F: FnMut(&B) -> G,
               G: FnMut(A) -> C,
               A: Clone
{
    move |b| f(b)(a.clone())
}

或更明确的版本:

fn apply<A, B, C, F, G>(mut f: F, a: A) -> impl for<'r> FnMut(&'r B) -> C
         where F: for<'r> FnMut(&'r B) -> G,
               G: FnMut(A) -> C,
               A: Clone
{
    move |b| f(b)(a.clone())
}

如果Rust最终支持higher-kinded types,那么可能是解决此问题的更优雅方式。