[英]Rust: Why can't I use anyhow::Context with this library?
I'm trying to simplify some code which uses the psa_crypto
library .我正在尝试简化一些使用
psa_crypto
library的代码。
More in details I'm using their first encryption example from their docs from their docs:更多详细信息,我正在使用他们文档中的第一个加密示例:
use psa_crypto::types::algorithm::{Aead, AeadWithDefaultLengthTag};
use psa_crypto::types::key::{Attributes, Type, Lifetime, Policy, UsageFlags};
use psa_crypto::operations::{key_management, aead};
let alg = Aead::AeadWithDefaultLengthTag(AeadWithDefaultLengthTag::Ccm);
let mut usage_flags: UsageFlags = Default::default();
usage_flags.set_encrypt();
let attributes = Attributes {
key_type: Type::Aes,
bits: 0,
lifetime: Lifetime::Volatile,
policy: Policy {
usage_flags,
permitted_algorithms: alg.into(),
},
};
psa_crypto::init().unwrap();
let my_key = key_management::import(attributes, None, &KEY_DATA).unwrap();
let output_buffer_size = attributes.aead_encrypt_output_size(alg.into(), INPUT_DATA.len()).unwrap();
let mut output_buffer = vec![0; output_buffer_size];
let length = aead::encrypt(my_key, alg, &NONCE, &ADDITIONAL_DATA, &INPUT_DATA, &mut output_buffer).unwrap();
output_buffer.resize(length, 0);
I put this code inside a function using anyhow::Result
I would like to replace the unwrap
s with some more idiomatic code.我将此代码放在一个函数中,使用
anyhow::Result
我想用一些更惯用的代码替换unwrap
。 Unfortunately if I try to change the first unwrap
with `.context("error") I get an error:不幸的是,如果我尝试使用 `
unwrap
("error") 更改第一个展开,我会收到一个错误:
let my_key = key_management::import(attributes, None, &KEY_DATA).context("error")?;
^^^^^^^ method cannot be called on `Result<Id, psa_crypto::types::status::Error>` due to unsatisfied trait bounds
|
504 | pub enum Result<T, E> {
| --------------------- doesn't satisfy `_: anyhow::Context<Id, psa_crypto::types::status::Error>`
|
::: /home/fedboz01/.cargo/registry/src/github.com-1ecc6299db9ec823/psa-crypto-0.9.1/src/types/status.rs:50:1
|
50 | pub enum Error {
| -------------- doesn't satisfy `_: anyhow::context::ext::StdError`
|
= note: the following trait bounds were not satisfied:
`psa_crypto::types::status::Error: anyhow::context::ext::StdError`
which is required by `Result<Id, psa_crypto::types::status::Error>: anyhow::Context<Id, psa_crypto::types::status::Error>`
What does this error mean?这个错误是什么意思? How can I change this code to make it work?
如何更改此代码以使其正常工作?
anyhow::Error
requires that the error it wraps implement std::error::Error
. anyhow::Error
要求它包装的错误实现std::error::Error
。 psa_crypto::types::status::Error
does not implement that trait, so in turn, it does not implement the Context
extension trait. psa_crypto::types::status::Error
没有实现该特征,因此反过来,它也没有实现Context
扩展特征。
This part of the error message hints at this, though it's a bit obscured, since it prints anyhow's internal alias for std::error::Error
:这部分错误消息暗示了这一点,尽管它有点模糊,因为它无论如何都会打印
std::error::Error
的内部别名:
50 | pub enum Error {
| -------------- doesn't satisfy `_: anyhow::context::ext::StdError`
|
= note: the following trait bounds were not satisfied:
`psa_crypto::types::status::Error: anyhow::context::ext::StdError`
which is required by `Result<Id, psa_crypto::types::status::Error>: anyhow::Context<Id, psa_crypto::types::status::Error>`
This seems like an oversight in the psa_crypto
library.这似乎是
psa_crypto
库中的疏忽。 It may be worth filing a bug asking them to implement that trait.可能值得提交一个错误,要求他们实现该特征。 As a workaround, you have a few options:
作为一种解决方法,您有几个选择:
psa_crypto::types::status::Error
implements std::fmt::Debug
, you can use this to get a String
error message: psa_crypto::init().map_err(|e| format!("{:?}", e).context("While initializing")
psa_crypto::types::status::Error
实现了std::fmt::Debug
,您可以使用它来获取String
错误消息: psa_crypto::init().map_err(|e| format!("{:?}", e).context("While initializing")
Error
(and its requirement, Display
).Error
(及其要求Display
)的包装器类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.