将外部解码为结构时,未为Vec <u8>实现parity_codec :: codec :: Input

时间:2019-05-17 15:15:31

标签: rust substrate

如何将标准Substrate extrinsic format解码为Transaction对象,使之能够获得Sender(最好是字符串)?

我从此代码开始,使用extrinsic_hex变量进行测试的硬编码示例外部数据:

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");
            ()
        }
    }
}

我得到的错误是:

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 crate sr_io : panic_impl .暂时不存在。

如果我正确理解,那是行不通的,因为Vec未实现parity_codec::Input trait,对吗?如果是这样,如何将这一特征添加到Vec中?更好的说是,我缺少的Substrate框架有哪些功能,因此自动提供Input特性?

1 个答案:

答案 0 :(得分:-1)

  

如果是这样,如何将这一特征添加到Vec中?

在您的代码中,您不能。出于连贯性的原因,Rust说,对于类型Y的特征X的实现必须存在于X的板条箱或Y的板条箱中。

  

或者更好的说,我缺少Susbtrate框架的功能是什么,因此会自动提供Input特性?

如果您使用look at the "Implementors" part of the Input trait documentation-您会看到实现std::io::Read的每种类型都将实现Input

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