Skip to content

Add new macros to allow assertions with custom exit code#131

Merged
mohanson merged 5 commits intonervosnetwork:masterfrom
xxuejie:assert-with-custom-exit-code
Mar 12, 2025
Merged

Add new macros to allow assertions with custom exit code#131
mohanson merged 5 commits intonervosnetwork:masterfrom
xxuejie:assert-with-custom-exit-code

Conversation

@xxuejie
Copy link
Collaborator

@xxuejie xxuejie commented Mar 5, 2025

This change adds 2 new macros & 1 utility functions, that allows one to set the return error code first, then runs assertions in Rust.

This allows us to get the benefit of both worlds: we could use assertion lines with decent debug message outputs, while at the same time we can also customize final exit code.

@xxuejie xxuejie requested review from XuJiandong, mohanson and quake March 5, 2025 08:25
src/asserts.rs Outdated
}};
}

pub fn set_error_expect<C: Into<i8>, T, E: Debug>(c: C, r: Result<T, E>, msg: &str) -> T {
Copy link
Collaborator

@XuJiandong XuJiandong Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to add following:

  1. Option's expect
  2. macro assert_ne!
  3. Result's expect_err

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mohanson
Copy link
Collaborator

mohanson commented Mar 6, 2025

https://github.com/rust-lang/rust/blob/master/library/core/src/macros/panic.md#current-implementation

If the main thread panics it will terminate all your threads and end your program with code 101.

It's better to keep Rust's conventions

@xxuejie
Copy link
Collaborator Author

xxuejie commented Mar 6, 2025

https://github.com/rust-lang/rust/blob/master/library/core/src/macros/panic.md#current-implementation

If the main thread panics it will terminate all your threads and end your program with code 101.

It's better to keep Rust's conventions

If you look at it, ckb-std already returns -1 when panic happens, which is different than Rust's convention. In addition, it is documented as "current implementation", not necessarily a convention.

@mohanson mohanson merged commit fe962ee into nervosnetwork:master Mar 12, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants