[rust-dev] Bitwise operations in rust.

John Grosen jmgrosen at gmail.com
Wed Jan 15 15:30:25 PST 2014


EnumSet (http://static.rust-lang.org/doc/master/extra/enum_set/struct.EnumSet.html) looks almost exactly like what you want — except, unfortunately, it doesn’t actually expose its `bits` field. Perhaps a new method should be added to it?

--  
John Grosen


On Wednesday, January 15, 2014 at 3:24 PM, Jan Niklas Hasse wrote:

> On Wed, Jan 15, 2014, at 08:45 AM, Philippe Delrieu wrote:
> > Thanks for you answer . I have the idea to see in the SDL port (I work  
> > on the SDL2 port) which is finished and has the same problems. They  
> > solve the problem like this :
> >  
> > pub fn unfold_bits(bitflags: u32) -> ~[SDL_WindowFlags] {
> > let flags = [SDL_WINDOW_FULLSCREEN,
> > SDL_WINDOW_OPENGL,
> > SDL_WINDOW_SHOWN,
> > SDL_WINDOW_HIDDEN,
> > SDL_WINDOW_BORDERLESS,
> > SDL_WINDOW_RESIZABLE,
> > SDL_WINDOW_MINIMIZED,
> > SDL_WINDOW_MAXIMIZED,
> > SDL_WINDOW_INPUT_GRABBED,
> > SDL_WINDOW_INPUT_FOCUS,
> > SDL_WINDOW_MOUSE_FOCUS,
> > SDL_WINDOW_FULLSCREEN_DESKTOP,
> > SDL_WINDOW_FOREIGN
> > ];
> >  
> > flags.iter().filter_map(|&flag| {
> > if bitflags & (flag as u32) != 0 { Some(flag) }
> > else { None }
> > }).collect()
> > }
> >  
> > Philippe
> >  
> > Le 15/01/2014 17:36, SiegeLord a écrit :
> > > On 01/15/2014 11:29 AM, Nicolas Silva wrote:
> > > > I think enums are not a good fit for bitwise operations, it's not really
> > > > meant for that.
> > > >  
> > >  
> > > I came to the same conclusion and came up with a nice macro to  
> > > automate that, seen here:  
> > > https://github.com/SiegeLord/RustAllegro/blob/master/src/rust_util.rs#L3..L56  
> > > and used here  
> > > https://github.com/SiegeLord/RustAllegro/blob/master/src/allegro/internal/bitmap_like.rs#L9..L23  
> > > .
> > >  
> > > This enables a nice, C-like API when composing/extracting flags via  
> > > the bit-or and bit-and operators.
> > >  
> > > -SL
> > > _______________________________________________
> > > Rust-dev mailing list
> > > Rust-dev at mozilla.org (mailto:Rust-dev at mozilla.org)
> > > https://mail.mozilla.org/listinfo/rust-dev
> > >  
> >  
> >  
> > _______________________________________________
> > Rust-dev mailing list
> > Rust-dev at mozilla.org (mailto:Rust-dev at mozilla.org)
> > https://mail.mozilla.org/listinfo/rust-dev
> >  
>  
>  
> Did you know that there are SDL2 bindings for Rust?
>  
> See:
> https://github.com/AngryLawyer/rust-sdl2/blob/master/src/sdl2/video.rs
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org (mailto:Rust-dev at mozilla.org)
> https://mail.mozilla.org/listinfo/rust-dev
>  
>  


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140115/2c600497/attachment.html>


More information about the Rust-dev mailing list