Whoops, excluded the list.
I always fancy some Erlang style binary pattern matching in rust:
case X of
<<2#00:2, _:6>> -> 1;
<<2#01:2, _:6>> -> 2;
<<2#10:2, _:6>> -> 3;
<<2#11:2, _:6>> -> 4
end.
Syntax is a bit alien but we can adapt that to the rust land.
-Ed
On Fri, Mar 28, 2014 at 9:09 PM, Vladimir Pouzanov <[email protected]>wrote:
> 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 <[email protected]>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
>> those type of errors!
>>
>> - Clark
>>
>>
>> On Fri, Mar 28, 2014 at 5:54 AM, Vladimir Pouzanov
>> <[email protected]>wrote:
>>
>>> 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 catch-all
>>> match, even if the code guarantees a limited set of values; also I lost
>>> 0b11, and there's no warning due to catch all.
>>>
>>> Is it possible to make rustc aware of such cases?
>>>
>>> What would be totally awesome is some kind of [] operator for ints, that
>>> would extract bits, like that:
>>>
>>> match val[6..7] { ... }
>>>
>>> Is that something of interest to community? I would be willing to write
>>> an RFC for that, and possibly extend the compiler.
>>>
>>> --
>>> Sincerely,
>>> Vladimir "Farcaller" Pouzanov
>>> http://farcaller.net/
>>>
>>> _______________________________________________
>>> Rust-dev mailing list
>>> [email protected]
>>> https://mail.mozilla.org/listinfo/rust-dev
>>>
>>>
>>
>>
>> --
>> Clark.
>>
>> Key ID : 0x78099922
>> Fingerprint: B292 493C 51AE F3AB D016 DD04 E5E3 C36F 5534 F907
>>
>
>
>
> --
> Sincerely,
> Vladimir "Farcaller" Pouzanov
> http://farcaller.net/
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev