假设你有一个枚举
enum Expr {
binExp, unExp, Literal, Group
}
Run Code Online (Sandbox Code Playgroud)
并且您想调用 上的方法Expr。
你会宁愿 :
match Expr {
Expr::binExp => todo!(),
Expr::unExp => todo!(),
Expr::Literal => todo!(),
Expr::Group => todo!()
}
Run Code Online (Sandbox Code Playgroud)
或者
if let Expression::binExp = self {
todo!()
} else if let Expression::unExp = self {
todo!()
} else if let Expression::Lit = self {
todo!()
} else if let Expression::Group(e) = self {
todo!()
} else {
todo!()
}
Run Code Online (Sandbox Code Playgroud)
换句话说,是match并行操作吗?我知道if/else将按顺序遍历每个表达式,在遇到错误时对其进行评估,最后停止。
match 语句是模仿这种行为还是直接跳转到正确的模式?我特别问这个问题是因为 …
因此,每种托盘类型都有或多或少相同的声明:pub struct Pallet<T>(_)或pub struct Pallet<T>(PhantomData<T>)where T: Config。我的问题是T代表什么?有人提到 代表T基质运行时,这让我产生疑问,如果一个节点有多个运行的托盘,它们是否都共享相同的定义T?
在 C 中,我们可以使用 ## 连接参数化宏的两个参数,如下所示:
arg1##arg2返回arg1arg2
我写了这段代码,希望它能连接并返回一个字符串文字,但我无法让它工作:
#define catstr(x, y) x##y
puts("catting these strings\t" catstr(lmao, elephant));
Run Code Online (Sandbox Code Playgroud)
返回以下错误:
define_directives.c:31:39: error: expected ‘)’ before ‘lmaoelephant’
31 | puts("catting these strings\t" catstr(lmao, elephant));
| ^
| )
Run Code Online (Sandbox Code Playgroud)
似乎字符串正在连接,但它们需要用引号括起来才能puts打印出来。但是这样做,宏不再起作用。我该如何解决这个问题?
c concatenation c-strings string-concatenation preprocessor-directive
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
/// The `AccountId` of the sudo key.
pub key: T::AccountId,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { key: Default::default() }
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
<Key<T>>::put(&self.key);
}
}
Run Code Online (Sandbox Code Playgroud)
genesis_config和genesis_build宏在这里发生了什么?一些阅读似乎建议实现特征GenesisBuild<T,I = ()>但我很困惑,因为文档读到:“T并且I是托盘特征和托盘实例的占位符。”
什么是托盘实例?我还假设托盘特征意味着Configsudo 托盘的特征。StorageValue与为创世块配置唯一性以便运行节点的每个实例都可以验证同一帐户是否为根帐户有什么关系吗?有人可以帮我分解一下吗?
在余额托盘中,配置特征有一项定义如下type AccountStore: StoredMap<Self::AccountId, AccountData<Self::Balance>>;:这对我来说有点奇怪,因为我期望一个普通的存储映射来存储从AccountId到 的映射AccountData,但在查看文档后,StoredMap我意识到这也是在 StorageMaps 上实现的一个特征。现在这更有意义了,所以我继续看看运行时如何定义这个字段,令我惊讶的是我在runtime/src/lib.rs:中找到了它type AccountStore = System;。现在我以前从未见过这样的运行时定义,因为如果我是正确的,System它应该代表frame_system托盘。所以我去查看frame_system::Config运行时,发现了这个定义:
type AccountData = pallet_balances::AccountData<Balance>;。
现在我不知道这些定义是如何进入pallet_balances's Configimpl 的,但我可以看到 FRAMESystem托盘包含两种类型,即:一种类型AccountData和一种类型AccountId。
最后我的两个问题是:
type AccountStore = System;具体类型如何判断呢?我不是 const 泛型方面的专家,但在尝试涉及对 const 泛型进行操作的新类型实例化时,我尝试了几种不同的方法,但都存在问题,例如:当尝试将此 const 泛型结构中的基数从 增加到K时K+1。
// Given a Base<K> return a Base<K+1>
pub struct Base<const K:u32> {}
pub const fn base_bump<const K: u32, const L: u32>(b: Base<K>,) -> Base<L> {
const L : u32 = K + 1;
Base::<L> {}
}
Run Code Online (Sandbox Code Playgroud)
错误 :
error[E0401]: can't use generic parameters from outer function
--> src/main.rs:5:20
|
2 | pub const fn base_bump<const K: u32, const L: u32>(
| - const parameter from outer function
... …Run Code Online (Sandbox Code Playgroud)