Skip to content

Commit 3c2c13b

Browse files
committed
auto merge of #11029 : huonw/rust/rm-vec-as-buf, r=cmr
For `str.as_mut_buf`, un-closure-ification is achieved by outright removal (see commit message). The others are replaced by `.as_ptr`, `.as_mut_ptr` and `.len`
2 parents b6933f8 + b906a8b commit 3c2c13b

File tree

20 files changed

+261
-401
lines changed

20 files changed

+261
-401
lines changed

src/libextra/flate.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,18 @@ static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adle
4444
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum
4545

4646
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
47-
bytes.as_imm_buf(|b, len| {
48-
unsafe {
49-
let mut outsz : size_t = 0;
50-
let res =
51-
rustrt::tdefl_compress_mem_to_heap(b as *c_void,
52-
len as size_t,
53-
&mut outsz,
54-
flags);
55-
assert!(res as int != 0);
47+
unsafe {
48+
let mut outsz : size_t = 0;
49+
let res = rustrt::tdefl_compress_mem_to_heap(bytes.as_ptr() as *c_void,
50+
bytes.len() as size_t,
51+
&mut outsz,
52+
flags);
53+
assert!(res as int != 0);
5654
let out = vec::raw::from_buf_raw(res as *u8,
5755
outsz as uint);
58-
libc::free(res);
59-
out
60-
}
61-
})
56+
libc::free(res);
57+
out
58+
}
6259
}
6360

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

7269
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
73-
bytes.as_imm_buf(|b, len| {
74-
unsafe {
75-
let mut outsz : size_t = 0;
76-
let res =
77-
rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
78-
len as size_t,
79-
&mut outsz,
80-
flags);
81-
assert!(res as int != 0);
82-
let out = vec::raw::from_buf_raw(res as *u8,
83-
outsz as uint);
84-
libc::free(res);
85-
out
86-
}
87-
})
70+
unsafe {
71+
let mut outsz : size_t = 0;
72+
let res = rustrt::tinfl_decompress_mem_to_heap(bytes.as_ptr() as *c_void,
73+
bytes.len() as size_t,
74+
&mut outsz,
75+
flags);
76+
assert!(res as int != 0);
77+
let out = vec::raw::from_buf_raw(res as *u8,
78+
outsz as uint);
79+
libc::free(res);
80+
out
81+
}
8882
}
8983

9084
pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {

src/librustc/back/link.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,7 @@ pub mod write {
354354
add(*arg);
355355
}
356356

357-
llvm_args.as_imm_buf(|p, len| {
358-
llvm::LLVMRustSetLLVMOptions(len as c_int, p);
359-
})
357+
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
360358
}
361359

362360
unsafe fn populate_llvm_passes(fpm: lib::llvm::PassManagerRef,

src/librustc/back/manifest.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mod windows {
5555
let mut t = s.to_utf16();
5656
// Null terminate before passing on.
5757
t.push(0u16);
58-
t.as_imm_buf(|buf, _len| f(buf))
58+
f(t.as_ptr())
5959
}
6060

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

89-
let ok = manifest.as_imm_buf(|p, len| {
90-
UpdateResourceW(hUpdate,
91-
MAKEINTRESOURCEW(24), // RT_MANIFEST
92-
MAKEINTRESOURCEW(1), // CREATEPROCESS_MANIFEST_RESOURCE_ID
93-
0, // LANG_NEUTRAL, SUBLANG_NEUTRAL
94-
p as LPCVOID,
95-
len as u32)
96-
});
89+
let ok = UpdateResourceW(hUpdate,
90+
MAKEINTRESOURCEW(24), // RT_MANIFEST
91+
MAKEINTRESOURCEW(1), // CREATEPROCESS_MANIFEST_RESOURCE_ID
92+
0, // LANG_NEUTRAL, SUBLANG_NEUTRAL
93+
manifest.as_ptr() as LPCVOID,
94+
manifest.len() as u32);
9795
if ok == FALSE {
9896
return Err(format!("failure in UpdateResourceW: {}", os::last_os_error()));
9997
}

src/librustc/middle/trans/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,9 +2404,9 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
24042404
(rust_main, args)
24052405
};
24062406

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

