【问题标题】:Store lambda returning iterator in the struct将 lambda 返回迭代器存储在结构中
【发布时间】:2019-06-23 10:33:44
【问题描述】:

我想将返回迭代器的 lambda 存储在结构中。 需要 lambda 是因为并非所有容器都实现 iter() 函数(例如 String::chars()),所以我需要一种通用的方法来从容器中获取迭代器。

use std::marker::PhantomData;

struct Foo<F, V, T> {
  foo: F,
  ph: PhantomData<V>,
  ph2: PhantomData<T>,
}

impl<F, V, T> Foo<F, V, T> where
  F: Fn(V) -> dyn Iterator<Item = T> {
}

很遗憾,我收到以下错误:

error[E0277]: the size for values of type `(dyn std::iter::Iterator<Item=T> + 'static)` cannot be known at compilation time
  --> main.rs:9:1
   |
9  | / impl<F, V, T> Foo<F, V, T> where
10 | |   F: Fn(V) -> dyn Iterator<Item = T>
11 | | {
12 | |
13 | | }
   | |_^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `(dyn std::iter::Iterator<Item=T> + 'static)`
   = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required by `std::ops::FnOnce`

我希望我理解它的含义,但我不知道如何解决它。

【问题讨论】:

    标签: lambda rust iterator trait-objects


    【解决方案1】:

    特征对象,如dyn Iterator&lt;Item = T&gt;,在编译时没有已知的大小。这样做的一个后果是函数不能返回“原始”特征对象 - 编译器需要提前知道在调用函数时要在堆栈上保留多少空间。

    为了有一个已知的大小,将 trait 对象包装在引用或智能指针中。例如,Box:

    impl<F, V, T> Foo<F, V, T>
    where
        F: Fn(V) -> Box<dyn Iterator<Item = T>>,
    {
    
    }
    

    【讨论】:

    • 顺便问一下,有没有办法避免“PhantomData”结构成员,或者没关系,我不需要担心他们?
    • @Alex 您可能需要 PhantomData,不幸的是,尽管您可以通过将它们组合成一个来使其更简洁:PhantomData&lt;(T, V)&gt;。当您有更多实际使用这些类型的代码时,您可能会发现它们受到了足够的约束并且不再需要 PhantomData
    猜你喜欢
    • 2022-06-30
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    相关资源
    最近更新 更多