2017-08-04 59 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的不同類型簽名。

相關問題