首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >特征声明中的类型构造函数

特征声明中的类型构造函数
EN

Stack Overflow用户
提问于 2017-08-04 15:14:28
回答 1查看 230关注 0票数 1

我希望将对Rusoto SQS客户机的引用传递给WebSocket服务器结构,以将消息推送到SQS队列中。

为此,我有一个简单的结构,如下所示:

代码语言:javascript
复制
struct MyHandler {
    sqsclient: &SqsClient<ProvideAwsCredentials, DispatchSignedRequest>,
}

这会产生以下错误:

代码语言:javascript
复制
error[E0106]: missing lifetime specifier
  --> src/main.rs:29:13
   |
29 |    sqsclient: &SqsClient<ProvideAwsCredentials, DispatchSignedRequest>,
   |               ^ expected lifetime parameter

我尝试了各种类型签名和生命周期技巧,但都在不同程度上失败了,但我总是遇到相同的错误:

代码语言:javascript
复制
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`

我也尝试过用RcBox包装它,但都无济于事。不过,我觉得我看错了地方。

当给MyHandler一个<'a>生命周期时,也会发生这种情况。我在这里对Rust类型系统有什么误解,我可以做些什么才能将对SqsClient<...> (以及最终来自Rusoto的其他东西)的引用传递到我自己的结构中?如果知道我在上面尝试做的是不是惯用的Rust,这将是很有用的。如果不是,我应该使用什么模式呢?

这是How do I pass a struct with type parameters as a function argument?的后续文章。

EN

回答 1

Stack Overflow用户

发布于 2017-08-04 16:32:00

解决了!DispatchSignedRequestProvideAwsCredentials (来自Rusoto)是特征。我需要使用这些特征的impl,也就是结构,所以现在代码看起来像这样:

代码语言:javascript
复制
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)都是结构,所以现在这段代码可以很好地编译。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45500412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档