s/vec::as_buf/vec::as_imm_buf/, fix comment, remove set.rs

hat tip to @jruderman
This commit is contained in:
Niko Matsakis 2012-09-13 11:46:10 -07:00
parent c43426e35b
commit 2f6b66ecd3
14 changed files with 36 additions and 99 deletions

View File

@ -238,7 +238,7 @@ fn select2<A: Send, B: Send>(p_a: Port<A>, p_b: Port<B>)
let mut resport: *rust_port;
resport = rusti::init::<*rust_port>();
do vec::as_buf(ports) |ports, n_ports| {
do vec::as_imm_buf(ports) |ports, n_ports| {
rustrt::rust_port_select(ptr::addr_of(resport), ports,
n_ports as size_t, yieldp);
}

View File

@ -169,7 +169,7 @@ impl T : FromStr {
/// Convert to a string in a given base
fn to_str(n: T, radix: uint) -> ~str {
do to_str_bytes(n, radix) |slice| {
do vec::as_buf(slice) |p, len| {
do vec::as_imm_buf(slice) |p, len| {
unsafe { str::raw::from_buf_len(p, len) }
}
}

View File

@ -299,7 +299,7 @@ fn test_buf_len() {
do str::as_c_str(s1) |p1| {
do str::as_c_str(s2) |p2| {
let v = ~[p0, p1, p2, null()];
do vec::as_buf(v) |vp, len| {
do vec::as_imm_buf(v) |vp, len| {
assert unsafe { buf_len(vp) } == 3u;
assert len == 4u;
}

View File

@ -92,7 +92,7 @@ fn with_argv<T>(prog: &str, args: &[~str],
vec::push_all(argptrs, str::as_c_str(*t, |b| ~[b]));
}
vec::push(argptrs, ptr::null());
vec::as_buf(argptrs, |buf, _len| cb(buf))
vec::as_imm_buf(argptrs, |buf, _len| cb(buf))
}
#[cfg(unix)]
@ -112,7 +112,7 @@ fn with_envp<T>(env: &Option<~[(~str,~str)]>,
vec::push_all(ptrs, str::as_c_str(*t, |b| ~[b]));
}
vec::push(ptrs, ptr::null());
vec::as_buf(ptrs, |p, _len|
vec::as_imm_buf(ptrs, |p, _len|
unsafe { cb(::unsafe::reinterpret_cast(&p)) }
)
}
@ -138,7 +138,7 @@ fn with_envp<T>(env: &Option<~[(~str,~str)]>,
::unsafe::forget(v);
}
blk += ~[0_u8];
vec::as_buf(blk, |p, _len| cb(::unsafe::reinterpret_cast(&p)))
vec::as_imm_buf(blk, |p, _len| cb(::unsafe::reinterpret_cast(&p)))
}
_ => cb(ptr::null())
}

View File

@ -1992,7 +1992,7 @@ mod raw {
unsafe fn from_buf_len(buf: *const u8, len: uint) -> ~str {
let mut v: ~[mut u8] = ~[mut];
vec::reserve(v, len + 1u);
vec::as_buf(v, |vbuf, _len| {
vec::as_imm_buf(v, |vbuf, _len| {
let vbuf = ::unsafe::transmute_mut_unsafe(vbuf);
ptr::memcpy(vbuf, buf as *u8, len)
});
@ -2049,7 +2049,7 @@ mod raw {
let mut v = ~[];
vec::reserve(v, end - begin + 1u);
unsafe {
do vec::as_buf(v) |vbuf, _vlen| {
do vec::as_imm_buf(v) |vbuf, _vlen| {
let vbuf = ::unsafe::transmute_mut_unsafe(vbuf);
let src = ptr::offset(sbuf, begin);
ptr::memcpy(vbuf, src, end - begin);

View File

@ -174,7 +174,7 @@ fn from_str_radix(buf: &str, radix: u64) -> Option<u64> {
*/
pure fn to_str(num: T, radix: uint) -> ~str {
do to_str_bytes(false, num, radix) |slice| {
do vec::as_buf(slice) |p, len| {
do vec::as_imm_buf(slice) |p, len| {
unsafe { str::raw::from_buf_len(p, len) }
}
}
@ -219,7 +219,7 @@ pure fn to_str_bytes<U>(neg: bool, num: T, radix: uint,
// in-bounds, no extra cost.
unsafe {
do vec::as_buf(buf) |p, len| {
do vec::as_imm_buf(buf) |p, len| {
let mp = p as *mut u8;
let mut i = len;
let mut n = num;

View File

@ -84,7 +84,7 @@ export riter;
export riteri;
export permute;
export windowed;
export as_buf;
export as_imm_buf;
export as_mut_buf;
export as_const_buf;
export raw;
@ -333,7 +333,7 @@ pure fn slice<T: Copy>(v: &[const T], start: uint, end: uint) -> ~[T] {
pure fn view<T>(v: &[T], start: uint, end: uint) -> &[T] {
assert (start <= end);
assert (end <= len(v));
do as_buf(v) |p, _len| {
do as_imm_buf(v) |p, _len| {
unsafe {
::unsafe::reinterpret_cast(
&(ptr::offset(p, start),
@ -502,7 +502,7 @@ fn unshift<T>(&v: ~[T], +x: T) {
}
fn consume<T>(+v: ~[T], f: fn(uint, +T)) unsafe {
do as_buf(v) |p, ln| {
do as_imm_buf(v) |p, ln| {
for uint::range(0, ln) |i| {
let x <- *ptr::offset(p, i);
f(i, move x);
@ -513,7 +513,7 @@ fn consume<T>(+v: ~[T], f: fn(uint, +T)) unsafe {
}
fn consume_mut<T>(+v: ~[mut T], f: fn(uint, +T)) unsafe {
do as_buf(v) |p, ln| {
do as_imm_buf(v) |p, ln| {
for uint::range(0, ln) |i| {
let x <- *ptr::offset(p, i);
f(i, move x);
@ -605,7 +605,7 @@ fn push_all<T: Copy>(&v: ~[const T], rhs: &[const T]) {
fn push_all_move<T>(&v: ~[const T], -rhs: ~[const T]) {
reserve(v, v.len() + rhs.len());
unsafe {
do as_buf(rhs) |p, len| {
do as_imm_buf(rhs) |p, len| {
for uint::range(0, len) |i| {
let x <- *ptr::offset(p, i);
push(v, move x);
@ -617,7 +617,7 @@ fn push_all_move<T>(&v: ~[const T], -rhs: ~[const T]) {
/// Shorten a vector, dropping excess elements.
fn truncate<T>(&v: ~[const T], newlen: uint) {
do as_buf(v) |p, oldlen| {
do as_imm_buf(v) |p, oldlen| {
assert(newlen <= oldlen);
unsafe {
// This loop is optimized out for non-drop types.
@ -1185,7 +1185,7 @@ element's value.
*/
#[inline(always)]
pure fn iter_between<T>(v: &[T], start: uint, end: uint, f: fn(T)) {
do as_buf(v) |base_ptr, len| {
do as_imm_buf(v) |base_ptr, len| {
assert start <= end;
assert end <= len;
unsafe {
@ -1212,7 +1212,7 @@ pure fn each<T>(v: &[T], f: fn(T) -> bool) {
// is that you are passing it to `f()` using
// an immutable.
do vec::as_buf(v) |p, n| {
do vec::as_imm_buf(v) |p, n| {
let mut n = n;
let mut p = p;
while n > 0u {
@ -1259,9 +1259,8 @@ fn each_mut_ref<T>(v: &[mut T], f: fn(elem: &mut T) -> bool) {
}
}
/// Like `each()`, but for the case where you have
/// a vector with mutable contents and you would like
/// to mutate the contents as you iterate.
/// Like `each()`, but for the case where you have a vector that *may or may
/// not* have mutable contents.
#[inline(always)]
pure fn each_const_ref<T>(v: &[const T], f: fn(elem: &const T) -> bool) {
let mut i = 0;
@ -1281,7 +1280,7 @@ pure fn each_const_ref<T>(v: &[const T], f: fn(elem: &const T) -> bool) {
*/
#[inline(always)]
pure fn eachi<T>(v: &[T], f: fn(uint, T) -> bool) {
do vec::as_buf(v) |p, n| {
do vec::as_imm_buf(v) |p, n| {
let mut i = 0u;
let mut p = p;
while i < n {
@ -1301,7 +1300,7 @@ pure fn eachi<T>(v: &[T], f: fn(uint, T) -> bool) {
*/
#[inline(always)]
pure fn reach<T>(v: &[T], blk: fn(T) -> bool) {
do vec::as_buf(v) |p, n| {
do vec::as_imm_buf(v) |p, n| {
let mut i = 1;
while i <= n {
unsafe {
@ -1319,7 +1318,7 @@ pure fn reach<T>(v: &[T], blk: fn(T) -> bool) {
*/
#[inline(always)]
pure fn reachi<T>(v: &[T], blk: fn(uint, T) -> bool) {
do vec::as_buf(v) |p, n| {
do vec::as_imm_buf(v) |p, n| {
let mut i = 1;
while i <= n {
unsafe {
@ -1431,10 +1430,10 @@ pure fn windowed<TT: Copy>(nn: uint, xx: &[TT]) -> ~[~[TT]] {
* foreign interop.
*/
#[inline(always)]
pure fn as_buf<T,U>(s: &[T], /* NB---this CANNOT be const, see below */
f: fn(*T, uint) -> U) -> U {
pure fn as_imm_buf<T,U>(s: &[T], /* NB---this CANNOT be const, see below */
f: fn(*T, uint) -> U) -> U {
// NB---People keep changing the type of s to `&[const T]`. This is
// NB---Do not change the type of s to `&[const T]`. This is
// unsound. The reason is that we are going to create immutable pointers
// into `s` and pass them to `f()`, but in fact they are potentially
// pointing at *mutable memory*. Use `as_const_buf` or `as_mut_buf`
@ -1448,7 +1447,7 @@ pure fn as_buf<T,U>(s: &[T], /* NB---this CANNOT be const, see below */
}
}
/// Similar to `as_buf` but passing a `*const T`
/// Similar to `as_imm_buf` but passing a `*const T`
#[inline(always)]
pure fn as_const_buf<T,U>(s: &[const T],
f: fn(*const T, uint) -> U) -> U {
@ -1461,7 +1460,7 @@ pure fn as_const_buf<T,U>(s: &[const T],
}
}
/// Similar to `as_buf` but passing a `*mut T`
/// Similar to `as_imm_buf` but passing a `*mut T`
#[inline(always)]
pure fn as_mut_buf<T,U>(s: &[mut T],
f: fn(*mut T, uint) -> U) -> U {

View File

@ -39,7 +39,7 @@ fn map_slices<A: Copy Send, B: Copy Send>(
log(info, ~"spawning tasks");
while base < len {
let end = uint::min(len, base + items_per_task);
do vec::as_buf(xs) |p, _len| {
do vec::as_imm_buf(xs) |p, _len| {
let f = f();
let f = do future_spawn() |move f, copy base| {
unsafe {

View File

@ -1,62 +0,0 @@
import dvec::dvec;
import map::{hashfn, eqfn, hashmap};
struct set<K: copy> {
mut implementation: option<set_implementation<K>>
}
struct list_set<K> {
hasher: hashfn<K>;
eqer: eqfn<K>;
elements: ~[K];
}
enum set_implementation<K: copy> {
impl_with_list(list_set<K>),
impl_with_map(hashmap<K, ()>)
}
const threshold: uint = 25; // completely arbitrary.
impl<K> &list_set {
pure fn contains(element: &K) {
for self.elements.each |existing_element| {
if self.eqer(element, existing_element) {
return true;
}
}
return false;
}
pure fn convert_to_map() -> hashmap<K, ()> {
...
}
}
impl<K: copy> set<K> {
fn add(+element: K) -> bool {
let mut set_impl = option::swap_unwrap(&mut self.implementation);
let contained_before = match set_impl {
impl_with_list(ref mut list_set) => {
if list_set.elements.len() >= threshold {
// convert to a map
self.implementation = some(list_set.convert_to_map());
return self.add(move element);
}
if list_set.contains(&element) {
false
} else {
vec::push(list_set.elements, element);
true
}
}
impl_with_map(ref map) => {
let contained_before = map.insert(element, ());
}
}
self.implementation = some(move set_impl);
return true;
}
}

View File

@ -825,7 +825,7 @@ unsafe fn ip4_name(src: &sockaddr_in) -> ~str {
// ipv4 addr max size: 15 + 1 trailing null byte
let dst: ~[u8] = ~[0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8,
0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8];
do vec::as_buf(dst) |dst_buf, size| {
do vec::as_imm_buf(dst) |dst_buf, size| {
rustrt::rust_uv_ip4_name(to_unsafe_ptr(src),
dst_buf, size as libc::size_t);
// seems that checking the result of uv_ip4_name
@ -845,7 +845,7 @@ unsafe fn ip6_name(src: &sockaddr_in6) -> ~str {
0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8,
0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8,
0u8,0u8,0u8,0u8,0u8,0u8];
do vec::as_buf(dst) |dst_buf, size| {
do vec::as_imm_buf(dst) |dst_buf, size| {
let src_unsafe_ptr = to_unsafe_ptr(src);
log(debug, fmt!("val of src *sockaddr_in6: %? sockaddr_in6: %?",
src_unsafe_ptr, src));

View File

@ -679,7 +679,7 @@ fn Call(cx: block, Fn: ValueRef, Args: &[ValueRef]) -> ValueRef {
val_str(cx.ccx().tn, Fn),
Args.map(|arg| val_str(cx.ccx().tn, arg)));
do vec::as_buf(Args) |ptr, len| {
do vec::as_imm_buf(Args) |ptr, len| {
llvm::LLVMBuildCall(B(cx), Fn, ptr, len as c_uint, noname())
}
}

View File

@ -1057,13 +1057,13 @@ fn C_zero_byte_arr(size: uint) -> ValueRef unsafe {
}
fn C_struct(elts: &[ValueRef]) -> ValueRef {
do vec::as_buf(elts) |ptr, len| {
do vec::as_imm_buf(elts) |ptr, len| {
llvm::LLVMConstStruct(ptr, len as c_uint, False)
}
}
fn C_named_struct(T: TypeRef, elts: &[ValueRef]) -> ValueRef {
do vec::as_buf(elts) |ptr, len| {
do vec::as_imm_buf(elts) |ptr, len| {
llvm::LLVMConstNamedStruct(T, ptr, len as c_uint)
}
}

View File

@ -60,7 +60,7 @@ fn const_deref(cx: @crate_ctxt, v: ValueRef) -> ValueRef {
}
fn const_get_elt(cx: @crate_ctxt, v: ValueRef, us: &[c_uint]) -> ValueRef {
let r = do vec::as_buf(us) |p, len| {
let r = do vec::as_imm_buf(us) |p, len| {
llvm::LLVMConstExtractValue(v, p, len as c_uint)
};

View File

@ -76,7 +76,7 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
fn struct_tys(ty: TypeRef) -> ~[TypeRef] {
let n = llvm::LLVMCountStructElementTypes(ty);
let elts = vec::from_elem(n as uint, ptr::null());
do vec::as_buf(elts) |buf, _len| {
do vec::as_imm_buf(elts) |buf, _len| {
llvm::LLVMGetStructElementTypes(ty, buf);
}
return elts;