Skip to content

De-closure-ify/remove vec & str .as_{imm,mut}_buf #11029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions src/libextra/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,18 @@ static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adle
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum

fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
bytes.as_imm_buf(|b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
rustrt::tdefl_compress_mem_to_heap(b as *c_void,
len as size_t,
&mut outsz,
flags);
assert!(res as int != 0);
unsafe {
let mut outsz : size_t = 0;
let res = rustrt::tdefl_compress_mem_to_heap(bytes.as_ptr() as *c_void,
bytes.len() as size_t,
&mut outsz,
flags);
assert!(res as int != 0);
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
libc::free(res);
out
}
})
libc::free(res);
out
}
}

pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
Expand All @@ -70,21 +67,18 @@ pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
}

fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
bytes.as_imm_buf(|b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
len as size_t,
&mut outsz,
flags);
assert!(res as int != 0);
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
libc::free(res);
out
}
})
unsafe {
let mut outsz : size_t = 0;
let res = rustrt::tinfl_decompress_mem_to_heap(bytes.as_ptr() as *c_void,
bytes.len() as size_t,
&mut outsz,
flags);
assert!(res as int != 0);
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
libc::free(res);
out
}
}

pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,7 @@ pub mod write {
add(*arg);
}

llvm_args.as_imm_buf(|p, len| {
llvm::LLVMRustSetLLVMOptions(len as c_int, p);
})
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
}

unsafe fn populate_llvm_passes(fpm: lib::llvm::PassManagerRef,
Expand Down
16 changes: 7 additions & 9 deletions src/librustc/back/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod windows {
let mut t = s.to_utf16();
// Null terminate before passing on.
t.push(0u16);
t.as_imm_buf(|buf, _len| f(buf))
f(t.as_ptr())
}

#[link_name = "kernel32"]
Expand Down Expand Up @@ -86,14 +86,12 @@ mod windows {
return Err(format!("failure in BeginUpdateResourceW: {}", os::last_os_error()));
}

let ok = manifest.as_imm_buf(|p, len| {
UpdateResourceW(hUpdate,
MAKEINTRESOURCEW(24), // RT_MANIFEST
MAKEINTRESOURCEW(1), // CREATEPROCESS_MANIFEST_RESOURCE_ID
0, // LANG_NEUTRAL, SUBLANG_NEUTRAL
p as LPCVOID,
len as u32)
});
let ok = UpdateResourceW(hUpdate,
MAKEINTRESOURCEW(24), // RT_MANIFEST
MAKEINTRESOURCEW(1), // CREATEPROCESS_MANIFEST_RESOURCE_ID
0, // LANG_NEUTRAL, SUBLANG_NEUTRAL
manifest.as_ptr() as LPCVOID,
manifest.len() as u32);
if ok == FALSE {
return Err(format!("failure in UpdateResourceW: {}", os::last_os_error()));
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2414,9 +2414,9 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
(rust_main, args)
};

let result = args.as_imm_buf(|buf, len| {
llvm::LLVMBuildCall(bld, start_fn, buf, len as c_uint, noname())
});
let result = llvm::LLVMBuildCall(bld, start_fn,
args.as_ptr(), args.len() as c_uint,
noname());

llvm::LLVMBuildRet(bld, result);
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/trans/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,11 @@ impl Builder {
let min = llvm::LLVMConstInt(t, lo, signed);
let max = llvm::LLVMConstInt(t, hi, signed);

[min, max].as_imm_buf(|ptr, len| {
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
llvm::LLVMMDNodeInContext(self.ccx.llcx,
ptr, len as c_uint));
})
let v = [min, max];

llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
llvm::LLVMMDNodeInContext(self.ccx.llcx,
v.as_ptr(), v.len() as c_uint));
}

value
Expand Down
21 changes: 9 additions & 12 deletions src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,9 +894,9 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
None => ()
}

let sc = s.as_imm_buf(|buf, buflen| {
llvm::LLVMConstStringInContext(cx.llcx, buf as *c_char, buflen as c_uint, False)
});
let sc = llvm::LLVMConstStringInContext(cx.llcx,
s.as_ptr() as *c_char, s.len() as c_uint,
False);

