Alex
Alex

Reputation: 10126

Store lambda returning iterator in the struct

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.

Upvotes: 0

Views: 525

Answers (1)

Peter Hall
Peter Hall

Reputation: 58815

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

}

Upvotes: 1

Related Questions