RUST:为什么我可以使用此库中的上下文来使用::上下文?
我正在尝试简化一些使用库。
更多详细信息,我正在使用他们的第一个来自他们的文档的加密示例从他们的文档中:
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);
我使用Any Howhow :: Result
将此代码放入函数中更多惯用代码。不幸的是,如果我尝试使用.context(“错误”)更改第一个UNWRAP
,我会得到一个错误:
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>`
此错误是什么意思?如何更改此代码以使其正常工作?
I'm trying to simplify some code which uses the 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. Unfortunately if I try to change the first unwrap
with `.context("error") I get an 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Any HOW ::错误
要求它包装的错误实现std :: error ::错误:: error
。 ::错误 没有实现该特征,因此,它又不会实现上下文
扩展特征。错误消息的这一部分暗示了这一点,尽管它有点模糊,因为它在
std :: error :: error :: error :: error
:这似乎是
psa_crypto
std :: error :: error :: error :: error :: /代码>库。可能值得提交错误要求他们实施该特征。作为解决方法,您有几个选择:
psa_crypto ::类型:: status :: status :: error
实施std :: fmt :: fmt :: debug
您可以使用它来获得A字符串
错误消息:psa_crypto :: init()。map_err(| e |格式! >
错误
的包装类型(及其要求,display
)。anyhow::Error
requires that the error it wraps implementstd::error::Error
.psa_crypto::types::status::Error
does not implement that trait, so in turn, it does not implement theContext
extension trait.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
:This seems like an oversight in the
psa_crypto
library. 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
implementsstd::fmt::Debug
, you can use this to get aString
error message:psa_crypto::init().map_err(|e| format!("{:?}", e).context("While initializing")
Error
(and its requirement,Display
).