pub extern crate r2d2;
pub extern crate tiberius;
pub extern crate futures;
use self::tiberius::BoxableIo;
use self::futures::prelude::*;
use core::fmt::Debug;
#[allow(unused_imports)]
use std::error::Error;
type TiberiusConnection = self::tiberius::SqlConnection<Box<BoxableIo>>;
#[derive(Debug)]
pub enum Errors { TiberiusError(tiberius::Error) }
#[derive(Debug)]
pub struct MSSQLConnectionManagerError(Errors);
impl ::std::error::Error for MSSQLConnectionManagerError {
fn description(&self) -> &str {
match self.0 {
Errors::TiberiusError(ref e) => {
match e {
tiberius::Error::Io(e) => e.description(),
tiberius::Error::Protocol(msg) => &msg,
tiberius::Error::Encoding(msg) => &msg,
tiberius::Error::Conversion(msg) => &msg,
tiberius::Error::Utf8(e) => e.description(),
tiberius::Error::Utf16(e) => e.description(),
tiberius::Error::ParseInt(e) => e.description(),
// TODO: parse the server token if possible and report the actual error that occurred, like invalid login, etc.
tiberius::Error::Server(_) => "TDS token error occurred! When connecting, most often an invalid login.",
tiberius::Error::Canceled => "Canceled!",
}
}
}
}
}
impl ::std::fmt::Display for MSSQLConnectionManagerError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match self.0 { Errors::TiberiusError(ref e) => e.fmt(f), }
}
}
pub struct MSSQLConnection(TiberiusConnection);
pub struct MSSQLConnectionManager { connection_string: String }
impl MSSQLConnectionManager {
pub fn new(connection_string: String) -> MSSQLConnectionManager {
MSSQLConnectionManager { connection_string }
}
pub fn from_env() -> Result<MSSQLConnectionManager, ::std::env::VarError> {
let connection_string = ::std::env::var("MSSQL_CONNECTION_STRING")?;
Ok(MSSQLConnectionManager { connection_string })
}
}
impl r2d2::ManageConnection for MSSQLConnectionManager {
type Connection = MSSQLConnection;
type Error = MSSQLConnectionManagerError;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
let connection_result = TiberiusConnection::connect(&self.connection_string)
.and_then(|c| Ok(c)).wait();
match connection_result {
Ok(c) => Ok(MSSQLConnection(c)),
Err(e) => Err(MSSQLConnectionManagerError(Errors::TiberiusError(e))),
}
}
fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> {
// TODO: Fix this quick and dirty implementation by checking the result of a simple query.
Ok(())
}
fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
// TODO: Fix this quick and dirty implementation by checking underlying TCP socket state.
false
}
}
编译器抱怨Ok(c) => Ok(Self::Connection(c)),
:
error[E0599]: no associated item named `Connection` found for type `persistence::mssql::MSSQLConnectionManager` in the current scope
--> src/persistence/mssql.rs:77:25
|
56 | pub struct MSSQLConnectionManager { connection_string: String }
| --------------------------------- associated item `Connection` not found for this
...
77 | Ok(c) => Ok(Self::Connection(c)),
| ^^^^^^^^^^^^^^^^ associated item not found in `persistence::mssql::MSSQLConnectionManager`
当我明确编写它时,就像这样:
match connection_result {
Ok(c) => Ok(MSSQLConnection(c)),
Err(e) => Err(MSSQLConnectionManagerError(Errors::TiberiusError(e))),
}
现在,它可以成功编译。
但是,如果我使用L10尝试通过返回Err(Self::Error(e))
,也会遇到相同的编译器错误。
为什么这不能正常工作?
答案 0 :(得分:3)
这是一个重现相同问题的最小示例。为了清楚起见,更改了一些类型名称。
trait Manager {
type Connection;
fn connect(&self) -> Self::Connection;
}
pub struct ConnectionId(usize);
pub struct FooManager;
impl Manager for FooManager {
type Connection = ConnectionId;
fn connect(&self) -> Self::Connection {
Self::Connection(5)
}
}
根据Connection
的实现定义,尝试将关联类型ConnectionId
用作具体类型Manager
的别名会出现错误。但是,关联类型的行为并不完全像类型别名。即使我们可以构造一个ConnectionId
(因为它是一个元组结构,并且在此模块中我们可以看到其成员),但是我们无法通过关联的类型Self::Connection
来做到这一点。我们可以做的是访问由其约束定义的其他符号。例如,如果我们有这个:
trait Manager {
type Connection: Default;
// ...
}
我们将能够从default
致电Self::Connection
。
因此,将原始示例中的表达式Ok(Self::Connection(c))
更改为Ok(MSSQLConnection(c)),
是解决此问题的正确方法。如果您甚至需要在此步骤中将类型抽象化,也可以将关联的类型约束为一个新的特征,以提供必要的构造方法。