【发布时间】:2021-05-08 08:12:28
【问题描述】:
创建身份验证服务器。
我想要一个PGUserRepo 结构,它实现了UserRepo trait async fn create。
PGUserRepo 应该有一个实现 Hasher 特征的字段。
但是,当我尝试将 PGUserRepo 字段限制为 dyn Hasher 时,出现错误
“特性std::marker::Send 没有为`(dyn domain::hasher::Hasher + 'static)”实现。
如果我将PGUserRepo 字段设为具体实现类型ArgonHasher,则不会出错。但我不希望PGUserRepo 关心给了它什么样的Hasher,只是它实现了Hasher 特征。
我尝试将 hasher 字段包装在 Box、Arc 和 Mutex 的各种组合中,但我不明白为什么称它为 ArgonHasher 很好但说它是 Hasher 的一些实现者的根本原因不是。
代码,为方便起见,折叠成一个文件:
pub trait Hasher {
fn hash(&self, password: String) -> Result<String, HasherError>;
}
pub struct ArgonHasher {}
impl Hasher for ArgonHasher {
fn hash(&self, password: String) -> Result<String, HasherError> {
let result = argon2id13::pwhash(
&password.as_bytes(),
argon2id13::OPSLIMIT_INTERACTIVE,
argon2id13::MEMLIMIT_INTERACTIVE,
);
match result {
Ok(hashed_password_result) => match std::str::from_utf8(&hashed_password_result.0) {
Ok(hashed_password_utf8) => Ok(String::from(hashed_password_utf8)),
Err(e) => Err(HasherError::from(e)),
},
Err(e) => Err(HasherError::from(e)),
}
}
}
impl From<()> for HasherError {
fn from(_: ()) -> Self {
HasherError::HasherError {
message: String::from(""),
}
}
}
impl From<std::str::Utf8Error> for HasherError {
fn from(cause: std::str::Utf8Error) -> Self {
HasherError::HasherError {
message: format!("{}", cause),
}
}
}
#[async_trait]
pub trait UserRepo {
async fn create(&self, user: User) -> Result<User, UserRepoError>;
}
#[derive(Debug)]
pub enum UserRepoError {
UserAlreadyExistsError { field: String, value: String },
UserRepoError { message: String },
}
impl fmt::Display for UserRepoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *&self {
UserRepoError::UserAlreadyExistsError { field, value } => {
f.write_str(&format!("User with {}={} already exists", field, value))
}
UserRepoError::UserRepoError { message } => f.write_str(&message),
}
}
}
pub struct PGUserRepo {
conn_pool: PgPool,
hasher: &'static dyn Hasher,
}
impl PGUserRepo {
pub fn new(conn_pool: PgPool, hasher: &'static dyn Hasher) -> Self {
PGUserRepo { conn_pool, hasher }
}
}
#[async_trait]
impl UserRepo for PGUserRepo {
async fn create(&self, user: User) -> Result<User, UserRepoError> {
# Compiler error is on this function
# hasher is not even used in this function yet, it's just a field on PGuserRepo
}
奖励:我真的不需要 Hasher 特征 hash 来引用 self 但没有它我会得到“错误[E0038]:特征 domain::hasher::Hasher 不能被制成对象” .
【问题讨论】:
-
另见Sharing a struct with trait objects as properties across threads Q&A 是针对盒装特征的,但概念是相同的。
标签: rust async-await