实现 actix-web 的处理程序时找不到关联类型“上下文”
associated type `Context` not found when implementing actix-web's Handler
我有以下基于 actix-web Database Integration sample 的代码。
extern crate actix;
extern crate actix_web;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate r2d2;
extern crate r2d2_mysql;
use actix::prelude::*;
use actix_web::{middleware::Logger, server, App, FutureResponse, HttpRequest, HttpResponse};
mod dbservices {
use actix::prelude::*;
use actix_web::dev::Handler;
use model::DataDictionary;
use r2d2::Pool;
use r2d2_mysql::*;
use std::io;
pub struct MysqlConnection {
db_pool: mysql::PooledConn,
}
impl Actor for MysqlConnection {
type Context = SyncContext<Self>;
}
impl MysqlConnection {
pub fn new(db_url: &str) -> MysqlConnection {
unimplemented!();
}
}
pub struct GetDD;
impl Message for GetDD {
type Result = io::Result<Vec<DataDictionary>>;
}
impl Handler<GetDD> for MysqlConnection {
type Result = io::Result<Vec<DataDictionary>>;
fn handle(&mut self, msg: GetDD, _: &mut Self::Context) -> Self::Result {
unimplemented!();
}
}
}
mod model {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataDictionary {
id: i32,
name: String,
}
}
struct State {
db: Addr<MysqlConnection>,
}
fn get_dd(req: HttpRequest<State>) -> FutureResponse<HttpResponse> {
req.clone()
.state()
.db
.send(GetDD)
.from_err()
.and_then(|result| Ok.json(result))
.responder()
}
fn main() {
std::env::set_var("RUST_LOG", "actix_web=debug,info");
const db_url: str = "mysql://connstring";
let addr = SyncArbiter::start(3, move || dbservices::MysqlConnection::new(db_url));
server::new(|| {
App::new()
.middleware(Logger::default())
.middleware(Logger::new("%a %{User-Agent}i"))
.prefix("/api")
.scope("/dd", |dp_scope| {
dp_scope.resource("/", |r| r.h(dbservices::GetDD()))
})
}).bind("127.0.0.1:8088")
.unwrap()
.run();
}
编译时出现以下错误。我不确定我做错了什么:
error[E0220]: associated type `Context` not found for `Self`
--> src/main.rs:43:50
|
43 | fn handle(&mut self, msg: GetDD, _: &mut Self::Context) -> Self::Result {
| ^^^^^^^^^^^^^ associated type `Context` not found
这是我的依赖项,来自 Cargo.toml:
[dependencies]
actix-web = "0.6.14"
actix = "0.6.1"
chrono = { version = "0.4.2", features = ["serde"] }
serde = "1.0.60"
serde_derive = "1.0.60"
serde_json = "1.0.17"
log = "0.4.1"
env_logger ="0.5.10"
futures = "0.2.1"
r2d2 = "*"
[dependencies.r2d2_mysql]
git = "https://github.com/outersky/r2d2-mysql"
version="*"
您可以将 Self::Context
替换为 SyncContext<Self>
这实际上很奇怪,因为只有一个关联类型 Context
但出于某种原因,Rust 希望您指定特定的关联类型:<Self as Actor>::Context
不需要,因为您只有一个 Context
类型
是否有任何其他错误阻止您的结构编译?
创建一个最小 MCVE 几乎总能使问题更容易发现:
extern crate actix;
extern crate actix_web;
use actix::prelude::*;
use actix_web::dev::Handler;
use std::io;
pub struct MysqlConnection;
impl Actor for MysqlConnection {
type Context = SyncContext<Self>;
}
pub struct DummyMessage;
impl Message for DummyMessage {
type Result = io::Result<String>;
}
impl Handler<DummyMessage> for MysqlConnection {
type Result = io::Result<String>;
fn handle(&mut self, _: DummyMessage, _: &mut Self::Context) -> Self::Result {
unimplemented!();
}
}
fn main() {}
error[E0220]: associated type `Context` not found for `Self`
--> src/main.rs:22:53
|
22 | fn handle(&mut self, _: DummyMessage, _: &mut Self::Context) -> Self::Result {
| ^^^^^^^^^^^^^ associated type `Context` not found
问题 1 — 多个版本
actix-web = "0.6.14"
actix = "0.6.1"
这引入了两个不同版本的 actix:
$ cargo tree -d
actix v0.5.8
└── actix-web v0.6.14
└── repro v0.1.0 (file:///private/tmp/repro)
actix v0.6.1
└── repro v0.1.0 (file:///private/tmp/repro)
选一个,actix 0.5.8。
另请参阅:
问题 2 — 错误的特征
pub trait Handler<S>: 'static {
type Result: Responder;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result;
}
您正在实施 actix::Handler
:
pub trait Handler<M>
where
Self: Actor,
M: Message,
{
type Result: MessageResponse<Self, M>;
fn handle(&mut self, msg: M, ctx: &mut Self::Context) -> Self::Result;
}
actix::Handler
有 Actor
作为它的超特性,这意味着它可以访问关联的类型 Context
。 actix_web::dev::Handler
没有超级特质,所以它不知道 Self::Context
。
选择合适的特征并正确实施。
我有以下基于 actix-web Database Integration sample 的代码。
extern crate actix;
extern crate actix_web;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate r2d2;
extern crate r2d2_mysql;
use actix::prelude::*;
use actix_web::{middleware::Logger, server, App, FutureResponse, HttpRequest, HttpResponse};
mod dbservices {
use actix::prelude::*;
use actix_web::dev::Handler;
use model::DataDictionary;
use r2d2::Pool;
use r2d2_mysql::*;
use std::io;
pub struct MysqlConnection {
db_pool: mysql::PooledConn,
}
impl Actor for MysqlConnection {
type Context = SyncContext<Self>;
}
impl MysqlConnection {
pub fn new(db_url: &str) -> MysqlConnection {
unimplemented!();
}
}
pub struct GetDD;
impl Message for GetDD {
type Result = io::Result<Vec<DataDictionary>>;
}
impl Handler<GetDD> for MysqlConnection {
type Result = io::Result<Vec<DataDictionary>>;
fn handle(&mut self, msg: GetDD, _: &mut Self::Context) -> Self::Result {
unimplemented!();
}
}
}
mod model {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataDictionary {
id: i32,
name: String,
}
}
struct State {
db: Addr<MysqlConnection>,
}
fn get_dd(req: HttpRequest<State>) -> FutureResponse<HttpResponse> {
req.clone()
.state()
.db
.send(GetDD)
.from_err()
.and_then(|result| Ok.json(result))
.responder()
}
fn main() {
std::env::set_var("RUST_LOG", "actix_web=debug,info");
const db_url: str = "mysql://connstring";
let addr = SyncArbiter::start(3, move || dbservices::MysqlConnection::new(db_url));
server::new(|| {
App::new()
.middleware(Logger::default())
.middleware(Logger::new("%a %{User-Agent}i"))
.prefix("/api")
.scope("/dd", |dp_scope| {
dp_scope.resource("/", |r| r.h(dbservices::GetDD()))
})
}).bind("127.0.0.1:8088")
.unwrap()
.run();
}
编译时出现以下错误。我不确定我做错了什么:
error[E0220]: associated type `Context` not found for `Self`
--> src/main.rs:43:50
|
43 | fn handle(&mut self, msg: GetDD, _: &mut Self::Context) -> Self::Result {
| ^^^^^^^^^^^^^ associated type `Context` not found
这是我的依赖项,来自 Cargo.toml:
[dependencies]
actix-web = "0.6.14"
actix = "0.6.1"
chrono = { version = "0.4.2", features = ["serde"] }
serde = "1.0.60"
serde_derive = "1.0.60"
serde_json = "1.0.17"
log = "0.4.1"
env_logger ="0.5.10"
futures = "0.2.1"
r2d2 = "*"
[dependencies.r2d2_mysql]
git = "https://github.com/outersky/r2d2-mysql"
version="*"
您可以将 Self::Context
替换为 SyncContext<Self>
这实际上很奇怪,因为只有一个关联类型 Context
但出于某种原因,Rust 希望您指定特定的关联类型:<Self as Actor>::Context
不需要,因为您只有一个 Context
类型
是否有任何其他错误阻止您的结构编译?
创建一个最小 MCVE 几乎总能使问题更容易发现:
extern crate actix;
extern crate actix_web;
use actix::prelude::*;
use actix_web::dev::Handler;
use std::io;
pub struct MysqlConnection;
impl Actor for MysqlConnection {
type Context = SyncContext<Self>;
}
pub struct DummyMessage;
impl Message for DummyMessage {
type Result = io::Result<String>;
}
impl Handler<DummyMessage> for MysqlConnection {
type Result = io::Result<String>;
fn handle(&mut self, _: DummyMessage, _: &mut Self::Context) -> Self::Result {
unimplemented!();
}
}
fn main() {}
error[E0220]: associated type `Context` not found for `Self`
--> src/main.rs:22:53
|
22 | fn handle(&mut self, _: DummyMessage, _: &mut Self::Context) -> Self::Result {
| ^^^^^^^^^^^^^ associated type `Context` not found
问题 1 — 多个版本
actix-web = "0.6.14"
actix = "0.6.1"
这引入了两个不同版本的 actix:
$ cargo tree -d
actix v0.5.8
└── actix-web v0.6.14
└── repro v0.1.0 (file:///private/tmp/repro)
actix v0.6.1
└── repro v0.1.0 (file:///private/tmp/repro)
选一个,actix 0.5.8。
另请参阅:
问题 2 — 错误的特征
pub trait Handler<S>: 'static {
type Result: Responder;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result;
}
您正在实施 actix::Handler
:
pub trait Handler<M>
where
Self: Actor,
M: Message,
{
type Result: MessageResponse<Self, M>;
fn handle(&mut self, msg: M, ctx: &mut Self::Context) -> Self::Result;
}
actix::Handler
有 Actor
作为它的超特性,这意味着它可以访问关联的类型 Context
。 actix_web::dev::Handler
没有超级特质,所以它不知道 Self::Context
。
选择合适的特征并正确实施。