[rust-dev] Bitwise operations in rust.

Jan Niklas Hasse jhasse at fastmail.fm
Wed Jan 15 15:24:41 PST 2014


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
> > https://mail.mozilla.org/listinfo/rust-dev
> >
> >
> 
> _______________________________________________
> Rust-dev mailing list
> 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


More information about the Rust-dev mailing list