Skip to content

Commit 19e0cbe

Browse files
committed
auto merge of #11682 : thestinger/rust/vector, r=brson
This is just an initial implementation and does not yet fully replace `~[T]`. A generic initialization syntax for containers is missing, and the slice functionality needs to be reworked to make auto-slicing unnecessary. Traits for supporting indexing properly are also required. This also needs to be fixed to make ring buffers as easy to use as vectors. The tests and documentation for `~[T]` can be ported over to this type when it is removed. I don't really expect DST to happen for vectors as having both `~[T]` and `Vec<T>` is overcomplicated and changing the slice representation to 3 words is not at all appealing. Unlike with traits, it's possible (and easy) to implement `RcSlice<T>` and `GcSlice<T>` without compiler help.
2 parents 52ba3b6 + b2ec71f commit 19e0cbe

File tree

15 files changed

+300
-68
lines changed

15 files changed

+300
-68
lines changed

doc/guide-ffi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<T: Send> Drop for Unique<T> {
230230
// We need to move the object out of the box, so that
231231
// the destructor is called (at the end of this scope.)
232232
ptr::replace_ptr(self.ptr, x);
233-
free(self.ptr as *c_void)
233+
free(self.ptr as *mut c_void)
234234
}
235235
}
236236
}

src/libextra/c_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ mod tests {
172172
let mem = malloc_raw(n);
173173

174174
CVec::new_with_dtor(mem as *mut u8, n,
175-
proc() { libc::free(mem as *c_void); })
175+
proc() { libc::free(mem as *mut c_void); })
176176
}
177177
}
178178

src/libextra/flate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
5353
assert!(res as int != 0);
5454
let out = vec::raw::from_buf_raw(res as *u8,
5555
outsz as uint);
56-
libc::free(res);
56+
libc::free(res as *mut c_void);
5757
out
5858
}
5959
}
@@ -76,7 +76,7 @@ fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
7676
assert!(res as int != 0);
7777
let out = vec::raw::from_buf_raw(res as *u8,
7878
outsz as uint);
79-
libc::free(res);
79+
libc::free(res as *mut c_void);
8080
out
8181
}
8282
}

src/libnative/io/file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,13 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
548548
let p = Path::new(p);
549549
let star = p.join("*");
550550
as_utf16_p(star.as_str().unwrap(), |path_ptr| {
551-
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint) as *c_void;
551+
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
552552
let find_handle = FindFirstFileW(path_ptr, wfd_ptr as HANDLE);
553553
if find_handle as libc::c_int != INVALID_HANDLE_VALUE {
554554
let mut paths = ~[];
555555
let mut more_files = 1 as libc::c_int;
556556
while more_files != 0 {
557-
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr);
557+
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr as *c_void);
558558
if fp_buf as uint == 0 {
559559
fail!("os::list_dir() failure: got null ptr from wfd");
560560
}
@@ -567,7 +567,7 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
567567
more_files = FindNextFileW(find_handle, wfd_ptr as HANDLE);
568568
}
569569
FindClose(find_handle);
570-
free(wfd_ptr);
570+
free(wfd_ptr as *mut c_void);
571571
Ok(paths)
572572
} else {
573573
Err(super::last_error())

src/librustc/lib/llvm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ impl TypeNames {
18361836
unsafe {
18371837
let s = llvm::LLVMTypeToString(ty.to_ref());
18381838
let ret = from_c_str(s);
1839-
free(s as *c_void);
1839+
free(s as *mut c_void);
18401840
ret
18411841
}
18421842
}
@@ -1850,7 +1850,7 @@ impl TypeNames {
18501850
unsafe {
18511851
let s = llvm::LLVMValueToString(val);
18521852
let ret = from_c_str(s);
1853-
free(s as *c_void);
1853+
free(s as *mut c_void);
18541854
ret
18551855
}
18561856
}

src/librustuv/uvll.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
373373
}
374374

375375
pub unsafe fn free_handle(v: *c_void) {
376-
free(v)
376+
free(v as *mut c_void)
377377
}
378378

379379
pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
@@ -383,7 +383,7 @@ pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
383383
}
384384

385385
pub unsafe fn free_req(v: *c_void) {
386-
free(v)
386+
free(v as *mut c_void)
387387
}
388388

389389
#[test]