let gsym = token::gensym("str");
let g = format!("str{}", gsym).with_c_str(|buf| {
Expand Down Expand Up @@ -952,17 +952,16 @@ pub fn C_zero_byte_arr(size: uint) -> ValueRef {

pub fn C_struct(elts: &[ValueRef], packed: bool) -> ValueRef {
unsafe {
elts.as_imm_buf(|ptr, len| {
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, packed as Bool)
})

llvm::LLVMConstStructInContext(base::task_llcx(),
elts.as_ptr(), elts.len() as c_uint,
packed as Bool)
}
}

pub fn C_named_struct(T: Type, elts: &[ValueRef]) -> ValueRef {
unsafe {
elts.as_imm_buf(|ptr, len| {
llvm::LLVMConstNamedStruct(T.to_ref(), ptr, len as c_uint)
})
llvm::LLVMConstNamedStruct(T.to_ref(), elts.as_ptr(), elts.len() as c_uint)
}
}

Expand All @@ -988,9 +987,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 = us.as_imm_buf(|p, len| {
llvm::LLVMConstExtractValue(v, p, len as c_uint)
});
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);

debug!("const_get_elt(v={}, us={:?}, r={})",
cx.tn.val_to_str(v), us, cx.tn.val_to_str(r));
Expand Down
8 changes: 3 additions & 5 deletions src/librustc/middle/trans/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,9 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: @mut CrateContext,
}

// Perform the call itself
let llrust_ret_val = llrust_args.as_imm_buf(|ptr, len| {
debug!("calling llrustfn = {}", ccx.tn.val_to_str(llrustfn));
llvm::LLVMBuildCall(builder, llrustfn, ptr,
len as c_uint, noname())
});
debug!("calling llrustfn = {}", ccx.tn.val_to_str(llrustfn));
let llrust_ret_val = llvm::LLVMBuildCall(builder, llrustfn, llrust_args.as_ptr(),
llrust_args.len() as c_uint, noname());

