ckb-std
ckb-std copied to clipboard
Disable `debug_assertions` won't ingore function calls in `debug!(..)` statements.
Issue
I have the following code:
#[cfg(debug_assertions)]
use ckb_std::ckb_types::prelude::*;
debug!("script hash = {:#x}", script_hash.pack());
When set debug_assertions = false
, the above could NOT be compiled.
Description
When users don't want to print the debug messages, all functions in debug!(..)
, such as script_hash.pack()
in above code, should be ignored and they should never be executed.
https://github.com/nervosnetwork/ckb-std/blob/676455542258235a22f6f443b18e4b4d887a661a/src/debug.rs#L38-L44
It's due to the "Avoid unused warnings. ". Try this:
use ckb_std::ckb_types::prelude::*;
debug!("script hash = {:#x}", script_hash.pack());
It's due to the "Avoid unused warnings. ". Try this:
use ckb_std::ckb_types::prelude::*; debug!("script hash = {:#x}", script_hash.pack());
Why execute functions (or cost cycles) when no log outputs are required?
I think it is a bug.
It was to avoid a warning here in release build that tx_hash
is not used: https://github.com/nervosnetwork/capsule/blob/a439682150afa2248fc53b1fa7990fb591ee4a07/templates/rust/contract/src/entry.rs#L31
Maybe you should fix the template instead. Then you don't need the drop.
Maybe you should fix the template instead. Then you don't need the drop.
Sorry, I didn’t get you.
Did you meaning I should remove the .pack()
?
In fact, my final goal is disabling any function calls in the debug!
macro.
Some type conversions (or data load) are only for debugging, they don't have to execute when in release mode.
They just waste cycles.
If you allowed, I could push a PR to remove the drop
function.
And I promise there will be no warnings.
It's due to the "Avoid unused warnings. ". Try this:
use ckb_std::ckb_types::prelude::*; debug!("script hash = {:#x}", script_hash.pack());
I think using drop in debug macro is wrong, for example, this code doesn't compiling:
let tx_hash = load_tx_hash()?;
debug!("tx hash is {:?}", tx_hash);
let h0 = tx_hash[0]
resolved by https://github.com/nervosnetwork/ckb-std/pull/98
I mean you should modify the template to maybe
debug!("tx hash is {:?}", load_tx_hash()?);
Or like what quake wrote which uses the tx_hash
in let h0 = tx_hash[0]
. So that there isn't an unused variable tx_hash
when debug assertions are not enabled.
I mean you should modify the template to maybe
debug!("tx hash is {:?}", load_tx_hash()?);
Or like what quake wrote which uses the
tx_hash
inlet h0 = tx_hash[0]
. So that there isn't an unused variabletx_hash
when debug assertions are not enabled.
I think that my code snippet doesn't compiling when debug assertions is disabled, we should remove the drop
from macro.
I think that my code snippet doesn't compiling when debug assertions is disabled, we should remove the
drop
from macro.
Yeah, I mean how to avoid the warning when the drop
is removed.