我想将对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?的后续跟进。
答案 0 :(得分:2)
解决了! DispatchSignedRequest
和ProvideAwsCredentials
(来自Rusoto)是 traits 。我需要使用那些特征的impl
s ,即结构,所以现在代码看起来像这样:
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
的不同类型签名。