auto merge of #15493 : brson/rust/tostr, r=pcwalton
This updates https://github.com/rust-lang/rust/pull/15075. Rename `ToStr::to_str` to `ToString::to_string`. The naive renaming ends up with two `to_string` functions defined on strings in the prelude (the other defined via `collections::str::StrAllocating`). To remedy this I removed `StrAllocating::to_string`, making all conversions from `&str` to `String` go through `Show`. This has a measurable impact on the speed of this conversion, but the sense I get from others is that it's best to go ahead and unify `to_string` and address performance for all `to_string` conversions in `core::fmt`. `String::from_str(...)` still works as a manual fast-path. Note that the patch was done with a script, and ended up renaming a number of other `*_to_str` functions, particularly inside of rustc. All the ones I saw looked correct, and I didn't notice any additional API breakage. Closes #15046.
This commit is contained in:
@@ -128,6 +128,126 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
|
||||
eof(&mut s.s)
|
||||
}
|
||||
|
||||
pub fn to_string(f: |&mut State| -> IoResult<()>) -> String {
|
||||
let mut s = rust_printer(box MemWriter::new());
|
||||
f(&mut s).unwrap();
|
||||
eof(&mut s.s).unwrap();
|
||||
unsafe {
|
||||
// FIXME(pcwalton): A nasty function to extract the string from an `io::Writer`
|
||||
// that we "know" to be a `MemWriter` that works around the lack of checked
|
||||
// downcasts.
|
||||
let (_, wr): (uint, Box<MemWriter>) = mem::transmute_copy(&s.s.out);
|
||||
let result =
|
||||
str::from_utf8_owned(Vec::from_slice(wr.get_ref())).unwrap();
|
||||
mem::forget(wr);
|
||||
result.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ty_to_string(ty: &ast::Ty) -> String {
|
||||
to_string(|s| s.print_type(ty))
|
||||
}
|
||||
|
||||
pub fn pat_to_string(pat: &ast::Pat) -> String {
|
||||
to_string(|s| s.print_pat(pat))
|
||||
}
|
||||
|
||||
pub fn expr_to_string(e: &ast::Expr) -> String {
|
||||
to_string(|s| s.print_expr(e))
|
||||
}
|
||||
|
||||
pub fn lifetime_to_string(e: &ast::Lifetime) -> String {
|
||||
to_string(|s| s.print_lifetime(e))
|
||||
}
|
||||
|
||||
pub fn tt_to_string(tt: &ast::TokenTree) -> String {
|
||||
to_string(|s| s.print_tt(tt))
|
||||
}
|
||||
|
||||
pub fn tts_to_string(tts: &[ast::TokenTree]) -> String {
|
||||
to_string(|s| s.print_tts(tts))
|
||||
}
|
||||
|
||||
pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
|
||||
to_string(|s| s.print_stmt(stmt))
|
||||
}
|
||||
|
||||
pub fn item_to_string(i: &ast::Item) -> String {
|
||||
to_string(|s| s.print_item(i))
|
||||
}
|
||||
|
||||
pub fn generics_to_string(generics: &ast::Generics) -> String {
|
||||
to_string(|s| s.print_generics(generics))
|
||||
}
|
||||
|
||||
pub fn ty_method_to_string(p: &ast::TypeMethod) -> String {
|
||||
to_string(|s| s.print_ty_method(p))
|
||||
}
|
||||
|
||||
pub fn method_to_string(p: &ast::Method) -> String {
|
||||
to_string(|s| s.print_method(p))
|
||||
}
|
||||
|
||||
pub fn fn_block_to_string(p: &ast::FnDecl) -> String {
|
||||
to_string(|s| s.print_fn_block_args(p))
|
||||
}
|
||||
|
||||
pub fn path_to_string(p: &ast::Path) -> String {
|
||||
to_string(|s| s.print_path(p, false))
|
||||
}
|
||||
|
||||
pub fn ident_to_string(id: &ast::Ident) -> String {
|
||||
to_string(|s| s.print_ident(*id))
|
||||
}
|
||||
|
||||
pub fn fun_to_string(decl: &ast::FnDecl, fn_style: ast::FnStyle, name: ast::Ident,
|
||||
opt_explicit_self: Option<ast::ExplicitSelf_>,
|
||||
generics: &ast::Generics) -> String {
|
||||
to_string(|s| {
|
||||
try!(s.print_fn(decl, Some(fn_style), abi::Rust,
|
||||
name, generics, opt_explicit_self, ast::Inherited));
|
||||
try!(s.end()); // Close the head box
|
||||
s.end() // Close the outer box
|
||||
})
|
||||
}
|
||||
|
||||
pub fn block_to_string(blk: &ast::Block) -> String {
|
||||
to_string(|s| {
|
||||
// containing cbox, will be closed by print-block at }
|
||||
try!(s.cbox(indent_unit));
|
||||
// head-ibox, will be closed by print-block after {
|
||||
try!(s.ibox(0u));
|
||||
s.print_block(blk)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn meta_item_to_string(mi: &ast::MetaItem) -> String {
|
||||
to_string(|s| s.print_meta_item(mi))
|
||||
}
|
||||
|
||||
pub fn attribute_to_string(attr: &ast::Attribute) -> String {
|
||||
to_string(|s| s.print_attribute(attr))
|
||||
}
|
||||
|
||||
pub fn lit_to_string(l: &ast::Lit) -> String {
|
||||
to_string(|s| s.print_literal(l))
|
||||
}
|
||||
|
||||
pub fn explicit_self_to_string(explicit_self: ast::ExplicitSelf_) -> String {
|
||||
to_string(|s| s.print_explicit_self(explicit_self, ast::MutImmutable).map(|_| {}))
|
||||
}
|
||||
|
||||
pub fn variant_to_string(var: &ast::Variant) -> String {
|
||||
to_string(|s| s.print_variant(var))
|
||||
}
|
||||
|
||||
pub fn arg_to_string(arg: &ast::Arg) -> String {
|
||||
to_string(|s| s.print_arg(arg))
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn to_str(f: |&mut State| -> IoResult<()>) -> String {
|
||||
let mut s = rust_printer(box MemWriter::new());
|
||||
f(&mut s).unwrap();
|
||||
@@ -144,62 +264,72 @@ pub fn to_str(f: |&mut State| -> IoResult<()>) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn ty_to_str(ty: &ast::Ty) -> String {
|
||||
to_str(|s| s.print_type(ty))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn pat_to_str(pat: &ast::Pat) -> String {
|
||||
to_str(|s| s.print_pat(pat))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn expr_to_str(e: &ast::Expr) -> String {
|
||||
to_str(|s| s.print_expr(e))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn lifetime_to_str(e: &ast::Lifetime) -> String {
|
||||
to_str(|s| s.print_lifetime(e))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn tt_to_str(tt: &ast::TokenTree) -> String {
|
||||
to_str(|s| s.print_tt(tt))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn tts_to_str(tts: &[ast::TokenTree]) -> String {
|
||||
to_str(|s| s.print_tts(tts))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn stmt_to_str(stmt: &ast::Stmt) -> String {
|
||||
to_str(|s| s.print_stmt(stmt))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn item_to_str(i: &ast::Item) -> String {
|
||||
to_str(|s| s.print_item(i))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn generics_to_str(generics: &ast::Generics) -> String {
|
||||
to_str(|s| s.print_generics(generics))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn ty_method_to_str(p: &ast::TypeMethod) -> String {
|
||||
to_str(|s| s.print_ty_method(p))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn method_to_str(p: &ast::Method) -> String {
|
||||
to_str(|s| s.print_method(p))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn fn_block_to_str(p: &ast::FnDecl) -> String {
|
||||
to_str(|s| s.print_fn_block_args(p))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn path_to_str(p: &ast::Path) -> String {
|
||||
to_str(|s| s.print_path(p, false))
|
||||
}
|
||||
|
||||
pub fn ident_to_str(id: &ast::Ident) -> String {
|
||||
to_str(|s| s.print_ident(*id))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn fun_to_str(decl: &ast::FnDecl, fn_style: ast::FnStyle, name: ast::Ident,
|
||||
opt_explicit_self: Option<ast::ExplicitSelf_>,
|
||||
generics: &ast::Generics) -> String {
|
||||
@@ -211,6 +341,7 @@ pub fn fun_to_str(decl: &ast::FnDecl, fn_style: ast::FnStyle, name: ast::Ident,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn block_to_str(blk: &ast::Block) -> String {
|
||||
to_str(|s| {
|
||||
// containing cbox, will be closed by print-block at }
|
||||
@@ -221,30 +352,39 @@ pub fn block_to_str(blk: &ast::Block) -> String {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn meta_item_to_str(mi: &ast::MetaItem) -> String {
|
||||
to_str(|s| s.print_meta_item(mi))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn attribute_to_str(attr: &ast::Attribute) -> String {
|
||||
to_str(|s| s.print_attribute(attr))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn lit_to_str(l: &ast::Lit) -> String {
|
||||
to_str(|s| s.print_literal(l))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn explicit_self_to_str(explicit_self: ast::ExplicitSelf_) -> String {
|
||||
to_str(|s| s.print_explicit_self(explicit_self, ast::MutImmutable).map(|_| {}))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn variant_to_str(var: &ast::Variant) -> String {
|
||||
to_str(|s| s.print_variant(var))
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
pub fn arg_to_str(arg: &ast::Arg) -> String {
|
||||
to_str(|s| s.print_arg(arg))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> String {
|
||||
match vis {
|
||||
ast::Public => format!("pub {}", s),
|
||||
@@ -674,7 +814,7 @@ impl<'a> State<'a> {
|
||||
}
|
||||
ast::ItemForeignMod(ref nmod) => {
|
||||
try!(self.head("extern"));
|
||||
try!(self.word_nbsp(nmod.abi.to_str().as_slice()));
|
||||
try!(self.word_nbsp(nmod.abi.to_string().as_slice()));
|
||||
try!(self.bopen());
|
||||
try!(self.print_foreign_mod(nmod, item.attrs.as_slice()));
|
||||
try!(self.bclose(item.span));
|
||||
@@ -898,7 +1038,7 @@ impl<'a> State<'a> {
|
||||
match *tt {
|
||||
ast::TTDelim(ref tts) => self.print_tts(tts.as_slice()),
|
||||
ast::TTTok(_, ref tk) => {
|
||||
try!(word(&mut self.s, parse::token::to_str(tk).as_slice()));
|
||||
try!(word(&mut self.s, parse::token::to_string(tk).as_slice()));
|
||||
match *tk {
|
||||
parse::token::DOC_COMMENT(..) => {
|
||||
hardbreak(&mut self.s)
|
||||
@@ -915,7 +1055,7 @@ impl<'a> State<'a> {
|
||||
match *sep {
|
||||
Some(ref tk) => {
|
||||
try!(word(&mut self.s,
|
||||
parse::token::to_str(tk).as_slice()));
|
||||
parse::token::to_string(tk).as_slice()));
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
@@ -1313,11 +1453,11 @@ impl<'a> State<'a> {
|
||||
ast::ExprBinary(op, ref lhs, ref rhs) => {
|
||||
try!(self.print_expr(&**lhs));
|
||||
try!(space(&mut self.s));
|
||||
try!(self.word_space(ast_util::binop_to_str(op)));
|
||||
try!(self.word_space(ast_util::binop_to_string(op)));
|
||||
try!(self.print_expr(&**rhs));
|
||||
}
|
||||
ast::ExprUnary(op, ref expr) => {
|
||||
try!(word(&mut self.s, ast_util::unop_to_str(op)));
|
||||
try!(word(&mut self.s, ast_util::unop_to_string(op)));
|
||||
try!(self.print_expr_maybe_paren(&**expr));
|
||||
}
|
||||
ast::ExprAddrOf(m, ref expr) => {
|
||||
@@ -1493,7 +1633,7 @@ impl<'a> State<'a> {
|
||||
ast::ExprAssignOp(op, ref lhs, ref rhs) => {
|
||||
try!(self.print_expr(&**lhs));
|
||||
try!(space(&mut self.s));
|
||||
try!(word(&mut self.s, ast_util::binop_to_str(op)));
|
||||
try!(word(&mut self.s, ast_util::binop_to_string(op)));
|
||||
try!(self.word_space("="));
|
||||
try!(self.print_expr(&**rhs));
|
||||
}
|
||||
@@ -2337,11 +2477,11 @@ impl<'a> State<'a> {
|
||||
}
|
||||
ast::LitInt(i, t) => {
|
||||
word(&mut self.s,
|
||||
ast_util::int_ty_to_str(t, Some(i)).as_slice())
|
||||
ast_util::int_ty_to_string(t, Some(i)).as_slice())
|
||||
}
|
||||
ast::LitUint(u, t) => {
|
||||
word(&mut self.s,
|
||||
ast_util::uint_ty_to_str(t, Some(u)).as_slice())
|
||||
ast_util::uint_ty_to_string(t, Some(u)).as_slice())
|
||||
}
|
||||
ast::LitIntUnsuffixed(i) => {
|
||||
word(&mut self.s, format!("{}", i).as_slice())
|
||||
@@ -2351,7 +2491,7 @@ impl<'a> State<'a> {
|
||||
format!(
|
||||
"{}{}",
|
||||
f.get(),
|
||||
ast_util::float_ty_to_str(t).as_slice()).as_slice())
|
||||
ast_util::float_ty_to_string(t).as_slice()).as_slice())
|
||||
}
|
||||
ast::LitFloatUnsuffixed(ref f) => word(&mut self.s, f.get()),
|
||||
ast::LitNil => word(&mut self.s, "()"),
|
||||
@@ -2489,7 +2629,7 @@ impl<'a> State<'a> {
|
||||
Some(abi::Rust) => Ok(()),
|
||||
Some(abi) => {
|
||||
try!(self.word_nbsp("extern"));
|
||||
self.word_nbsp(abi.to_str().as_slice())
|
||||
self.word_nbsp(abi.to_string().as_slice())
|
||||
}
|
||||
None => Ok(())
|
||||
}
|
||||
@@ -2500,7 +2640,7 @@ impl<'a> State<'a> {
|
||||
match opt_abi {
|
||||
Some(abi) => {
|
||||
try!(self.word_nbsp("extern"));
|
||||
self.word_nbsp(abi.to_str().as_slice())
|
||||
self.word_nbsp(abi.to_string().as_slice())
|
||||
}
|
||||
None => Ok(())
|
||||
}
|
||||
@@ -2516,7 +2656,7 @@ impl<'a> State<'a> {
|
||||
|
||||
if abi != abi::Rust {
|
||||
try!(self.word_nbsp("extern"));
|
||||
try!(self.word_nbsp(abi.to_str().as_slice()));
|
||||
try!(self.word_nbsp(abi.to_string().as_slice()));
|
||||
}
|
||||
|
||||
word(&mut self.s, "fn")
|
||||
@@ -2547,7 +2687,7 @@ mod test {
|
||||
use parse::token;
|
||||
|
||||
#[test]
|
||||
fn test_fun_to_str() {
|
||||
fn test_fun_to_string() {
|
||||
let abba_ident = token::str_to_ident("abba");
|
||||
|
||||
let decl = ast::FnDecl {
|
||||
@@ -2559,13 +2699,13 @@ mod test {
|
||||
variadic: false
|
||||
};
|
||||
let generics = ast_util::empty_generics();
|
||||
assert_eq!(&fun_to_str(&decl, ast::NormalFn, abba_ident,
|
||||
assert_eq!(&fun_to_string(&decl, ast::NormalFn, abba_ident,
|
||||
None, &generics),
|
||||
&"fn abba()".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_variant_to_str() {
|
||||
fn test_variant_to_string() {
|
||||
let ident = token::str_to_ident("principal_skinner");
|
||||
|
||||
let var = codemap::respan(codemap::DUMMY_SP, ast::Variant_ {
|
||||
@@ -2578,7 +2718,7 @@ mod test {
|
||||
vis: ast::Public,
|
||||
});
|
||||
|
||||
let varstr = variant_to_str(&var);
|
||||
let varstr = variant_to_string(&var);
|
||||
assert_eq!(&varstr,&"pub principal_skinner".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user