From fda6f0a74adf211be1e55d58253f14ed5fffd9ea Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Fri, 25 Jul 2014 22:29:05 +0300 Subject: [PATCH 1/2] Latest Rust fixes --- src/codegen/main.rs | 6 +++--- src/sdl2/audio.rs | 10 ++++++---- src/sdl2/clipboard.rs | 6 +++--- src/sdl2/event.rs | 4 ++-- src/sdl2/filesystem.rs | 6 +++--- src/sdl2/keyboard.rs | 8 +++++--- src/sdl2/render.rs | 4 ++-- src/sdl2/video.rs | 10 +++++----- 8 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/codegen/main.rs b/src/codegen/main.rs index b3acf521..2e2383aa 100644 --- a/src/codegen/main.rs +++ b/src/codegen/main.rs @@ -21,18 +21,18 @@ fn main() { os::set_exit_status(1); }, 3 => { - let output_dir = GenericPath::new(args.get(2).as_slice()); + let output_dir = GenericPath::new(args[2].as_slice()); match mkdir_recursive(&output_dir, UserDir) { Err(e) => fail!("Could not create directory for generated sources: {:s}", e.desc), Ok(_) => {}, }; - if "keycode.rs" == args.get(1).as_slice() { + if "keycode.rs" == args[1].as_slice() { match keycode::generate(&output_dir) { Ok(_) => {}, Err(e) => fail!("Could not automatically generate sources for keycodes: {:s}", e.desc), }; - } else if "scancode.rs" == args.get(1).as_slice() { + } else if "scancode.rs" == args[1].as_slice() { match scancode::generate(&output_dir) { Ok(_) => {}, Err(e) => fail!("Could not automatically generate sources for scancodes: {:s}", e.desc), diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index 366ee230..7401c8cb 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -221,11 +221,12 @@ extern "C" fn c_audio_callback(userdata: *const c_void, stream: *const uint8_t, impl<'a> AudioSpec<'a> { - pub fn load_wav(path: &Path) -> SdlResult<(AudioSpec, CVec)> { - AudioSpec::load_wav_rw(&try!(RWops::from_file(path, "rb"))) + pub fn load_wav<'b>(path: &Path) -> SdlResult<(AudioSpec<'b>, CVec)> { + let ops = try!(RWops::from_file(path, "rb")); + AudioSpec::load_wav_rw(&ops) } - pub fn load_wav_rw(src: &RWops) -> SdlResult<(AudioSpec, CVec)> { + pub fn load_wav_rw<'b>(src: &RWops) -> SdlResult<(AudioSpec<'b>, CVec)> { assert_eq!(mem::size_of::(), mem::size_of::()); let mut spec = unsafe { mem::uninitialized::() }; let audio_buf = ptr::null::(); @@ -261,7 +262,8 @@ impl AudioDevice { } } - pub fn open(device: Option<&str>, iscapture: int, spec: &AudioSpec) -> SdlResult<(AudioDevice, AudioSpec)> { + pub fn open<'a>(device: Option<&str>, iscapture: int, spec: &AudioSpec) + -> SdlResult<(AudioDevice, AudioSpec<'a>)> { //! SDL_OpenAudioDevice let obtained = unsafe { mem::uninitialized::() }; unsafe { diff --git a/src/sdl2/clipboard.rs b/src/sdl2/clipboard.rs index 8b378466..72e192c8 100644 --- a/src/sdl2/clipboard.rs +++ b/src/sdl2/clipboard.rs @@ -1,4 +1,4 @@ -use std::str; +use std::string; use SdlResult; use get_error; @@ -31,8 +31,8 @@ pub fn set_clipboard_text(text: &String) -> SdlResult<()> { pub fn get_clipboard_text() -> SdlResult { let result = unsafe { - let cstr = ll::SDL_GetClipboardText(); - str::raw::from_c_str(cstr) + let cstr = ll::SDL_GetClipboardText() as *const u8; + string::raw::from_buf(cstr) }; if result.len() == 0 { diff --git a/src/sdl2/event.rs b/src/sdl2/event.rs index 4bf9dc2f..2192b294 100644 --- a/src/sdl2/event.rs +++ b/src/sdl2/event.rs @@ -5,7 +5,7 @@ Event Handling use std::mem; use libc::{c_int, c_void, uint32_t}; use std::num::FromPrimitive; -use std::str; +use std::string; use std::ptr; use controller; @@ -991,7 +991,7 @@ impl Event { DropFileEventType => { let event = *raw.drop(); - let text = str::raw::from_c_str(event.file); + let text = string::raw::from_buf(event.file as *const u8); ll::SDL_free(event.file as *const c_void); DropFileEvent(event.timestamp as uint, text) diff --git a/src/sdl2/filesystem.rs b/src/sdl2/filesystem.rs index cb2b569b..f7317c19 100644 --- a/src/sdl2/filesystem.rs +++ b/src/sdl2/filesystem.rs @@ -1,4 +1,4 @@ -use std::str; +use std::string; use SdlResult; use get_error; @@ -15,7 +15,7 @@ pub mod ll { pub fn get_base_path() -> SdlResult { let result = unsafe { let cstr = ll::SDL_GetBasePath(); - str::raw::from_c_str(cstr) + string::raw::from_buf(cstr as *const u8) }; if result.len() == 0 { @@ -32,7 +32,7 @@ pub fn get_pref_path(org: &str, app: &str) -> SdlResult { app.with_c_str(|app_cstr| { ll::SDL_GetPrefPath(org_cstr, app_cstr) })}); - str::raw::from_c_str(cstr) + string::raw::from_buf(cstr as *const u8) }; if result.len() == 0 { diff --git a/src/sdl2/keyboard.rs b/src/sdl2/keyboard.rs index b387afcb..d594060a 100644 --- a/src/sdl2/keyboard.rs +++ b/src/sdl2/keyboard.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::num::FromPrimitive; use std::ptr; -use std::str; +use std::string; use std::vec; use keycode::KeyCode; @@ -116,7 +116,8 @@ pub fn get_scancode_from_key(key: KeyCode) -> ScanCode { pub fn get_scancode_name(scancode: ScanCode) -> String { unsafe { - str::raw::from_c_str(ll::SDL_GetScancodeName(scancode.code() as u32)) + let scancode_name = ll::SDL_GetScancodeName(scancode.code() as u32); + string::raw::from_buf(scancode_name as *const u8) } } @@ -130,7 +131,8 @@ pub fn get_scancode_from_name(name: &str) -> ScanCode { pub fn get_key_name(key: KeyCode) -> String { unsafe { - str::raw::from_c_str(ll::SDL_GetKeyName(key.code())) + let key_name = ll::SDL_GetKeyName(key.code()); + string::raw::from_buf(key_name as *const u8) } } diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index d6c92bea..49e7d98c 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -8,7 +8,7 @@ use SdlResult; use std::ptr; use libc; use libc::{c_int, uint32_t, c_float, c_double, c_void, size_t}; -use std::str; +use std::string; use std::mem; use rect::Point; use rect::Rect; @@ -191,7 +191,7 @@ impl RendererInfo { }).collect(); RendererInfo { - name: str::raw::from_c_str(mem::transmute_copy(&info.name)), + name: string::raw::from_buf(mem::transmute_copy(&info.name)), flags: actual_flags, texture_formats: texture_formats, max_texture_width: info.max_texture_width as int, diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 6815b31e..4674a9f2 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -1,6 +1,6 @@ use libc::{c_int, c_float, uint32_t}; use std::ptr; -use std::str; +use std::string; use std::mem; use std::vec::Vec; @@ -436,7 +436,7 @@ impl Window { pub fn get_title(&self) -> String { unsafe { let cstr = ll::SDL_GetWindowTitle(self.raw); - str::raw::from_c_str(mem::transmute_copy(&cstr)) + string::raw::from_buf(mem::transmute_copy(&cstr)) } } @@ -617,7 +617,7 @@ pub fn get_num_video_drivers() -> SdlResult { pub fn get_video_driver(id: int) -> String { unsafe { let cstr = ll::SDL_GetVideoDriver(id as c_int); - str::raw::from_c_str(mem::transmute_copy(&cstr)) + string::raw::from_buf(mem::transmute_copy(&cstr)) } } @@ -634,7 +634,7 @@ pub fn video_quit() { pub fn get_current_video_driver() -> String { unsafe { let cstr = ll::SDL_GetCurrentVideoDriver(); - str::raw::from_c_str(mem::transmute_copy(&cstr)) + string::raw::from_buf(mem::transmute_copy(&cstr)) } } @@ -650,7 +650,7 @@ pub fn get_num_video_displays() -> SdlResult { pub fn get_display_name(display_index: int) -> String { unsafe { let cstr = ll::SDL_GetDisplayName(display_index as c_int); - str::raw::from_c_str(mem::transmute_copy(&cstr)) + string::raw::from_buf(mem::transmute_copy(&cstr)) } } From 542a3b646e0c6c3a6db732cfd1b8c02af1dfe21d Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Fri, 25 Jul 2014 23:57:25 +0300 Subject: [PATCH 2/2] Removed extra lifetimes --- src/sdl2/audio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index 7401c8cb..b2402174 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -221,12 +221,12 @@ extern "C" fn c_audio_callback(userdata: *const c_void, stream: *const uint8_t, impl<'a> AudioSpec<'a> { - pub fn load_wav<'b>(path: &Path) -> SdlResult<(AudioSpec<'b>, CVec)> { + pub fn load_wav(path: &Path) -> SdlResult<(AudioSpec<'a>, CVec)> { let ops = try!(RWops::from_file(path, "rb")); AudioSpec::load_wav_rw(&ops) } - pub fn load_wav_rw<'b>(src: &RWops) -> SdlResult<(AudioSpec<'b>, CVec)> { + pub fn load_wav_rw(src: &RWops) -> SdlResult<(AudioSpec<'a>, CVec)> { assert_eq!(mem::size_of::(), mem::size_of::()); let mut spec = unsafe { mem::uninitialized::() }; let audio_buf = ptr::null::();