我希望将对Rusoto SQS客户机的引用传递给WebSocket服务器结构,以将消息推送到SQS队列中。
为此,我有一个简单的结构,如下所示:
struct MyHandler {
sqsclient: &SqsClient<ProvideAwsCredentials, DispatchSignedRequest>,
}这会产生以下错误:
error[E0106]: missing lifetime specifier
--> src/main.rs:29:13
|
29 | sqsclient: &SqsClient<ProvideAwsCredentials, DispatchSignedRequest>,
| ^ expected lifetime parameter我尝试了各种类型签名和生命周期技巧,但都在不同程度上失败了,但我总是遇到相同的错误:
error[E0277]: the trait bound `rusoto_core::ProvideAwsCredentials + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:29:2
|
29 | sqsclient: &'static SqsClient<ProvideAwsCredentials, DispatchSignedRequest>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `rusoto_core::ProvideAwsCredentials + 'static` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `rusoto_core::ProvideAwsCredentials + 'static`
= note: required by `rusoto_sqs::SqsClient`
error[E0277]: the trait bound `rusoto_core::DispatchSignedRequest + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:29:2
|
29 | sqsclient: &'static SqsClient<ProvideAwsCredentials, DispatchSignedRequest>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `rusoto_core::DispatchSignedRequest + 'static` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `rusoto_core::DispatchSignedRequest + 'static`
= note: required by `rusoto_sqs::SqsClient`我也尝试过用Rc和Box包装它,但都无济于事。不过,我觉得我看错了地方。
当给MyHandler一个<'a>生命周期时,也会发生这种情况。我在这里对Rust类型系统有什么误解,我可以做些什么才能将对SqsClient<...> (以及最终来自Rusoto的其他东西)的引用传递到我自己的结构中?如果知道我在上面尝试做的是不是惯用的Rust,这将是很有用的。如果不是,我应该使用什么模式呢?
这是How do I pass a struct with type parameters as a function argument?的后续文章。
发布于 2017-08-04 16:32:00
解决了!DispatchSignedRequest和ProvideAwsCredentials (来自Rusoto)是特征。我需要使用这些特征的impl,也就是结构,所以现在代码看起来像这样:
extern crate rusoto_core;
extern crate hyper;
use hyper::Client;
use rusoto_sqs::{ Sqs, SqsClient };
use rusoto_core::{ DefaultCredentialsProvider };
struct MyHandler<'a> {
sqsclient: &'a SqsClient<DefaultCredentialsProvider, Client>,
}
impl<'a> Handler for MyHandler<'a> {
// ...
}DefaultCredentialsProvider和Client (来自Hyper)都是结构,所以现在这段代码可以很好地编译。
我在这里使用的是Hyper 0.10。Hyper 0.11要求Client使用不同类型的签名。
https://stackoverflow.com/questions/45500412
复制相似问题