From 967c7d828aacd9f788973c05acabafc194bd5772 Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Mon, 10 Jun 2013 17:27:18 -0400 Subject: [PATCH 1/3] Replace str::raw::buf_as_slice with c_str_to_static_slice. Close #3843. --- src/libstd/rt/uv/mod.rs | 16 +--------------- src/libstd/str.rs | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/libstd/rt/uv/mod.rs b/src/libstd/rt/uv/mod.rs index 10c8b84bc512a..dd66a76eead91 100644 --- a/src/libstd/rt/uv/mod.rs +++ b/src/libstd/rt/uv/mod.rs @@ -238,20 +238,6 @@ pub fn last_uv_error>(watcher: &W) -> UvError { } pub fn uv_error_to_io_error(uverr: UvError) -> IoError { - - // XXX: Could go in str::raw - unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str { - let s = s as *u8; - let mut (curr, len) = (s, 0u); - while *curr != 0u8 { - len += 1u; - curr = ptr::offset(s, len); - } - - str::raw::buf_as_slice(s, len, |d| cast::transmute(d)) - } - - unsafe { // Importing error constants use rt::uv::uvll::*; @@ -259,7 +245,7 @@ pub fn uv_error_to_io_error(uverr: UvError) -> IoError { // uv error descriptions are static let c_desc = uvll::strerror(&*uverr); - let desc = c_str_to_static_slice(c_desc); + let desc = str::raw::c_str_to_static_slice(c_desc); let kind = match uverr.code { UNKNOWN => OtherIoError, diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 8cd69f32e4969..f270964c3b569 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1396,12 +1396,19 @@ pub mod raw { /// Converts a byte to a string. pub unsafe fn from_byte(u: u8) -> ~str { raw::from_bytes([u]) } - /// Form a slice from a *u8 buffer of the given length without copying. - pub unsafe fn buf_as_slice(buf: *u8, len: uint, - f: &fn(v: &str) -> T) -> T { - let v = (buf, len + 1); + /// Form a slice from a C string. Unsafe because the caller must ensure the + /// C string has the static lifetime, or else the return value may be + /// invalidated later. + pub unsafe fn c_str_to_static_slice(s: *libc::c_char) -> &'static str { + let s = s as *u8; + let mut (curr, len) = (s, 0u); + while *curr != 0u8 { + len += 1u; + curr = ptr::offset(s, len); + } + let v = (s, len + 1); assert!(is_utf8(::cast::transmute(v))); - f(::cast::transmute(v)) + ::cast::transmute(v) } /** From d25fae0e10d92d7329211170a5b38b104a5093f0 Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Mon, 10 Jun 2013 17:48:14 -0400 Subject: [PATCH 2/3] Remove 'this could be clearer' FIXME. Looks fine. Close #2618. --- src/libextra/timer.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libextra/timer.rs b/src/libextra/timer.rs index f251336666309..7a4ad34b50892 100644 --- a/src/libextra/timer.rs +++ b/src/libextra/timer.rs @@ -131,7 +131,6 @@ pub fn recv_timeout(iotask: &IoTask, unsafe { let wait_po = cast::transmute_mut(wait_po); - // FIXME: This could be written clearer (#2618) either::either( |_| { None From 8081aea3b86d9e25746ff03acabe53c3644b600c Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Mon, 10 Jun 2013 18:18:04 -0400 Subject: [PATCH 3/3] Tag a bunch of destructors that need mutable self with FIXME for #4330. Close #4943. --- src/libstd/pipes.rs | 1 + src/libstd/rt/rc.rs | 2 +- src/libstd/run.rs | 2 +- src/libstd/task/spawn.rs | 1 + src/libstd/unstable/atomics.rs | 1 + 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libstd/pipes.rs b/src/libstd/pipes.rs index 1137540ae7061..012ad0ed80d26 100644 --- a/src/libstd/pipes.rs +++ b/src/libstd/pipes.rs @@ -315,6 +315,7 @@ struct BufferResource { impl Drop for BufferResource { fn finalize(&self) { unsafe { + // FIXME(#4330) Need self by value to get mutability. let this: &mut BufferResource = transmute(self); let mut b = move_it!(this.buffer); diff --git a/src/libstd/rt/rc.rs b/src/libstd/rt/rc.rs index 1c0c8c14fdfa6..2977d08150812 100644 --- a/src/libstd/rt/rc.rs +++ b/src/libstd/rt/rc.rs @@ -78,7 +78,7 @@ impl Drop for RC { assert!(self.refcount() > 0); unsafe { - // XXX: Mutable finalizer + // FIXME(#4330) Need self by value to get mutability. let this: &mut RC = cast::transmute_mut(self); match *this.get_mut_state() { diff --git a/src/libstd/run.rs b/src/libstd/run.rs index 41bc573f10d3f..85015c9bc5e41 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -429,7 +429,7 @@ impl Process { impl Drop for Process { fn finalize(&self) { - // FIXME #4943: transmute is bad. + // FIXME(#4330) Need self by value to get mutability. let mut_self: &mut Process = unsafe { cast::transmute(self) }; mut_self.finish(); diff --git a/src/libstd/task/spawn.rs b/src/libstd/task/spawn.rs index 87e9296657f48..bc409e066330b 100644 --- a/src/libstd/task/spawn.rs +++ b/src/libstd/task/spawn.rs @@ -323,6 +323,7 @@ impl Drop for TCB { // Runs on task exit. fn finalize(&self) { unsafe { + // FIXME(#4330) Need self by value to get mutability. let this: &mut TCB = transmute(self); // If we are failing, the whole taskgroup needs to die. diff --git a/src/libstd/unstable/atomics.rs b/src/libstd/unstable/atomics.rs index 58d0c01f990d0..856f4e6e3c196 100644 --- a/src/libstd/unstable/atomics.rs +++ b/src/libstd/unstable/atomics.rs @@ -275,6 +275,7 @@ impl Drop for AtomicOption { // This will ensure that the contained data is // destroyed, unless it's null. unsafe { + // FIXME(#4330) Need self by value to get mutability. let this : &mut AtomicOption = cast::transmute(self); let _ = this.take(SeqCst); }