Skip to content

Commit

Permalink
auto merge of #5698 : Thiez/rust/de-mutfielding, r=pcwalton
Browse files Browse the repository at this point in the history
This pull request removes some mut-fields from at_vec, str, vec, unstable, and cell. Sadly in case of Cell this required using either transmute_mut (2 instances) or changing the interface. I chose the former. Perhaps it would be a good idea to merge Cell and Option, and take that opportunity to change the interface to use '&mut self' instead of '&self' (which would enable removing the transmutations) for take and put_back.

r?
  • Loading branch information
bors committed Apr 3, 2013
2 parents 5b933ae + 397a478 commit 17ab718
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/libcore/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub mod raw {
*/
#[inline(always)]
pub unsafe fn set_len<T>(v: @[T], new_len: uint) {
let repr: **VecRepr = ::cast::reinterpret_cast(&addr_of(&v));
let repr: **mut VecRepr = ::cast::reinterpret_cast(&addr_of(&v));
(**repr).unboxed.fill = new_len * sys::size_of::<T>();
}

Expand All @@ -226,7 +226,7 @@ pub mod raw {

#[inline(always)] // really pretty please
pub unsafe fn push_fast<T>(v: &mut @[T], initval: T) {
let repr: **VecRepr = ::cast::reinterpret_cast(&v);
let repr: **mut VecRepr = ::cast::reinterpret_cast(&v);
let fill = (**repr).unboxed.fill;
(**repr).unboxed.fill += sys::size_of::<T>();
let p = addr_of(&((**repr).unboxed.data));
Expand Down
12 changes: 5 additions & 7 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! A mutable, nullable memory location

use cast::transmute;
use cast::transmute_mut;
use prelude::*;

/*
Expand All @@ -20,16 +20,12 @@ Similar to a mutable option type, but friendlier.
*/

pub struct Cell<T> {
mut value: Option<T>
value: Option<T>
}

impl<T:cmp::Eq> cmp::Eq for Cell<T> {
fn eq(&self, other: &Cell<T>) -> bool {
unsafe {
let frozen_self: &Option<T> = transmute(&mut self.value);
let frozen_other: &Option<T> = transmute(&mut other.value);
frozen_self == frozen_other
}
(self.value) == (other.value)
}
fn ne(&self, other: &Cell<T>) -> bool { !self.eq(other) }
}
Expand All @@ -46,6 +42,7 @@ pub fn empty_cell<T>() -> Cell<T> {
pub impl<T> Cell<T> {
/// Yields the value, failing if the cell is empty.
fn take(&self) -> T {
let mut self = unsafe { transmute_mut(self) };
if self.is_empty() {
fail!(~"attempt to take an empty cell");
}
Expand All @@ -57,6 +54,7 @@ pub impl<T> Cell<T> {

/// Returns the value, failing if the cell is full.
fn put_back(&self, value: T) {
let mut self = unsafe { transmute_mut(self) };
if !self.is_empty() {
fail!(~"attempt to put a value back into a full cell");
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,8 +2274,8 @@ pub mod raw {

/// Sets the length of the string and adds the null terminator
pub unsafe fn set_len(v: &mut ~str, new_len: uint) {
let v: **vec::raw::VecRepr = cast::transmute(v);
let repr: *vec::raw::VecRepr = *v;
let v: **mut vec::raw::VecRepr = cast::transmute(v);
let repr: *mut vec::raw::VecRepr = *v;
(*repr).unboxed.fill = new_len + 1u;
let null = ptr::mut_offset(cast::transmute(&((*repr).unboxed.data)),
new_len);
Expand Down
14 changes: 7 additions & 7 deletions src/libcore/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ fn compare_and_swap(address: &mut int, oldval: int, newval: int) -> bool {
****************************************************************************/

struct ArcData<T> {
mut count: libc::intptr_t,
count: libc::intptr_t,
// FIXME(#3224) should be able to make this non-option to save memory
mut data: Option<T>,
data: Option<T>,
}

struct ArcDestruct<T> {
mut data: *libc::c_void,
data: *libc::c_void,
}

#[unsafe_destructor]
impl<T> Drop for ArcDestruct<T>{
fn finalize(&self) {
unsafe {
do task::unkillable {
let data: ~ArcData<T> = cast::reinterpret_cast(&self.data);
let mut data: ~ArcData<T> = cast::reinterpret_cast(&self.data);
let new_count =
intrinsics::atomic_xsub(&mut data.count, 1) - 1;
assert!(new_count >= 0);
Expand Down Expand Up @@ -185,7 +185,7 @@ pub unsafe fn get_shared_immutable_state<'a,T:Owned>(
pub unsafe fn clone_shared_mutable_state<T:Owned>(rc: &SharedMutableState<T>)
-> SharedMutableState<T> {
unsafe {
let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
let mut ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
let new_count = intrinsics::atomic_xadd(&mut ptr.count, 1) + 1;
assert!(new_count >= 2);
cast::forget(ptr);
Expand Down Expand Up @@ -252,15 +252,15 @@ pub impl LittleLock {
}
}

struct ExData<T> { lock: LittleLock, mut failed: bool, mut data: T, }
struct ExData<T> { lock: LittleLock, failed: bool, data: T, }
/**
* An arc over mutable data that is protected by a lock. For library use only.
*/
pub struct Exclusive<T> { x: SharedMutableState<ExData<T>> }

pub fn exclusive<T:Owned>(user_data: T) -> Exclusive<T> {
let data = ExData {
lock: LittleLock(), mut failed: false, mut data: user_data
lock: LittleLock(), failed: false, data: user_data
};
Exclusive { x: unsafe { shared_mutable_state(data) } }
}
Expand Down
12 changes: 6 additions & 6 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ pub fn push<T>(v: &mut ~[T], initval: T) {
// This doesn't bother to make sure we have space.
#[inline(always)] // really pretty please
unsafe fn push_fast<T>(v: &mut ~[T], initval: T) {
let repr: **raw::VecRepr = ::cast::transmute(v);
let repr: **mut raw::VecRepr = ::cast::transmute(v);
let fill = (**repr).unboxed.fill;
(**repr).unboxed.fill += sys::nonzero_size_of::<T>();
let p = addr_of(&((**repr).unboxed.data));
Expand Down Expand Up @@ -2148,8 +2148,8 @@ pub unsafe fn from_buf<T>(ptr: *T, elts: uint) -> ~[T] {

/// The internal 'unboxed' representation of a vector
pub struct UnboxedVecRepr {
mut fill: uint,
mut alloc: uint,
fill: uint,
alloc: uint,
data: u8
}

Expand All @@ -2171,8 +2171,8 @@ pub mod raw {
}

pub struct SliceRepr {
mut data: *u8,
mut len: uint
data: *u8,
len: uint
}

/**
Expand All @@ -2184,7 +2184,7 @@ pub mod raw {
*/
#[inline(always)]
pub unsafe fn set_len<T>(v: &mut ~[T], new_len: uint) {
let repr: **VecRepr = ::cast::transmute(v);
let repr: **mut VecRepr = ::cast::transmute(v);
(**repr).unboxed.fill = new_len * sys::nonzero_size_of::<T>();
}

Expand Down

0 comments on commit 17ab718

Please sign in to comment.