// Get the return value where the foreign fn expects it.
let llforeign_ret_ty = match tys.fn_ty.ret_ty.cast {
Expand Down
5 changes: 2 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ fn render(w: &mut io::Writer, s: &str) {
let markdown = sd_markdown_new(extensions, 16, &callbacks,
&options as *html_renderopt as *libc::c_void);

s.as_imm_buf(|data, len| {
sd_markdown_render(ob, data, len as libc::size_t, markdown);
});

sd_markdown_render(ob, s.as_ptr(), s.len() as libc::size_t, markdown);
sd_markdown_free(markdown);

vec::raw::buf_as_slice((*ob).data, (*ob).size as uint, |buf| {
Expand Down
6 changes: 3 additions & 3 deletions src/librustuv/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Process {
},
flags: 0,
stdio_count: stdio.len() as libc::c_int,
stdio: stdio.as_imm_buf(|p, _| p),
stdio: stdio.as_ptr(),
uid: 0,
gid: 0,
};
Expand Down Expand Up @@ -163,7 +163,7 @@ fn with_argv<T>(prog: &str, args: &[~str], f: |**libc::c_char| -> T) -> T {
c_args.push(s.with_ref(|p| p));
}
c_args.push(ptr::null());
c_args.as_imm_buf(|buf, _| f(buf))
f(c_args.as_ptr())
}

/// Converts the environment to the env array expected by libuv
Expand All @@ -182,7 +182,7 @@ fn with_env<T>(env: Option<&[(~str, ~str)]>, f: |**libc::c_char| -> T) -> T {
c_envp.push(s.with_ref(|p| p));
}
c_envp.push(ptr::null());
c_envp.as_imm_buf(|buf, _| f(buf))
f(c_envp.as_ptr())
}

impl HomingIO for Process {
Expand Down
43 changes: 20 additions & 23 deletions src/libstd/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,16 @@ impl<'a> ToCStr for &'a [u8] {
}

unsafe fn to_c_str_unchecked(&self) -> CString {
self.as_imm_buf(|self_buf, self_len| {
let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
if buf.is_null() {
fail!("failed to allocate memory!");
}
let self_len = self.len();
let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
if buf.is_null() {
fail!("failed to allocate memory!");
}

ptr::copy_memory(buf, self_buf, self_len);
*ptr::mut_offset(buf, self_len as int) = 0;
ptr::copy_memory(buf, self.as_ptr(), self_len);
*ptr::mut_offset(buf, self_len as int) = 0;

CString::new(buf as *libc::c_char, true)
})
CString::new(buf as *libc::c_char, true)
}

fn with_c_str<T>(&self, f: |*libc::c_char| -> T) -> T {
Expand All @@ -296,13 +295,12 @@ unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
vec::bytes::copy_memory(buf, v);
buf[v.len()] = 0;

buf.as_mut_buf(|buf, _| {
if checked {
check_for_null(v, buf as *mut libc::c_char);
}
let buf = buf.as_mut_ptr();
if checked {
check_for_null(v, buf as *mut libc::c_char);
}

f(buf as *libc::c_char)
})
f(buf as *libc::c_char)
} else if checked {
v.to_c_str().with_ref(f)
} else {
Expand Down Expand Up @@ -575,15 +573,14 @@ mod bench {

#[inline]
fn check(s: &str, c_str: *libc::c_char) {
s.as_imm_buf(|s_buf, s_len| {
for i in range(0, s_len) {
unsafe {
assert_eq!(
*ptr::offset(s_buf, i as int) as libc::c_char,
*ptr::offset(c_str, i as int));
}
let s_buf = s.as_ptr();
for i in range(0, s.len()) {
unsafe {
assert_eq!(
*ptr::offset(s_buf, i as int) as libc::c_char,
*ptr::offset(c_str, i as int));
}
})
}
}

static s_short: &'static str = "Mary";
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/io/native/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
#[cfg(windows)] static eintr: int = 0; // doesn't matter
#[cfg(not(windows))] static eintr: int = libc::EINTR as int;

let (data, origamt) = data.as_imm_buf(|data, amt| (data, amt));
let mut data = data;
let origamt = data.len();
let mut data = data.as_ptr();
let mut amt = origamt;
while amt > 0 {
let mut ret;
Expand Down
9 changes: 5 additions & 4 deletions src/libstd/io/native/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use cast;
use io;
use libc::{pid_t, c_void, c_int};
use libc;
Expand All @@ -17,6 +16,8 @@ use prelude::*;
use ptr;
use rt::rtio;
use super::file;
#[cfg(windows)]
use cast;

use p = io::process;

Expand Down Expand Up @@ -453,7 +454,7 @@ fn with_argv<T>(prog: &str, args: &[~str], cb: |**libc::c_char| -> T) -> T {
// Finally, make sure we add a null pointer.
ptrs.push(ptr::null());

ptrs.as_imm_buf(|buf, _| cb(buf))
cb(ptrs.as_ptr())
}

#[cfg(unix)]
Expand All @@ -476,7 +477,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*c_void| -> T) -> T {
let mut ptrs = tmps.map(|tmp| tmp.with_ref(|buf| buf));
ptrs.push(ptr::null());

ptrs.as_imm_buf(|buf, _| unsafe { cb(cast::transmute(buf)) })
cb(ptrs.as_ptr() as *c_void)
}
_ => cb(ptr::null())
}
Expand All @@ -499,7 +500,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*mut c_void| -> T) -> T {

blk.push(0);

blk.as_imm_buf(|p, _len| unsafe { cb(cast::transmute(p)) })
cb(blk.as_mut_ptr() as *mut c_void)
}
_ => cb(ptr::mut_null())
}
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,10 @@ mod tests {
#[test]
fn test_get_str() {
let x = ~"test";
let addr_x = x.as_imm_buf(|buf, _len| buf);
let addr_x = x.as_ptr();
let opt = Some(x);
let y = opt.unwrap();
let addr_y = y.as_imm_buf(|buf, _len| buf);
let addr_y = y.as_ptr();
assert_eq!(addr_x, addr_y);
}

Expand Down
Loading