I advise you to check the tests accompanying EnumSet (in the source code):
http://static.rust-lang.org/doc/master/src/collections/home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libcollections/enum_set.rs.html#144-158
They show a simple implementation:
impl CLike for Foo {
fn to_uint(&self) -> uint {
*self as uint
}
fn from_uint(v: uint) -> Foo {
unsafe { mem::transmute(v) }
}
}
which uses transmute to avoid that manual maintenance.
Note though that in general if you wanted to add new enum values while
maintaining it sorted alphabetically and still be backward-compatible you
would need to handle the values manually.
On Fri, May 30, 2014 at 8:41 PM, Igor Bukanov <[email protected]> wrote:
> Is it possible to somehow automatically derive
> collections::enum_set::CLike for a enum? The idea of writing
>
> impl CLike for MyEnum {
> fn to_uint(&self) -> uint {
> return *self as uint;
> }
>
> fn from_uint(n: uint) -> Flag {
> match n {
> 0 => EnumConst1,
> ...
> _ => fail!("{} does not match any enum case, n)
> }
> }
> }
>
> just to get a type safe bit set EnumSet<MyEnum> is rather discouraging.
>
> On a related note I see that EnumSet never checks that CLike::to_int
> result stays below the word size. Is it a bug?
> _______________________________________________
> 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