[rust-dev] Bitwise operations in rust.

Philippe Delrieu philippe.delrieu at free.fr
Wed Jan 15 08:17:25 PST 2014


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



More information about the Rust-dev mailing list