From 1868cf50736e76add8355f5104fe4fab437e8153 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 6 May 2014 23:28:32 -0700 Subject: [PATCH 1/5] std: Remove bump_box_refcount. Deprecated and unused. Deprecused. --- src/libcore/cast.rs | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/libcore/cast.rs b/src/libcore/cast.rs index 8cea197fbfa4a..25b6fd1b2cf1c 100644 --- a/src/libcore/cast.rs +++ b/src/libcore/cast.rs @@ -33,13 +33,6 @@ pub unsafe fn transmute_copy(src: &T) -> U { #[inline] pub unsafe fn forget(thing: T) { intrinsics::forget(thing); } -/** - * Force-increment the reference count on a shared box. If used - * carelessly, this can leak the box. - */ -#[inline] -pub unsafe fn bump_box_refcount(t: @T) { forget(t); } - /** * Transform a value of one type into a value of another type. * Both types must have the same size and alignment. @@ -106,7 +99,7 @@ pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T { #[cfg(test)] mod tests { - use cast::{bump_box_refcount, transmute}; + use cast::transmute; use raw; use realstd::str::StrAllocating; @@ -115,21 +108,6 @@ mod tests { assert_eq!(1u, unsafe { ::cast::transmute_copy(&1) }); } - #[test] - fn test_bump_managed_refcount() { - unsafe { - let managed = @"box box box".to_owned(); // refcount 1 - bump_box_refcount(managed); // refcount 2 - let ptr: *int = transmute(managed); // refcount 2 - let _box1: @~str = ::cast::transmute_copy(&ptr); - let _box2: @~str = ::cast::transmute_copy(&ptr); - assert!(*_box1 == "box box box".to_owned()); - assert!(*_box2 == "box box box".to_owned()); - // Will destroy _box1 and _box2. Without the bump, this would - // use-after-free. With too many bumps, it would leak. - } - } - #[test] fn test_transmute() { unsafe { From a993703f93cdfa210f9e790de6683e86e0204a1a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 6 May 2014 23:29:57 -0700 Subject: [PATCH 2/5] std: Doc typos --- src/libstd/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 72d41ae1dd291..ce64bb84ca07f 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -81,7 +81,7 @@ //! memory types, including [`atomics`](sync/atomics/index.html). //! //! Common types of I/O, including files, TCP, UPD, pipes, Unix domain sockets, -//! timers, and process spawning, are defined in the [`io`](io/index.html). +//! timers, and process spawning, are defined in the [`io`](io/index.html) module. //! //! Rust's I/O and concurrency depends on a small runtime interface //! that lives, along with its support code, in mod [`rt`](rt/index.html). @@ -90,10 +90,11 @@ //! //! ## The Rust prelude and macros //! -//! Finally, the [`prelude`](prelude/index.html) defines a set of +//! Finally, the [`prelude`](prelude/index.html) defines a //! common set of traits, types, and functions that are made available //! to all code by default. [`macros`](macros/index.html) contains -//! all the standard macros, such as `assert!`, `fail!`, `println!`. +//! all the standard macros, such as `assert!`, `fail!`, `println!`, +//! and `format!`, also available to all Rust code. #![crate_id = "std#0.11-pre"] #![comment = "The Rust standard library"] From 3a11509e00e5ed27e69522dc456c5b82c29404ef Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 6 May 2014 23:33:08 -0700 Subject: [PATCH 3/5] std: Reorder definitions in cast Prioritize `transmute` and `forget`. --- src/libcore/cast.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/libcore/cast.rs b/src/libcore/cast.rs index 25b6fd1b2cf1c..e0edeb53c6ebc 100644 --- a/src/libcore/cast.rs +++ b/src/libcore/cast.rs @@ -14,25 +14,6 @@ use mem; use intrinsics; use ptr::copy_nonoverlapping_memory; -/// Casts the value at `src` to U. The two types must have the same length. -#[inline] -pub unsafe fn transmute_copy(src: &T) -> U { - let mut dest: U = mem::uninit(); - let dest_ptr: *mut u8 = transmute(&mut dest); - let src_ptr: *u8 = transmute(src); - copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::()); - dest -} - -/** - * Move a thing into the void - * - * The forget function will take ownership of the provided value but neglect - * to run any required cleanup or memory-management operations on it. - */ -#[inline] -pub unsafe fn forget(thing: T) { intrinsics::forget(thing); } - /** * Transform a value of one type into a value of another type. * Both types must have the same size and alignment. @@ -51,6 +32,25 @@ pub unsafe fn transmute(thing: L) -> G { intrinsics::transmute(thing) } +/** + * Move a thing into the void + * + * The forget function will take ownership of the provided value but neglect + * to run any required cleanup or memory-management operations on it. + */ +#[inline] +pub unsafe fn forget(thing: T) { intrinsics::forget(thing); } + +/// Casts the value at `src` to U. The two types must have the same length. +#[inline] +pub unsafe fn transmute_copy(src: &T) -> U { + let mut dest: U = mem::uninit(); + let dest_ptr: *mut u8 = transmute(&mut dest); + let src_ptr: *u8 = transmute(src); + copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::()); + dest +} + /// Coerce an immutable reference to be mutable. #[inline] #[deprecated="casting &T to &mut T is undefined behaviour: use Cell, RefCell or Unsafe"] From eb5f9feadbe60efce32275fb1abd0efd11deeba4 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 6 May 2014 23:33:41 -0700 Subject: [PATCH 4/5] std: Change names of transmute's type parameters from L, G to T, U. I don't know what L and G mean. T, U easier to understand. --- src/libcore/cast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cast.rs b/src/libcore/cast.rs index e0edeb53c6ebc..cf99fdbd5d4c2 100644 --- a/src/libcore/cast.rs +++ b/src/libcore/cast.rs @@ -28,7 +28,7 @@ use ptr::copy_nonoverlapping_memory; * ``` */ #[inline] -pub unsafe fn transmute(thing: L) -> G { +pub unsafe fn transmute(thing: T) -> U { intrinsics::transmute(thing) } From 2aa42533772a6b3cd406c34f0de12327320cb875 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 7 May 2014 12:12:24 -0700 Subject: [PATCH 5/5] std: Small doc tweaks --- src/libcore/cell.rs | 2 +- src/libcore/ptr.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 8951c7d806ab2..0413b31e8b76e 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Types dealing with dynamic mutability +//! Types that provide interior mutability. use clone::Clone; use cmp::Eq; diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index e3a3f78dcb925..bb587c0e42d03 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// FIXME: talk about offset, copy_memory, copy_nonoverlapping_memory + //! Conveniences for working with unsafe pointers, the `*T`, and `*mut T` types. //! //! Working with unsafe pointers in Rust is fairly uncommon,