24112411
llvm::LLVMBuildRet(bld, result);
24122412
}

src/librustc/middle/trans/builder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,11 @@ impl Builder {
464464
let min = llvm::LLVMConstInt(t, lo, signed);
465465
let max = llvm::LLVMConstInt(t, hi, signed);
466466

467-
[min, max].as_imm_buf(|ptr, len| {
468-
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
469-
llvm::LLVMMDNodeInContext(self.ccx.llcx,
470-
ptr, len as c_uint));
471-
})
467+
let v = [min, max];
468+
469+
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
470+
llvm::LLVMMDNodeInContext(self.ccx.llcx,
471+
v.as_ptr(), v.len() as c_uint));
472472
}
473473

474474
value

src/librustc/middle/trans/common.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,9 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
894894
None => ()
895895
}
896896

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

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

953953
pub fn C_struct(elts: &[ValueRef], packed: bool) -> ValueRef {
954954
unsafe {
955-
elts.as_imm_buf(|ptr, len| {
956-
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, packed as Bool)
957-
})
955+
956+
llvm::LLVMConstStructInContext(base::task_llcx(),
957+
elts.as_ptr(), elts.len() as c_uint,
958+
packed as Bool)
958959
}
959960
}
960961

961962
pub fn C_named_struct(T: Type, elts: &[ValueRef]) -> ValueRef {
962963
unsafe {
963-
elts.as_imm_buf(|ptr, len| {
964-
llvm::LLVMConstNamedStruct(T.to_ref(), ptr, len as c_uint)
965-
})
964+
llvm::LLVMConstNamedStruct(T.to_ref(), elts.as_ptr(), elts.len() as c_uint)
966965
}
967966
}
968967

