繁体   English   中英

Rust:为什么我不能在这个库中使用 anyhow::Context?

[英]Rust: Why can't I use anyhow::Context with this library?

我正在尝试简化一些使用psa_crypto library的代码。

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

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);

我将此代码放在一个函数中,使用anyhow::Result我想用一些更惯用的代码替换unwrap 不幸的是,如果我尝试使用 ` 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>`

这个错误是什么意思? 如何更改此代码以使其正常工作?

anyhow::Error要求它包装的错误实现std::error::Error psa_crypto::types::status::Error没有实现该特征,因此反过来,它也没有实现Context扩展特征。

这部分错误消息暗示了这一点,尽管它有点模糊,因为它无论如何都会打印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>`

这似乎是psa_crypto库中的疏忽。 可能值得提交一个错误,要求他们实现该特征。 作为一种解决方法,您有几个选择:

  • 由于psa_crypto::types::status::Error实现了std::fmt::Debug ,您可以使用它来获取String错误消息: psa_crypto::init().map_err(|e| format!("{:?}", e).context("While initializing")
  • 一个更漂亮的解决方案是实现一个确实实现Error (及其要求Display )的包装器类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM