How could I convert pinned Iterator to Stream?

There are some iterators, but they are !Unpin. Like this:

struct Iter(PhantomPinned);

impl Iterator for Pin<&mut Iter> {
    type Item = ();
    fn next(&mut self) -> Option<Self::Item> { None }
}

Now, I want to create a struct that wraps them, in order to implement Stream.

struct PinIterStream<'a, A> {
    iter: A,
    phantom: PhantomData<&'a ()>,
}

impl<'a, A: 'a> Stream for PinIterStream<'a, A>
where
    Pin<&'a mut A>: Iterator,
{ ... }

But there are some error in my implementation, and I don't know how to solve it (except with std::mem::transmute). The complete code is here.

If this conversion is feasible, is there a simple method like futures::stream::iter to do it.

use futures::stream::Stream;
use futures::task::Context;
use futures::task::Poll;
use pin_project::pin_project;
use std::marker::PhantomPinned;
use std::pin::Pin;

struct Iter {
    _pin: PhantomPinned,
}

impl Iterator for Pin<&mut Iter> {
    type Item = ();

    fn next(&mut self) -> Option<Self::Item> {
        None
    }
}

#[pin_project]
struct PinIterStream<A> {
    #[pin]
    iter: A,
}

impl<A, T> Stream for PinIterStream<A>
where
    for<'a> Pin<&'a mut A>: Iterator<Item = T>,
{
    type Item = T;

    fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Poll::Ready(self.project().iter.next())
    }
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.