diff --git a/src/sdl2_image/lib.rs b/src/sdl2_image/lib.rs index b4805924..81baa46d 100644 --- a/src/sdl2_image/lib.rs +++ b/src/sdl2_image/lib.rs @@ -19,6 +19,9 @@ use sdl2::rwops::RWops; use sdl2::version::Version; use sdl2::get_error; +// FIXME: this should be done in rust-sdl2 +pub type SdlResult = Result; + // Setup linking for all targets. #[cfg(target_os="macos")] mod mac { @@ -55,19 +58,19 @@ bitflags!(flags InitFlag : u32 { pub trait LoadSurface { // Self is only returned here to type hint to the compiler. // The syntax for type hinting in this case is not yet defined. - // The intended return value is Result<~Surface, ~str>. - fn from_file(filename: &Path) -> Result; - fn from_xpm_array(xpm: **i8) -> Result; + // The intended return value is SdlResult<~Surface>. + fn from_file(filename: &Path) -> SdlResult; + fn from_xpm_array(xpm: **i8) -> SdlResult; } /// Method extensions to Surface for saving to disk pub trait SaveSurface { - fn save(&self, filename: &Path) -> Result<(), ~str>; - fn save_rw(&self, dst: &mut RWops) -> Result<(), ~str>; + fn save(&self, filename: &Path) -> SdlResult<()>; + fn save_rw(&self, dst: &mut RWops) -> SdlResult<()>; } impl LoadSurface for Surface { - fn from_file(filename: &Path) -> Result { + fn from_file(filename: &Path) -> SdlResult { //! Loads an SDL Surface from a file unsafe { let raw = ffi::IMG_Load(filename.to_c_str().unwrap()); @@ -79,7 +82,7 @@ impl LoadSurface for Surface { } } - fn from_xpm_array(xpm: **i8) -> Result { + fn from_xpm_array(xpm: **i8) -> SdlResult { //! Loads an SDL Surface from XPM data unsafe { let raw = ffi::IMG_ReadXPMFromArray(xpm as **c_char); @@ -93,7 +96,7 @@ impl LoadSurface for Surface { } impl SaveSurface for Surface { - fn save(&self, filename: &Path) -> Result<(), ~str> { + fn save(&self, filename: &Path) -> SdlResult<()> { //! Saves an SDL Surface to a file unsafe { let status = ffi::IMG_SavePNG(self.raw, @@ -106,7 +109,7 @@ impl SaveSurface for Surface { } } - fn save_rw(&self, dst: &mut RWops) -> Result<(), ~str> { + fn save_rw(&self, dst: &mut RWops) -> SdlResult<()> { //! Saves an SDL Surface to an RWops unsafe { let status = ffi::IMG_SavePNG_RW(self.raw, dst.raw, 0); @@ -122,11 +125,11 @@ impl SaveSurface for Surface { /// Method extensions for creating Textures from a Renderer pub trait LoadTexture { - fn load_texture(&self, filename: &Path) -> Result; + fn load_texture(&self, filename: &Path) -> SdlResult; } -impl LoadTexture for Renderer { - fn load_texture(&self, filename: &Path) -> Result { +impl LoadTexture for Renderer { + fn load_texture(&self, filename: &Path) -> SdlResult { //! Loads an SDL Texture from a file unsafe { let raw = ffi::IMG_LoadTexture(self.raw, @@ -162,7 +165,7 @@ pub fn get_linked_version() -> Version { } #[inline] -fn to_surface_result(raw: *sdl2::surface::ll::SDL_Surface) -> Result { +fn to_surface_result(raw: *sdl2::surface::ll::SDL_Surface) -> SdlResult { if raw == ptr::null() { Err(get_error()) } else { @@ -172,25 +175,25 @@ fn to_surface_result(raw: *sdl2::surface::ll::SDL_Surface) -> Result Result; + fn load(&self) -> SdlResult; /// load as a surface. This can load all supported image formats. - fn load_typed(&self, _type: &str) -> Result; + fn load_typed(&self, _type: &str) -> SdlResult; - fn load_cur(&self) -> Result; - fn load_ico(&self) -> Result; - fn load_bmp(&self) -> Result; - fn load_pnm(&self) -> Result; - fn load_xpm(&self) -> Result; - fn load_xcf(&self) -> Result; - fn load_pcx(&self) -> Result; - fn load_gif(&self) -> Result; - fn load_jpg(&self) -> Result; - fn load_tif(&self) -> Result; - fn load_png(&self) -> Result; - fn load_tga(&self) -> Result; - fn load_lbm(&self) -> Result; - fn load_xv(&self) -> Result; - fn load_webp(&self) -> Result; + fn load_cur(&self) -> SdlResult; + fn load_ico(&self) -> SdlResult; + fn load_bmp(&self) -> SdlResult; + fn load_pnm(&self) -> SdlResult; + fn load_xpm(&self) -> SdlResult; + fn load_xcf(&self) -> SdlResult; + fn load_pcx(&self) -> SdlResult; + fn load_gif(&self) -> SdlResult; + fn load_jpg(&self) -> SdlResult; + fn load_tif(&self) -> SdlResult; + fn load_png(&self) -> SdlResult; + fn load_tga(&self) -> SdlResult; + fn load_lbm(&self) -> SdlResult; + fn load_xv(&self) -> SdlResult; + fn load_webp(&self) -> SdlResult; fn is_cur(&self) -> bool; fn is_ico(&self) -> bool; @@ -209,76 +212,76 @@ pub trait ImageRWops { } impl ImageRWops for RWops { - fn load(&self) -> Result { + fn load(&self) -> SdlResult { let raw = unsafe { ffi::IMG_Load_RW(self.raw, 0) }; to_surface_result(raw) } - fn load_typed(&self, _type: &str) -> Result { + fn load_typed(&self, _type: &str) -> SdlResult { let raw = unsafe { ffi::IMG_LoadTyped_RW(self.raw, 0, _type.to_c_str().unwrap()) }; to_surface_result(raw) } - fn load_cur(&self) -> Result { + fn load_cur(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadCUR_RW(self.raw) }; to_surface_result(raw) } - fn load_ico(&self) -> Result { + fn load_ico(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadICO_RW(self.raw) }; to_surface_result(raw) } - fn load_bmp(&self) -> Result { + fn load_bmp(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadBMP_RW(self.raw) }; to_surface_result(raw) } - fn load_pnm(&self) -> Result { + fn load_pnm(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadPNM_RW(self.raw) }; to_surface_result(raw) } - fn load_xpm(&self) -> Result { + fn load_xpm(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadXPM_RW(self.raw) }; to_surface_result(raw) } - fn load_xcf(&self) -> Result { + fn load_xcf(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadXCF_RW(self.raw) }; to_surface_result(raw) } - fn load_pcx(&self) -> Result { + fn load_pcx(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadPCX_RW(self.raw) }; to_surface_result(raw) } - fn load_gif(&self) -> Result { + fn load_gif(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadGIF_RW(self.raw) }; to_surface_result(raw) } - fn load_jpg(&self) -> Result { + fn load_jpg(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadJPG_RW(self.raw) }; to_surface_result(raw) } - fn load_tif(&self) -> Result { + fn load_tif(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadTIF_RW(self.raw) }; to_surface_result(raw) } - fn load_png(&self) -> Result { + fn load_png(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadPNG_RW(self.raw) }; to_surface_result(raw) } - fn load_tga(&self) -> Result { + fn load_tga(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadTGA_RW(self.raw) }; to_surface_result(raw) } - fn load_lbm(&self) -> Result { + fn load_lbm(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadLBM_RW(self.raw) }; to_surface_result(raw) } - fn load_xv(&self) -> Result { + fn load_xv(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadXV_RW(self.raw) }; to_surface_result(raw) } - fn load_webp(&self) -> Result { + fn load_webp(&self) -> SdlResult { let raw = unsafe { ffi::IMG_LoadWEBP_RW(self.raw) }; to_surface_result(raw) }