Sometimes when using rust-postgres-binary-copy, which takes values as a streaming iterator, I want to use a slice of borrowed ToSql
values. I might not own these values, so I can't give ownership to a streaming iterator in the normal way.
However, since a streaming iterator yields references to the iterator's item anyway, it should be possible to use these APIs without having to provide ownership to the iterator. I've been working around this by implementing the StreamingIterator
trait on top of an existing Iterator
of references:
use streaming_iterator::StreamingIterator;
pub struct FakeStreamingIterator<'a, I, T>
where I: Iterator<Item=&'a &'a T>, T: 'a + ?Sized {
inner: Box<I>,
current: Option<&'a &'a T>,
}
impl<'a, I, T> FakeStreamingIterator<'a, I, T>
where I: Iterator<Item=&'a &'a T>, T: ?Sized {
fn new(inner: I) -> FakeStreamingIterator<'a, I, T> {
FakeStreamingIterator { inner: Box::new(inner), current: None }
}
}
impl<'a, I, T> StreamingIterator for FakeStreamingIterator<'a, I, T>
where I: Iterator<Item=&'a &'a T>, T: ?Sized {
type Item = T;
fn advance(&mut self) { self.current = self.inner.next(); }
fn get(&self) -> Option<&T> { self.current.map(|x| *x) }
}
pub fn convert<'a, I, T>(iterator: I) -> FakeStreamingIterator<'a, I, T>
where I: Iterator<Item=&'a &'a T>, T: ?Sized {
FakeStreamingIterator::new(iterator)
}
This then allows me to construct a FakeStreamingIterator
from a slice of references, and treat it as a normal StreamingIterator
. However, it's a bit of a hack, so I'm curious what the officially supported way to handle this case is — maybe something like my FakeStreamingIterator
should be included to support this use case?