如何在基板FRAME托盘中的结构中包含<T as Trait> :: Blocknumber

问题描述

有人可以告诉我如何在我的货盘模块中的结构中包含<T as Trait>::BlockNumber<T as Trait>::AccountId吗?

我当前的解决方案是添加一个将Trait绑定到“ Trait”的通用参数T。 代码https://github.com/sea212/superorganism/blob/162d1232d405960f7924a05e237eed9c7744b417/pallets/community_identity/src/lib.rs#L31-L42

我认为在decl_module!(第72行)中将该泛型结构用作函数参数类型确实会导致以下错误

错误[E0277]:T未实现std::fmt::Debug

-片段-

=帮助:std::fmt::Debug的特征T未实现

=注意:由于std::fmt::DebugPhysicalProof<T,[u8; 32]>的隐含要求而必需

=注意:由于std::fmt::Debug(PhysicalProof<T,[u8; 32]>,)的隐含要求而必需

=注意:std::fmt::Debug::fmt必需

=注意:此错误源于宏(在Nightly builds中,使用-Z macro-backtrace运行以获取更多信息)

帮助:考虑进一步限制此限制

impl $ crate :: dispatch :: fmt :: Debug

我尝试在该结构中为T手动实现fmt::Debug,但这不是解决方案,或者我无法正确执行。

解决方法

要派生Debug作为结构,它的所有字段都必须能够实现Debug。现在,编译器不知道T是否可调试。

通过添加T作为类型Debug的绑定,可以告诉编译器Debug实现了T

#[derive(Debug)]
pub struct PhysicalProof<T,ProofData> where
    // `T` must implement `Debug`   
    T: Trait + Debug,{
    proof: ProofData,date: T::BlockNumber,}
,

来自Guillaumesubstrate-technical Element chat的建议解决了这个问题:

生锈代码:

impl<..> Debug for PhysicalPool<...> where ...,ProofData: Debug,T: Debug

在您所处的情况下,/// Structure that contains the proof #[derive(Debug)] pub struct PhysicalProof<BlockNumber ProofData> where ProofData: Codec + Clone + Debug + Decode + Encode + Eq + PartialEq,BLockNumber: ... { proof: ProofData,date: BlockNumber,} 会不需要这些额外的边界,但是无论如何,锈都会添加它们。 因此,当您尝试使用该stuct时,它说它没有实现Debug,因为T没有实现Debug。

一种解决方案是手动实施,另一种解决方案是:

using