我想从toml配置文件加载nonce
。在pub fn get_nonce()
中检索到nonce
。我想将lazy_static宏类型HarshBuilder
的结果实例化为salt
。特性绑定T:从<结果<T, Error>>不满意
use config::{Config, File, FileFormat, ConfigError};
use harsh::{Harsh, HarshBuilder};
use settings::Server;
const CFG_DEFAULT: &'static str = "conf/default";
lazy_static! {
static ref MASK: Harsh = HarshBuilder::new()
.length(7)
.salt(get_nonce())
.init()
.expect("invalid harsh build");
}
fn conf_file() -> Config {
let mut cfg = Config::default();
cfg.merge(File::from_str(CFG_DEFAULT, FileFormat::Toml))
.unwrap();
cfg
}
pub fn get_nonce() -> Result<Vec<u8>, ConfigError> {
let conf = conf_file();
let search: Server = conf.get("server").unwrap();
let nonce: Vec<u8> = search.nonce.into_bytes();
Ok(nonce)
}
编译器返回一个错误:
error[E0277]: the trait bound `std::vec::Vec<u8>: std::convert::From<std::result::Result<std::vec::Vec<u8>, config::ConfigError>>` is not satisfied
--> lib.rs:40:14
|
40 | .salt(get_nonce())
| ^^^^ the trait
|
`std::convert::From<std::result::Result<std::vec::Vec<u8>, config::ConfigError>>` is not implemented for `std::vec::Vec<u8>`
|
= help: the following implementations were found:
<std::vec::Vec<u8> as std::convert::From<std::ffi::CString>>
<std::vec::Vec<u8> as std::convert::From<std::string::String>>
<std::vec::Vec<T> as std::convert::From<&'a mut [T]>>
<std::vec::Vec<T> as std::convert::From<std::borrow::Cow<'a, [T]>>>
and 5 others
= note: required because of the requirements on the impl of `std::convert::Into<std::vec::Vec<u8>>` for `std::result::Result<std::vec::Vec<u8>, config::ConfigError>`
所以get_nonce()
返回Result<String, ConfigError>
枚举结果。这似乎不符合salt Option<Vec<u8>>
。您上面看到的尝试是将Result
枚举转换为Vec<u8>
。但是,这并不能解决这个错误。
这里是审查HarshBuilder
特征实现:
/// Note that this factory will be consumed upon initialization.
#[derive(Debug, Default)]
pub struct HarshBuilder {
salt: Option<Vec<u8>>,
// ...ommitted for brevity
}
impl HarshBuilder {
/// Creates a new `HarshBuilder` instance.
pub fn new() -> HarshBuilder {
HarshBuilder {
salt: None,
// ...ommited for brevity
}
}
/// Note that this salt will be converted into a `[u8]` before use, meaning
/// that multi-byte utf8 character values should be avoided.
pub fn salt<T: Into<Vec<u8>>>(mut self, salt: T) -> HarshBuilder {
self.salt = Some(salt.into());
self
}
特质界限,终身省音仍然是我试图绕到我的头一个问题。我真的可以使用一些指导。也许,这可能是为什么答案在我这里不是完全明显的原因。
感谢您深入解释这一点。 – Alex