src/libstd/c_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Drop for CString {
183183
fn drop(&mut self) {
184184
if self.owns_buffer_ {
185185
unsafe {
186-
libc::free(self.buf as *libc::c_void)
186+
libc::free(self.buf as *mut libc::c_void)
187187
}
188188
}
189189
}
@@ -459,7 +459,7 @@ mod tests {
459459
#[test]
460460
fn test_unwrap() {
461461
let c_str = "hello".to_c_str();
462-
unsafe { libc::free(c_str.unwrap() as *libc::c_void) }
462+
unsafe { libc::free(c_str.unwrap() as *mut libc::c_void) }
463463
}
464464

465465
#[test]

src/libstd/hashmap.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ use rand::Rng;
6666
use rand;
6767
use uint;
6868
use util::replace;
69-
use vec::{ImmutableVector, MutableVector, OwnedVector};
70-
use vec;
69+
use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems};
70+
use vec_ng;
71+
use vec_ng::Vec;
7172

7273
static INITIAL_CAPACITY: uint = 32u; // 2^5
7374

@@ -90,7 +91,7 @@ pub struct HashMap<K,V> {
9091
priv k1: u64,
9192
priv resize_at: uint,
9293
priv size: uint,
93-
priv buckets: ~[Option<Bucket<K, V>>],
94+
priv buckets: Vec<Option<Bucket<K, V>>>
9495
}
9596

9697
// We could rewrite FoundEntry to have type Option<&Bucket<K, V>>
@@ -151,7 +152,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
151152
-> SearchResult {
152153
let mut ret = TableFull;
153154
self.bucket_sequence(hash, |i| {
154-
match self.buckets[i] {
155+
match self.buckets.as_slice()[i] {
155156
Some(ref bkt) if bkt.hash == hash && *k == bkt.key => {
156157
ret = FoundEntry(i); false
157158
},
@@ -169,7 +170,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
169170
-> SearchResult {
170171
let mut ret = TableFull;
171172
self.bucket_sequence(hash, |i| {
172-
match self.buckets[i] {
173+
match self.buckets.as_slice()[i] {
173174
Some(ref bkt) if bkt.hash == hash && k.equiv(&bkt.key) => {
174175
ret = FoundEntry(i); false
175176
},
@@ -194,7 +195,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
194195
self.resize_at = resize_at(new_capacity);
195196

196197
let old_buckets = replace(&mut self.buckets,
197-
vec::from_fn(new_capacity, |_| None));
198+
Vec::from_fn(new_capacity, |_| None));
198199

199200
self.size = 0;
200201
for bucket in old_buckets.move_iter() {
@@ -213,15 +214,15 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
213214

214215
#[inline]
215216
fn value_for_bucket<'a>(&'a self, idx: uint) -> &'a V {
216-
match self.buckets[idx] {
217+
match self.buckets.as_slice()[idx] {
217218
Some(ref bkt) => &bkt.value,
218219
None => fail!("HashMap::find: internal logic error"),
219220
}
220221
}
221222

222223
#[inline]
223224
fn mut_value_for_bucket<'a>(&'a mut self, idx: uint) -> &'a mut V {
224-
match self.buckets[idx] {
225+
match self.buckets.as_mut_slice()[idx] {
225226
Some(ref mut bkt) => &mut bkt.value,
226227
None => unreachable!()
227228
}
@@ -234,13 +235,12 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
234235
match self.bucket_for_key_with_hash(hash, &k) {
235236
TableFull => { fail!("Internal logic error"); }
236237
FoundHole(idx) => {
237-
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
238-
value: v});
238+
self.buckets.as_mut_slice()[idx] = Some(Bucket{hash: hash, key: k, value: v});
239239
self.size += 1;
240240
None
241241
}
242242
FoundEntry(idx) => {
243-
match self.buckets[idx] {
243+
match self.buckets.as_mut_slice()[idx] {
244244
None => { fail!("insert_internal: Internal logic error") }
245245
Some(ref mut b) => {
246246
b.hash = hash;
@@ -273,16 +273,16 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
273273
};
274274

275275
let len_buckets = self.buckets.len();
276-
let bucket = self.buckets[idx].take();
276+
let bucket = self.buckets.as_mut_slice()[idx].take();
277277

278278
let value = bucket.map(|bucket| bucket.value);
279279

280280
/* re-inserting buckets may cause changes in size, so remember
281281
what our new size is ahead of time before we start insertions */
282282
let size = self.size - 1;
283283
idx = self.next_bucket(idx, len_buckets);
284-
while self.buckets[idx].is_some() {
285-
let bucket = self.buckets[idx].take();
284+
while self.buckets.as_slice()[idx].is_some() {
285+
let bucket = self.buckets.as_mut_slice()[idx].take();
286286
self.insert_opt_bucket(bucket);
287287
idx = self.next_bucket(idx, len_buckets);
288288
}
@@ -300,7 +300,7 @@ impl<K:Hash + Eq,V> Container for HashMap<K, V> {
300300
impl<K:Hash + Eq,V> Mutable for HashMap<K, V> {
301301
/// Clear the map, removing all key-value pairs.
302302
fn clear(&mut self) {
303-
for bkt in self.buckets.mut_iter() {
303+
for bkt in self.buckets.as_mut_slice().mut_iter() {
304304
*bkt = None;
305305
}
306306
self.size = 0;
@@ -380,7 +380,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
380380
k0: k0, k1: k1,
381381
resize_at: resize_at(cap),
382382
size: 0,
383-
buckets: vec::from_fn(cap, |_| None)
383+
buckets: Vec::from_fn(cap, |_| None)
384384
}
385385
}
386386

@@ -455,7 +455,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
455455
FoundEntry(idx) => { found(&k, self.mut_value_for_bucket(idx), a); idx }
456456
FoundHole(idx) => {
457457
let v = not_found(&k, a);
458-
self.buckets[idx] = Some(Bucket{hash: hash, key: k, value: v});
458+
self.buckets.as_mut_slice()[idx] = Some(Bucket{hash: hash, key: k, value: v});
459459
self.size += 1;
460460
idx
461461
}
@@ -541,14 +541,14 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
541541
/// An iterator visiting all key-value pairs in arbitrary order.
542542
/// Iterator element type is (&'a K, &'a V).
543543
pub fn iter<'a>(&'a self) -> Entries<'a, K, V> {
544-
Entries { iter: self.buckets.iter() }
544+
Entries { iter: self.buckets.as_slice().iter() }
545545
}
546546

547547
/// An iterator visiting all key-value pairs in arbitrary order,
548548
/// with mutable references to the values.
549549
/// Iterator element type is (&'a K, &'a mut V).
550550
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
551-
MutEntries { iter: self.buckets.mut_iter() }
551+
MutEntries { iter: self.buckets.as_mut_slice().mut_iter() }
552552
}
553553

554554
/// Creates a consuming iterator, that is, one that moves each key-value
@@ -599,17 +599,17 @@ impl<K:Hash + Eq + Clone,V:Clone> Clone for HashMap<K,V> {
599599
/// HashMap iterator
600600
#[deriving(Clone)]
601601
pub struct Entries<'a, K, V> {
602-
priv iter: vec::Items<'a, Option<Bucket<K, V>>>,
602+
priv iter: Items<'a, Option<Bucket<K, V>>>,
603603
}
604604

605605
/// HashMap mutable values iterator
606606
pub struct MutEntries<'a, K, V> {
607-
priv iter: vec::MutItems<'a, Option<Bucket<K, V>>>,
607+
priv iter: MutItems<'a, Option<Bucket<K, V>>>,
608608
}
609609

610610
/// HashMap move iterator
611611
pub struct MoveEntries<K, V> {
612-
priv iter: vec::MoveItems<Option<Bucket<K, V>>>,
612+
priv iter: vec_ng::MoveItems<Option<Bucket<K, V>>>,
613613
}
614614

615615
/// HashMap keys iterator
@@ -623,12 +623,12 @@ pub type Values<'a, K, V> =
623623
/// HashSet iterator
624624
#[deriving(Clone)]
625625
pub struct SetItems<'a, K> {
626-
priv iter: vec::Items<'a, Option<Bucket<K, ()>>>,
626+
priv iter: Items<'a, Option<Bucket<K, ()>>>,
627627
}
628628

629629
/// HashSet move iterator
630630
pub struct SetMoveItems<K> {
631-
priv iter: vec::MoveItems<Option<Bucket<K, ()>>>,
631+
priv iter: vec_ng::MoveItems<Option<Bucket<K, ()>>>,
632632
}
633633

634634
impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> {
@@ -807,7 +807,7 @@ impl<T:Hash + Eq> HashSet<T> {
807807
/// An iterator visiting all elements in arbitrary order.
808808
/// Iterator element type is &'a T.
809809
pub fn iter<'a>(&'a self) -> SetItems<'a, T> {
810-
SetItems { iter: self.map.buckets.iter() }
810+
SetItems { iter: self.map.buckets.as_slice().iter() }
811811
}
812812

813813
/// Creates a consuming iterator, that is, one that moves each value out

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub mod char;
110110
pub mod tuple;
111111

112112
pub mod vec;
113+
pub mod vec_ng;
113114
pub mod at_vec;
114115
pub mod str;
115116

src/libstd/libc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3225,7 +3225,7 @@ pub mod funcs {
32253225
pub fn calloc(nobj: size_t, size: size_t) -> *c_void;
32263226
pub fn malloc(size: size_t) -> *mut c_void;
32273227
pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
3228-
pub fn free(p: *c_void);
3228+
pub fn free(p: *mut c_void);
32293229
pub fn exit(status: c_int) -> !;
32303230
// Omitted: atexit.
32313231
pub fn system(s: *c_char) -> c_int;

src/libstd/rt/global_heap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
5252
// `realloc(ptr, 0)` may allocate, but it may also return a null pointer
5353
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html
5454
if size == 0 {
55-
free(ptr as *c_void);
55+
free(ptr as *mut c_void);
5656
mut_null()
5757
} else {
5858
let p = realloc(ptr as *mut c_void, size as size_t);
@@ -107,7 +107,7 @@ pub unsafe fn exchange_free_(ptr: *u8) {
107107

108108
#[inline]
109109
pub unsafe fn exchange_free(ptr: *u8) {
110-
free(ptr as *c_void);
110+
free(ptr as *mut c_void);
111111
}
112112

113113
#[cfg(test)]

src/libstd/sync/deque.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<T: Send> Buffer<T> {
389389
impl<T: Send> Drop for Buffer<T> {
390390
fn drop(&mut self) {
391391
// It is assumed that all buffers are empty on drop.
392-
unsafe { libc::free(self.storage as *libc::c_void) }
392+
unsafe { libc::free(self.storage as *mut libc::c_void) }
393393
}
394394
}
395395

0 commit comments

Comments
 (0)