-
Notifications
You must be signed in to change notification settings - Fork 44
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
Type: Symbol, etc: Cannot be used in match statements #198
Comments
Options:
|
The underlying weirdness here is that rust marks derived-eq types as I don't think "doesn't optimize |
@graydon pointed out that as an interim solution we can use the I'm going to keep this issue open because I think we should address this problem holistically. Specifically for contract developers who will not know that they need to call |
I was doing a compare against a constant with the if-else blocks and still seeing much more code being generated. |
@graydon fixed this recently 🎉. RawVal still cannot be used in matches but other types like Symbol can be. 🎉 |
Doh. I was looking at the wrong code 🤦🏻 . This hasn't happened yet. |
Here's an example of where this is inconvenient: const DEPOSIT_RAW: u64 = symbol!("deposit").to_raw().get_payload();
match function.to_raw().get_payload() {
DEPOSIT_RAW => { |
So I think what I can do here is make the new small-value wrapper types ( |
I should note that this will only work with |
(postponing this for now -- will discuss whether it's worth it on the small-value cases with @leighmcculloch when he's back) |
It's not possible afaik to do match on Symbol now that Symbol's are no longer constant values. Symbols can be handles to host data, and therefore cannot be constant. |
Symbol, and any other type we define as a TaggedVal or with a RawVal cannot be used in a
match
statement.They can't be used in match patterns because the RawVal type is not
#[derive(Eq, PartialEq)]
, and types must derive those traits to be used in pattern matching. It's not sufficient that we implement the traits manually, they must be derived.This has an unfortunate affect that mapping we need to do on Symbol's and other primitive non-object RawVal/TaggedVal types must occur within a set of
if
blocks, which Rust does not optimize as well as it doesmatch
blocks.For a small
match
block of only 3 conditions this causes aSymbol
mapping to be an extra 37 bytes in compiled WASM. Given that we useSymbol
s as constants in enum encoding, this problem seems to be worth solving.Related discussion: https://discord.com/channels/897514728459468821/996186142007369778
The text was updated successfully, but these errors were encountered: