[rust-dev] Bitwise operations in rust.

Nicolas Silva nical.silva at gmail.com
Wed Jan 15 08:29:05 PST 2014


I ran into a similar situation in my own code and did something which would
transpose to:

type WindowFlags = u32;
static SDL_WINDOW_FULLSCREEN : u32 = ffi::video::SDL_WINDOW_FULLSCREEN;
...

I think enums are not a good fit for bitwise operations, it's not really
meant for that.

Cheers,

Nical



On Wed, Jan 15, 2014 at 5:17 PM, Philippe Delrieu
<philippe.delrieu at free.fr>wrote:

> I work on the SDL2 binding and I have the following code :
>
> pub enum SDL_WindowFlags {
>     SDL_WINDOW_FULLSCREEN           = ffi::video::SDL_WINDOW_FULLSCREEN,
>     SDL_WINDOW_OPENGL               = ffi::video::SDL_WINDOW_OPENGL,
>     SDL_WINDOW_SHOWN                = ffi::video::SDL_WINDOW_SHOWN,
>     SDL_WINDOW_HIDDEN               = ffi::video::SDL_WINDOW_HIDDEN,
>     SDL_WINDOW_BORDERLESS           = ffi::video::SDL_WINDOW_BORDERLESS,
>     SDL_WINDOW_RESIZABLE            = ffi::video::SDL_WINDOW_RESIZABLE,
>     SDL_WINDOW_MINIMIZED            = ffi::video::SDL_WINDOW_MINIMIZED,
>     SDL_WINDOW_MAXIMIZED            = ffi::video::SDL_WINDOW_MAXIMIZED,
>     SDL_WINDOW_INPUT_GRABBED        = ffi::video::SDL_WINDOW_INPUT_
> GRABBED,
>     SDL_WINDOW_INPUT_FOCUS          = ffi::video::SDL_WINDOW_INPUT_FOCUS,
>     SDL_WINDOW_MOUSE_FOCUS          = ffi::video::SDL_WINDOW_MOUSE_FOCUS,
>     SDL_WINDOW_FULLSCREEN_DESKTOP   = ffi::video::SDL_WINDOW_
> FULLSCREEN_DESKTOP,
>     SDL_WINDOW_FOREIGN              = ffi::video::SDL_WINDOW_FOREIGN,
> }
>
> impl SDL_WindowFlags {
>     pub fn fold_bits(flags: &[SDL_WindowFlags]) -> Uint32 {
>         flags.iter().fold(0, |acc, &flag| acc | flag as Uint32)
>     }
> }
>
> I would like to do the opposite operation of the fold_bits function :
>     pub fn unfold_bits(bits: Uint32) -> ~[SDL_WindowFlags]    {
>         ??
>     }
>
> I don't have a simple way to do it (like the fold method) . Anybody can
> help me ?
>
> Philippe Delrieu
>
> _______________________________________________
> Rust-dev mailing list
> 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/516e6652/attachment.html>


More information about the Rust-dev mailing list