-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement require macros and fix log docs #493
Conversation
#[macro_export] | ||
macro_rules! require { | ||
($cond:expr $(,)?) => { | ||
if !$cond { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, seems like these will be less costly on CPU cycles than assert
s, is that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that will necessarily be the case, but it should reduce the code size from not including file/line numbers of where the assert
is. This also depends on the use case, though.
@matklad would love a quick opinion on this if you think this is a good direction for having this type of assertion available without the Rust internals. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of thoughts:
- I wonder if we should just call this
assert
. Therequire
name doesn't really point to the difference - You probably don't need
_eq/_neq
versions -- they exist solely to provide better assertion messages. we don't allow assertion message,assert!(x == y)
is enough. - Would be cool to get some specific numbers on code saving here: I know that fmt is heavy in principle, but I don't have a gut feeling for how much.
Overall seems like a good idea!
near-sdk/src/utils/mod.rs
Outdated
}; | ||
($left:expr, $right:expr, $message:expr $(,)?) => { | ||
if $left != $right { | ||
$crate::env::panic_str($message.as_ref()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not entirely sure, but it seems that built-in format uses &$message
rather than $message.as_ref()
- https://github.com/rust-lang/rust/blob/d59b80d588368cdcfcc1d54e119374a3d78169ff/compiler/rustc_builtin_macros/src/format.rs#L797
- https://github.com/rust-lang/rust/blob/d59b80d588368cdcfcc1d54e119374a3d78169ff/compiler/rustc_expand/src/build.rs#L232-L235
- https://github.com/rust-lang/rust/blob/d59b80d588368cdcfcc1d54e119374a3d78169ff/compiler/rustc_ast/src/ast.rs#L1362-L1364
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the rationale was that I wanted the ability to supply something that wasn't just a static string or if keeping expr
then forcing the user to pass in a &str
, in place of not having the formatting machinery.
I was thinking it would be used as the log!
macro where it allows String
, &str
, or anything that implements AsRef<str>
. I suppose all we want here is to allow owned or str references, so I think I will take this suggestion because it's unlikely someone will assert with the message being a single struct
I personally think that keeping it named
Good point! I'll remove those for now. My rationale was that it would be easier for migration to these from the assert variants, but can always just add later if we want
So it's hard to tell, since most of |
Just in case of ambiguity, I'm assuming your approval as invalid @mikedotexe because I've made changes since your approval. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I tried to think of why require
wouldn't be a good name (since it's overloaded with node's old way of importing modules), but given that they are moving away from that to import * from *
I think it's good.
Basically want to provide macros to mimic similar behaviour to the rust
assert!
macros without it adding extra rust details and file information about the assertion.Built on #492 to avoid conflicts and use the correct method signature, but can switch if we want this and not that PR.Optional which opinions would be appreciated:
panic()
syscall directly? I assumed not because it seems like a micro-optimization that would be hard to debug by default. Pinpointing that it's a require statement might help developers