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 => ...
    }

could expand to

    match (val >> 6) & ((1 << 2) - 1) {
        0b00 => ...,
        ...
        _ => unreachable!()
    }

wherein the bitfield! macro either emits either an arbitrary-sized
type (I'm not sure how feasible this is-- LLVM allows arbitrary-width
integers, but I don't know how that would work with rustc) or is able
to verify on its own that the provided patterns are exhaustive for a
n-bit value (in which case the macro must contain the match block as
well).

On Fri, Mar 28, 2014 at 8:04 AM, 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
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>



-- 
Peter Marheine
Don't Panic
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to