Skip to content

Commit

Permalink
Convert vec::{as_imm_buf, as_mut_buf} to methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
huonw committed Jul 3, 2013
1 parent a732a2d commit cdea73c
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/libextra/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
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 =
Expand All @@ -63,7 +63,7 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
}

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 =
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/par.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn map_slices<A:Copy + Send,B:Copy + Send>(
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() || {
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/uv_ll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ pub 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_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
Expand All @@ -1066,7 +1066,7 @@ pub 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_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);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ pub fn LoadRangeAssert(cx: block, PointerVal: ValueRef, lo: c_ulonglong,
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));
Expand Down Expand Up @@ -942,7 +942,7 @@ pub fn Call(cx: block, Fn: ValueRef, Args: &[ValueRef]) -> ValueRef {
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())
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,23 +774,23 @@ pub fn C_zero_byte_arr(size: uint) -> ValueRef {

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)
}
}
Expand Down Expand Up @@ -826,7 +826,7 @@ pub fn get_param(fndecl: ValueRef, param: uint) -> ValueRef {
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)
};

Expand Down
6 changes: 3 additions & 3 deletions src/libstd/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ fn convert_whence(whence: SeekStyle) -> i32 {
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,
Expand Down Expand Up @@ -1152,7 +1152,7 @@ impl<W:Writer,C> Writer for Wrapper<W, C> {
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,
Expand Down Expand Up @@ -1203,7 +1203,7 @@ impl Writer for fd_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);
Expand Down
10 changes: 5 additions & 5 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn as_c_charp<T>(s: &str, f: &fn(*c_char) -> T) -> 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))
Expand Down Expand Up @@ -122,7 +122,7 @@ pub mod win32 {
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;
Expand All @@ -147,7 +147,7 @@ pub mod win32 {
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))
}
}

Expand Down Expand Up @@ -937,7 +937,7 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
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);
Expand Down Expand Up @@ -1683,7 +1683,7 @@ mod tests {
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)
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub mod ptr_tests {
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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ pub fn seed() -> ~[u8] {
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
Expand Down Expand Up @@ -1087,7 +1087,7 @@ mod tests {
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);
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/rt/uv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use str::raw::from_c_str;
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};
Expand Down Expand Up @@ -300,7 +301,7 @@ pub fn vec_to_uv_buf(v: ~[u8]) -> Buf {
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)
}
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use prelude::*;
use ptr;
use str;
use task;
use vec;
use vec::ImmutableVector;

/**
* A value representing a child process.
Expand Down Expand Up @@ -703,7 +703,7 @@ fn with_argv<T>(prog: &str, args: &[~str],
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)]
Expand All @@ -722,7 +722,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*c_void) -> T) -> T {
}

ptrs.push(ptr::null());
vec::as_imm_buf(ptrs, |p, _len|
ptrs.as_imm_buf(|p, _len|
unsafe { cb(::cast::transmute(p)) }
)
}
Expand All @@ -743,7 +743,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*mut c_void) -> T) -> T {
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)) }
)
}
Expand Down
7 changes: 4 additions & 3 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ pub mod raw {
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 {
Expand All @@ -841,7 +842,7 @@ pub mod raw {
/// 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);
Expand All @@ -863,7 +864,7 @@ pub mod raw {

/// 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)
}
}
Expand Down Expand Up @@ -917,7 +918,7 @@ pub mod raw {
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);
Expand Down
Loading

0 comments on commit cdea73c

Please sign in to comment.