@@ -988,9 +987,7 @@ pub fn get_param(fndecl: ValueRef, param: uint) -> ValueRef {
988987
pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint])
989988
-> ValueRef {
990989
unsafe {
991-
let r = us.as_imm_buf(|p, len| {
992-
llvm::LLVMConstExtractValue(v, p, len as c_uint)
993-
});
990+
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
994991

995992
debug!("const_get_elt(v={}, us={:?}, r={})",
996993
cx.tn.val_to_str(v), us, cx.tn.val_to_str(r));

src/librustc/middle/trans/foreign.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,9 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: @mut CrateContext,
646646
}
647647

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

655653
// Get the return value where the foreign fn expects it.
656654
let llforeign_ret_ty = match tys.fn_ty.ret_ty.cast {

src/librustdoc/html/markdown.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ fn render(w: &mut io::Writer, s: &str) {
106106
let markdown = sd_markdown_new(extensions, 16, &callbacks,
107107
&options as *html_renderopt as *libc::c_void);
108108

109-
s.as_imm_buf(|data, len| {
110-
sd_markdown_render(ob, data, len as libc::size_t, markdown);
111-
});
109+
110+
sd_markdown_render(ob, s.as_ptr(), s.len() as libc::size_t, markdown);
112111
sd_markdown_free(markdown);
113112

114113
vec::raw::buf_as_slice((*ob).data, (*ob).size as uint, |buf| {

src/librustuv/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Process {
7070
},
7171
flags: 0,
7272
stdio_count: stdio.len() as libc::c_int,
73-
stdio: stdio.as_imm_buf(|p, _| p),
73+
stdio: stdio.as_ptr(),
7474
uid: 0,
7575
gid: 0,
7676
};
@@ -163,7 +163,7 @@ fn with_argv<T>(prog: &str, args: &[~str], f: |**libc::c_char| -> T) -> T {
163163
c_args.push(s.with_ref(|p| p));
164164
}
165165
c_args.push(ptr::null());
166-
c_args.as_imm_buf(|buf, _| f(buf))
166+
f(c_args.as_ptr())
167167
}
168168

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

188188
impl HomingIO for Process {

src/libstd/c_str.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,16 @@ impl<'a> ToCStr for &'a [u8] {
267267
}
268268

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

276-
ptr::copy_memory(buf, self_buf, self_len);
277-
*ptr::mut_offset(buf, self_len as int) = 0;
276+
ptr::copy_memory(buf, self.as_ptr(), self_len);
277+
*ptr::mut_offset(buf, self_len as int) = 0;
278278

279-
CString::new(buf as *libc::c_char, true)
280-
})
279+
CString::new(buf as *libc::c_char, true)
281280
}
282281

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

299-
buf.as_mut_buf(|buf, _| {
300-
if checked {
301-
check_for_null(v, buf as *mut libc::c_char);
302-
}
298+
let buf = buf.as_mut_ptr();
299+
if checked {
300+
check_for_null(v, buf as *mut libc::c_char);
301+
}
303302

304-
f(buf as *libc::c_char)
305-
})
303+
f(buf as *libc::c_char)
306304
} else if checked {
307305
v.to_c_str().with_ref(f)
308306
} else {
@@ -575,15 +573,14 @@ mod bench {
575573

576574
#[inline]
577575
fn check(s: &str, c_str: *libc::c_char) {
578-
s.as_imm_buf(|s_buf, s_len| {
579-
for i in range(0, s_len) {
580-
unsafe {
581-
assert_eq!(
582-
*ptr::offset(s_buf, i as int) as libc::c_char,
583-
*ptr::offset(c_str, i as int));
584-
}
576+
let s_buf = s.as_ptr();
577+
for i in range(0, s.len()) {
578+
unsafe {
579+
assert_eq!(
580+
*ptr::offset(s_buf, i as int) as libc::c_char,
581+
*ptr::offset(c_str, i as int));
585582
}
586-
})
583+
}
587584
}
588585

589586
static s_short: &'static str = "Mary";

src/libstd/io/native/file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
3737
#[cfg(windows)] static eintr: int = 0; // doesn't matter
3838
#[cfg(not(windows))] static eintr: int = libc::EINTR as int;
3939

40-
let (data, origamt) = data.as_imm_buf(|data, amt| (data, amt));
41-
let mut data = data;
40+
let origamt = data.len();
41+
let mut data = data.as_ptr();
4242
let mut amt = origamt;
4343
while amt > 0 {
4444
let mut ret;

src/libstd/io/native/process.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use cast;
1211
use io;
1312
use libc::{pid_t, c_void, c_int};
1413
use libc;
@@ -17,6 +16,8 @@ use prelude::*;
1716
use ptr;
1817
use rt::rtio;
1918
use super::file;
19+
#[cfg(windows)]
20+
use cast;
2021

2122
use p = io::process;
2223

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

456-
ptrs.as_imm_buf(|buf, _| cb(buf))
457+
cb(ptrs.as_ptr())
457458
}
458459

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

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

500501
blk.push(0);
501502

502-
blk.as_imm_buf(|p, _len| unsafe { cb(cast::transmute(p)) })
503+
cb(blk.as_mut_ptr() as *mut c_void)
503504
}
504505
_ => cb(ptr::mut_null())
505506
}

src/libstd/option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,10 @@ mod tests {
436436
#[test]
437437
fn test_get_str() {
438438
let x = ~"test";
439-
let addr_x = x.as_imm_buf(|buf, _len| buf);
439+
let addr_x = x.as_ptr();
440440
let opt = Some(x);
441441
let y = opt.unwrap();
442-
let addr_y = y.as_imm_buf(|buf, _len| buf);
442+
let addr_y = y.as_ptr();
443443
assert_eq!(addr_x, addr_y);
444444
}
445445

0 commit comments

Comments
 (0)