gpt4 book ai didi

rust - tokio::run() 和发送标记的编译错误

转载 作者:行者123 更新时间:2023-11-29 08:20:25 24 4
gpt4 key购买 nike

我正在尝试构建一个定义通用数据源的库,该数据源可以同步和异步地从各种来源提取数据。在构建异步部分时,我遇到了以下我不知道如何解决的编译问题:

这是我的简化代码(playground link)

extern crate futures; // futures = "0.1.24"
extern crate tokio; // tokio = "0.1.8"
extern crate serde_json;

use futures::Future;
use serde_json::Value;

use std::collections::HashMap;

trait DataSource {
type Data;

fn read_async(&self, Option<HashMap<String, Value>>) -> Box<futures::Future<Item=Option<Self::Data>, Error=String>>
where Self::Data: 'static + Send;
}

struct DataSourceImpl;
impl DataSource for DataSourceImpl {
type Data = Vec<String>;

fn read_async(&self, _params: Option<HashMap<String, Value>>) -> Box<futures::Future<Item=Option<Self::Data>, Error=String>>
where Self::Data: 'static + Send
{
Box::new(futures::future::ok(Some(vec!["some data".to_string()])))
}

}

fn main() {
let datasource = DataSourceImpl{};

let params = HashMap::new();
tokio::run(datasource.read_async(Some(params))
.map(|content| {
println!("Content read = {:?}", &content);
()
})
.map_err(|err| {
println!("Error {}", &err);
()
})
);
}

我得到以下编译错误:

error[E0277]: `dyn futures::Future<Item=std::option::Option<std::vec::Vec<std::string::String>>, Error=std::string::String>` cannot be sent between threads safely
--> src/main.rs:45:13
|
45 | runtime.spawn(future);
| ^^^^^ `dyn futures::Future<Item=std::option::Option<std::vec::Vec<std::string::String>>, Error=std::string::String>` cannot be sent between threads safely
|
= help: the trait `std::marker::Send` is not implemented for `dyn futures::Future<Item=std::option::Option<std::vec::Vec<std::string::String>>, Error=std::string::String>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<dyn futures::Future<Item=std::option::Option<std::vec::Vec<std::string::String>>, Error=std::string::String>>`
= note: required because it appears within the type `std::boxed::Box<dyn futures::Future<Item=std::option::Option<std::vec::Vec<std::string::String>>, Error=std::string::String>>`
= note: required because it appears within the type `futures::Map<std::boxed::Box<dyn futures::Future<Item=std::option::Option<std::vec::Vec<std::string::String>>, Error=std::string::String>>, [closure@src/main.rs:34:14: 37:10]>`
= note: required because it appears within the type `futures::MapErr<futures::Map<std::boxed::Box<dyn futures::Future<Item=std::option::Option<std::vec::Vec<std::string::String>>, Error=std::string::String>>, [closure@src/main.rs:34:14: 37:10]>, [closure@src/main.rs:38:18: 41:10]>`

然而,在查看标准库时,我发现了以下实现:

  • impl<T: ?Sized> Send for Box<T> where T: Send
  • impl<T> Send for Option<T> where T: Send
  • impl<T> Send for Vec<T> where T: Send
  • impl Send for String
  • impl Send for [failure::]Error

我错过了什么?

如果我去掉这个特征并替换 Box<Future<...>>通过 impl Future<...>然后它起作用了(playground link for the new code);但我不明白这个特征有什么问题 & Box实现...

extern crate failure;
extern crate futures; // futures = "0.1.24"
extern crate tokio; // tokio = "0.1.8"
extern crate serde_json;

use futures::Future;
use serde_json::Value;

use std::collections::HashMap;

fn read_async(_params: Option<HashMap<String, Value>>) -> impl futures::Future<Item=Option<Vec<String>>, Error=failure::Error> {
futures::future::ok(Some(vec!["some data".to_string()]))
}

fn main() {
let params = HashMap::new();
let future = read_async(Some(params))
.map(|content| {
println!("Content read = {:?}", &content);
()
})
.map_err(|err| {
println!("Error {}", &err);
()
});

tokio::run(future);
}

最佳答案

看起来我只需要将函数签名更改为

fn read_async(
&self,
_: Option<HashMap<String, Value>>,
) -> Box<Future<Item = Option<Self::Data>, Error = String> + Send> {
// ^^^^^^^

确实,Box<T>应该是 Send什么时候TSend , 但我必须拼写出来因为 Future不会手动/自动导出它。

感谢Tobz感谢您指出这一点。

关于rust - tokio::run() 和发送标记的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539464/

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