Error: implementation of `std::iter::Iterator` is not general enough

Hi, this snippet fails with the error:

use futures; // 0.3.4
use futures::stream::{StreamExt}; 

async fn test() {
    let data = std::sync::Arc::new(vec![1i32,2,3,4]);
    
    tokio::spawn(async move {
        futures::stream::iter(data.iter())
            .map(|_| async move {
                12i32
            })
            .buffer_unordered(32usize)
            .collect::<Vec<_>>()
            .await;
    });
}

here is the error:

error: implementation of `std::iter::Iterator` is not general enough
    --> src/lib.rs:7:5
     |
7    |       tokio::spawn(async move {
     |       ^^^^^^^^^^^^ implementation of `std::iter::Iterator` is not general enough
     |
     = note: `std::iter::Iterator` would have to be implemented for the type `std::slice::Iter<'0, i32>`, for any lifetime `'0`...
     = note: ...but `std::iter::Iterator` is actually implemented for the type `std::slice::Iter<'1, i32>`, for some specific lifetime `'1`

Please anyone explain me whats happening there, I found workaround for that using copied:

use futures; // 0.3.4
use futures::stream::{StreamExt}; 

async fn test() {
    let data = std::sync::Arc::new(vec![1i32,2,3,4]);
    
    tokio::spawn(async move {
        futures::stream::iter(data.iter().copied())
            .map(|_| async move {
                12i32
            })
            .buffer_unordered(32usize)
            .collect::<Vec<_>>()
            .await;
    });
}

But I am not quite understand why this error happen?

Wow, that's a poor error message. It should probably be accepted. This compiles:

use futures::stream::{Stream, StreamExt};
use std::future::Future;

fn make_stream<'a>(i: std::slice::Iter<'a, i32>) -> impl Future<Output = Vec<i32>> + 'a {
    futures::stream::iter(i)
        .map(|_| async move { 12i32 })
        .buffer_unordered(32usize)
        .collect::<Vec<i32>>()
}

async fn test() {
    tokio::spawn(async move {
        let data = vec![1i32, 2, 3, 4];
        make_stream(data.iter()).await;
    });
}
3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.