2017-08-04 72 views
1

我想将对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` 

我也试过wr应用它在RcBox无济于事。虽然我觉得我在这些地方看错了地方。

这种情况发生在给予MyHandler<'a>寿命。我对这里的Rust类型系统有什么误解?我能做些什么来将SqsClient<...>(以及其他来自Rusoto的其他内容)的引用传递给我自己的结构?知道我上面试图做的是惯用的Rust会很有用。如果不是,我应该使用什么样的模式呢?

这是从How do I pass a struct with type parameters as a function argument?的后续行动。

+0

试试:'MyHandler的结构{<'a> sqsclient:&'一个SqsClient , }' –

+0

@TiborBenke同样的错误不幸。我试过''静态'也无济于事。 – Bojangles

回答

2

解决了! DispatchSignedRequestProvideAwsCredentials(来自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> { 
    // ... 
} 

DefaultCredentialsProviderClient(从超)都是结构,所以现在这个代码编译罚款。

我在这里使用Hyper 0.10。 Hyper 0.11需要Client的不同类型签名。