1
Fork 0

Add ToCStr method .with_c_str()

.with_c_str() is a replacement for the old .as_c_str(), to avoid
unnecessary boilerplate.

Replace all usages of .to_c_str().with_ref() with .with_c_str().
This commit is contained in:
Kevin Ballard 2013-08-14 19:21:59 -07:00
parent 48265b779f
commit 03ef71e262
33 changed files with 159 additions and 136 deletions

View file

@ -32,7 +32,7 @@ pub mod rustrt {
/// Add a line to history /// Add a line to history
pub unsafe fn add_history(line: &str) -> bool { pub unsafe fn add_history(line: &str) -> bool {
do line.to_c_str().with_ref |buf| { do line.with_c_str |buf| {
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
} }
} }
@ -44,21 +44,21 @@ pub unsafe fn set_history_max_len(len: int) -> bool {
/// Save line history to a file /// Save line history to a file
pub unsafe fn save_history(file: &str) -> bool { pub unsafe fn save_history(file: &str) -> bool {
do file.to_c_str().with_ref |buf| { do file.with_c_str |buf| {
rustrt::linenoiseHistorySave(buf) == 1 as c_int rustrt::linenoiseHistorySave(buf) == 1 as c_int
} }
} }
/// Load line history from a file /// Load line history from a file
pub unsafe fn load_history(file: &str) -> bool { pub unsafe fn load_history(file: &str) -> bool {
do file.to_c_str().with_ref |buf| { do file.with_c_str |buf| {
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
} }
} }
/// Print out a prompt and then wait for input and return it /// Print out a prompt and then wait for input and return it
pub unsafe fn read(prompt: &str) -> Option<~str> { pub unsafe fn read(prompt: &str) -> Option<~str> {
do prompt.to_c_str().with_ref |buf| { do prompt.with_c_str |buf| {
let line = rustrt::linenoise(buf); let line = rustrt::linenoise(buf);
if line.is_null() { None } if line.is_null() { None }
@ -80,7 +80,7 @@ pub unsafe fn complete(cb: CompletionCb) {
unsafe { unsafe {
do cb(str::raw::from_c_str(line)) |suggestion| { do cb(str::raw::from_c_str(line)) |suggestion| {
do suggestion.to_c_str().with_ref |buf| { do suggestion.with_c_str |buf| {
rustrt::linenoiseAddCompletion(completions, buf); rustrt::linenoiseAddCompletion(completions, buf);
} }
} }

View file

@ -78,10 +78,10 @@ pub fn WriteOutputFile(sess: Session,
OptLevel: c_int, OptLevel: c_int,
EnableSegmentedStacks: bool) { EnableSegmentedStacks: bool) {
unsafe { unsafe {
do Triple.to_c_str().with_ref |Triple| { do Triple.with_c_str |Triple| {
do Cpu.to_c_str().with_ref |Cpu| { do Cpu.with_c_str |Cpu| {
do Feature.to_c_str().with_ref |Feature| { do Feature.with_c_str |Feature| {
do Output.to_c_str().with_ref |Output| { do Output.with_c_str |Output| {
let result = llvm::LLVMRustWriteOutputFile( let result = llvm::LLVMRustWriteOutputFile(
PM, PM,
M, M,
@ -152,7 +152,7 @@ pub mod jit {
debug!("linking: %s", path); debug!("linking: %s", path);
do path.to_c_str().with_ref |buf_t| { do path.with_c_str |buf_t| {
if !llvm::LLVMRustLoadCrate(manager, buf_t) { if !llvm::LLVMRustLoadCrate(manager, buf_t) {
llvm_err(sess, ~"Could not link"); llvm_err(sess, ~"Could not link");
} }
@ -171,7 +171,7 @@ pub mod jit {
// Next, we need to get a handle on the _rust_main function by // Next, we need to get a handle on the _rust_main function by
// looking up it's corresponding ValueRef and then requesting that // looking up it's corresponding ValueRef and then requesting that
// the execution engine compiles the function. // the execution engine compiles the function.
let fun = do "_rust_main".to_c_str().with_ref |entry| { let fun = do "_rust_main".with_c_str |entry| {
llvm::LLVMGetNamedFunction(m, entry) llvm::LLVMGetNamedFunction(m, entry)
}; };
if fun.is_null() { if fun.is_null() {
@ -270,14 +270,14 @@ pub mod write {
output_type_bitcode => { output_type_bitcode => {
if opts.optimize != session::No { if opts.optimize != session::No {
let filename = output.with_filetype("no-opt.bc"); let filename = output.with_filetype("no-opt.bc");
do filename.to_c_str().with_ref |buf| { do filename.with_c_str |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf); llvm::LLVMWriteBitcodeToFile(llmod, buf);
} }
} }
} }
_ => { _ => {
let filename = output.with_filetype("bc"); let filename = output.with_filetype("bc");
do filename.to_c_str().with_ref |buf| { do filename.with_c_str |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf); llvm::LLVMWriteBitcodeToFile(llmod, buf);
} }
} }
@ -340,7 +340,7 @@ pub mod write {
// Always output the bitcode file with --save-temps // Always output the bitcode file with --save-temps
let filename = output.with_filetype("opt.bc"); let filename = output.with_filetype("opt.bc");
do filename.to_c_str().with_ref |buf| { do filename.with_c_str |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf) llvm::LLVMWriteBitcodeToFile(llmod, buf)
}; };
// Save the assembly file if -S is used // Save the assembly file if -S is used
@ -401,13 +401,13 @@ pub mod write {
if output_type == output_type_llvm_assembly { if output_type == output_type_llvm_assembly {
// Given options "-S --emit-llvm": output LLVM assembly // Given options "-S --emit-llvm": output LLVM assembly
do output.to_c_str().with_ref |buf_o| { do output.with_c_str |buf_o| {
llvm::LLVMRustAddPrintModulePass(pm.llpm, llmod, buf_o); llvm::LLVMRustAddPrintModulePass(pm.llpm, llmod, buf_o);
} }
} else { } else {
// If only a bitcode file is asked for by using the // If only a bitcode file is asked for by using the
// '--emit-llvm' flag, then output it here // '--emit-llvm' flag, then output it here
do output.to_c_str().with_ref |buf| { do output.with_c_str |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf); llvm::LLVMWriteBitcodeToFile(llmod, buf);
} }
} }

View file

@ -173,7 +173,7 @@ pub fn populate_pass_manager(sess: Session, pm: &mut PassManager, pass_list:&[~s
} }
pub fn create_pass(name:&str) -> Option<PassRef> { pub fn create_pass(name:&str) -> Option<PassRef> {
do name.to_c_str().with_ref |s| { do name.with_c_str |s| {
unsafe { unsafe {
let p = llvm::LLVMCreatePass(s); let p = llvm::LLVMCreatePass(s);
if p.is_null() { if p.is_null() {

View file

@ -2271,7 +2271,7 @@ pub struct TargetData {
} }
pub fn mk_target_data(string_rep: &str) -> TargetData { pub fn mk_target_data(string_rep: &str) -> TargetData {
let lltd = do string_rep.to_c_str().with_ref |buf| { let lltd = do string_rep.with_c_str |buf| {
unsafe { llvm::LLVMCreateTargetData(buf) } unsafe { llvm::LLVMCreateTargetData(buf) }
}; };

View file

@ -199,7 +199,7 @@ pub fn metadata_matches(extern_metas: &[@ast::MetaItem],
fn get_metadata_section(os: os, fn get_metadata_section(os: os,
filename: &Path) -> Option<@~[u8]> { filename: &Path) -> Option<@~[u8]> {
unsafe { unsafe {
let mb = do filename.to_c_str().with_ref |buf| { let mb = do filename.with_c_str |buf| {
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf) llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
}; };
if mb as int == 0 { return option::None::<@~[u8]>; } if mb as int == 0 { return option::None::<@~[u8]>; }

View file

@ -120,8 +120,8 @@ pub fn trans_inline_asm(bcx: @mut Block, ia: &ast::inline_asm) -> @mut Block {
ast::asm_intel => lib::llvm::AD_Intel ast::asm_intel => lib::llvm::AD_Intel
}; };
let r = do ia.asm.to_c_str().with_ref |a| { let r = do ia.asm.with_c_str |a| {
do constraints.to_c_str().with_ref |c| { do constraints.with_c_str |c| {
InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect) InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
} }
}; };

View file

@ -181,7 +181,7 @@ impl<'self> Drop for StatRecorder<'self> {
} }
pub fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv, ty: Type) -> ValueRef { pub fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv, ty: Type) -> ValueRef {
let llfn: ValueRef = do name.to_c_str().with_ref |buf| { let llfn: ValueRef = do name.with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMGetOrInsertFunction(llmod, buf, ty.to_ref()) llvm::LLVMGetOrInsertFunction(llmod, buf, ty.to_ref())
} }
@ -221,7 +221,7 @@ pub fn get_extern_const(externs: &mut ExternMap, llmod: ModuleRef,
None => () None => ()
} }
unsafe { unsafe {
let c = do name.to_c_str().with_ref |buf| { let c = do name.with_c_str |buf| {
llvm::LLVMAddGlobal(llmod, ty.to_ref(), buf) llvm::LLVMAddGlobal(llmod, ty.to_ref(), buf)
}; };
externs.insert(name, c); externs.insert(name, c);
@ -523,7 +523,7 @@ pub fn get_res_dtor(ccx: @mut CrateContext,
// Structural comparison: a rather involved form of glue. // Structural comparison: a rather involved form of glue.
pub fn maybe_name_value(cx: &CrateContext, v: ValueRef, s: &str) { pub fn maybe_name_value(cx: &CrateContext, v: ValueRef, s: &str) {
if cx.sess.opts.save_temps { if cx.sess.opts.save_temps {
do s.to_c_str().with_ref |buf| { do s.with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMSetValueName(v, buf) llvm::LLVMSetValueName(v, buf)
} }
@ -1136,7 +1136,7 @@ pub fn new_block(cx: @mut FunctionContext,
opt_node_info: Option<NodeInfo>) opt_node_info: Option<NodeInfo>)
-> @mut Block { -> @mut Block {
unsafe { unsafe {
let llbb = do name.to_c_str().with_ref |buf| { let llbb = do name.with_c_str |buf| {
llvm::LLVMAppendBasicBlockInContext(cx.ccx.llcx, cx.llfn, buf) llvm::LLVMAppendBasicBlockInContext(cx.ccx.llcx, cx.llfn, buf)
}; };
let bcx = @mut Block::new(llbb, let bcx = @mut Block::new(llbb,
@ -1553,7 +1553,7 @@ pub struct BasicBlocks {
pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef { pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
unsafe { unsafe {
let cx = task_llcx(); let cx = task_llcx();
do "static_allocas".to_c_str().with_ref | buf| { do "static_allocas".with_c_str | buf| {
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf) llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
} }
} }
@ -1562,7 +1562,7 @@ pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
pub fn mk_return_basic_block(llfn: ValueRef) -> BasicBlockRef { pub fn mk_return_basic_block(llfn: ValueRef) -> BasicBlockRef {
unsafe { unsafe {
let cx = task_llcx(); let cx = task_llcx();
do "return".to_c_str().with_ref |buf| { do "return".with_c_str |buf| {
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf) llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
} }
} }
@ -2332,7 +2332,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
}; };
decl_cdecl_fn(ccx.llmod, main_name, llfty) decl_cdecl_fn(ccx.llmod, main_name, llfty)
}; };
let llbb = do "top".to_c_str().with_ref |buf| { let llbb = do "top".with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf) llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf)
} }
@ -2342,7 +2342,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
llvm::LLVMPositionBuilderAtEnd(bld, llbb); llvm::LLVMPositionBuilderAtEnd(bld, llbb);
let crate_map = ccx.crate_map; let crate_map = ccx.crate_map;
let opaque_crate_map = do "crate_map".to_c_str().with_ref |buf| { let opaque_crate_map = do "crate_map".with_c_str |buf| {
llvm::LLVMBuildPointerCast(bld, crate_map, Type::i8p().to_ref(), buf) llvm::LLVMBuildPointerCast(bld, crate_map, Type::i8p().to_ref(), buf)
}; };
@ -2360,7 +2360,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
}; };
let args = { let args = {
let opaque_rust_main = do "rust_main".to_c_str().with_ref |buf| { let opaque_rust_main = do "rust_main".with_c_str |buf| {
llvm::LLVMBuildPointerCast(bld, rust_main, Type::i8p().to_ref(), buf) llvm::LLVMBuildPointerCast(bld, rust_main, Type::i8p().to_ref(), buf)
}; };
@ -2442,7 +2442,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
unsafe { unsafe {
let llty = llvm::LLVMTypeOf(v); let llty = llvm::LLVMTypeOf(v);
let g = do sym.to_c_str().with_ref |buf| { let g = do sym.with_c_str |buf| {
llvm::LLVMAddGlobal(ccx.llmod, llty, buf) llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
}; };
@ -2475,7 +2475,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) { match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
Some(sect) => unsafe { Some(sect) => unsafe {
do sect.to_c_str().with_ref |buf| { do sect.with_c_str |buf| {
llvm::LLVMSetSection(v, buf); llvm::LLVMSetSection(v, buf);
} }
}, },
@ -2516,7 +2516,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
} }
ast::foreign_item_static(*) => { ast::foreign_item_static(*) => {
let ident = token::ident_to_str(&ni.ident); let ident = token::ident_to_str(&ni.ident);
let g = do ident.to_c_str().with_ref |buf| { let g = do ident.with_c_str |buf| {
unsafe { unsafe {
let ty = type_of(ccx, ty); let ty = type_of(ccx, ty);
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
@ -2623,7 +2623,7 @@ pub fn trans_constant(ccx: &mut CrateContext, it: @ast::item) {
let s = mangle_exported_name(ccx, p, ty::mk_int()).to_managed(); let s = mangle_exported_name(ccx, p, ty::mk_int()).to_managed();
let disr_val = vi[i].disr_val; let disr_val = vi[i].disr_val;
note_unique_llvm_symbol(ccx, s); note_unique_llvm_symbol(ccx, s);
let discrim_gvar = do s.to_c_str().with_ref |buf| { let discrim_gvar = do s.with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
} }
@ -2818,7 +2818,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
} }
let gc_metadata_name = ~"_gc_module_metadata_" + llmod_id; let gc_metadata_name = ~"_gc_module_metadata_" + llmod_id;
let gc_metadata = do gc_metadata_name.to_c_str().with_ref |buf| { let gc_metadata = do gc_metadata_name.with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
} }
@ -2833,7 +2833,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
pub fn create_module_map(ccx: &mut CrateContext) -> ValueRef { pub fn create_module_map(ccx: &mut CrateContext) -> ValueRef {
let elttype = Type::struct_([ccx.int_type, ccx.int_type], false); let elttype = Type::struct_([ccx.int_type, ccx.int_type], false);
let maptype = Type::array(&elttype, (ccx.module_data.len() + 1) as u64); let maptype = Type::array(&elttype, (ccx.module_data.len() + 1) as u64);
let map = do "_rust_mod_map".to_c_str().with_ref |buf| { let map = do "_rust_mod_map".with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAddGlobal(ccx.llmod, maptype.to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, maptype.to_ref(), buf)
} }
@ -2881,7 +2881,7 @@ pub fn decl_crate_map(sess: session::Session, mapmeta: LinkMeta,
let sym_name = ~"_rust_crate_map_" + mapname; let sym_name = ~"_rust_crate_map_" + mapname;
let arrtype = Type::array(&int_type, n_subcrates as u64); let arrtype = Type::array(&int_type, n_subcrates as u64);
let maptype = Type::struct_([Type::i32(), Type::i8p(), int_type, arrtype], false); let maptype = Type::struct_([Type::i32(), Type::i8p(), int_type, arrtype], false);
let map = do sym_name.to_c_str().with_ref |buf| { let map = do sym_name.with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAddGlobal(llmod, maptype.to_ref(), buf) llvm::LLVMAddGlobal(llmod, maptype.to_ref(), buf)
} }
@ -2900,7 +2900,7 @@ pub fn fill_crate_map(ccx: @mut CrateContext, map: ValueRef) {
cdata.name, cdata.name,
cstore::get_crate_vers(cstore, i), cstore::get_crate_vers(cstore, i),
cstore::get_crate_hash(cstore, i)); cstore::get_crate_hash(cstore, i));
let cr = do nm.to_c_str().with_ref |buf| { let cr = do nm.with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
} }
@ -2963,21 +2963,21 @@ pub fn write_metadata(cx: &mut CrateContext, crate: &ast::Crate) {
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item); let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
let llmeta = C_bytes(encoder::encode_metadata(encode_parms, crate)); let llmeta = C_bytes(encoder::encode_metadata(encode_parms, crate));
let llconst = C_struct([llmeta]); let llconst = C_struct([llmeta]);
let mut llglobal = do "rust_metadata".to_c_str().with_ref |buf| { let mut llglobal = do "rust_metadata".with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAddGlobal(cx.llmod, val_ty(llconst).to_ref(), buf) llvm::LLVMAddGlobal(cx.llmod, val_ty(llconst).to_ref(), buf)
} }
}; };
unsafe { unsafe {
llvm::LLVMSetInitializer(llglobal, llconst); llvm::LLVMSetInitializer(llglobal, llconst);
do cx.sess.targ_cfg.target_strs.meta_sect_name.to_c_str().with_ref |buf| { do cx.sess.targ_cfg.target_strs.meta_sect_name.with_c_str |buf| {
llvm::LLVMSetSection(llglobal, buf) llvm::LLVMSetSection(llglobal, buf)
}; };
lib::llvm::SetLinkage(llglobal, lib::llvm::InternalLinkage); lib::llvm::SetLinkage(llglobal, lib::llvm::InternalLinkage);
let t_ptr_i8 = Type::i8p(); let t_ptr_i8 = Type::i8p();
llglobal = llvm::LLVMConstBitCast(llglobal, t_ptr_i8.to_ref()); llglobal = llvm::LLVMConstBitCast(llglobal, t_ptr_i8.to_ref());
let llvm_used = do "llvm.used".to_c_str().with_ref |buf| { let llvm_used = do "llvm.used".with_c_str |buf| {
llvm::LLVMAddGlobal(cx.llmod, Type::array(&t_ptr_i8, 1).to_ref(), buf) llvm::LLVMAddGlobal(cx.llmod, Type::array(&t_ptr_i8, 1).to_ref(), buf)
}; };
lib::llvm::SetLinkage(llvm_used, lib::llvm::AppendingLinkage); lib::llvm::SetLinkage(llvm_used, lib::llvm::AppendingLinkage);
@ -2991,7 +2991,7 @@ fn mk_global(ccx: &CrateContext,
internal: bool) internal: bool)
-> ValueRef { -> ValueRef {
unsafe { unsafe {
let llglobal = do name.to_c_str().with_ref |buf| { let llglobal = do name.with_c_str |buf| {
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llval).to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, val_ty(llval).to_ref(), buf)
}; };
llvm::LLVMSetInitializer(llglobal, llval); llvm::LLVMSetInitializer(llglobal, llval);

View file

@ -423,7 +423,7 @@ impl Builder {
if name.is_empty() { if name.is_empty() {
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname()) llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
} else { } else {
do name.to_c_str().with_ref |c| { do name.with_c_str |c| {
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c) llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c)
} }
} }
@ -739,7 +739,7 @@ impl Builder {
let sanitized = text.replace("$", ""); let sanitized = text.replace("$", "");
let comment_text = fmt!("# %s", sanitized.replace("\n", "\n\t# ")); let comment_text = fmt!("# %s", sanitized.replace("\n", "\n\t# "));
self.count_insn("inlineasm"); self.count_insn("inlineasm");
let asm = do comment_text.to_c_str().with_ref |c| { let asm = do comment_text.with_c_str |c| {
unsafe { unsafe {
llvm::LLVMConstInlineAsm(Type::func([], &Type::void()).to_ref(), llvm::LLVMConstInlineAsm(Type::func([], &Type::void()).to_ref(),
c, noname(), False, False) c, noname(), False, False)
@ -895,7 +895,7 @@ impl Builder {
let BB: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder); let BB: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
let FN: ValueRef = llvm::LLVMGetBasicBlockParent(BB); let FN: ValueRef = llvm::LLVMGetBasicBlockParent(BB);
let M: ModuleRef = llvm::LLVMGetGlobalParent(FN); let M: ModuleRef = llvm::LLVMGetGlobalParent(FN);
let T: ValueRef = do "llvm.trap".to_c_str().with_ref |buf| { let T: ValueRef = do "llvm.trap".with_c_str |buf| {
llvm::LLVMGetNamedFunction(M, buf) llvm::LLVMGetNamedFunction(M, buf)
}; };
assert!((T as int != 0)); assert!((T as int != 0));

View file

@ -712,7 +712,7 @@ pub fn C_integral(t: Type, u: u64, sign_extend: bool) -> ValueRef {
pub fn C_floating(s: &str, t: Type) -> ValueRef { pub fn C_floating(s: &str, t: Type) -> ValueRef {
unsafe { unsafe {
do s.to_c_str().with_ref |buf| { do s.with_c_str |buf| {
llvm::LLVMConstRealOfString(t.to_ref(), buf) llvm::LLVMConstRealOfString(t.to_ref(), buf)
} }
} }
@ -760,12 +760,12 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
None => () None => ()
} }
let sc = do s.to_c_str().with_ref |buf| { let sc = do s.with_c_str |buf| {
llvm::LLVMConstStringInContext(cx.llcx, buf, s.len() as c_uint, False) llvm::LLVMConstStringInContext(cx.llcx, buf, s.len() as c_uint, False)
}; };
let gsym = token::gensym("str"); let gsym = token::gensym("str");
let g = do fmt!("str%u", gsym).to_c_str().with_ref |buf| { let g = do fmt!("str%u", gsym).with_c_str |buf| {
llvm::LLVMAddGlobal(cx.llmod, val_ty(sc).to_ref(), buf) llvm::LLVMAddGlobal(cx.llmod, val_ty(sc).to_ref(), buf)
}; };
llvm::LLVMSetInitializer(g, sc); llvm::LLVMSetInitializer(g, sc);

View file

@ -102,7 +102,7 @@ pub fn const_vec(cx: @mut CrateContext, e: &ast::expr, es: &[@ast::expr])
fn const_addr_of(cx: &mut CrateContext, cv: ValueRef) -> ValueRef { fn const_addr_of(cx: &mut CrateContext, cv: ValueRef) -> ValueRef {
unsafe { unsafe {
let gv = do "const".to_c_str().with_ref |name| { let gv = do "const".with_c_str |name| {
llvm::LLVMAddGlobal(cx.llmod, val_ty(cv).to_ref(), name) llvm::LLVMAddGlobal(cx.llmod, val_ty(cv).to_ref(), name)
}; };
llvm::LLVMSetInitializer(gv, cv); llvm::LLVMSetInitializer(gv, cv);
@ -528,7 +528,7 @@ fn const_expr_unadjusted(cx: @mut CrateContext, e: &ast::expr) -> ValueRef {
ast::expr_vec(ref es, ast::m_imm) => { ast::expr_vec(ref es, ast::m_imm) => {
let (cv, sz, llunitty) = const_vec(cx, e, *es); let (cv, sz, llunitty) = const_vec(cx, e, *es);
let llty = val_ty(cv); let llty = val_ty(cv);
let gv = do "const".to_c_str().with_ref |name| { let gv = do "const".with_c_str |name| {
llvm::LLVMAddGlobal(cx.llmod, llty.to_ref(), name) llvm::LLVMAddGlobal(cx.llmod, llty.to_ref(), name)
}; };
llvm::LLVMSetInitializer(gv, cv); llvm::LLVMSetInitializer(gv, cv);

View file

@ -128,15 +128,15 @@ impl CrateContext {
unsafe { unsafe {
let llcx = llvm::LLVMContextCreate(); let llcx = llvm::LLVMContextCreate();
set_task_llcx(llcx); set_task_llcx(llcx);
let llmod = do name.to_c_str().with_ref |buf| { let llmod = do name.with_c_str |buf| {
llvm::LLVMModuleCreateWithNameInContext(buf, llcx) llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
}; };
let data_layout: &str = sess.targ_cfg.target_strs.data_layout; let data_layout: &str = sess.targ_cfg.target_strs.data_layout;
let targ_triple: &str = sess.targ_cfg.target_strs.target_triple; let targ_triple: &str = sess.targ_cfg.target_strs.target_triple;
do data_layout.to_c_str().with_ref |buf| { do data_layout.with_c_str |buf| {
llvm::LLVMSetDataLayout(llmod, buf) llvm::LLVMSetDataLayout(llmod, buf)
}; };
do targ_triple.to_c_str().with_ref |buf| { do targ_triple.with_c_str |buf| {
llvm::LLVMSetTarget(llmod, buf) llvm::LLVMSetTarget(llmod, buf)
}; };
let targ_cfg = sess.targ_cfg; let targ_cfg = sess.targ_cfg;

View file

@ -238,7 +238,7 @@ pub fn trans_log(log_ex: &ast::expr,
ccx, modpath, "loglevel"); ccx, modpath, "loglevel");
let global; let global;
unsafe { unsafe {
global = do s.to_c_str().with_ref |buf| { global = do s.with_c_str |buf| {
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
}; };
llvm::LLVMSetGlobalConstant(global, False); llvm::LLVMSetGlobalConstant(global, False);

View file

@ -207,7 +207,7 @@ pub fn create_argument_metadata(bcx: @mut Block,
argument_index as c_uint argument_index as c_uint
}; };
let arg_metadata = do name.to_c_str().with_ref |name| { let arg_metadata = do name.with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateLocalVariable( llvm::LLVMDIBuilderCreateLocalVariable(
DIB(cx), DIB(cx),
@ -351,8 +351,8 @@ pub fn create_function_metadata(fcx: &mut FunctionContext) -> DISubprogram {
}; };
let fn_metadata = let fn_metadata =
do cx.sess.str_of(ident).to_c_str().with_ref |name| { do cx.sess.str_of(ident).with_c_str |name| {
do cx.sess.str_of(ident).to_c_str().with_ref |linkage| { do cx.sess.str_of(ident).with_c_str |linkage| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateFunction( llvm::LLVMDIBuilderCreateFunction(
DIB(cx), DIB(cx),
@ -420,11 +420,11 @@ fn compile_unit_metadata(cx: @mut CrateContext) {
let work_dir = cx.sess.working_dir.to_str(); let work_dir = cx.sess.working_dir.to_str();
let producer = fmt!("rustc version %s", env!("CFG_VERSION")); let producer = fmt!("rustc version %s", env!("CFG_VERSION"));
do crate_name.to_c_str().with_ref |crate_name| { do crate_name.with_c_str |crate_name| {
do work_dir.to_c_str().with_ref |work_dir| { do work_dir.with_c_str |work_dir| {
do producer.to_c_str().with_ref |producer| { do producer.with_c_str |producer| {
do "".to_c_str().with_ref |flags| { do "".with_c_str |flags| {
do "".to_c_str().with_ref |split_name| { do "".with_c_str |split_name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateCompileUnit(dcx.builder, llvm::LLVMDIBuilderCreateCompileUnit(dcx.builder,
DW_LANG_RUST as c_uint, crate_name, work_dir, producer, DW_LANG_RUST as c_uint, crate_name, work_dir, producer,
@ -449,7 +449,7 @@ fn declare_local(bcx: @mut Block,
let type_metadata = type_metadata(cx, variable_type, span); let type_metadata = type_metadata(cx, variable_type, span);
let scope = scope_metadata(bcx.fcx, node_id, span); let scope = scope_metadata(bcx.fcx, node_id, span);
let var_metadata = do name.to_c_str().with_ref |name| { let var_metadata = do name.with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateLocalVariable( llvm::LLVMDIBuilderCreateLocalVariable(
DIB(cx), DIB(cx),
@ -501,8 +501,8 @@ fn file_metadata(cx: &mut CrateContext, full_path: &str) -> DIFile {
}; };
let file_metadata = let file_metadata =
do file_name.to_c_str().with_ref |file_name| { do file_name.with_c_str |file_name| {
do work_dir.to_c_str().with_ref |work_dir| { do work_dir.with_c_str |work_dir| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateFile(DIB(cx), file_name, work_dir) llvm::LLVMDIBuilderCreateFile(DIB(cx), file_name, work_dir)
} }
@ -566,7 +566,7 @@ fn basic_type_metadata(cx: &mut CrateContext, t: ty::t) -> DIType {
let llvm_type = type_of::type_of(cx, t); let llvm_type = type_of::type_of(cx, t);
let (size, align) = size_and_align_of(cx, llvm_type); let (size, align) = size_and_align_of(cx, llvm_type);
let ty_metadata = do name.to_c_str().with_ref |name| { let ty_metadata = do name.with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateBasicType( llvm::LLVMDIBuilderCreateBasicType(
DIB(cx), DIB(cx),
@ -587,7 +587,7 @@ fn pointer_type_metadata(cx: &mut CrateContext,
let pointer_llvm_type = type_of::type_of(cx, pointer_type); let pointer_llvm_type = type_of::type_of(cx, pointer_type);
let (pointer_size, pointer_align) = size_and_align_of(cx, pointer_llvm_type); let (pointer_size, pointer_align) = size_and_align_of(cx, pointer_llvm_type);
let name = ty_to_str(cx.tcx, pointer_type); let name = ty_to_str(cx.tcx, pointer_type);
let ptr_metadata = do name.to_c_str().with_ref |name| { let ptr_metadata = do name.with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreatePointerType( llvm::LLVMDIBuilderCreatePointerType(
DIB(cx), DIB(cx),
@ -681,7 +681,7 @@ fn enum_metadata(cx: &mut CrateContext,
let name: &str = cx.sess.str_of(v.name); let name: &str = cx.sess.str_of(v.name);
let discriminant_value = v.disr_val as c_ulonglong; let discriminant_value = v.disr_val as c_ulonglong;
do name.to_c_str().with_ref |name| { do name.with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateEnumerator( llvm::LLVMDIBuilderCreateEnumerator(
DIB(cx), DIB(cx),
@ -695,7 +695,7 @@ fn enum_metadata(cx: &mut CrateContext,
let loc = span_start(cx, span); let loc = span_start(cx, span);
let file_metadata = file_metadata(cx, loc.file.name); let file_metadata = file_metadata(cx, loc.file.name);
let discriminant_type_metadata = do enum_name.to_c_str().with_ref |enum_name| { let discriminant_type_metadata = do enum_name.with_c_str |enum_name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateEnumerationType( llvm::LLVMDIBuilderCreateEnumerationType(
DIB(cx), DIB(cx),
@ -732,7 +732,7 @@ fn enum_metadata(cx: &mut CrateContext,
Some(discriminant_type_metadata), Some(discriminant_type_metadata),
span); span);
do "".to_c_str().with_ref |name| { do "".with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateMemberType( llvm::LLVMDIBuilderCreateMemberType(
DIB(cx), DIB(cx),
@ -752,7 +752,7 @@ fn enum_metadata(cx: &mut CrateContext,
let enum_llvm_type = type_of::type_of(cx, enum_type); let enum_llvm_type = type_of::type_of(cx, enum_type);
let (enum_type_size, enum_type_align) = size_and_align_of(cx, enum_llvm_type); let (enum_type_size, enum_type_align) = size_and_align_of(cx, enum_llvm_type);
return do enum_name.to_c_str().with_ref |enum_name| { return do enum_name.with_c_str |enum_name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateUnionType( llvm::LLVMDIBuilderCreateUnionType(
DIB(cx), DIB(cx),
@ -836,7 +836,7 @@ fn composite_type_metadata(cx: &mut CrateContext,
let member_offset = machine::llelement_offset(cx, composite_llvm_type, i); let member_offset = machine::llelement_offset(cx, composite_llvm_type, i);
let member_name: &str = member_names[i]; let member_name: &str = member_names[i];
do member_name.to_c_str().with_ref |member_name| { do member_name.with_c_str |member_name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateMemberType( llvm::LLVMDIBuilderCreateMemberType(
DIB(cx), DIB(cx),
@ -854,7 +854,7 @@ fn composite_type_metadata(cx: &mut CrateContext,
}) })
.collect(); .collect();
return do composite_type_name.to_c_str().with_ref |name| { return do composite_type_name.with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateStructType( llvm::LLVMDIBuilderCreateStructType(
DIB(cx), DIB(cx),
@ -1080,7 +1080,7 @@ fn unimplemented_type_metadata(cx: &mut CrateContext, t: ty::t) -> DIType {
debug!("unimplemented_type_metadata: %?", ty::get(t)); debug!("unimplemented_type_metadata: %?", ty::get(t));
let name = ty_to_str(cx.tcx, t); let name = ty_to_str(cx.tcx, t);
let metadata = do fmt!("NYI<%s>", name).to_c_str().with_ref |name| { let metadata = do fmt!("NYI<%s>", name).with_c_str |name| {
unsafe { unsafe {
llvm::LLVMDIBuilderCreateBasicType( llvm::LLVMDIBuilderCreateBasicType(
DIB(cx), DIB(cx),

View file

@ -1031,7 +1031,7 @@ fn trans_lvalue_unadjusted(bcx: @mut Block, expr: @ast::expr) -> DatumBlock {
let symbol = csearch::get_symbol( let symbol = csearch::get_symbol(
bcx.ccx().sess.cstore, bcx.ccx().sess.cstore,
did); did);
let llval = do symbol.to_c_str().with_ref |buf| { let llval = do symbol.with_c_str |buf| {
llvm::LLVMAddGlobal(bcx.ccx().llmod, llvm::LLVMAddGlobal(bcx.ccx().llmod,
llty.to_ref(), llty.to_ref(),
buf) buf)

View file

@ -673,7 +673,7 @@ pub fn declare_tydesc(ccx: &mut CrateContext, t: ty::t) -> @mut tydesc_info {
let name = mangle_internal_name_by_type_and_seq(ccx, t, "tydesc").to_managed(); let name = mangle_internal_name_by_type_and_seq(ccx, t, "tydesc").to_managed();
note_unique_llvm_symbol(ccx, name); note_unique_llvm_symbol(ccx, name);
debug!("+++ declare_tydesc %s %s", ppaux::ty_to_str(ccx.tcx, t), name); debug!("+++ declare_tydesc %s %s", ppaux::ty_to_str(ccx.tcx, t), name);
let gvar = do name.to_c_str().with_ref |buf| { let gvar = do name.with_c_str |buf| {
unsafe { unsafe {
llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type.to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, ccx.tydesc_type.to_ref(), buf)
} }

View file

@ -537,7 +537,7 @@ pub fn make_vtable(ccx: &mut CrateContext,
let tbl = C_struct(components); let tbl = C_struct(components);
let vtable = ccx.sess.str_of(gensym_name("vtable")); let vtable = ccx.sess.str_of(gensym_name("vtable"));
let vt_gvar = do vtable.to_c_str().with_ref |buf| { let vt_gvar = do vtable.with_c_str |buf| {
llvm::LLVMAddGlobal(ccx.llmod, val_ty(tbl).to_ref(), buf) llvm::LLVMAddGlobal(ccx.llmod, val_ty(tbl).to_ref(), buf)
}; };
llvm::LLVMSetInitializer(vt_gvar, tbl); llvm::LLVMSetInitializer(vt_gvar, tbl);

View file

@ -171,7 +171,7 @@ impl Type {
pub fn named_struct(name: &str) -> Type { pub fn named_struct(name: &str) -> Type {
let ctx = base::task_llcx(); let ctx = base::task_llcx();
ty!(name.to_c_str().with_ref(|s| llvm::LLVMStructCreateNamed(ctx, s))) ty!(name.with_c_str(|s| llvm::LLVMStructCreateNamed(ctx, s)))
} }
pub fn empty_struct() -> Type { pub fn empty_struct() -> Type {

View file

@ -398,8 +398,8 @@ pub fn link_exe(src: &Path, dest: &Path) -> bool {
use std::libc; use std::libc;
unsafe { unsafe {
do src.to_c_str().with_ref |src_buf| { do src.with_c_str |src_buf| {
do dest.to_c_str().with_ref |dest_buf| { do dest.with_c_str |dest_buf| {
libc::link(src_buf, dest_buf) == 0 as libc::c_int && libc::link(src_buf, dest_buf) == 0 as libc::c_int &&
libc::chmod(dest_buf, 755) == 0 as libc::c_int libc::chmod(dest_buf, 755) == 0 as libc::c_int
} }

View file

@ -133,6 +133,29 @@ pub trait ToCStr {
/// Unsafe variant of `to_c_str()` that doesn't check for nulls. /// Unsafe variant of `to_c_str()` that doesn't check for nulls.
unsafe fn to_c_str_unchecked(&self) -> CString; unsafe fn to_c_str_unchecked(&self) -> CString;
/// Work with a temporary CString constructed from the receiver.
/// The provided `*libc::c_char` will be freed immediately upon return.
///
/// # Example
///
/// ~~~ {.rust}
/// let s = "PATH".with_c_str(|path| libc::getenv(path))
/// ~~~
///
/// # Failure
///
/// Raises the `null_byte` condition if the receiver has an interior null.
#[inline]
fn with_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
self.to_c_str().with_ref(f)
}
/// Unsafe variant of `with_c_str()` that doesn't check for nulls.
#[inline]
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
self.to_c_str_unchecked().with_ref(f)
}
} }
impl<'self> ToCStr for &'self str { impl<'self> ToCStr for &'self str {

View file

@ -1041,8 +1041,8 @@ pub fn stdin() -> @Reader {
} }
pub fn file_reader(path: &Path) -> Result<@Reader, ~str> { pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
let f = do path.to_c_str().with_ref |pathbuf| { let f = do path.with_c_str |pathbuf| {
do "rb".to_c_str().with_ref |modebuf| { do "rb".with_c_str |modebuf| {
unsafe { libc::fopen(pathbuf, modebuf as *libc::c_char) } unsafe { libc::fopen(pathbuf, modebuf as *libc::c_char) }
} }
}; };
@ -1291,7 +1291,7 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
} }
} }
let fd = unsafe { let fd = unsafe {
do path.to_c_str().with_ref |pathbuf| { do path.with_c_str |pathbuf| {
libc::open(pathbuf, fflags, (S_IRUSR | S_IWUSR) as c_int) libc::open(pathbuf, fflags, (S_IRUSR | S_IWUSR) as c_int)
} }
}; };
@ -1574,8 +1574,8 @@ pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> {
// FIXME: fileflags // #2004 // FIXME: fileflags // #2004
pub fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str> { pub fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str> {
unsafe { unsafe {
let f = do path.to_c_str().with_ref |pathbuf| { let f = do path.with_c_str |pathbuf| {
do "w".to_c_str().with_ref |modebuf| { do "w".with_c_str |modebuf| {
libc::fopen(pathbuf, modebuf) libc::fopen(pathbuf, modebuf)
} }
}; };

View file

@ -239,7 +239,7 @@ pub fn env() -> ~[(~str,~str)] {
pub fn getenv(n: &str) -> Option<~str> { pub fn getenv(n: &str) -> Option<~str> {
unsafe { unsafe {
do with_env_lock { do with_env_lock {
let s = do n.to_c_str().with_ref |buf| { let s = do n.with_c_str |buf| {
libc::getenv(buf) libc::getenv(buf)
}; };
if s.is_null() { if s.is_null() {
@ -274,8 +274,8 @@ pub fn getenv(n: &str) -> Option<~str> {
pub fn setenv(n: &str, v: &str) { pub fn setenv(n: &str, v: &str) {
unsafe { unsafe {
do with_env_lock { do with_env_lock {
do n.to_c_str().with_ref |nbuf| { do n.with_c_str |nbuf| {
do v.to_c_str().with_ref |vbuf| { do v.with_c_str |vbuf| {
libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1); libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
} }
} }
@ -306,7 +306,7 @@ pub fn unsetenv(n: &str) {
fn _unsetenv(n: &str) { fn _unsetenv(n: &str) {
unsafe { unsafe {
do with_env_lock { do with_env_lock {
do n.to_c_str().with_ref |nbuf| { do n.with_c_str |nbuf| {
libc::funcs::posix01::unistd::unsetenv(nbuf); libc::funcs::posix01::unistd::unsetenv(nbuf);
} }
} }
@ -328,7 +328,7 @@ pub fn unsetenv(n: &str) {
} }
pub fn fdopen(fd: c_int) -> *FILE { pub fn fdopen(fd: c_int) -> *FILE {
do "r".to_c_str().with_ref |modebuf| { do "r".with_c_str |modebuf| {
unsafe { unsafe {
libc::fdopen(fd, modebuf) libc::fdopen(fd, modebuf)
} }
@ -464,7 +464,7 @@ pub fn self_exe_path() -> Option<Path> {
let mut path = [0 as c_char, .. TMPBUF_SZ]; let mut path = [0 as c_char, .. TMPBUF_SZ];
do path.as_mut_buf |buf, len| { do path.as_mut_buf |buf, len| {
let len = do "/proc/self/exe".to_c_str().with_ref |proc_self_buf| { let len = do "/proc/self/exe".with_c_str |proc_self_buf| {
readlink(proc_self_buf, buf, len as size_t) as uint readlink(proc_self_buf, buf, len as size_t) as uint
}; };
@ -593,7 +593,7 @@ pub fn walk_dir(p: &Path, f: &fn(&Path) -> bool) -> bool {
/// Indicates whether a path represents a directory /// Indicates whether a path represents a directory
pub fn path_is_dir(p: &Path) -> bool { pub fn path_is_dir(p: &Path) -> bool {
unsafe { unsafe {
do p.to_c_str().with_ref |buf| { do p.with_c_str |buf| {
rustrt::rust_path_is_dir(buf) != 0 as c_int rustrt::rust_path_is_dir(buf) != 0 as c_int
} }
} }
@ -602,7 +602,7 @@ pub fn path_is_dir(p: &Path) -> bool {
/// Indicates whether a path exists /// Indicates whether a path exists
pub fn path_exists(p: &Path) -> bool { pub fn path_exists(p: &Path) -> bool {
unsafe { unsafe {
do p.to_c_str().with_ref |buf| { do p.with_c_str |buf| {
rustrt::rust_path_exists(buf) != 0 as c_int rustrt::rust_path_exists(buf) != 0 as c_int
} }
} }
@ -645,7 +645,7 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool {
#[cfg(unix)] #[cfg(unix)]
fn mkdir(p: &Path, mode: c_int) -> bool { fn mkdir(p: &Path, mode: c_int) -> bool {
do p.to_c_str().with_ref |buf| { do p.with_c_str |buf| {
unsafe { unsafe {
libc::mkdir(buf, mode as libc::mode_t) == (0 as c_int) libc::mkdir(buf, mode as libc::mode_t) == (0 as c_int)
} }
@ -697,7 +697,7 @@ pub fn list_dir(p: &Path) -> ~[~str] {
let mut strings = ~[]; let mut strings = ~[];
debug!("os::list_dir -- BEFORE OPENDIR"); debug!("os::list_dir -- BEFORE OPENDIR");
let dir_ptr = do p.to_c_str().with_ref |buf| { let dir_ptr = do p.with_c_str |buf| {
opendir(buf) opendir(buf)
}; };
@ -819,7 +819,7 @@ pub fn remove_dir(p: &Path) -> bool {
#[cfg(unix)] #[cfg(unix)]
fn rmdir(p: &Path) -> bool { fn rmdir(p: &Path) -> bool {
do p.to_c_str().with_ref |buf| { do p.with_c_str |buf| {
unsafe { unsafe {
libc::rmdir(buf) == (0 as c_int) libc::rmdir(buf) == (0 as c_int)
} }
@ -844,7 +844,7 @@ pub fn change_dir(p: &Path) -> bool {
#[cfg(unix)] #[cfg(unix)]
fn chdir(p: &Path) -> bool { fn chdir(p: &Path) -> bool {
do p.to_c_str().with_ref |buf| { do p.with_c_str |buf| {
unsafe { unsafe {
libc::chdir(buf) == (0 as c_int) libc::chdir(buf) == (0 as c_int)
} }
@ -872,8 +872,8 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
#[cfg(unix)] #[cfg(unix)]
fn do_copy_file(from: &Path, to: &Path) -> bool { fn do_copy_file(from: &Path, to: &Path) -> bool {
unsafe { unsafe {
let istream = do from.to_c_str().with_ref |fromp| { let istream = do from.with_c_str |fromp| {
do "rb".to_c_str().with_ref |modebuf| { do "rb".with_c_str |modebuf| {
libc::fopen(fromp, modebuf) libc::fopen(fromp, modebuf)
} }
}; };
@ -884,8 +884,8 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
let from_mode = from.get_mode().expect("copy_file: couldn't get permissions \ let from_mode = from.get_mode().expect("copy_file: couldn't get permissions \
for source file"); for source file");
let ostream = do to.to_c_str().with_ref |top| { let ostream = do to.with_c_str |top| {
do "w+b".to_c_str().with_ref |modebuf| { do "w+b".with_c_str |modebuf| {
libc::fopen(top, modebuf) libc::fopen(top, modebuf)
} }
}; };
@ -917,7 +917,7 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
fclose(ostream); fclose(ostream);
// Give the new file the old file's permissions // Give the new file the old file's permissions
if do to.to_c_str().with_ref |to_buf| { if do to.with_c_str |to_buf| {
libc::chmod(to_buf, from_mode as libc::mode_t) libc::chmod(to_buf, from_mode as libc::mode_t)
} != 0 { } != 0 {
return false; // should be a condition... return false; // should be a condition...
@ -944,7 +944,7 @@ pub fn remove_file(p: &Path) -> bool {
#[cfg(unix)] #[cfg(unix)]
fn unlink(p: &Path) -> bool { fn unlink(p: &Path) -> bool {
unsafe { unsafe {
do p.to_c_str().with_ref |buf| { do p.with_c_str |buf| {
libc::unlink(buf) == (0 as c_int) libc::unlink(buf) == (0 as c_int)
} }
} }
@ -1282,7 +1282,7 @@ pub fn glob(pattern: &str) -> ~[Path] {
} }
let mut g = default_glob_t(); let mut g = default_glob_t();
do pattern.to_c_str().with_ref |c_pattern| { do pattern.with_c_str |c_pattern| {
unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) } unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
}; };
do(|| { do(|| {
@ -1929,14 +1929,14 @@ mod tests {
let out = tempdir.push("out.txt"); let out = tempdir.push("out.txt");
/* Write the temp input file */ /* Write the temp input file */
let ostream = do input.to_c_str().with_ref |fromp| { let ostream = do input.with_c_str |fromp| {
do "w+b".to_c_str().with_ref |modebuf| { do "w+b".with_c_str |modebuf| {
libc::fopen(fromp, modebuf) libc::fopen(fromp, modebuf)
} }
}; };
assert!((ostream as uint != 0u)); assert!((ostream as uint != 0u));
let s = ~"hello"; let s = ~"hello";
do "hello".to_c_str().with_ref |buf| { do "hello".with_c_str |buf| {
let write_len = libc::fwrite(buf as *c_void, let write_len = libc::fwrite(buf as *c_void,
1u as size_t, 1u as size_t,
(s.len() + 1u) as size_t, (s.len() + 1u) as size_t,
@ -2013,11 +2013,11 @@ mod tests {
remove_file(&path); remove_file(&path);
let fd = unsafe { let fd = unsafe {
let fd = do path.to_c_str().with_ref |path| { let fd = do path.with_c_str |path| {
open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR) open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)
}; };
lseek_(fd, size); lseek_(fd, size);
do "x".to_c_str().with_ref |x| { do "x".with_c_str |x| {
assert!(write(fd, x as *c_void, 1) == 1); assert!(write(fd, x as *c_void, 1) == 1);
} }
fd fd

View file

@ -381,7 +381,7 @@ mod stat {
#[cfg(target_os = "win32")] #[cfg(target_os = "win32")]
impl WindowsPath { impl WindowsPath {
pub fn stat(&self) -> Option<libc::stat> { pub fn stat(&self) -> Option<libc::stat> {
do self.to_c_str().with_ref |buf| { do self.with_c_str |buf| {
let mut st = stat::arch::default_stat(); let mut st = stat::arch::default_stat();
match unsafe { libc::stat(buf, &mut st) } { match unsafe { libc::stat(buf, &mut st) } {
0 => Some(st), 0 => Some(st),
@ -415,7 +415,7 @@ impl WindowsPath {
#[cfg(not(target_os = "win32"))] #[cfg(not(target_os = "win32"))]
impl PosixPath { impl PosixPath {
pub fn stat(&self) -> Option<libc::stat> { pub fn stat(&self) -> Option<libc::stat> {
do self.to_c_str().with_ref |buf| { do self.with_c_str |buf| {
let mut st = stat::arch::default_stat(); let mut st = stat::arch::default_stat();
match unsafe { libc::stat(buf as *libc::c_char, &mut st) } { match unsafe { libc::stat(buf as *libc::c_char, &mut st) } {
0 => Some(st), 0 => Some(st),
@ -493,7 +493,7 @@ impl PosixPath {
#[cfg(unix)] #[cfg(unix)]
impl PosixPath { impl PosixPath {
pub fn lstat(&self) -> Option<libc::stat> { pub fn lstat(&self) -> Option<libc::stat> {
do self.to_c_str().with_ref |buf| { do self.with_c_str |buf| {
let mut st = stat::arch::default_stat(); let mut st = stat::arch::default_stat();
match unsafe { libc::lstat(buf, &mut st) } { match unsafe { libc::lstat(buf, &mut st) } {
0 => Some(st), 0 => Some(st),

View file

@ -481,7 +481,7 @@ pub mod ptr_tests {
fn test_position() { fn test_position() {
use libc::c_char; use libc::c_char;
do "hello".to_c_str().with_ref |p| { do "hello".with_c_str |p| {
unsafe { unsafe {
assert!(2u == position(p, |c| *c == 'l' as c_char)); assert!(2u == position(p, |c| *c == 'l' as c_char));
assert!(4u == position(p, |c| *c == 'o' as c_char)); assert!(4u == position(p, |c| *c == 'o' as c_char));
@ -492,9 +492,9 @@ pub mod ptr_tests {
#[test] #[test]
fn test_buf_len() { fn test_buf_len() {
do "hello".to_c_str().with_ref |p0| { do "hello".with_c_str |p0| {
do "there".to_c_str().with_ref |p1| { do "there".with_c_str |p1| {
do "thing".to_c_str().with_ref |p2| { do "thing".with_c_str |p2| {
let v = ~[p0, p1, p2, null()]; let v = ~[p0, p1, p2, null()];
do v.as_imm_buf |vp, len| { do v.as_imm_buf |vp, len| {
assert_eq!(unsafe { buf_len(vp) }, 3u); assert_eq!(unsafe { buf_len(vp) }, 3u);

View file

@ -52,7 +52,7 @@ unsafe fn fail_borrowed(box: *mut raw::Box<()>, file: *c_char, line: size_t) {
match try_take_task_borrow_list() { match try_take_task_borrow_list() {
None => { // not recording borrows None => { // not recording borrows
let msg = "borrowed"; let msg = "borrowed";
do msg.to_c_str().with_ref |msg_p| { do msg.with_c_str |msg_p| {
sys::begin_unwind_(msg_p, file, line); sys::begin_unwind_(msg_p, file, line);
} }
} }
@ -68,7 +68,7 @@ unsafe fn fail_borrowed(box: *mut raw::Box<()>, file: *c_char, line: size_t) {
sep = " and at "; sep = " and at ";
} }
} }
do msg.to_c_str().with_ref |msg_p| { do msg.with_c_str |msg_p| {
sys::begin_unwind_(msg_p, file, line) sys::begin_unwind_(msg_p, file, line)
} }
} }
@ -208,7 +208,7 @@ pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
let br = borrow_list.pop(); let br = borrow_list.pop();
if br.box != a || br.file != file || br.line != line { if br.box != a || br.file != file || br.line != line {
let err = fmt!("wrong borrow found, br=%?", br); let err = fmt!("wrong borrow found, br=%?", br);
do err.to_c_str().with_ref |msg_p| { do err.with_c_str |msg_p| {
sys::begin_unwind_(msg_p, file, line) sys::begin_unwind_(msg_p, file, line)
} }
} }

View file

@ -66,7 +66,7 @@ pub fn init(crate_map: *u8) {
let log_spec = os::getenv("RUST_LOG"); let log_spec = os::getenv("RUST_LOG");
match log_spec { match log_spec {
Some(spec) => { Some(spec) => {
do spec.to_c_str().with_ref |buf| { do spec.with_c_str |buf| {
unsafe { rust_update_log_settings(crate_map, buf) } unsafe { rust_update_log_settings(crate_map, buf) }
} }
} }

View file

@ -654,7 +654,7 @@ impl RtioUdpSocket for UvUdpSocket {
fn join_multicast(&mut self, multi: IpAddr) -> Result<(), IoError> { fn join_multicast(&mut self, multi: IpAddr) -> Result<(), IoError> {
let r = unsafe { let r = unsafe {
do multi.to_str().to_c_str().with_ref |m_addr| { do multi.to_str().with_c_str |m_addr| {
uvll::udp_set_membership(self.native_handle(), m_addr, uvll::udp_set_membership(self.native_handle(), m_addr,
ptr::null(), uvll::UV_JOIN_GROUP) ptr::null(), uvll::UV_JOIN_GROUP)
} }
@ -668,7 +668,7 @@ impl RtioUdpSocket for UvUdpSocket {
fn leave_multicast(&mut self, multi: IpAddr) -> Result<(), IoError> { fn leave_multicast(&mut self, multi: IpAddr) -> Result<(), IoError> {
let r = unsafe { let r = unsafe {
do multi.to_str().to_c_str().with_ref |m_addr| { do multi.to_str().with_c_str |m_addr| {
uvll::udp_set_membership(self.native_handle(), m_addr, uvll::udp_set_membership(self.native_handle(), m_addr,
ptr::null(), uvll::UV_LEAVE_GROUP) ptr::null(), uvll::UV_LEAVE_GROUP)
} }

View file

@ -373,12 +373,12 @@ pub unsafe fn is_ip6_addr(addr: *sockaddr) -> bool {
} }
pub unsafe fn malloc_ip4_addr(ip: &str, port: int) -> *sockaddr_in { pub unsafe fn malloc_ip4_addr(ip: &str, port: int) -> *sockaddr_in {
do ip.to_c_str().with_ref |ip_buf| { do ip.with_c_str |ip_buf| {
rust_uv_ip4_addrp(ip_buf as *u8, port as libc::c_int) rust_uv_ip4_addrp(ip_buf as *u8, port as libc::c_int)
} }
} }
pub unsafe fn malloc_ip6_addr(ip: &str, port: int) -> *sockaddr_in6 { pub unsafe fn malloc_ip6_addr(ip: &str, port: int) -> *sockaddr_in6 {
do ip.to_c_str().with_ref |ip_buf| { do ip.with_c_str |ip_buf| {
rust_uv_ip6_addrp(ip_buf as *u8, port as libc::c_int) rust_uv_ip6_addrp(ip_buf as *u8, port as libc::c_int)
} }
} }

View file

@ -506,7 +506,7 @@ fn spawn_process_os(prog: &str, args: &[~str],
do with_envp(env) |envp| { do with_envp(env) |envp| {
do with_dirp(dir) |dirp| { do with_dirp(dir) |dirp| {
do cmd.to_c_str().with_ref |cmdp| { do cmd.with_c_str |cmdp| {
let created = CreateProcessA(ptr::null(), cast::transmute(cmdp), let created = CreateProcessA(ptr::null(), cast::transmute(cmdp),
ptr::mut_null(), ptr::mut_null(), TRUE, ptr::mut_null(), ptr::mut_null(), TRUE,
0, envp, dirp, &mut si, &mut pi); 0, envp, dirp, &mut si, &mut pi);
@ -775,7 +775,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*mut c_void) -> T) -> T {
fn with_dirp<T>(d: Option<&Path>, cb: &fn(*libc::c_char) -> T) -> T { fn with_dirp<T>(d: Option<&Path>, cb: &fn(*libc::c_char) -> T) -> T {
match d { match d {
Some(dir) => dir.to_c_str().with_ref(|buf| cb(buf)), Some(dir) => dir.with_c_str(|buf| cb(buf)),
None => cb(ptr::null()) None => cb(ptr::null())
} }
} }

View file

@ -105,8 +105,8 @@ pub trait FailWithCause {
impl FailWithCause for ~str { impl FailWithCause for ~str {
fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! { fn fail_with(cause: ~str, file: &'static str, line: uint) -> ! {
do cause.to_c_str().with_ref |msg_buf| { do cause.with_c_str |msg_buf| {
do file.to_c_str().with_ref |file_buf| { do file.with_c_str |file_buf| {
begin_unwind_(msg_buf, file_buf, line as libc::size_t) begin_unwind_(msg_buf, file_buf, line as libc::size_t)
} }
} }
@ -115,8 +115,8 @@ impl FailWithCause for ~str {
impl FailWithCause for &'static str { impl FailWithCause for &'static str {
fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! { fn fail_with(cause: &'static str, file: &'static str, line: uint) -> ! {
do cause.to_c_str().with_ref |msg_buf| { do cause.with_c_str |msg_buf| {
do file.to_c_str().with_ref |file_buf| { do file.with_c_str |file_buf| {
begin_unwind_(msg_buf, file_buf, line as libc::size_t) begin_unwind_(msg_buf, file_buf, line as libc::size_t)
} }
} }

View file

@ -66,7 +66,7 @@ impl DynamicLibrary {
// T but that feature is still unimplemented // T but that feature is still unimplemented
let maybe_symbol_value = do dl::check_for_errors_in { let maybe_symbol_value = do dl::check_for_errors_in {
do symbol.to_c_str().with_ref |raw_string| { do symbol.with_c_str |raw_string| {
dl::symbol(self.handle, raw_string) dl::symbol(self.handle, raw_string)
} }
}; };
@ -145,7 +145,7 @@ mod dl {
use result::*; use result::*;
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void { pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
do filename.to_c_str().with_ref |raw_name| { do filename.with_c_str |raw_name| {
dlopen(raw_name, Lazy as libc::c_int) dlopen(raw_name, Lazy as libc::c_int)
} }
} }

View file

@ -29,7 +29,7 @@ pub fn fail_bounds_check(file: *c_char, line: size_t,
index: size_t, len: size_t) { index: size_t, len: size_t) {
let msg = fmt!("index out of bounds: the len is %d but the index is %d", let msg = fmt!("index out of bounds: the len is %d but the index is %d",
len as int, index as int); len as int, index as int);
do msg.to_c_str().with_ref |buf| { do msg.with_c_str |buf| {
fail_(buf, file, line); fail_(buf, file, line);
} }
} }

View file

@ -20,11 +20,11 @@ mod libc {
} }
fn atol(s: ~str) -> int { fn atol(s: ~str) -> int {
s.to_c_str().with_ref(|x| unsafe { libc::atol(x as *u8) }) s.with_c_str(|x| unsafe { libc::atol(x as *u8) })
} }
fn atoll(s: ~str) -> i64 { fn atoll(s: ~str) -> i64 {
s.to_c_str().with_ref(|x| unsafe { libc::atoll(x as *u8) }) s.with_c_str(|x| unsafe { libc::atoll(x as *u8) })
} }
pub fn main() { pub fn main() {

View file

@ -26,7 +26,7 @@ mod libc {
fn strlen(str: ~str) -> uint { fn strlen(str: ~str) -> uint {
unsafe { unsafe {
// C string is terminated with a zero // C string is terminated with a zero
do str.to_c_str().with_ref |buf| { do str.with_c_str |buf| {
libc::my_strlen(buf as *u8) libc::my_strlen(buf as *u8)
} }
} }