gpt4 book ai didi

rust - 无法为生命周期参数克隆特征对象推断适当的生命周期

转载 作者:行者123 更新时间:2023-11-29 07:55:43 26 4
gpt4 key购买 nike

这个问题的重复似乎并没有为我解决问题。以下代码给我错误:

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);
}

在 Playground 上奔跑here .

我得到:

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 || {
| ^^^^^^^^^^^^^

我认为它告诉我它不能自动推断 thread_spiders 的生命周期,因为它需要是 'static 才能为线程存活足够长的时间,但它不能超过输入参数的生命周期 'a

问题是,我可以在 parallel_get 中克隆其他对象,它们会毫无问题地移动到新线程中。但出于某种原因,thread_spiders 似乎使编译器出错。如果我是正确的,它应该有 'a 的生命周期,然后进入线程闭包。

我已经尝试将显式生命周期参数添加到 parallel_get,但没有任何效果。我怎样才能编译这段代码?

最佳答案

在这种情况下,错误消息非常困惑,但请注意这里的双符号: (expected &&std::collections::HashMap<std::string::String, Box<Spider>>, found &&std::collections::HashMap<std::string::String, Box<Spider + 'static>>) .

看起来它试图克隆引用。我假设您想克隆整个 HashMap .打电话clone明确为 Clone::clone(spiders)给出更清晰的错误信息:

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`

您正在调用 Clone::cloneBox<Spider> ,一个拥有的特征对象。 How do I clone a HashMap containing a boxed trait object?说明它可以通过向您的特征引入克隆方法来实现,如下所示:

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?建议为这种多态克隆方法创建一个单独的特征。

关于rust - 无法为生命周期参数克隆特征对象推断适当的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41412031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com