如何返回一个通用的Map结构体? [英] How to return a generic Map struct?

查看:190
本文介绍了如何返回一个通用的Map结构体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何返回一个通用的 Map 结构,因为 Map 的声明已经改变。新声明是:

  pub struct Map< A,B,I:Iterator< A>,F:FnMut< ,),B> {
//一些字段省略
}

从函数返回其中一个:

  fn something< A:Clone,I:Iterator< A> :I) - >地图< A,(A,A),I,**这里是什么**> {
iter.map(| x |(x.clone(),x))
}


b $ b

我试过使用我使用的闭包的签名...

  fn something< A:克隆,I:迭代器>(iter:I) - >地图< A,(A,A),I,| A | - > (A,A)> {
iter.map(| x |(x.clone(),x))
}


b $ b

但是,然后铁锈抱怨,我需要一个明确的终身界限。我不知道为什么我需要这个,但我还是添加它:

  fn something<'a, I:迭代器>(iter:I) - >地图< A,(A,A),I,| A |:'a  - (A,A)> {
iter.map(| x |(x.clone(),x))
}


b $ b

但是我得到的错误: trait core :: ops :: Fn <(A,),(A,A)未实现类型'a | A |:'a - > (A,A)



我还没有进一步的进展,在这一点上,我只是尝试随机的东西。有人可以帮我理解如何使用新的 Map struct?

解决方案

你不能在 **中有什么有意义的东西** 。您定义的闭包有一个编译器生成的类型,您不能指定它(也就是说,直到抽象返回类型)。



由于你的闭包没有从它的环境中捕获任何东西,你可以用一个普通函数来替换它。

 使用std :: iter :: Map; 

fn duplicate< T:Clone>(a:T) - > (T,T){
(a.clone(),a)
}

fn something< A:Clone,I:Iterator& I) - >映射< A,(A,A),I,fn(A) (A,A)> {
iter.map(duplicate as fn(A) - >(A,A))//从PR#19891
开始需要转换
pre>

但是,如果你需要捕获一些状态,最好的(不诉诸拳击)定义自己的类型实现 FnMut 。 (这个例子有一个空的struct,因为没有状态; call_mut 可以通过 self 访问和改变结构的状态。

 #![feature(unboxed_closures)] 

use std :: iter :: Map;

struct复制;

impl< T:Clone> FnMut(T) - > (T,T)用于重复{
externrust-callfn call_mut(& mut self,(a,):(T,)) - > (T,T){
(a.clone(),a)
}
}

fn something< A:Clone,I:Iterator< A& >(iter:I) - >地图< A,(A,A),I,Duplicate> {
iter.map(Duplicate)
}


I'm not sure how return a generic Map struct now that the declaration for Map has changed. The new declaration is:

pub struct Map<A, B, I: Iterator<A>, F: FnMut<(A,), B>> {
    // some fields omitted
}

I'm not sure how to return one of these from a function:

fn something<A: Clone, I: Iterator<A>>(iter: I) -> Map<A, (A,A), I, **what goes here**> {
    iter.map(|x| (x.clone(), x))
}

I've tried using the signature of the closure I'm using...

fn something<A: Clone, I: Iterator<A>>(iter: I) -> Map<A, (A,A), I, |A| -> (A,A)> {
    iter.map(|x| (x.clone(), x))
}

But then rust complains that I need an explicit lifetime bound. I'm not sure why I need this, but I add it anyway:

fn something<'a, A: Clone, I: Iterator<A>>(iter: I) -> Map<A, (A,A), I, |A|:'a -> (A,A)> {
    iter.map(|x| (x.clone(), x))
}

But then I get the error: the trait core::ops::Fn<(A,), (A, A)> is not implemented for the type 'a |A|:'a -> (A, A)

I haven't been able to progress any further, and at this point I'm just trying random things. Can someone help me understand how to use the new Map struct?

解决方案

You can't put anything meaningful in **what goes here**. The closure you define has a compiler-generated type that you can't name (that is, until abstract return types are implemented).

Since your closure doesn't capture anything from its environment, you can replace it with a normal function.

use std::iter::Map;

fn duplicate<T: Clone>(a: T) -> (T, T) {
    (a.clone(), a)
}

fn something<A: Clone, I: Iterator<A>>(iter: I) -> Map<A, (A,A), I, fn(A) -> (A, A)> {
    iter.map(duplicate as fn(A) -> (A, A)) // cast required since PR #19891
}

However, if you do need to capture some state, the best you can do (without resorting to boxing) for the moment is to define your own type that implements FnMut. (This example has an empty struct because there's no state; call_mut could access and alter the struct's state through self.)

#![feature(unboxed_closures)]

use std::iter::Map;

struct Duplicate;

impl<T: Clone> FnMut(T) -> (T, T) for Duplicate {
    extern "rust-call" fn call_mut(&mut self, (a,): (T,)) -> (T, T) {
        (a.clone(), a)
    }
}

fn something<A: Clone, I: Iterator<A>>(iter: I) -> Map<A, (A,A), I, Duplicate> {
    iter.map(Duplicate)
}

这篇关于如何返回一个通用的Map结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