0

I want to store the lambda that will return iterator in the struct. The lambda is needed because not all the containers implement iter() function (e.g. String::chars()), so I need a generic way to get iterator from container.

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> {
}

Unfortunately, I get the following error:

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`

I hope I understand its meaning, but I have no idea how to fix it.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Alex
  • 9,891
  • 11
  • 53
  • 87

1 Answers1

1

A trait object, like dyn Iterator<Item = T>, doesn't have a size known at compile-time. One consequence of this is that a "raw" trait object cannot be returned by a function - the compiler needs to know in advance how much space to reserve on the stack when the function is called.

In order to have a known size, wrap the trait object in a reference or smart pointer. For example, a Box:

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

}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • By the way, Is there a way to avoid "PhantomData" struct members, or it is OK and I don't need to worry on them? – Alex Jun 23 '19 at 11:01
  • 1
    @Alex you probably need the PhantomData, unfortunately, although you can make it more concise by combining them into one: `PhantomData<(T, V)>`. When you have more code, that actually uses those types, you _may_ find that they are constrained enough and the `PhantomData` is no longer necessary. – Peter Hall Jun 23 '19 at 11:07