【问题标题】:Where is the type Log defined inside of the Substrate runtime crate?在 Substrate 运行时 crate 中定义的 Log 类型在哪里?
【发布时间】:2019-05-24 14:04:34
【问题描述】:

看看this implementation

impl consensus::Trait for Runtime {
    type Log = Log;
    type SessionKey = AuthorityId;

    // The Aura module handles offline-reports internally
    // rather than using an explicit report system.
    type InherentOfflineReport = ();
}

Log 是如何定义的?没有用于导入此符号的 use 子句。

跑步

cargo rustc -- -Z unstable-options --pretty=expanded

不显示任何带有type 子句的Log 条目。在 0 级扩展宏后,它确实显示了其他宏声明,但我不确定这是否相关。

我尝试使用 Atom IDE,因为它会自动解析文件并让您找到符号的定义,但没有帮助。

如何找到Log 的定义方式?

【问题讨论】:

  • @turbulencetoo ,不,我已经检查过了:doc.rust-lang.org/std/prelude/index.html,那里没有Log
  • 那是std::prelude,不是rstd:prelude...虽然我在其中一个都找不到日志
  • @turbulencetoo,你知道如何将--pretty扩展到所有级别吗?不只是第一级?也许这就是问题
  • 它似乎是由construct_runtime! 定义的——如果你去宏声明,输出中有enum Log。但是,我不知道如何让编译器将代码扩展至该点。

标签: rust substrate


【解决方案1】:

Logconstruct_runtime 宏定义。 以下是一些相关代码:

construct_runtime宏:

https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L79

macro_rules! construct_runtime {
    (
        pub enum $runtime:ident with Log ($log_internal:ident: DigestItem<$( $log_genarg:ty ),+>)
            where
                Block = $block:ident,
                NodeBlock = $node_block:ty,
                UncheckedExtrinsic = $uncheckedextrinsic:ident
        {
            $( $rest:tt )*
        }
    )

致电__decl_outer_log

https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L267

    $crate::__decl_outer_log!(
        $runtime;
        $log_internal < $( $log_genarg ),* >;
        {};
        $(
            $name: $module:: $( < $module_instance >:: )? { $( $modules $( ( $( $modules_args )* ) )* )* }
        )*
    );

__decl_outer_log

https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L706

macro_rules! __decl_outer_log {
    (
        $runtime:ident;
        $log_internal:ident <$( $log_genarg:ty ),+>;
        { $( $parsed:tt )* };
        $name:ident: $module:ident:: $(<$module_instance:ident>::)? {
            Log ( $( $args:ident )* ) $( $modules:ident $( ( $( $modules_args:ident )* ) )* )*
        }
        $( $rest:tt )*
    ) => {

致电impl_outer_log

https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/srml/support/src/runtime.rs#L763

(
    $runtime:ident;
    $log_internal:ident <$( $log_genarg:ty ),+>;
    { $(
        $parsed_modules:ident $(< $parsed_instance:ident >)? ( $( $parsed_args:ident )* )
    )* };
) => {
    $crate::paste::item! {
        $crate::runtime_primitives::impl_outer_log!(
            pub enum Log($log_internal: DigestItem<$( $log_genarg ),*>) for $runtime {
                $( [< $parsed_modules $(_ $parsed_instance)? >] $(< $parsed_modules::$parsed_instance >)? ( $( $parsed_args ),* ) ),*
            }
        );
    }
};

impl_outer_log 宏: https://github.com/paritytech/substrate/blob/950e90e75dc7d16dcf99972fcc733945a832dc3e/core/sr-primitives/src/lib.rs#L630

macro_rules! impl_outer_log {
    (
        $(#[$attr:meta])*
        pub enum $name:ident ($internal:ident: DigestItem<$( $genarg:ty ),*>) for $trait:ident {
            $( $module:ident $(<$instance:path>)? ( $( $sitem:ident ),* ) ),*
        }
    )

实际上声明和实现Log结构

cargo expand runtime crate 应该可以看到结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    相关资源
    最近更新 更多