首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在将外部变量解码为结构时,不会为Vec<u8>实现parity_codec::codec::Input

在将外部变量解码为结构时,不会为Vec<u8>实现parity_codec::codec::Input
EN

Stack Overflow用户
提问于 2019-05-17 23:15:31
回答 1查看 550关注 0票数 2

如何将标准Substrate extrinsic format解码为Transaction对象,以便能够获取Sender,最好是字符串?

我已经从这段代码开始,其中包含用于在extrinsic_hex变量中测试的硬编码样本外部数据:

代码语言:javascript
复制
use hex::decode;
use hex_literal::hex;
use parity_codec::{Decode, Encode, Input};
use primitives::generic::UncheckedMortalExtrinsic;
use std::fmt;
use std::fmt::Debug;

fn main() {
    let extrinsic_hex: &'static str =  "81ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d3c6b8941e2976034e67bdd1a999c3eff4403c8ceaf717f18d9760ab18573ab2ce870e9b751c2f14dd9883e54746e1eb6639978ceab49968c25176cc0d2507205040003000ca10f";
    let result = hex::decode(extrinsic_hex);
    match result {
        Ok(v1) => {
            let extr_option = UncheckedMortalExtrinsic::decode(&mut v1);
            ()
        }
        _ => {
            println!("Error decoding");
            ()
        }
    }
}

我得到的错误是:

代码语言:javascript
复制
error: duplicate lang item in crate `sr_io`: `panic_impl`.
  |
  = note: first defined in crate `std`.

error: duplicate lang item in crate `sr_io`: `oom`.
  |
  = note: first defined in crate `std`.

error[E0277]: the trait bound `std::vec::Vec<u8>: parity_codec::codec::Input` is not satisfied
  --> core/decaddr/src/main.rs:13:20
   |
13 |             let extr_option=UncheckedMortalExtrinsic::decode(&mut v1);
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `parity_codec::codec::Input` is not implemented for `std::vec::Vec<u8>`
   |
   = note: required by `parity_codec::codec::Decode::decode`

让我们假设错误error: duplicate lang item in cratesr_io:panic_impl.暂时不存在。

如果我没理解错的话,它不能工作是因为Vec没有实现parity_codec::Input trait,对吗?如果是这样,如何将此特性添加到Vec中?或者更好地说,为了自动提供Input特征,我遗漏了底层框架的哪些功能?

EN

回答 1

Stack Overflow用户

发布于 2019-05-18 03:37:24

如果是这样的话,如何将这个特性添加到Vec中?

在你的代码中,你不能。出于一致性的原因,Rust说类型Y的特征X的实现必须存在于X的框或Y的框中。

或者更好地说,我遗漏了哪些来自Susbtrate框架的函数来自动提供输入特征?

如果你使用look at the "Implementors" part of the Input trait documentation -你可以看到每一个实现std::io::Read的类型都会实现Input

正如评论所说,对您有用的实现是&[u8]上的实现,它之所以存在,是因为它实现了std::io::Read

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56189290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档