Re: [rust-dev] matching on a few bits in int

2014-04-08 Thread Vladimir Pouzanov
Awesome use of unreachable! On Tue, Apr 8, 2014 at 4:53 PM, Peter Marheine wrote: > I had a go at building a macro to do this sort of thing, and it turned > out to be easier than I expected. > > https://gist.github.com/tari/10144385 > > Use like this: > let x = match_bitfield!(do_something(

Re: [rust-dev] matching on a few bits in int

2014-04-08 Thread Peter Marheine
I had a go at building a macro to do this sort of thing, and it turned out to be easier than I expected. https://gist.github.com/tari/10144385 Use like this: let x = match_bitfield!(do_something(), bits 4 to 8 { 0 => DivideBy1, 1 => DivideBy2, 2 => DivideBy4, _

Re: [rust-dev] matching on a few bits in int

2014-04-04 Thread Vladimir Pouzanov
I've submitted an RFC for this one: https://github.com/rust-lang/rfcs/pull/29 On Sat, Mar 29, 2014 at 6:14 PM, Bill Myers wrote: > I think the best solution is to add uN and sN types where N is not a power > of two, which LLVM should already support. > > Then you can write your match like this:

Re: [rust-dev] matching on a few bits in int

2014-03-29 Thread Bill Myers
I think the best solution is to add uN and sN types where N is not a power of two, which LLVM should already support. Then you can write your match like this: match (val >> 6) as u2 { ... } And it will work as desired. Biggest issue is that to make it work nicely you'd need to add some way to

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Jared Forsyth
WOW procedural macros! Hadn't seen before that rust has those. that's so cool ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread comex
On Fri, Mar 28, 2014 at 10:43 AM, Peter Marheine wrote: > On Fri, Mar 28, 2014 at 9:30 AM, Jared Forsyth wrote: >> Is rust's macro system powerful enough to do calculations, such that you >> would be able to guarantee that the provided patterns were exhaustive? >> From what I've seen, the macro s

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Evan G
Whoops, I accidentally sent this to Peter instead of the whole list... This doesn't even need to be a very hard feature to implement, or require any new syntax. All that needs to happen is for the compiler to know that "& 0b11" means the only viable options are 00, 01, 10, and 11. True? val[6..7]

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Peter Marheine
Adding the list for Evan's benefit. :) >> On Fri, Mar 28, 2014 at 10:21 AM, Evan G wrote: >> > Sorry, I didn't see your message about an n-bit integer type. It sounds >> > like >> > a great solution! I was just responding to the "warrant a core feature". >> > I'm >> > just saying it doesn't reall

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Peter Marheine
On Fri, Mar 28, 2014 at 9:57 AM, Evan G wrote: > This doesn't even need to be a very hard feature to implement, or require > any new syntax. All that needs to happen is for the compiler to know that "& > 0b11" means the only viable options are 00, 01, 10, and 11. True? This is true, but doesn't m

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Peter Marheine
On Fri, Mar 28, 2014 at 9:30 AM, Jared Forsyth wrote: > Is rust's macro system powerful enough to do calculations, such that you > would be able to guarantee that the provided patterns were exhaustive? > From what I've seen, the macro system can mostly just do syntax expansion - > not logic, etc.

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Jared Forsyth
On Fri, Mar 28, 2014 at 7:56 AM, Peter Marheine wrote: > This sounds like a good use case for a macro, though > Is rust's macro system powerful enough to do calculations, such that you would be able to guarantee that the provided patterns were exhaustive? >From what I've seen, the macro system c

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Clark Gaebel
I think it's a great little feature that would set rust apart in the embedded development space, and perfectly viable to be a compiler feature. It's not for everyone, but the people that would find it useful would find it REALLY useful. Rust might even find a niche in safety critical software. -

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Peter Marheine
While potentially useful, I don't think this use case is common enough to warrant being a core feature. This sounds like a good use case for a macro, though. Something like: match bitfield!(val, 6..7) { 0b00 => ..., 0b01 => ..., 0b10 => ..., 0b11 => ... } c

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Vladimir Pouzanov
Sorry, that's definitely a typo, (val >> 6) & 0b11 makes sense, (val & 0b11) >> 6 does not. On Fri, Mar 28, 2014 at 1:04 PM, Clark Gaebel wrote: > I like this! Although I think that match might've been better written > `(val >> 6) & 0b11`, but it'd be really nice for the compiler to catch > tho

Re: [rust-dev] matching on a few bits in int

2014-03-28 Thread Clark Gaebel
I like this! Although I think that match might've been better written `(val >> 6) & 0b11`, but it'd be really nice for the compiler to catch those type of errors! - Clark On Fri, Mar 28, 2014 at 5:54 AM, Vladimir Pouzanov wrote: > There's one thing that I often have to deal in embedded code —

[rust-dev] matching on a few bits in int

2014-03-28 Thread Vladimir Pouzanov
There's one thing that I often have to deal in embedded code — doing match on a few bits from an I/O register, which is commonly u32: let val : u32 = ...; match (val & 0b11) >> 6 { 0b00 => ..., 0b01 => ..., 0b10 => ..., _ => {} } You can clearly see two problems here: I need to provide a