From 304a95431c871f61de8a9eecead05b35d8afca40 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 23 May 2014 19:41:13 +0400 Subject: [PATCH 1/4] Added SdlResult and switched to StrBuf --- src/sdl2/sdl.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index 2126eeaa..428f8c33 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -1,5 +1,6 @@ use std::mem; use std::str; +use std::c_str::CString; // Setup linking for all targets. #[cfg(target_os="macos")] @@ -113,9 +114,8 @@ pub fn was_inited(flags: InitFlag) -> InitFlag { pub fn get_error() -> String { unsafe { - let cstr = ll::SDL_GetError(); - - str::raw::from_c_str(mem::transmute_copy(&cstr)) + let cstr = CString::new(ll::SDL_GetError(), false); + cstr.as_str().to_strbuf() } } From 9325869492559c27e21334d78fdef3a53677431a Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 23 May 2014 19:52:49 +0400 Subject: [PATCH 2/4] Migrated everything to SdlResult --- src/sdl2/audio.rs | 17 +++++----- src/sdl2/event.rs | 15 +++++---- src/sdl2/mouse.rs | 7 ++-- src/sdl2/render.rs | 79 +++++++++++++++++++++++---------------------- src/sdl2/rwops.rs | 7 ++-- src/sdl2/sdl.rs | 4 +-- src/sdl2/surface.rs | 13 ++++---- src/sdl2/version.rs | 2 +- src/sdl2/video.rs | 39 +++++++++++----------- 9 files changed, 95 insertions(+), 88 deletions(-) diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index 4177e91d..915728ba 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -9,6 +9,7 @@ use std::raw::Slice; use get_error; use rwops::RWops; +use SdlResult; #[allow(non_camel_case_types)] @@ -221,13 +222,13 @@ extern "C" fn c_audio_callback(userdata: *c_void, stream: *uint8_t, len: c_int) impl<'a> AudioSpec<'a> { - pub fn load_wav(path: &Path) -> Result<(AudioSpec, CVec), String> { + pub fn load_wav(path: &Path) -> SdlResult<(AudioSpec, CVec)> { AudioSpec::load_wav_rw(&try!(RWops::from_file(path, "rb"))) } - pub fn load_wav_rw(src: &RWops) -> Result<(AudioSpec, CVec), String> { + pub fn load_wav_rw(src: &RWops) -> SdlResult<(AudioSpec, CVec)> { assert_eq!(mem::size_of::(), mem::size_of::()); - let mut spec = unsafe { mem::uninitialized::() }; + let mut spec = unsafe { mem::uninit::() }; let audio_buf = ptr::null::(); let audio_len = 0u32; unsafe { @@ -261,9 +262,9 @@ impl AudioDevice { } } - pub fn open(device: Option<&str>, iscapture: int, spec: &AudioSpec) -> Result<(AudioDevice, AudioSpec), String> { + pub fn open(device: Option<&str>, iscapture: int, spec: &AudioSpec) -> SdlResult<(AudioDevice, AudioSpec)> { //! SDL_OpenAudioDevice - let obtained = unsafe { mem::uninitialized::() }; + let obtained = unsafe { mem::uninit::() }; unsafe { let device_c_str = match device { None => ptr::null(), @@ -334,7 +335,7 @@ impl Drop for AudioCVT { impl AudioCVT { pub fn new(src_format: AudioFormat, src_channels: u8, src_rate: int, - dst_format: AudioFormat, dst_channels: u8, dst_rate: int) -> Result { + dst_format: AudioFormat, dst_channels: u8, dst_rate: int) -> SdlResult { unsafe { let c_cvt_p = libc::malloc(mem::size_of::() as size_t) as *mut ll::SDL_AudioCVT; let ret = ll::SDL_BuildAudioCVT(c_cvt_p, @@ -348,12 +349,12 @@ impl AudioCVT { } } - pub fn convert(&self, src: CVec) -> Result, String> { + pub fn convert(&self, src: CVec) -> SdlResult> { //! Convert audio data to a desired audio format. unsafe { if (*self.raw).needed != 1 { - return Err("no convertion needed!".to_owned()) + return Err("no convertion needed!".to_strbuf()) } // set len (*self.raw).len = src.len() as c_int; diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index d12b2088..bf6560d0 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -21,6 +21,7 @@ use mouse::{Mouse, MouseState}; use scancode::ScanCode; use video; use get_error; +use SdlResult; #[doc(hidden)] #[allow(non_camel_case_types)] @@ -563,9 +564,9 @@ pub enum Event { KeyDownEvent(uint, video::Window, KeyCode, ScanCode, Mod), KeyUpEvent(uint, video::Window, KeyCode, ScanCode, Mod), /// (timestamp, window, text, start, length) - TextEditingEvent(uint, video::Window, String, int, int), + TextEditingEvent(uint, video::Window, ~str, int, int), /// (timestamp, window, text) - TextInputEvent(uint, video::Window, String), + TextInputEvent(uint, video::Window, ~str), /// (timestamp, window, which, [MouseState], x, y, xrel, yrel) MouseMotionEvent(uint, video::Window, uint, MouseState, int, int, @@ -614,7 +615,7 @@ pub enum Event { ClipboardUpdateEvent(uint), /// (timestamp, filename) - DropFileEvent(uint, String), + DropFileEvent(uint, ~str), /// (timestamp, Window, type, code) UserEvent(uint, video::Window, uint, int), @@ -1062,7 +1063,7 @@ pub fn poll_event() -> Event { } /// Wait indefinitely for the next available event. -pub fn wait_event() -> Result { +pub fn wait_event() -> SdlResult { let raw = null_event(); let success = unsafe { ll::SDL_WaitEvent(&raw) == 1 as c_int }; @@ -1071,7 +1072,7 @@ pub fn wait_event() -> Result { } /// Wait until the specified timeout (in milliseconds) for the next available event. -pub fn wait_event_timeout(timeout: int) -> Result { +pub fn wait_event_timeout(timeout: int) -> SdlResult { let raw = null_event(); let success = unsafe { ll::SDL_WaitEventTimeout(&raw, timeout as c_int) == 1 as c_int }; @@ -1133,7 +1134,7 @@ pub fn register_events(num: int) -> Option { } /// add an event to the event queue -pub fn push_event(event: Event) -> Result<(), String> { +pub fn push_event(event: Event) -> SdlResult<()> { match event.to_ll() { Some(raw_event) => { let ok = unsafe { ll::SDL_PushEvent(&raw_event) == 1 }; @@ -1141,7 +1142,7 @@ pub fn push_event(event: Event) -> Result<(), String> { else { Err(get_error()) } }, None => { - Err("Unsupport event type to push back to queue.".to_owned()) + Err("Unsupport event type to push back to queue.".to_strbuf()) } } } diff --git a/src/sdl2/mouse.rs b/src/sdl2/mouse.rs index 8adec924..5ed1aa3f 100644 --- a/src/sdl2/mouse.rs +++ b/src/sdl2/mouse.rs @@ -1,6 +1,7 @@ use std::ptr; use get_error; +use SdlResult; use surface; use video; @@ -87,7 +88,7 @@ impl Drop for Cursor { } impl Cursor { - pub fn new(data: &[u8], mask: &[u8], width: int, height: int, hot_x: int, hot_y: int) -> Result { + pub fn new(data: &[u8], mask: &[u8], width: int, height: int, hot_x: int, hot_y: int) -> SdlResult { unsafe { let raw = ll::SDL_CreateCursor(data.as_ptr(), mask.as_ptr(), @@ -103,7 +104,7 @@ impl Cursor { } // TODO: figure out how to pass Surface in here correctly - pub fn from_surface(surface: &surface::Surface, hot_x: int, hot_y: int) -> Result { + pub fn from_surface(surface: &surface::Surface, hot_x: int, hot_y: int) -> SdlResult { unsafe { let raw = ll::SDL_CreateColorCursor(surface.raw(), hot_x as i32, hot_y as i32); @@ -116,7 +117,7 @@ impl Cursor { } } - pub fn from_system(cursor: SystemCursor) -> Result { + pub fn from_system(cursor: SystemCursor) -> SdlResult { unsafe { let raw = ll::SDL_CreateSystemCursor(cursor as u32); diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index b4a61713..c4eb7714 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -4,6 +4,7 @@ use surface; use surface::Surface; use pixels; use get_error; +use SdlResult; use std::ptr; use libc; use libc::{c_int, uint32_t, c_float, c_double, c_void, size_t}; @@ -219,7 +220,7 @@ impl Drop for Renderer { } impl Renderer { - pub fn from_window(window: Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> Result, String> { + pub fn from_window(window: Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> SdlResult> { let index = match index { DriverAuto => -1, DriverIndex(x) => x @@ -236,7 +237,7 @@ impl Renderer { } } - pub fn new_with_window(width: int, height: int, window_flags: video::WindowFlags) -> Result, String> { + pub fn new_with_window(width: int, height: int, window_flags: video::WindowFlags) -> SdlResult> { let raw_window: *video::ll::SDL_Window = ptr::null(); let raw_renderer: *ll::SDL_Renderer = ptr::null(); let result = unsafe { ll::SDL_CreateWindowAndRenderer(width as c_int, height as c_int, window_flags.bits(), &raw_window, &raw_renderer) == 0}; @@ -254,7 +255,7 @@ impl Renderer { } impl Renderer { - pub fn from_surface(surface: surface::Surface) -> Result, String> { + pub fn from_surface(surface: surface::Surface) -> SdlResult> { let result = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw()) }; if result == ptr::null() { Ok(Renderer { @@ -284,7 +285,7 @@ impl Renderer { #[inline] pub fn owned(&self) -> bool { self.owned } - pub fn set_draw_color(&self, color: pixels::Color) -> Result<(), String> { + pub fn set_draw_color(&self, color: pixels::Color) -> SdlResult<()> { let ret = match color { pixels::RGB(r, g, b) => { unsafe { ll::SDL_SetRenderDrawColor(self.raw, r, g, b, 255) } @@ -297,7 +298,7 @@ impl Renderer { else { Err(get_error()) } } - pub fn get_draw_color(&self) -> Result { + pub fn get_draw_color(&self) -> SdlResult { let r: u8 = 0; let g: u8 = 0; let b: u8 = 0; @@ -310,7 +311,7 @@ impl Renderer { } } - pub fn clear(&self) -> Result<(), String> { + pub fn clear(&self) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderClear(self.raw) }; if ret == 0 { Ok(()) } else { Err(get_error()) } @@ -320,7 +321,7 @@ impl Renderer { unsafe { ll::SDL_RenderPresent(self.raw) } } - pub fn get_output_size(&self) -> Result<(int, int), String> { + pub fn get_output_size(&self) -> SdlResult<(int, int)> { let width: c_int = 0; let height: c_int = 0; @@ -333,7 +334,7 @@ impl Renderer { } } - pub fn create_texture(&self, format: pixels::PixelFormatFlag, access: TextureAccess, width: int, height: int) -> Result { + pub fn create_texture(&self, format: pixels::PixelFormatFlag, access: TextureAccess, width: int, height: int) -> SdlResult { let result = unsafe { ll::SDL_CreateTexture(self.raw, format as uint32_t, access as c_int, width as c_int, height as c_int) }; if result == ptr::null() { Err(get_error()) @@ -342,7 +343,7 @@ impl Renderer { } } - pub fn create_texture_from_surface(&self, surface: &surface::Surface) -> Result { + pub fn create_texture_from_surface(&self, surface: &surface::Surface) -> SdlResult { let result = unsafe { ll::SDL_CreateTextureFromSurface(self.raw, surface.raw()) }; if result == ptr::null() { Err(get_error()) @@ -355,7 +356,7 @@ impl Renderer { unsafe { ll::SDL_RenderTargetSupported(self.raw) == 1 } } - pub fn set_render_target(&self, texture: Option<&Texture>) -> Result<(), String> { + pub fn set_render_target(&self, texture: Option<&Texture>) -> SdlResult<()> { unsafe { let actual_texture = match texture { Some(texture) => mem::transmute(texture.raw), @@ -369,7 +370,7 @@ impl Renderer { } } - pub fn get_render_target(&self) -> Result { + pub fn get_render_target(&self) -> SdlResult { let raw = unsafe { ll::SDL_GetRenderTarget(self.raw) }; if raw == ptr::null() { @@ -382,7 +383,7 @@ impl Renderer { } } - pub fn set_logical_size(&self, width: int, height: int) -> Result<(), String> { + pub fn set_logical_size(&self, width: int, height: int) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderSetLogicalSize(self.raw, width as c_int, height as c_int) }; if ret == 0 { Ok(()) } @@ -399,7 +400,7 @@ impl Renderer { (width as int, height as int) } - pub fn set_viewport(&self, rect: &Rect) -> Result<(), String> { + pub fn set_viewport(&self, rect: &Rect) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderSetViewport(self.raw, rect) }; if ret == 0 { Ok(()) } @@ -417,7 +418,7 @@ impl Renderer { rect } - pub fn set_clip_rect(&self, rect: &Rect) -> Result<(), String> { + pub fn set_clip_rect(&self, rect: &Rect) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderSetClipRect(self.raw, rect) }; if ret == 0 { Ok(()) } @@ -435,7 +436,7 @@ impl Renderer { rect } - pub fn set_scale(&self, scale_x: f64, scale_y: f64) -> Result<(), String> { + pub fn set_scale(&self, scale_x: f64, scale_y: f64) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderSetScale(self.raw, scale_x as c_float, scale_y as c_float) }; if ret == 0 { Ok(()) } @@ -449,14 +450,14 @@ impl Renderer { (scale_x as f64, scale_y as f64) } - pub fn draw_point(&self, point: Point) -> Result<(), String> { + pub fn draw_point(&self, point: Point) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderDrawPoint(self.raw, point.x, point.y) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - pub fn draw_points(&self, points: &[Point]) -> Result<(), String> { + pub fn draw_points(&self, points: &[Point]) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderDrawPoints(self.raw, mem::transmute(points.as_ptr()), points.len() as c_int) }; @@ -465,14 +466,14 @@ impl Renderer { else { Err(get_error()) } } - pub fn draw_line(&self, start: Point, end: Point) -> Result<(), String> { + pub fn draw_line(&self, start: Point, end: Point) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderDrawLine(self.raw, start.x, start.y, end.x, end.y) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - pub fn draw_lines(&self, points: &[Point]) -> Result<(), String> { + pub fn draw_lines(&self, points: &[Point]) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderDrawLines(self.raw, mem::transmute(points.as_ptr()), points.len() as c_int) }; @@ -481,14 +482,14 @@ impl Renderer { else { Err(get_error()) } } - pub fn draw_rect(&self, rect: &Rect) -> Result<(), String> { + pub fn draw_rect(&self, rect: &Rect) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderDrawRect(self.raw, rect) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - pub fn draw_rects(&self, rects: &[Rect]) -> Result<(), String> { + pub fn draw_rects(&self, rects: &[Rect]) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderDrawRects(self.raw, mem::transmute(rects.as_ptr()), rects.len() as c_int) }; @@ -497,14 +498,14 @@ impl Renderer { else { Err(get_error()) } } - pub fn fill_rect(&self, rect: &Rect) -> Result<(), String> { + pub fn fill_rect(&self, rect: &Rect) -> Result<(), ~str> { let ret = unsafe { ll::SDL_RenderFillRect(self.raw, rect) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - pub fn fill_rects(&self, rects: &[Rect]) -> Result<(), String> { + pub fn fill_rects(&self, rects: &[Rect]) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderFillRects(self.raw, mem::transmute(rects.as_ptr()), rects.len() as c_int) }; @@ -513,7 +514,7 @@ impl Renderer { else { Err(get_error()) } } - pub fn copy(&self, texture: &Texture, src: Option, dst: Option) -> Result<(), String> { + pub fn copy(&self, texture: &Texture, src: Option, dst: Option) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderCopy( self.raw, @@ -534,7 +535,7 @@ impl Renderer { } //TODO: Check whether RendererFlip is supposed to be combinable - pub fn copy_ex(&self, texture: &Texture, src: Option, dst: Option, angle: f64, center: Option, flip: RendererFlip) -> Result<(), String> { + pub fn copy_ex(&self, texture: &Texture, src: Option, dst: Option, angle: f64, center: Option, flip: RendererFlip) -> SdlResult<()> { let ret = unsafe { ll::SDL_RenderCopyEx( self.raw, @@ -560,7 +561,7 @@ impl Renderer { else { Err(get_error()) } } - pub fn read_pixels(&self, rect: Option, format: pixels::PixelFormatFlag) -> Result, String> { + pub fn read_pixels(&self, rect: Option, format: pixels::PixelFormatFlag) -> SdlResult> { unsafe { let (actual_rect, w, h) = match rect { Some(rect) => (mem::transmute(&rect), rect.w as uint, rect.h as uint), @@ -609,7 +610,7 @@ impl Drop for Texture { impl Texture { - pub fn query(&self) -> Result { + pub fn query(&self) -> SdlResult { let format: uint32_t = 0; let access: c_int = 0; let width: c_int = 0; @@ -628,14 +629,14 @@ impl Texture { } } - pub fn set_color_mod(&self, red: u8, green: u8, blue: u8) -> Result<(), String> { + pub fn set_color_mod(&self, red: u8, green: u8, blue: u8) -> SdlResult<()> { let ret = unsafe { ll::SDL_SetTextureColorMod(self.raw, red, green, blue) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - pub fn get_color_mod(&self) -> Result<(u8, u8, u8), String> { + pub fn get_color_mod(&self) -> SdlResult<(u8, u8, u8)> { let r = 0; let g = 0; let b = 0; @@ -648,14 +649,14 @@ impl Texture { } } - pub fn set_alpha_mod(&self, alpha: u8) -> Result<(), String> { + pub fn set_alpha_mod(&self, alpha: u8) -> SdlResult<()> { let ret = unsafe { ll::SDL_SetTextureAlphaMod(self.raw, alpha) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - pub fn get_alpha_mod(&self) -> Result { + pub fn get_alpha_mod(&self) -> SdlResult { let alpha = 0; let result = unsafe { ll::SDL_GetTextureAlphaMod(self.raw, &alpha) == 0 }; @@ -666,14 +667,14 @@ impl Texture { } } - pub fn set_blend_mode(&self, blend: BlendMode) -> Result<(), String> { + pub fn set_blend_mode(&self, blend: BlendMode) -> SdlResult<()> { let ret = unsafe { ll::SDL_SetTextureBlendMode(self.raw, FromPrimitive::from_i64(blend as i64).unwrap()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } - pub fn get_blend_mode(&self) -> Result { + pub fn get_blend_mode(&self) -> SdlResult { let blend: i64 = 0; let result = unsafe { ll::SDL_GetTextureBlendMode(self.raw, &FromPrimitive::from_i64(blend as i64).unwrap()) == 0 }; if result { @@ -683,7 +684,7 @@ impl Texture { } } - pub fn update(&self, rect: Option, pixel_data: &[u8], pitch: int) -> Result<(), String> { + pub fn update(&self, rect: Option, pixel_data: &[u8], pitch: int) -> SdlResult<()> { let ret = unsafe { let actual_rect = match rect { Some(rect) => mem::transmute(&rect), @@ -697,7 +698,7 @@ impl Texture { else { Err(get_error()) } } - pub fn lock(&self, rect: Option) -> Result, String> { + pub fn lock(&self, rect: Option) -> SdlResult> { let q = try!(self.query()); unsafe { let actual_rect = match rect { @@ -721,7 +722,7 @@ impl Texture { unsafe { ll::SDL_UnlockTexture(self.raw) } } - pub fn gl_bind_texture(&self) -> Result<(f64, f64), String> { + pub fn gl_bind_texture(&self) -> SdlResult<(f64, f64)> { let texw: c_float = 0.0; let texh: c_float = 0.0; @@ -732,7 +733,7 @@ impl Texture { if result { Ok((texw as f64, texh as f64)) } else { - Err("Operation not supported".to_owned()) + Err("Operation not supported".to_strbuf()) } } @@ -753,7 +754,7 @@ impl Texture { } -pub fn get_num_render_drivers() -> Result { +pub fn get_num_render_drivers() -> SdlResult { let result = unsafe { ll::SDL_GetNumRenderDrivers() }; if result > 0 { Ok(result as int) @@ -762,7 +763,7 @@ pub fn get_num_render_drivers() -> Result { } } -pub fn get_render_driver_info(index: int) -> Result { +pub fn get_render_driver_info(index: int) -> SdlResult { let out = ll::SDL_RendererInfo { name: ptr::null(), flags: 0, diff --git a/src/sdl2/rwops.rs b/src/sdl2/rwops.rs index fadc89fa..f6dc5021 100644 --- a/src/sdl2/rwops.rs +++ b/src/sdl2/rwops.rs @@ -3,6 +3,9 @@ use std::io::IoResult; use get_error; use libc::{c_void, c_int, size_t}; +use get_error; +use SdlResult; + #[allow(non_camel_case_types)] pub mod ll { use libc::{c_uchar, uint32_t, c_char, FILE, c_void}; @@ -52,7 +55,7 @@ impl_owned_accessors!(RWops, close_on_drop) /// A structure that provides an abstract interface to stream I/O. impl RWops { - pub fn from_file(path: &Path, mode: &str) -> Result { + pub fn from_file(path: &Path, mode: &str) -> SdlResult { let raw = unsafe { ll::SDL_RWFromFile(path.to_c_str().unwrap(), mode.to_c_str().unwrap()) }; @@ -60,7 +63,7 @@ impl RWops { else { Ok(RWops{raw: raw, close_on_drop: true}) } } - pub fn from_bytes(buf: &[u8]) -> Result { + pub fn from_bytes(buf: &[u8]) -> SdlResult { let raw = unsafe { ll::SDL_RWFromConstMem(buf.as_ptr() as *c_void, buf.len() as c_int) }; diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index 428f8c33..8b32eb85 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -1,5 +1,3 @@ -use std::mem; -use std::str; use std::c_str::CString; // Setup linking for all targets. @@ -115,7 +113,7 @@ pub fn was_inited(flags: InitFlag) -> InitFlag { pub fn get_error() -> String { unsafe { let cstr = CString::new(ll::SDL_GetError(), false); - cstr.as_str().to_strbuf() + cstr.as_str().unwrap().to_strbuf() } } diff --git a/src/sdl2/surface.rs b/src/sdl2/surface.rs index ed545b96..93f0fbd7 100644 --- a/src/sdl2/surface.rs +++ b/src/sdl2/surface.rs @@ -1,6 +1,7 @@ use std::mem; use rect::Rect; use get_error; +use SdlResult; use std::ptr; use libc::c_int; use pixels; @@ -105,7 +106,7 @@ impl_raw_constructor!(Surface -> Surface (raw: *ll::SDL_Surface, owned: bool)) impl Surface { pub fn new(surface_flags: SurfaceFlag, width: int, height: int, bpp: int, - rmask: u32, gmask: u32, bmask: u32, amask: u32) -> Result { + rmask: u32, gmask: u32, bmask: u32, amask: u32) -> SdlResult { unsafe { let raw = ll::SDL_CreateRGBSurface(surface_flags.bits(), width as c_int, height as c_int, bpp as c_int, rmask, gmask, bmask, amask); @@ -166,7 +167,7 @@ impl Surface { unsafe { ll::SDL_UnlockSurface(self.raw); } } - pub fn from_bmp(path: &Path) -> Result { + pub fn from_bmp(path: &Path) -> SdlResult { let raw = unsafe { ll::SDL_LoadBMP_RW(try!(rwops::RWops::from_file(path, "rb")).raw(), 0) }; @@ -175,7 +176,7 @@ impl Surface { else { Ok(Surface{raw: raw, owned: true}) } } - pub fn save_bmp(&self, path: &Path) -> Result<(), String> { + pub fn save_bmp(&self, path: &Path) -> SdlResult<()> { let ret = unsafe { ll::SDL_SaveBMP_RW(self.raw, try!(rwops::RWops::from_file(path, "rb")).raw(), 0) }; @@ -201,7 +202,7 @@ impl Surface { } } - pub fn set_color_key(&self, enable: bool, color: pixels::Color) -> Result<(), String> { + pub fn set_color_key(&self, enable: bool, color: pixels::Color) -> SdlResult<()> { let key = color.to_u32(&self.get_pixel_format()); let result = unsafe { ll::SDL_SetColorKey(self.raw, ::std::bool::to_bit(enable), key) @@ -213,7 +214,7 @@ impl Surface { } } - pub fn get_color_key(&self) -> Result { + pub fn get_color_key(&self) -> SdlResult { let key: u32 = 0; let result = unsafe { ll::SDL_GetColorKey(self.raw, &key) @@ -237,7 +238,7 @@ impl Surface { } } - pub fn get_color_mod(&self) -> Result { + pub fn get_color_mod(&self) -> SdlResult { let r: u8 = 0; let g: u8 = 0; let b: u8 = 0; diff --git a/src/sdl2/version.rs b/src/sdl2/version.rs index 48924d31..a2e2e7d1 100644 --- a/src/sdl2/version.rs +++ b/src/sdl2/version.rs @@ -61,7 +61,7 @@ pub fn get_version() -> Version { pub fn get_revision() -> String { unsafe { let ret = ll::SDL_GetRevision(); - CString::new(ret, false).as_str().unwrap().into_owned() + CString::new(ret, false).as_str().unwrap().to_strbuf() } } diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 756c4370..d9600f4c 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -7,6 +7,7 @@ use std::vec::Vec; use rect::Rect; use surface::Surface; use pixels; +use SdlResult; use std::num::FromPrimitive; use get_error; @@ -343,7 +344,7 @@ impl Drop for Window { } impl Window { - pub fn new(title: &str, x: WindowPos, y: WindowPos, width: int, height: int, window_flags: WindowFlags) -> Result { + pub fn new(title: &str, x: WindowPos, y: WindowPos, width: int, height: int, window_flags: WindowFlags) -> SdlResult { unsafe { let raw = title.with_c_str(|buff| { ll::SDL_CreateWindow( @@ -364,7 +365,7 @@ impl Window { } } - pub fn from_id(id: u32) -> Result { + pub fn from_id(id: u32) -> SdlResult { let raw = unsafe { ll::SDL_GetWindowFromID(id) }; if raw == ptr::null() { Err(get_error()) @@ -373,7 +374,7 @@ impl Window { } } - pub fn get_display_index(&self) -> Result { + pub fn get_display_index(&self) -> SdlResult { let result = unsafe { ll::SDL_GetWindowDisplayIndex(self.raw) }; if result < 0 { return Err(get_error()) @@ -394,7 +395,7 @@ impl Window { } } - pub fn get_display_mode(&self, display_mode: &DisplayMode) -> Result { + pub fn get_display_mode(&self, display_mode: &DisplayMode) -> SdlResult { let dm = empty_sdl_display_mode(); let result = unsafe { @@ -522,7 +523,7 @@ impl Window { unsafe { ll::SDL_SetWindowFullscreen(self.raw, fullscreen_type as uint32_t) == 0 } } - pub fn get_surface(&self) -> Result { + pub fn get_surface(&self) -> SdlResult { let raw = unsafe { ll::SDL_GetWindowSurface(self.raw) }; if raw == ptr::null() { @@ -574,7 +575,7 @@ impl Window { } } - pub fn get_gamma_ramp(&self) -> Result<(Vec, Vec, Vec), String> { + pub fn get_gamma_ramp(&self) -> SdlResult<(Vec, Vec, Vec)> { let red: Vec = Vec::with_capacity(256); let green: Vec = Vec::with_capacity(256); let blue: Vec = Vec::with_capacity(256); @@ -586,7 +587,7 @@ impl Window { } } - pub fn gl_create_context(&self) -> Result { + pub fn gl_create_context(&self) -> SdlResult { let result = unsafe { ll::SDL_GL_CreateContext(self.raw) }; if result == ptr::null() { Err(get_error()) @@ -604,7 +605,7 @@ impl Window { } } -pub fn get_num_video_drivers() -> Result { +pub fn get_num_video_drivers() -> SdlResult { let result = unsafe { ll::SDL_GetNumVideoDrivers() }; if result < 0 { Err(get_error()) @@ -637,7 +638,7 @@ pub fn get_current_video_driver() -> String { } } -pub fn get_num_video_displays() -> Result { +pub fn get_num_video_displays() -> SdlResult { let result = unsafe { ll::SDL_GetNumVideoDisplays() }; if result < 0 { Err(get_error()) @@ -653,7 +654,7 @@ pub fn get_display_name(display_index: int) -> String { } } -pub fn get_display_bounds(display_index: int) -> Result { +pub fn get_display_bounds(display_index: int) -> SdlResult { let out: Rect = Rect::new(0, 0, 0, 0); let result = unsafe { ll::SDL_GetDisplayBounds(display_index as c_int, &out) == 0 }; @@ -664,7 +665,7 @@ pub fn get_display_bounds(display_index: int) -> Result { } } -pub fn get_num_display_modes(display_index: int) -> Result { +pub fn get_num_display_modes(display_index: int) -> SdlResult { let result = unsafe { ll::SDL_GetNumDisplayModes(display_index as c_int) }; if result < 0 { Err(get_error()) @@ -673,7 +674,7 @@ pub fn get_num_display_modes(display_index: int) -> Result { } } -pub fn get_display_mode(display_index: int, mode_index: int) -> Result { +pub fn get_display_mode(display_index: int, mode_index: int) -> SdlResult { let dm = empty_sdl_display_mode(); let result = unsafe { ll::SDL_GetDisplayMode(display_index as c_int, mode_index as c_int, &dm) == 0}; @@ -684,7 +685,7 @@ pub fn get_display_mode(display_index: int, mode_index: int) -> Result Result { +pub fn get_desktop_display_mode(display_index: int) -> SdlResult { let dm = empty_sdl_display_mode(); let result = unsafe { ll::SDL_GetDesktopDisplayMode(display_index as c_int, &dm) == 0}; @@ -695,7 +696,7 @@ pub fn get_desktop_display_mode(display_index: int) -> Result Result { +pub fn get_current_display_mode(display_index: int) -> SdlResult { let dm = empty_sdl_display_mode(); let result = unsafe { ll::SDL_GetCurrentDisplayMode(display_index as c_int, &dm) == 0}; @@ -706,7 +707,7 @@ pub fn get_current_display_mode(display_index: int) -> Result Result { +pub fn get_closest_display_mode(display_index: int, mode: &DisplayMode) -> SdlResult { let input = mode.to_ll(); let out = empty_sdl_display_mode(); @@ -731,7 +732,7 @@ pub fn disable_screen_saver() { unsafe { ll::SDL_DisableScreenSaver() } } -pub fn gl_load_library(path: &str) -> Result<(), String> { +pub fn gl_load_library(path: &str) -> SdlResult<()> { unsafe { path.with_c_str(|path| { if ll::SDL_GL_LoadLibrary(path) == 0 { @@ -765,7 +766,7 @@ pub fn gl_set_attribute(attr: GLAttr, value: int) -> bool { unsafe { ll::SDL_GL_SetAttribute(FromPrimitive::from_u64(attr as u64).unwrap(), value as c_int) == 0 } } -pub fn gl_get_attribute(attr: GLAttr) -> Result { +pub fn gl_get_attribute(attr: GLAttr) -> SdlResult { let out: c_int = 0; let result = unsafe { ll::SDL_GL_GetAttribute(FromPrimitive::from_u64(attr as u64).unwrap(), &out) } == 0; @@ -776,7 +777,7 @@ pub fn gl_get_attribute(attr: GLAttr) -> Result { } } -pub fn gl_get_current_window() -> Result { +pub fn gl_get_current_window() -> SdlResult { let raw = unsafe { ll::SDL_GL_GetCurrentWindow() }; if raw == ptr::null() { Err(get_error()) @@ -785,7 +786,7 @@ pub fn gl_get_current_window() -> Result { } } -pub fn gl_get_current_context() -> Result { +pub fn gl_get_current_context() -> SdlResult { let raw = unsafe { ll::SDL_GL_GetCurrentContext() }; if raw == ptr::null() { Err(get_error()) From 977845acfd6701bb4e15d4bc5ebb94986a10bf30 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 26 May 2014 20:03:26 +0400 Subject: [PATCH 3/4] Fixed incorrectly merged files --- src/sdl2/audio.rs | 4 ++-- src/sdl2/event.rs | 6 +++--- src/sdl2/render.rs | 2 +- src/sdl2/sdl.rs | 2 ++ 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index 915728ba..a6a41df3 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -228,7 +228,7 @@ impl<'a> AudioSpec<'a> { pub fn load_wav_rw(src: &RWops) -> SdlResult<(AudioSpec, CVec)> { assert_eq!(mem::size_of::(), mem::size_of::()); - let mut spec = unsafe { mem::uninit::() }; + let mut spec = unsafe { mem::uninitialized::() }; let audio_buf = ptr::null::(); let audio_len = 0u32; unsafe { @@ -264,7 +264,7 @@ impl AudioDevice { pub fn open(device: Option<&str>, iscapture: int, spec: &AudioSpec) -> SdlResult<(AudioDevice, AudioSpec)> { //! SDL_OpenAudioDevice - let obtained = unsafe { mem::uninit::() }; + let obtained = unsafe { mem::uninitialized::() }; unsafe { let device_c_str = match device { None => ptr::null(), diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index bf6560d0..5b494a3c 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -564,9 +564,9 @@ pub enum Event { KeyDownEvent(uint, video::Window, KeyCode, ScanCode, Mod), KeyUpEvent(uint, video::Window, KeyCode, ScanCode, Mod), /// (timestamp, window, text, start, length) - TextEditingEvent(uint, video::Window, ~str, int, int), + TextEditingEvent(uint, video::Window, String, int, int), /// (timestamp, window, text) - TextInputEvent(uint, video::Window, ~str), + TextInputEvent(uint, video::Window, String), /// (timestamp, window, which, [MouseState], x, y, xrel, yrel) MouseMotionEvent(uint, video::Window, uint, MouseState, int, int, @@ -615,7 +615,7 @@ pub enum Event { ClipboardUpdateEvent(uint), /// (timestamp, filename) - DropFileEvent(uint, ~str), + DropFileEvent(uint, String), /// (timestamp, Window, type, code) UserEvent(uint, video::Window, uint, int), diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index c4eb7714..b4fbbc4a 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -498,7 +498,7 @@ impl Renderer { else { Err(get_error()) } } - pub fn fill_rect(&self, rect: &Rect) -> Result<(), ~str> { + pub fn fill_rect(&self, rect: &Rect) -> Result<(), String> { let ret = unsafe { ll::SDL_RenderFillRect(self.raw, rect) }; if ret == 0 { Ok(()) } diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index 8b32eb85..7eaa72c0 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -83,6 +83,8 @@ pub enum Error { UnsupportedError = ll::SDL_UNSUPPORTED as int } +pub type SdlResult = Result; + pub fn init(flags: InitFlag) -> bool { unsafe { ll::SDL_Init(flags.bits()) == 0 From fd596ed90bf44d59a655fd7e278bf8b870edb8e0 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 26 May 2014 20:06:45 +0400 Subject: [PATCH 4/4] Switched from to_strbuf to into_owned --- src/sdl2/audio.rs | 2 +- src/sdl2/event.rs | 2 +- src/sdl2/render.rs | 2 +- src/sdl2/sdl.rs | 2 +- src/sdl2/version.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index a6a41df3..42425216 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -354,7 +354,7 @@ impl AudioCVT { unsafe { if (*self.raw).needed != 1 { - return Err("no convertion needed!".to_strbuf()) + return Err("no convertion needed!".into_owned()) } // set len (*self.raw).len = src.len() as c_int; diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index 5b494a3c..9c56311b 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -1142,7 +1142,7 @@ pub fn push_event(event: Event) -> SdlResult<()> { else { Err(get_error()) } }, None => { - Err("Unsupport event type to push back to queue.".to_strbuf()) + Err("Unsupport event type to push back to queue.".into_owned()) } } } diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index b4fbbc4a..08a03988 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -733,7 +733,7 @@ impl Texture { if result { Ok((texw as f64, texh as f64)) } else { - Err("Operation not supported".to_strbuf()) + Err("Operation not supported".into_owned()) } } diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index 7eaa72c0..6d8a0dfd 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -115,7 +115,7 @@ pub fn was_inited(flags: InitFlag) -> InitFlag { pub fn get_error() -> String { unsafe { let cstr = CString::new(ll::SDL_GetError(), false); - cstr.as_str().unwrap().to_strbuf() + cstr.as_str().unwrap().into_owned() } } diff --git a/src/sdl2/version.rs b/src/sdl2/version.rs index a2e2e7d1..48924d31 100644 --- a/src/sdl2/version.rs +++ b/src/sdl2/version.rs @@ -61,7 +61,7 @@ pub fn get_version() -> Version { pub fn get_revision() -> String { unsafe { let ret = ll::SDL_GetRevision(); - CString::new(ret, false).as_str().unwrap().to_strbuf() + CString::new(ret, false).as_str().unwrap().into_owned() } }