static LZ_BEST : c_int = 0xfff; // LZ with 4095 probes, "best"
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
- do vec::as_imm_buf(bytes) |b, len| {
+ do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
}
pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
- do vec::as_imm_buf(bytes) |b, len| {
+ do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
info!("spawning tasks");
while base < len {
let end = uint::min(len, base + items_per_task);
- do vec::as_imm_buf(xs) |p, _len| {
+ do xs.as_imm_buf |p, _len| {
let f = f();
let base = base;
let f = do future_spawn() || {
// 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_imm_buf(dst) |dst_buf, size| {
+ do dst.as_imm_buf |dst_buf, size| {
rust_uv_ip4_name(to_unsafe_ptr(src),
dst_buf, size as libc::size_t);
// seems that checking the result of uv_ip4_name
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_imm_buf(dst) |dst_buf, size| {
+ do dst.as_imm_buf |dst_buf, size| {
let src_unsafe_ptr = to_unsafe_ptr(src);
let result = rust_uv_ip6_name(src_unsafe_ptr,
dst_buf, size as libc::size_t);
let min = llvm::LLVMConstInt(t, lo, signed);
let max = llvm::LLVMConstInt(t, hi, signed);
- do vec::as_imm_buf([min, max]) |ptr, len| {
+ do [min, max].as_imm_buf |ptr, len| {
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
ptr, len as c_uint));
cx.val_to_str(Fn),
Args.map(|arg| cx.val_to_str(*arg)));
- do vec::as_imm_buf(Args) |ptr, len| {
+ do Args.as_imm_buf |ptr, len| {
llvm::LLVMBuildCall(B(cx), Fn, ptr, len as c_uint, noname())
}
}
pub fn C_struct(elts: &[ValueRef]) -> ValueRef {
unsafe {
- do vec::as_imm_buf(elts) |ptr, len| {
+ do elts.as_imm_buf |ptr, len| {
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, False)
}
}
pub fn C_packed_struct(elts: &[ValueRef]) -> ValueRef {
unsafe {
- do vec::as_imm_buf(elts) |ptr, len| {
+ do elts.as_imm_buf |ptr, len| {
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, True)
}
}
pub fn C_named_struct(T: Type, elts: &[ValueRef]) -> ValueRef {
unsafe {
- do vec::as_imm_buf(elts) |ptr, len| {
+ do elts.as_imm_buf |ptr, len| {
llvm::LLVMConstNamedStruct(T.to_ref(), ptr, len as c_uint)
}
}
pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint])
-> ValueRef {
unsafe {
- let r = do vec::as_imm_buf(us) |p, len| {
+ let r = do us.as_imm_buf |p, len| {
llvm::LLVMConstExtractValue(v, p, len as c_uint)
};
impl Reader for *libc::FILE {
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
unsafe {
- do vec::as_mut_buf(bytes) |buf_p, buf_len| {
+ do bytes.as_mut_buf |buf_p, buf_len| {
assert!(buf_len >= len);
let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
impl Writer for *libc::FILE {
fn write(&self, v: &[u8]) {
unsafe {
- do vec::as_imm_buf(v) |vbuf, len| {
+ do v.as_imm_buf |vbuf, len| {
let nout = libc::fwrite(vbuf as *c_void,
1,
len as size_t,
fn write(&self, v: &[u8]) {
unsafe {
let mut count = 0u;
- do vec::as_imm_buf(v) |vbuf, len| {
+ do v.as_imm_buf |vbuf, len| {
while count < len {
let vb = ptr::offset(vbuf, count) as *c_void;
let nout = libc::write(*self, vb, len as size_t);
pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool)
-> Option<~str> {
let mut buf = vec::from_elem(TMPBUF_SZ, 0u8 as c_char);
- do vec::as_mut_buf(buf) |b, sz| {
+ do buf.as_mut_buf |b, sz| {
if f(b, sz as size_t) {
unsafe {
Some(str::raw::from_buf(b as *u8))
while !done {
let mut k: DWORD = 0;
let mut buf = vec::from_elem(n as uint, 0u16);
- do vec::as_mut_buf(buf) |b, _sz| {
+ do buf.as_mut_buf |b, _sz| {
k = f(b, TMPBUF_SZ as DWORD);
if k == (0 as DWORD) {
done = true;
let mut t = s.to_utf16();
// Null terminate before passing on.
t.push(0u16);
- vec::as_imm_buf(t, |buf, _len| f(buf))
+ t.as_imm_buf(|buf, _len| f(buf))
}
}
let mut done = false;
let mut ok = true;
while !done {
- do vec::as_mut_buf(buf) |b, _sz| {
+ do buf.as_mut_buf |b, _sz| {
let nread = libc::fread(b as *mut c_void, 1u as size_t,
bufsize as size_t,
istream);
let s = ~"hello";
let mut buf = s.as_bytes_with_null().to_owned();
let len = buf.len();
- do vec::as_mut_buf(buf) |b, _len| {
+ do buf.as_mut_buf |b, _len| {
assert_eq!(libc::fwrite(b as *c_void, 1u as size_t,
(s.len() + 1u) as size_t, ostream),
len as size_t)
do str::as_c_str(s1) |p1| {
do str::as_c_str(s2) |p2| {
let v = ~[p0, p1, p2, null()];
- do vec::as_imm_buf(v) |vp, len| {
+ do v.as_imm_buf |vp, len| {
assert_eq!(unsafe { buf_len(vp) }, 3u);
assert_eq!(len, 4u);
}
unsafe {
let n = rustrt::rand_seed_size() as uint;
let mut s = vec::from_elem(n, 0_u8);
- do vec::as_mut_buf(s) |p, sz| {
+ do s.as_mut_buf |p, sz| {
rustrt::rand_gen_seed(p, sz as size_t)
}
s
for 10.times {
unsafe {
let seed = super::seed();
- let rt_rng = do vec::as_imm_buf(seed) |p, sz| {
+ let rt_rng = do seed.as_imm_buf |p, sz| {
rustrt::rand_new_seeded(p, sz as size_t)
};
let mut rng = IsaacRng::new_seeded(seed);
use to_str::ToStr;
use ptr::RawPtr;
use vec;
+use vec::ImmutableVector;
use ptr;
use str;
use libc::{c_void, c_int, size_t, malloc, free};
unsafe {
let data = malloc(v.len() as size_t) as *u8;
assert!(data.is_not_null());
- do vec::as_imm_buf(v) |b, l| {
+ do v.as_imm_buf |b, l| {
let data = data as *mut u8;
ptr::copy_memory(data, b, l)
}
use ptr;
use str;
use task;
-use vec;
+use vec::ImmutableVector;
/**
* A value representing a child process.
argptrs.push(str::as_c_str(*t, |b| b));
}
argptrs.push(ptr::null());
- vec::as_imm_buf(argptrs, |buf, _len| cb(buf))
+ argptrs.as_imm_buf(|buf, _len| cb(buf))
}
#[cfg(unix)]
}
ptrs.push(ptr::null());
- vec::as_imm_buf(ptrs, |p, _len|
+ ptrs.as_imm_buf(|p, _len|
unsafe { cb(::cast::transmute(p)) }
)
}
blk.push_all(kv.as_bytes_with_null_consume());
}
blk.push(0);
- vec::as_imm_buf(blk, |p, _len|
+ blk.as_imm_buf(|p, _len|
unsafe { cb(::cast::transmute(p)) }
)
}
use str::raw;
use str::{as_buf, is_utf8};
use vec;
+ use vec::MutableVector;
/// Create a Rust string from a null-terminated *u8 buffer
pub unsafe fn from_buf(buf: *u8) -> ~str {
/// Create a Rust string from a *u8 buffer of the given length
pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
let mut v: ~[u8] = vec::with_capacity(len + 1);
- vec::as_mut_buf(v, |vbuf, _len| {
+ v.as_mut_buf(|vbuf, _len| {
ptr::copy_memory(vbuf, buf as *u8, len)
});
vec::raw::set_len(&mut v, len);
/// Converts a vector of bytes to a new owned string.
pub unsafe fn from_bytes(v: &[u8]) -> ~str {
- do vec::as_imm_buf(v) |buf, len| {
+ do v.as_imm_buf |buf, len| {
from_buf_len(buf, len)
}
}
assert!((end <= n));
let mut v = vec::with_capacity(end - begin + 1u);
- do vec::as_imm_buf(v) |vbuf, _vlen| {
+ do v.as_imm_buf |vbuf, _vlen| {
let vbuf = ::cast::transmute_mut_unsafe(vbuf);
let src = ptr::offset(sbuf, begin);
ptr::copy_memory(vbuf, src, end - begin);
pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> ~[T] {
unsafe {
let mut v = with_capacity(n_elts);
- do as_mut_buf(v) |p, _len| {
+ do v.as_mut_buf |p, _len| {
let mut i: uint = 0u;
while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i));
// vec::with_capacity/ptr::set_memory for primitive types.
unsafe {
let mut v = with_capacity(n_elts);
- do as_mut_buf(v) |p, _len| {
+ do v.as_mut_buf |p, _len| {
let mut i = 0u;
while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), copy t);
}
}
-/**
- * Work with the buffer of a vector.
- *
- * Allows for unsafe manipulation of vector contents, which is useful for
- * foreign interop.
- */
-#[inline]
-pub fn as_imm_buf<T,U>(s: &[T],
- /* NB---this CANNOT be const, see below */
- f: &fn(*T, uint) -> U) -> U {
-
- // 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`
- // instead!
-
- unsafe {
- let v : *(*T,uint) = transmute(&s);
- let (buf,len) = *v;
- f(buf, len / sys::nonzero_size_of::<T>())
- }
-}
-
-/// Similar to `as_imm_buf` but passing a `*mut T`
-#[inline]
-pub fn as_mut_buf<T,U>(s: &mut [T], f: &fn(*mut T, uint) -> U) -> U {
- unsafe {
- let v : *(*mut T,uint) = transmute(&s);
- let (buf,len) = *v;
- f(buf, len / sys::nonzero_size_of::<T>())
- }
-}
-
// Equality
#[cfg(not(test))]
/// Returns true if a vector contains no elements
#[inline]
fn is_empty(&self) -> bool {
- as_imm_buf(*self, |_p, len| len == 0u)
+ self.as_imm_buf(|_p, len| len == 0u)
}
/// Returns the length of a vector
#[inline]
fn len(&self) -> uint {
- as_imm_buf(*self, |_p, len| len)
+ self.as_imm_buf(|_p, len| len)
}
}
/// Returns true if a vector contains no elements
#[inline]
fn is_empty(&self) -> bool {
- as_imm_buf(*self, |_p, len| len == 0u)
+ self.as_imm_buf(|_p, len| len == 0u)
}
/// Returns the length of a vector
#[inline]
fn len(&self) -> uint {
- as_imm_buf(*self, |_p, len| len)
+ self.as_imm_buf(|_p, len| len)
}
}
fn bsearch(&self, f: &fn(&T) -> Ordering) -> Option<uint>;
fn map<U>(&self, &fn(t: &T) -> U) -> ~[U];
+
+ fn as_imm_buf<U>(&self, f: &fn(*T, uint) -> U) -> U;
}
/// Extension methods for vectors
fn slice(&self, start: uint, end: uint) -> &'self [T] {
assert!(start <= end);
assert!(end <= self.len());
- do as_imm_buf(*self) |p, _len| {
+ do self.as_imm_buf |p, _len| {
unsafe {
transmute((ptr::offset(p, start),
(end - start) * sys::nonzero_size_of::<T>()))
fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U] {
self.iter().transform(f).collect()
}
+
+ /**
+ * Work with the buffer of a vector.
+ *
+ * Allows for unsafe manipulation of vector contents, which is useful for
+ * foreign interop.
+ */
+ #[inline]
+ fn as_imm_buf<U>(&self,
+ /* NB---this CANNOT be const, see below */
+ f: &fn(*T, uint) -> U) -> U {
+ // 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_mut_buf` instead!
+
+ unsafe {
+ let v : *(*T,uint) = transmute(self);
+ let (buf,len) = *v;
+ f(buf, len / sys::nonzero_size_of::<T>())
+ }
+ }
}
#[allow(missing_doc)]
let new_len = self.len() + rhs.len();
self.reserve(new_len);
unsafe {
- do as_mut_buf(rhs) |p, len| {
+ do rhs.as_mut_buf |p, len| {
for uint::range(0, len) |i| {
let x = ptr::replace_ptr(ptr::mut_offset(p, i),
intrinsics::uninit());
/// Shorten a vector, dropping excess elements.
fn truncate(&mut self, newlen: uint) {
- do as_mut_buf(*self) |p, oldlen| {
+ do self.as_mut_buf |p, oldlen| {
assert!(newlen <= oldlen);
unsafe {
// This loop is optimized out for non-drop types.
if self.len() == 0 { return; }
let mut last_written = 0;
let mut next_to_read = 1;
- do as_mut_buf(*self) |p, ln| {
+ do self.as_mut_buf |p, ln| {
// last_written < next_to_read <= ln
while next_to_read < ln {
// last_written < next_to_read < ln
unsafe fn unsafe_mut_ref(&self, index: uint) -> *mut T;
unsafe fn unsafe_set(&self, index: uint, val: T);
+
+ fn as_mut_buf<U>(&self, f: &fn(*mut T, uint) -> U) -> U;
}
impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
fn mut_slice(self, start: uint, end: uint) -> &'self mut [T] {
assert!(start <= end);
assert!(end <= self.len());
- do as_mut_buf(self) |p, _len| {
+ do self.as_mut_buf |p, _len| {
unsafe {
transmute((ptr::mut_offset(p, start),
(end - start) * sys::nonzero_size_of::<T>()))
unsafe fn unsafe_set(&self, index: uint, val: T) {
*self.unsafe_mut_ref(index) = val;
}
+
+ /// Similar to `as_imm_buf` but passing a `*mut T`
+ #[inline]
+ fn as_mut_buf<U>(&self, f: &fn(*mut T, uint) -> U) -> U {
+ unsafe {
+ let v : *(*mut T,uint) = transmute(self);
+ let (buf,len) = *v;
+ f(buf, len / sys::nonzero_size_of::<T>())
+ }
+ }
+
}
/// Trait for ~[T] where T is Cloneable
use ptr;
use sys;
use unstable::intrinsics;
- use vec::{UnboxedVecRepr, as_imm_buf, as_mut_buf, with_capacity};
+ use vec::{UnboxedVecRepr, with_capacity, ImmutableVector, MutableVector};
use util;
/// The internal representation of a (boxed) vector
*/
#[inline]
pub unsafe fn get<T:Copy>(v: &[T], i: uint) -> T {
- as_imm_buf(v, |p, _len| copy *ptr::offset(p, i))
+ v.as_imm_buf(|p, _len| copy *ptr::offset(p, i))
}
/**
#[inline]
pub unsafe fn init_elem<T>(v: &mut [T], i: uint, val: T) {
let mut box = Some(val);
- do as_mut_buf(v) |p, _len| {
+ do v.as_mut_buf |p, _len| {
let box2 = util::replace(&mut box, None);
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)),
box2.unwrap());
pub unsafe fn from_buf_raw<T>(ptr: *T, elts: uint) -> ~[T] {
let mut dst = with_capacity(elts);
set_len(&mut dst, elts);
- as_mut_buf(dst, |p_dst, _len_dst| ptr::copy_memory(p_dst, ptr, elts));
+ dst.as_mut_buf(|p_dst, _len_dst| ptr::copy_memory(p_dst, ptr, elts));
dst
}
assert!(dst.len() >= count);
assert!(src.len() >= count);
- do as_mut_buf(dst) |p_dst, _len_dst| {
- do as_imm_buf(src) |p_src, _len_src| {
+ do dst.as_mut_buf |p_dst, _len_dst| {
+ do src.as_imm_buf |p_src, _len_src| {
ptr::copy_memory(p_dst, p_src, count)
}
}
impl<'self> MutableByteVector for &'self mut [u8] {
#[inline]
fn set_memory(self, value: u8) {
- do vec::as_mut_buf(self) |p, len| {
+ do self.as_mut_buf |p, len| {
unsafe { ptr::set_memory(p, value, len) };
}
}
#[should_fail]
fn test_as_imm_buf_fail() {
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
- do as_imm_buf(v) |_buf, _i| {
+ do v.as_imm_buf |_buf, _i| {
fail!()
}
}
#[should_fail]
fn test_as_mut_buf_fail() {
let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
- do as_mut_buf(v) |_buf, _i| {
+ do v.as_mut_buf |_buf, _i| {
fail!()
}
}
// error-pattern:index out of bounds
use std::sys;
-use std::vec;
fn main() {
// huge).
let x = ~[1u,2u,3u];
- do vec::as_imm_buf(x) |p, _len| {
+ do x.as_imm_buf |p, _len| {
let base = p as uint;
let idx = base / sys::size_of::<uint>();
error!("ov1 base = 0x%x", base);