Skip to content

Commit 03ef71e

Browse files
committed
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().
1 parent 48265b7 commit 03ef71e

33 files changed

+159
-136
lines changed

src/libextra/rl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub mod rustrt {
3232

3333
/// Add a line to history
3434
pub unsafe fn add_history(line: &str) -> bool {
35-
do line.to_c_str().with_ref |buf| {
35+
do line.with_c_str |buf| {
3636
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
3737
}
3838
}
@@ -44,21 +44,21 @@ pub unsafe fn set_history_max_len(len: int) -> bool {
4444

4545
/// Save line history to a file
4646
pub unsafe fn save_history(file: &str) -> bool {
47-
do file.to_c_str().with_ref |buf| {
47+
do file.with_c_str |buf| {
4848
rustrt::linenoiseHistorySave(buf) == 1 as c_int
4949
}
5050
}
5151

5252
/// Load line history from a file
5353
pub unsafe fn load_history(file: &str) -> bool {
54-
do file.to_c_str().with_ref |buf| {
54+
do file.with_c_str |buf| {
5555
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
5656
}
5757
}
5858

5959
/// Print out a prompt and then wait for input and return it
6060
pub unsafe fn read(prompt: &str) -> Option<~str> {
61-
do prompt.to_c_str().with_ref |buf| {
61+
do prompt.with_c_str |buf| {
6262
let line = rustrt::linenoise(buf);
6363

6464
if line.is_null() { None }
@@ -80,7 +80,7 @@ pub unsafe fn complete(cb: CompletionCb) {
8080

8181
unsafe {
8282
do cb(str::raw::from_c_str(line)) |suggestion| {
83-
do suggestion.to_c_str().with_ref |buf| {
83+
do suggestion.with_c_str |buf| {
8484
rustrt::linenoiseAddCompletion(completions, buf);
8585
}
8686
}

src/librustc/back/link.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ pub fn WriteOutputFile(sess: Session,
7878
OptLevel: c_int,
7979
EnableSegmentedStacks: bool) {
8080
unsafe {
81-
do Triple.to_c_str().with_ref |Triple| {
82-
do Cpu.to_c_str().with_ref |Cpu| {
83-
do Feature.to_c_str().with_ref |Feature| {
84-
do Output.to_c_str().with_ref |Output| {
81+
do Triple.with_c_str |Triple| {
82+
do Cpu.with_c_str |Cpu| {
83+
do Feature.with_c_str |Feature| {
84+
do Output.with_c_str |Output| {
8585
let result = llvm::LLVMRustWriteOutputFile(
8686
PM,
8787
M,
@@ -152,7 +152,7 @@ pub mod jit {
152152

153153
debug!("linking: %s", path);
154154

155-
do path.to_c_str().with_ref |buf_t| {
155+
do path.with_c_str |buf_t| {
156156
if !llvm::LLVMRustLoadCrate(manager, buf_t) {
157157
llvm_err(sess, ~"Could not link");
158158
}
@@ -171,7 +171,7 @@ pub mod jit {
171171
// Next, we need to get a handle on the _rust_main function by
172172
// looking up it's corresponding ValueRef and then requesting that
173173
// the execution engine compiles the function.
174-
let fun = do "_rust_main".to_c_str().with_ref |entry| {
174+
let fun = do "_rust_main".with_c_str |entry| {
175175
llvm::LLVMGetNamedFunction(m, entry)
176176
};
177177
if fun.is_null() {
@@ -270,14 +270,14 @@ pub mod write {
270270
output_type_bitcode => {
271271
if opts.optimize != session::No {
272272
let filename = output.with_filetype("no-opt.bc");
273-
do filename.to_c_str().with_ref |buf| {
273+
do filename.with_c_str |buf| {
274274
llvm::LLVMWriteBitcodeToFile(llmod, buf);
275275
}
276276
}
277277
}
278278
_ => {
279279
let filename = output.with_filetype("bc");
280-
do filename.to_c_str().with_ref |buf| {
280+
do filename.with_c_str |buf| {
281281
llvm::LLVMWriteBitcodeToFile(llmod, buf);
282282
}
283283
}
@@ -340,7 +340,7 @@ pub mod write {
340340
// Always output the bitcode file with --save-temps
341341

342342
let filename = output.with_filetype("opt.bc");
343-
do filename.to_c_str().with_ref |buf| {
343+
do filename.with_c_str |buf| {
344344
llvm::LLVMWriteBitcodeToFile(llmod, buf)
345345
};
346346
// Save the assembly file if -S is used
@@ -401,13 +401,13 @@ pub mod write {
401401

402402
if output_type == output_type_llvm_assembly {
403403
// Given options "-S --emit-llvm": output LLVM assembly
404-
do output.to_c_str().with_ref |buf_o| {
404+
do output.with_c_str |buf_o| {
405405
llvm::LLVMRustAddPrintModulePass(pm.llpm, llmod, buf_o);
406406
}
407407
} else {
408408
// If only a bitcode file is asked for by using the
409409
// '--emit-llvm' flag, then output it here
410-
do output.to_c_str().with_ref |buf| {
410+
do output.with_c_str |buf| {
411411
llvm::LLVMWriteBitcodeToFile(llmod, buf);
412412
}
413413
}

src/librustc/back/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub fn populate_pass_manager(sess: Session, pm: &mut PassManager, pass_list:&[~s
173173
}
174174

175175
pub fn create_pass(name:&str) -> Option<PassRef> {
176-
do name.to_c_str().with_ref |s| {
176+
do name.with_c_str |s| {
177177
unsafe {
178178
let p = llvm::LLVMCreatePass(s);
179179
if p.is_null() {

src/librustc/lib/llvm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ pub struct TargetData {
22712271
}
22722272

22732273
pub fn mk_target_data(string_rep: &str) -> TargetData {
2274-
let lltd = do string_rep.to_c_str().with_ref |buf| {
2274+
let lltd = do string_rep.with_c_str |buf| {
22752275
unsafe { llvm::LLVMCreateTargetData(buf) }
22762276
};
22772277

src/librustc/metadata/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub fn metadata_matches(extern_metas: &[@ast::MetaItem],
199199
fn get_metadata_section(os: os,
200200
filename: &Path) -> Option<@~[u8]> {
201201
unsafe {
202-
let mb = do filename.to_c_str().with_ref |buf| {
202+
let mb = do filename.with_c_str |buf| {
203203
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
204204
};
205205
if mb as int == 0 { return option::None::<@~[u8]>; }

src/librustc/middle/trans/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ pub fn trans_inline_asm(bcx: @mut Block, ia: &ast::inline_asm) -> @mut Block {
120120
ast::asm_intel => lib::llvm::AD_Intel
121121
};
122122

123-
let r = do ia.asm.to_c_str().with_ref |a| {
124-
do constraints.to_c_str().with_ref |c| {
123+
let r = do ia.asm.with_c_str |a| {
124+
do constraints.with_c_str |c| {
125125
InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
126126
}
127127
};

src/librustc/middle/trans/base.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'self> Drop for StatRecorder<'self> {
181181
}
182182

183183
pub fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv, ty: Type) -> ValueRef {
184-
let llfn: ValueRef = do name.to_c_str().with_ref |buf| {
184+
let llfn: ValueRef = do name.with_c_str |buf| {
185185
unsafe {
186186
llvm::LLVMGetOrInsertFunction(llmod, buf, ty.to_ref())
187187
}
@@ -221,7 +221,7 @@ pub fn get_extern_const(externs: &mut ExternMap, llmod: ModuleRef,
221221
None => ()
222222
}
223223
unsafe {
224-
let c = do name.to_c_str().with_ref |buf| {
224+
let c = do name.with_c_str |buf| {
225225
llvm::LLVMAddGlobal(llmod, ty.to_ref(), buf)
226226
};
227227
externs.insert(name, c);
@@ -523,7 +523,7 @@ pub fn get_res_dtor(ccx: @mut CrateContext,
523523
// Structural comparison: a rather involved form of glue.
524524
pub fn maybe_name_value(cx: &CrateContext, v: ValueRef, s: &str) {
525525
if cx.sess.opts.save_temps {
526-
do s.to_c_str().with_ref |buf| {
526+
do s.with_c_str |buf| {
527527
unsafe {
528528
llvm::LLVMSetValueName(v, buf)
529529
}
@@ -1136,7 +1136,7 @@ pub fn new_block(cx: @mut FunctionContext,
11361136
opt_node_info: Option<NodeInfo>)
11371137
-> @mut Block {
11381138
unsafe {
1139-
let llbb = do name.to_c_str().with_ref |buf| {
1139+
let llbb = do name.with_c_str |buf| {
11401140
llvm::LLVMAppendBasicBlockInContext(cx.ccx.llcx, cx.llfn, buf)
11411141
};
11421142
let bcx = @mut Block::new(llbb,
@@ -1553,7 +1553,7 @@ pub struct BasicBlocks {
15531553
pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
15541554
unsafe {
15551555
let cx = task_llcx();
1556-
do "static_allocas".to_c_str().with_ref | buf| {
1556+
do "static_allocas".with_c_str | buf| {
15571557
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
15581558
}
15591559
}
@@ -1562,7 +1562,7 @@ pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
15621562
pub fn mk_return_basic_block(llfn: ValueRef) -> BasicBlockRef {
15631563
unsafe {
15641564
let cx = task_llcx();
1565-
do "return".to_c_str().with_ref |buf| {
1565+
do "return".with_c_str |buf| {
15661566
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
15671567
}
15681568
}
@@ -2332,7 +2332,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
23322332
};
23332333
decl_cdecl_fn(ccx.llmod, main_name, llfty)
23342334
};
2335-
let llbb = do "top".to_c_str().with_ref |buf| {
2335+
let llbb = do "top".with_c_str |buf| {
23362336
unsafe {
23372337
llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf)
23382338
}
@@ -2342,7 +2342,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
23422342
llvm::LLVMPositionBuilderAtEnd(bld, llbb);
23432343

23442344
let crate_map = ccx.crate_map;
2345-
let opaque_crate_map = do "crate_map".to_c_str().with_ref |buf| {
2345+
let opaque_crate_map = do "crate_map".with_c_str |buf| {
23462346
llvm::LLVMBuildPointerCast(bld, crate_map, Type::i8p().to_ref(), buf)
23472347
};
23482348

@@ -2360,7 +2360,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
23602360
};
23612361

23622362
let args = {
2363-
let opaque_rust_main = do "rust_main".to_c_str().with_ref |buf| {
2363+
let opaque_rust_main = do "rust_main".with_c_str |buf| {
23642364
llvm::LLVMBuildPointerCast(bld, rust_main, Type::i8p().to_ref(), buf)
23652365
};
23662366

@@ -2442,7 +2442,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
24422442

24432443
unsafe {
24442444
let llty = llvm::LLVMTypeOf(v);
2445-
let g = do sym.to_c_str().with_ref |buf| {
2445+
let g = do sym.with_c_str |buf| {
24462446
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
24472447
};
24482448

@@ -2475,7 +2475,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
24752475

24762476
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
24772477
Some(sect) => unsafe {
2478-
do sect.to_c_str().with_ref |buf| {
2478+
do sect.with_c_str |buf| {
24792479
llvm::LLVMSetSection(v, buf);
24802480
}
24812481
},
@@ -2516,7 +2516,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
25162516
}
25172517
ast::foreign_item_static(*) => {
25182518
let ident = token::ident_to_str(&ni.ident);
2519-
let g = do ident.to_c_str().with_ref |buf| {
2519+
let g = do ident.with_c_str |buf| {
25202520
unsafe {
25212521
let ty = type_of(ccx, ty);
25222522
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
@@ -2623,7 +2623,7 @@ pub fn trans_constant(ccx: &mut CrateContext, it: @ast::item) {
26232623
let s = mangle_exported_name(ccx, p, ty::mk_int()).to_managed();
26242624
let disr_val = vi[i].disr_val;
26252625
note_unique_llvm_symbol(ccx, s);
2626-
let discrim_gvar = do s.to_c_str().with_ref |buf| {
2626+
let discrim_gvar = do s.with_c_str |buf| {
26272627
unsafe {
26282628
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
26292629
}
@@ -2818,7 +2818,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
28182818
}
28192819

28202820
let gc_metadata_name = ~"_gc_module_metadata_" + llmod_id;
2821-
let gc_metadata = do gc_metadata_name.to_c_str().with_ref |buf| {
2821+
let gc_metadata = do gc_metadata_name.with_c_str |buf| {
28222822
unsafe {
28232823
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
28242824
}
@@ -2833,7 +2833,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
28332833
pub fn create_module_map(ccx: &mut CrateContext) -> ValueRef {
28342834
let elttype = Type::struct_([ccx.int_type, ccx.int_type], false);
28352835
let maptype = Type::array(&elttype, (ccx.module_data.len() + 1) as u64);
2836-
let map = do "_rust_mod_map".to_c_str().with_ref |buf| {
2836+
let map = do "_rust_mod_map".with_c_str |buf| {
28372837
unsafe {
28382838
llvm::LLVMAddGlobal(ccx.llmod, maptype.to_ref(), buf)
28392839
}
@@ -2881,7 +2881,7 @@ pub fn decl_crate_map(sess: session::Session, mapmeta: LinkMeta,
28812881
let sym_name = ~"_rust_crate_map_" + mapname;
28822882
let arrtype = Type::array(&int_type, n_subcrates as u64);
28832883
let maptype = Type::struct_([Type::i32(), Type::i8p(), int_type, arrtype], false);
2884-
let map = do sym_name.to_c_str().with_ref |buf| {
2884+
let map = do sym_name.with_c_str |buf| {
28852885
unsafe {
28862886
llvm::LLVMAddGlobal(llmod, maptype.to_ref(), buf)
28872887
}
@@ -2900,7 +2900,7 @@ pub fn fill_crate_map(ccx: @mut CrateContext, map: ValueRef) {
29002900
cdata.name,
29012901
cstore::get_crate_vers(cstore, i),
29022902
cstore::get_crate_hash(cstore, i));
2903-
let cr = do nm.to_c_str().with_ref |buf| {
2903+
let cr = do nm.with_c_str |buf| {
29042904
unsafe {
29052905
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
29062906
}
@@ -2963,21 +2963,21 @@ pub fn write_metadata(cx: &mut CrateContext, crate: &ast::Crate) {
29632963
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
29642964
let llmeta = C_bytes(encoder::encode_metadata(encode_parms, crate));
29652965
let llconst = C_struct([llmeta]);
2966-
let mut llglobal = do "rust_metadata".to_c_str().with_ref |buf| {
2966+
let mut llglobal = do "rust_metadata".with_c_str |buf| {
29672967
unsafe {
29682968
llvm::LLVMAddGlobal(cx.llmod, val_ty(llconst).to_ref(), buf)
29692969
}
29702970
};
29712971
unsafe {
29722972
llvm::LLVMSetInitializer(llglobal, llconst);
2973-
do cx.sess.targ_cfg.target_strs.meta_sect_name.to_c_str().with_ref |buf| {
2973+
do cx.sess.targ_cfg.target_strs.meta_sect_name.with_c_str |buf| {
29742974
llvm::LLVMSetSection(llglobal, buf)
29752975
};
29762976
lib::llvm::SetLinkage(llglobal, lib::llvm::InternalLinkage);
29772977

29782978
let t_ptr_i8 = Type::i8p();
29792979
llglobal = llvm::LLVMConstBitCast(llglobal, t_ptr_i8.to_ref());
2980-
let llvm_used = do "llvm.used".to_c_str().with_ref |buf| {
2980+
let llvm_used = do "llvm.used".with_c_str |buf| {
29812981
llvm::LLVMAddGlobal(cx.llmod, Type::array(&t_ptr_i8, 1).to_ref(), buf)
29822982
};
29832983
lib::llvm::SetLinkage(llvm_used, lib::llvm::AppendingLinkage);
@@ -2991,7 +2991,7 @@ fn mk_global(ccx: &CrateContext,
29912991
internal: bool)
29922992
-> ValueRef {
29932993
unsafe {
2994-
let llglobal = do name.to_c_str().with_ref |buf| {
2994+
let llglobal = do name.with_c_str |buf| {
29952995
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llval).to_ref(), buf)
29962996
};
29972997
llvm::LLVMSetInitializer(llglobal, llval);

src/librustc/middle/trans/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl Builder {
423423
if name.is_empty() {
424424
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
425425
} else {
426-
do name.to_c_str().with_ref |c| {
426+
do name.with_c_str |c| {
427427
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c)
428428
}
429429
}
@@ -739,7 +739,7 @@ impl Builder {
739739
let sanitized = text.replace("$", "");
740740
let comment_text = fmt!("# %s", sanitized.replace("\n", "\n\t# "));
741741
self.count_insn("inlineasm");
742-
let asm = do comment_text.to_c_str().with_ref |c| {
742+
let asm = do comment_text.with_c_str |c| {
743743
unsafe {
744744
llvm::LLVMConstInlineAsm(Type::func([], &Type::void()).to_ref(),
745745
c, noname(), False, False)
@@ -895,7 +895,7 @@ impl Builder {
895895
let BB: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
896896
let FN: ValueRef = llvm::LLVMGetBasicBlockParent(BB);
897897
let M: ModuleRef = llvm::LLVMGetGlobalParent(FN);
898-
let T: ValueRef = do "llvm.trap".to_c_str().with_ref |buf| {
898+
let T: ValueRef = do "llvm.trap".with_c_str |buf| {
899899
llvm::LLVMGetNamedFunction(M, buf)
900900
};
901901
assert!((T as int != 0));

src/librustc/middle/trans/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ pub fn C_integral(t: Type, u: u64, sign_extend: bool) -> ValueRef {
712712

713713
pub fn C_floating(s: &str, t: Type) -> ValueRef {
714714
unsafe {
715-
do s.to_c_str().with_ref |buf| {
715+
do s.with_c_str |buf| {
716716
llvm::LLVMConstRealOfString(t.to_ref(), buf)
717717
}
718718
}
@@ -760,12 +760,12 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
760760
None => ()
761761
}
762762

763-
let sc = do s.to_c_str().with_ref |buf| {
763+
let sc = do s.with_c_str |buf| {
764764
llvm::LLVMConstStringInContext(cx.llcx, buf, s.len() as c_uint, False)
765765
};
766766

767767
let gsym = token::gensym("str");
768-
let g = do fmt!("str%u", gsym).to_c_str().with_ref |buf| {
768+
let g = do fmt!("str%u", gsym).with_c_str |buf| {
769769
llvm::LLVMAddGlobal(cx.llmod, val_ty(sc).to_ref(), buf)
770770
};
771771
llvm::LLVMSetInitializer(g, sc);

0 commit comments

Comments
 (0)