hello everyone I have this code:
impl FetchJob
{
pub fn fetch_chunk<I>(chunk: usize, iter: Arc<Mutex<I>>) -> impl Future<Item = bool, Error = ()>
where
I: Iterator<Item = hyper::Uri>
{
//let client = build_https_client().unwrap(); //TODO make this at initialization to not remake the client at every interval.
let client = Client::new();
let mut requests: Vec<_> = Vec::new();
for (uri, count) in &*iter.lock().expect("error") {
requests.push(client
.clone()
.get(uri)
.map(|res| {
println!("Response: {}", res.status());
println!("Headers: {:#?}", res.headers());
})
.map_err(|e| println!("{:#?}", e)));
}
future::join_all(requests)
.map(move |res: Vec<()>| res.len() == chunk)
.map_err(|_| ())
}
}
struct FetchStream<I> {
uri_iter: Arc<Mutex<I>>,
interval: Interval,
time_pass: TimePass,
fetch_job: FetchJob,
fetch_future: Option<Box<dyn Future<Item = bool, Error = ()> + Send>>,
}
impl<I> FetchStream<I> {
pub fn new(chunk: usize, interval: std::time::Duration, iter: I ) -> Self {
Self {
uri_iter: Arc::new(Mutex::new(iter)),
interval: Interval::new_interval(interval),
time_pass: TimePass::Ready,
fetch_job: FetchJob {
chunk
},
fetch_future: None
}
}
}
impl<I> Future for FetchStream<I>
where
I: Iterator<Item = hyper::Uri> + 'static,
{
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<Self::Item, ()> {
loop {
match self.time_pass {
TimePass::Ready => {
if let Some(_) = self.fetch_future {
let res: bool = try_ready!(self.fetch_future.poll().map_err(|_| ())).unwrap();
if !res {
return Ok(Async::Ready(()))
}
} else {
let iter = self.uri_iter.clone();
self.fetch_future: Option<Box<dyn Future<Item = bool, Error = ()> + Send>> =
Some(Box::new(FetchJob::fetch_chunk(23, iter)));
but it gives me:
error[E0277]: `&I` is not an iterator
--> src/fetch/fetch_stream.rs:86:29
|
86 | for (uri, count) in &*iter.lock().expect("error") {
| -^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `&I` is not an iterator
| help: consider removing 1 leading `&`-references
|
= help: the trait `std::iter::Iterator` is not implemented for `&I`
= note: required by `std::iter::IntoIterator::into_iter`
but even if I take off the & I get:
error[E0507]: cannot move out of borrowed content
--> src/fetch/fetch_stream.rs:85:20
|
85 | for uri in *iter.lock().expect("error") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot move out of borrowed content
I have a Arc<mutex<>> around it so I don't see why it is complaining about it.