Skip to content

Commit

Permalink
c_vec: Remove the mutable cast be forcing the pointer to be mutable t…
Browse files Browse the repository at this point in the history
…hroughout (discussion in #1217).
  • Loading branch information
jwise authored and nikomatsakis committed Nov 28, 2011
1 parent c2eb084 commit 45dc535
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lib/c_vec.rs
Expand Up @@ -43,7 +43,7 @@ export ptr;
*/

tag t<T> {
t({ base: *T, size: uint, rsrc: @dtor_res});
t({ base: *mutable T, size: uint, rsrc: @dtor_res});
}

resource dtor_res(dtor: option::t<fn@()>) {
Expand All @@ -57,14 +57,15 @@ resource dtor_res(dtor: option::t<fn@()>) {
Section: Introduction forms
*/

unsafe fn create<T>(base: *T, size: uint) -> t<T> {
unsafe fn create<T>(base: *mutable T, size: uint) -> t<T> {
ret t({base: base,
size: size,
rsrc: @dtor_res(option::none)
});
}

unsafe fn create_with_dtor<T>(base: *T, size: uint, dtor: fn@()) -> t<T> {
unsafe fn create_with_dtor<T>(base: *mutable T, size: uint, dtor: fn@())
-> t<T> {
ret t({base: base,
size: size,
rsrc: @dtor_res(option::some(dtor))
Expand All @@ -77,12 +78,12 @@ unsafe fn create_with_dtor<T>(base: *T, size: uint, dtor: fn@()) -> t<T> {

fn get<copy T>(t: t<T>, ofs: uint) -> T {
assert ofs < (*t).size;
ret unsafe { *ptr::offset((*t).base, ofs) };
ret unsafe { *ptr::mut_offset((*t).base, ofs) };
}

fn set<copy T>(t: t<T>, ofs: uint, v: T) {
assert ofs < (*t).size;
unsafe { *(ptr::offset((*t).base, ofs) as *mutable T) = v };
unsafe { *ptr::mut_offset((*t).base, ofs) = v };
}

/*
Expand All @@ -93,6 +94,6 @@ fn size<T>(t: t<T>) -> uint {
ret (*t).size;
}

unsafe fn ptr<T>(t: t<T>) -> *T {
unsafe fn ptr<T>(t: t<T>) -> *mutable T {
ret (*t).base;
}

0 comments on commit 45dc535

Please sign in to comment.