Skip to content
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

Recommendation for pattern matching #22

Open
sffc opened this issue Aug 23, 2020 · 9 comments
Open

Recommendation for pattern matching #22

sffc opened this issue Aug 23, 2020 · 9 comments

Comments

@sffc
Copy link
Collaborator

sffc commented Aug 23, 2020

On Rust Nightly, I got the following pattern match to work:

const FOO: TinyStr16 = tinystr16!("foo");
const BAR: TinyStr16 = tinystr16!("bar");
match tinystr_input {
    FOO => /* ... */,
    BAR => /* ... */,
    _ => /* ... */,
}

This is fine, but it would be nicer if I could write

match tinystr_input {
    tinystr16!("foo") => /* ... */,
    tinystr16!("bar") => /* ... */,
    _ => /* ... */,
}

Unfortunately, this doesn't seem to work right now.

Is the first example the best way to do pattern matching on TinyStr, or is there a better way?

@Manishearth
Copy link
Collaborator

Deref it and match against string

@sffc
Copy link
Collaborator Author

sffc commented Aug 24, 2020

I would expect a string match to be slower than a TinyStr (primitive int) match. I haven't done benchmarks.

@Manishearth
Copy link
Collaborator

oh, good point. Might be worth making a macro for matching then

@zbraniecki
Copy link
Owner

@sffc
Copy link
Collaborator Author

sffc commented Aug 26, 2020

I think the compiler has the option to do additional optimization when matches are used, so I don't know if PartialEq tells the whole story. I made a new benchmark to specifically test match statement behavior: #23

I have functions with a 15-leaf match, some with str and some with tinystr. Results for me on my Lenovo X1 Carbon:

String Length Match(&str) Match(tinystr)
4 64.4 ns 35.1 ns
8 58.2 ns 32.0 ns
16 46.7 ns 42.8 ns

So, matching on TinyStr is faster, but not greatly so, especially on TinyStr16.

@sffc
Copy link
Collaborator Author

sffc commented Apr 11, 2021

The solution for pattern matching is to just make a bunch of const TinyStrs, and then you can pass them on the left side of a match statement as the pattern. We should document this with an example.

@Manishearth
Copy link
Collaborator

I wonder if tinystr16!() can be written as a const fn now that that's stable

if so it's possible TinyStr::new("en") => ... might work (?)

@sffc
Copy link
Collaborator Author

sffc commented Apr 11, 2021

The problem I previously had with that was const functions don't have a great answer for indexing into variable length strings. We could perhaps have a const function that takes [u8, 4], however.

@Manishearth
Copy link
Collaborator

if so it's possible TinyStr::new("en") => ... might work (?)

Ah, this is still not allowed. I think a custom match macro is probably your best bet

There is the vesta crate, a new crate that makes it possible to do generic matches. It may be useful, unsure, but really we can just write our own macro.

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

No branches or pull requests

3 participants