Rust-SDL2 / rust-sdl2

SDL2 bindings for Rust
MIT License
2.77k stars 474 forks source link

Should all SDL types have raw() accessors? #309

Open nukep opened 9 years ago

nukep commented 9 years ago

Back when "refactoring", I inadvertently broke the rust-sdl2_image library by removing the raw accessor from Renderer: https://github.com/AngryLawyer/rust-sdl2/commit/6070bfb19c3c75ee3bcd015c4af510c6ca17666d#commitcomment-9502773

Turns out the raw accessors are useful for more than just internal use.

For anyone extending rust-sdl2 to provide functionality that's out of scope here (such as sdl2-mixer, sdl2-ttf, etc.), it could be useful to obtain the raw pointers.

If we decide to do this, I propose adding a raw_mut() accessor as well. This will be especially important for when https://github.com/AngryLawyer/rust-sdl2/issues/108 is addressed):

#[inline] pub unsafe fn raw(&self) -> *const ll::SDL_Renderer { self.raw }

#[inline] pub unsafe fn raw_mut(&self) -> *mut ll::SDL_Renderer {
    use std::mem;
    unsafe { mem::transmute(self.raw) }
}

raw_mut() would accept the immutable &self to not break things like Renderer.

Thoughts?

AngryLawyer commented 9 years ago

I'd say yeah. As long as we mark things unsafe, and document them as 'Don't use this unless you know what you're doing!', then it makes sense.

nukep commented 9 years ago

How about from_ll() as well, for raw -> wrapped conversions? https://github.com/xsleonard/rust-sdl2_image/pull/28 needs to interop with textures and obtain the rust-sdl2 types from raw pointers.

AngryLawyer commented 9 years ago

Definitely.