4

The duplicates of this question don't seem to solve things for me. The following code gives me errors:

use std::collections::HashMap;
use std::thread;


pub trait Spider : Sync + Send {
    fn add_request_headers(&self, headers: &mut Vec<String>);
}

pub struct Google {}

impl Spider for Google {
    fn add_request_headers(&self, headers: &mut Vec<String>) {
        headers.push("Hello".to_string())
    }
}

fn parallel_get(spiders: &HashMap<String, Box<Spider>>) -> std::thread::JoinHandle<()> {
    let thread_spiders = spiders.clone();
    thread::spawn(move || {
        let headers = &mut vec![];
        let spider = thread_spiders.get("Google").unwrap();
        spider.add_request_headers(headers);
    })
}

fn main() {
    let spiders = HashMap::new();
    let spider = Box::new(Google{});
    spiders.insert("Google", spider);
}

Run on the playground here.

I get:

rustc 1.14.0 (e8a012324 2016-12-16)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> <anon>:18:34
   |
18 |     let thread_spiders = spiders.clone();
   |                                  ^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the block at 17:87...
  --> <anon>:17:88
   |
17 | fn parallel_get(spiders: &HashMap<String, Box<Spider>>) -> std::thread::JoinHandle<()> {
   |                                                                                        ^
note: ...so that types are compatible (expected &&std::collections::HashMap<std::string::String, Box<Spider>>, found &&std::collections::HashMap<std::string::String, Box<Spider + 'static>>)
  --> <anon>:18:34
   |
18 |     let thread_spiders = spiders.clone();
   |                                  ^^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@<anon>:19:19: 23:6 thread_spiders:&std::collections::HashMap<std::string::String, Box<Spider>>]` will meet its required lifetime bounds
  --> <anon>:19:5
   |
19 |     thread::spawn(move || {
   |     ^^^^^^^^^^^^^

I think it's telling me that it can't automatically infer the lifetime of thread_spiders because it needs to be 'static to live long enough for the thread, but it can't outlive 'a which is the lifetime of the input parameter.

The thing is, I can clone other objects in parallel_get and they get moved into the new thread without issue. But for some reason thread_spiders seems to trip up the compiler. It should have a lifetime of 'a if I'm correct and then get moved into the thread closure.

I've tried adding explicit lifetime parameters to parallel_get but haven't been able to get anything working. How can I make this code compile?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
jbrown
  • 7,518
  • 16
  • 69
  • 117
  • Please **link to** the duplicates you have read and identified as not helping, otherwise we are likely to just repeat them when answering your question, then no one is helped. – Shepmaster Dec 31 '16 at 22:19
  • For example, [How do I clone a HashMap containing a boxed trait object?](http://stackoverflow.com/q/39120183/155423) seems to match quite closely with your actual code. – Shepmaster Dec 31 '16 at 22:28

1 Answers1

-1

The error message is quite confusing in this case, but notice a double ampersand here: (expected &&std::collections::HashMap<std::string::String, Box<Spider>>, found &&std::collections::HashMap<std::string::String, Box<Spider + 'static>>).

It looks like it tries to clone the reference. I assume you wanted to clone the entire HashMap. Calling clone explicitly as Clone::clone(spiders) gives much clearer error message:

error[E0277]: the trait bound `Spider: std::clone::Clone` is not satisfied
error[E0277]: the trait bound `Spider: std::marker::Sized` is not satisfied
  --> error_orig.rs:19:26
   |
19 |     let thread_spiders = Clone::clone(spiders);
   |                          ^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `Spider`
   |
   = note: `Spider` does not have a constant size known at compile-time
   = note: required because of the requirements on the impl of `std::clone::Clone` for `Box<Spider>`
   = note: required because of the requirements on the impl of `std::clone::Clone` for `std::collections::HashMap<std::string::String, Box<Spider>>`
   = note: required by `std::clone::Clone::clone`

You are calling Clone::clone on Box<Spider>, an owned trait object. How do I clone a HashMap containing a boxed trait object? illustrates that it can be implemented by introducing a cloning method to your trait, like so:

pub trait Spider: Sync + Send {
    fn add_request_headers(&self, headers: &mut Vec<String>);
    fn clone_into_box(&self) -> Box<Spider>;
}

impl Clone for Box<Spider> {
    fn clone(&self) -> Self {
        self.clone_into_box()
    }
}

#[derive(Clone)]
pub struct Google {}

impl Spider for Google {
    fn add_request_headers(&self, headers: &mut Vec<String>) {
        headers.push("Hello".to_string())
    }

    fn clone_into_box(&self) -> Box<Spider> {
        Box::new(self.clone())
    }
}

How to clone a struct storing a trait object? suggests creating a separate trait for this polymorphic cloning method.

Community
  • 1
  • 1
sawyer
  • 473
  • 4
  • 15