Rust : calls in constant functions are limited to constant functions, tuple structs and tuple varian

//runtime/src/lib.rs

...

parameter_types! {
   pub  const TombstoneDeposit: Balance = deposit(
      1,
      <pallet_contracts::Pallet<Runtime>>::contract_info_size()
   );
   pub const DepositPerContract: Balance = TombstoneDeposit::get();

	...

编译时报错:

...

     Compiling node-template-runtime v3.0.0 (/home/liucz/SubstratePrivateNetwork/substrate-node-template/runtime)
  error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
     --> /home/liucz/SubstratePrivateNetwork/substrate-node-template/runtime/src/lib.rs:332:7
      |
  332 |       <pallet_contracts::Pallet<Runtime>>::contract_info_size()
      |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  error: aborting due to previous error

  For more information about this error, try `rustc --explain E0015`.
  error: could not compile `node-template-runtime`

  To learn more, run the command again with --verbose.

即:error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
原因:deposit()是const声明的函数,而调用时传参的contract_info_size()不是const声明的函数,所以报错。

犹如在pow中使用常量:

const N: usize = 3;
const N_CELLS: usize = ((2 * N + 1).pow(2)) - N * (N + 1);

会报错:

error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
 --> src/lib.rs:2:24
  |
2 | const N_CELLS: usize = ((2 * N + 1).pow(2)) - N * (N + 1);
  |                        ^^^^^^^^^^^^^^^^^^^^

You will have to expand the multiplication yourself:

const N_CELLS: usize = {
    let a = 2 * N + 1;
    let b = N * (N + 1);
    a * a - b
};

How can I guarantee that overflow will not occur in const variables?
Rust 全局静态变量(match、lazy_static! 、phf)

Derived PartialEq cannot be used in a const function #77695

rust的tuple struct

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值