[rust-dev] Static_assert on a hash

Vladimir Pouzanov farcaller at gmail.com
Sun Mar 30 02:30:40 PDT 2014


That shifts the issue even more into runtime, doesn't it?

My goal is to find a way to resolve a tuple of (u8, u8, FuncNameEnum) to
u8, where FuncNameEnum is a long enum of all possible functions. And I need
to do that in compile time.

One way I see that is to make a really long list of enums like:

PORT0_PIN0_GPIO,
PORT0_PIN0_RD1,
...

for all possible port/pin/function combinations, then I can make a macro to
match on that to port/pin/fun u8 (that are actually used to configure
registers). The only thing is that I'd like to pass port and pin in as u8
values, that makes it much more readable.

Well... Maybe it's time to do some big macro for matching all the stuff and
hope that compiler will optimise it anyway.


On Sun, Mar 30, 2014 at 10:12 AM, Simon Sapin <simon.sapin at exyr.org> wrote:

> On 30/03/2014 09:50, Vladimir Pouzanov wrote:
>
>> I have a chunk of code that toggles different functions on hardware
>> pins, that looks like this:
>>
>>    enum Function {
>>      GPIO = 0,
>>      F1 = 1,
>>      F2 = 2,
>>      F3 = 3,
>>    }
>>    fn set_mode(port: u8, pin: u8, fun: Function)
>>
>> What I would like to have is an enum of human-readable values instead of
>> F1/F2/F3
>>
>> [...]
>>
>>
>> let fun_idx: u8 = FUNCTIONS[port][pin][fun]
>>
>
>
> I don’t know if you can directly access the static strings for the
> variants’ names behind this, but you can use #[deriving(Show)] to get a
> string representation of any enum value:
>
> For example:
>
>     #[deriving(Show)]
>
>     enum Function {
>         GPIO = 0,
>         F1 = 1,
>         F2 = 2,
>         F3 = 3,
>     }
>
>     fn main() {
>         let f = GPIO;
>
>         println!("{:?}", f)
>
>         let s: ~str = format!("{:?}", f);
>         println!("{}", s)
>     }
>
> Output:
>
>     GPIO
>     GPIO
>
> --
> Simon Sapin
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>



-- 
Sincerely,
Vladimir "Farcaller" Pouzanov
http://farcaller.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140330/196c1e7a/attachment.html>


More information about the Rust-dev mailing list