[英]Trait bound T: From<Result<T, Error>> is not satisfied
I want to load nonce
from a toml config file. 我想从toml配置文件加载
nonce
。 nonce
is retrieved in pub fn get_nonce()
. nonce
在pub fn get_nonce()
检索。 I'd like to instantiate the result to salt
of lazy_static macro type HarshBuilder
. 我想将结果实例化为lazy_static宏类型
HarshBuilder
salt
。
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)
}
The compiler returns an error: 编译器返回错误:
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>`
So get_nonce()
returns an enum result of Result<String, ConfigError>
. 因此
get_nonce()
返回Result<String, ConfigError>
的枚举结果。 This does not appear to satisfy salt Option<Vec<u8>>
. 这似乎不满足
salt Option<Vec<u8>>
。 The attempt you see above is to convert Result
enum to Vec<u8>
. 您在上面看到的尝试是将
Result
枚举转换为Vec<u8>
。 However, this does not fix the error. 但是,这不能解决错误。
Here is the HarshBuilder
trait implementation for review: 这是
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
}
Trait bounds and lifetime elision is still a subject that I'm trying to wrap my head around. 特质界限和终生淘汰仍然是我要努力解决的问题。 I can really use some guidance.
我真的可以使用一些指导。 Perhaps, this may be the reason as to why the answer is not completely obvious for me here.
也许,这可能就是为什么答案对我来说并不完全明显的原因。
Since your get_nonce
function returns a Result
, you need to handle the possible error. 由于您的
get_nonce
函数返回Result
,因此您需要处理可能的错误。 There are three ways you can fix your code here: 您可以通过三种方式在此处修复代码:
get_nonce
never returns an error, you can simply change it so that it returns nonce
directly instead of Ok(nonce)
. get_nonce
永远不会返回错误,您可以简单地对其进行更改,以使其直接返回nonce
而不是Ok(nonce)
。 unwrap
on the result to access the Vec<u8>
that's inside (and crash if you later change get_nonce
to generate errors). unwrap
,以访问其中的Vec<u8>
(如果以后更改get_nonce
来生成错误, get_nonce
崩溃)。 unwrap
s and use try!
or the ?
operator to propagate errors and catch them properly at some top-level point). unwrap
并使用try!
或?
运算符传播错误并在某个顶级点正确捕获它们)。 The Option<Vec<u8>>
is a red herring, the important thing is the prototype of salt()
, and as you can see in the definition of salt
: Option<Vec<u8>>
是一个红色鲱鱼,重要的是salt()
的原型,正如您在salt
的定义中所看到的:
pub fn salt<T: Into<Vec<u8>>>(mut self, salt: T)
it expects an argument that satisfies the trait Into<Vec<u8>>
. 它期望一个满足特征
Into<Vec<u8>>
。 From the documentation you can see that there are these generic implementations of Into<T>
: 从文档中,您可以看到
Into<T>
这些通用实现:
From<T>
for U
implies Into<U>
for T
From<T>
为U
意味着Into<U>
为T
Into
is reflexive, which means that Into<T>
for T
is implemented. Into
是自反的,这意味着已实现T
Into<T>
。 So you may pass to salt
either: 因此,您可以通过以下任一方法来
salt
:
Vec<u8>
. Vec<u8>
类型的值。 T
if there is such From<T>
is implemented for Vec<u8>
. Vec<u8>
实现了这样的From<T>
则类型为T
的值。 Into<Vec<u8>>
directly. Into<Vec<u8>>
。 Now, you have a value of type Result<Vec<u8>, ConfigError>
, that satisfies none of the above. 现在,您有一个类型
Result<Vec<u8>, ConfigError>
,不能满足上述任何一个条件。 And that is what all those error messages are trying to tell you. 这就是所有这些错误消息都试图告诉您的。
The easy solution is to change your function into: 简单的解决方案是将您的功能更改为:
pub fn get_nonce() -> Vec<u8> {
....
nonce
}
If you cannot change that return type you can use unwrap()
to get the real value from a Result()
(and crash on error): 如果您无法更改该返回类型,则可以使用
unwrap()
从Result()
获取实际值(并在发生错误时崩溃):
.length(7)
.salt(get_nonce().unwrap())
.init()
If the get_nonce()
function can really fail, then you would have to manage the error properly, maybe making your MASK
value of type Result<Harsh, ConfigError>
? 如果
get_nonce()
函数确实可能失败,那么您将不得不正确管理该错误,也许使您的MASK
值为Result<Harsh, ConfigError>
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.