RUST:为什么我可以使用此库中的上下文来使用::上下文?

发布于 2025-02-11 03:22:24 字数 2371 浏览 1 评论 0原文

我正在尝试简化一些使用

更多详细信息,我正在使用他们的第一个来自他们的文档的加密示例从他们的文档中:

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 unwraps 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2025-02-18 03:22:24

Any HOW ::错误要求它包装的错误实现std :: error ::错误:: error::错误 没有实现该特征,因此,它又不会实现上下文扩展特征。

错误消息的这一部分暗示了这一点,尽管它有点模糊,因为它在std :: error :: error :: 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>`

这似乎是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 implement std::error::Error. psa_crypto::types::status::Error does not implement that trait, so in turn, it does not implement the Context 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:

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. It may be worth filing a bug asking them to implement that trait. As a workaround, you have a few options:

  • Since 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")
  • A prettier solution would be to implement a wrapper type that does implement Error (and its requirement, Display).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文