diff options
| author | bors <bors@rust-lang.org> | 2014-02-24 04:11:53 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-02-24 04:11:53 -0800 |
| commit | 672097753a217d4990129cbdfab16ef8c9b08b21 (patch) | |
| tree | cf206fc1ba6465032dac4fdce670860538da0140 /src/libsyntax | |
| parent | a5342d5970d57dd0cc4805ba0f5385d7f3859c94 (diff) | |
| parent | 8761f79485f11ef03eb6cb569ccb9f4c73e68f11 (diff) | |
| download | rust-672097753a217d4990129cbdfab16ef8c9b08b21.tar.gz rust-672097753a217d4990129cbdfab16ef8c9b08b21.zip | |
auto merge of #12412 : alexcrichton/rust/deriving-show, r=huonw
This commit removes deriving(ToStr) in favor of deriving(Show), migrating all impls of ToStr to fmt::Show. Most of the details can be found in the first commit message. Closes #12477
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/abi.rs | 21 | ||||
| -rw-r--r-- | src/libsyntax/ast.rs | 55 | ||||
| -rw-r--r-- | src/libsyntax/ast_map.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/attr.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/crateid.rs | 11 | ||||
| -rw-r--r-- | src/libsyntax/diagnostic.rs | 15 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/mod.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/to_str.rs | 132 |
8 files changed, 63 insertions, 183 deletions
diff --git a/src/libsyntax/abi.rs b/src/libsyntax/abi.rs index 13725ef2473..9349e5c8e98 100644 --- a/src/libsyntax/abi.rs +++ b/src/libsyntax/abi.rs @@ -9,6 +9,8 @@ // except according to those terms. use std::hash::{Hash, sip}; +use std::fmt; +use std::fmt::Show; #[deriving(Eq)] pub enum Os { OsWin32, OsMacos, OsLinux, OsAndroid, OsFreebsd, } @@ -271,20 +273,23 @@ impl Hash for Abi { } } -impl ToStr for Abi { - fn to_str(&self) -> ~str { - self.data().name.to_str() +impl fmt::Show for Abi { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.data().name.fmt(f) } } -impl ToStr for AbiSet { - fn to_str(&self) -> ~str { - let mut strs = ~[]; +impl fmt::Show for AbiSet { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + try!(write!(f.buf, "\"")); + let mut first = true; self.each(|abi| { - strs.push(abi.data().name); + if first { first = false; } + else { let _ = write!(f.buf, " "); } + let _ = write!(f.buf, "{}", abi.data().name); true }); - format!("\"{}\"", strs.connect(" ")) + write!(f.buf, "\"") } } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 4e43592ec5c..7561d8cbbae 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -17,11 +17,12 @@ use opt_vec::OptVec; use parse::token::{InternedString, special_idents, str_to_ident}; use parse::token; +use std::fmt; +use std::fmt::Show; use std::cell::RefCell; use collections::HashMap; use std::option::Option; use std::rc::Rc; -use std::to_str::ToStr; use serialize::{Encodable, Decodable, Encoder, Decoder}; /// A pointer abstraction. FIXME(eddyb) #10676 use Rc<T> in the future. @@ -39,7 +40,7 @@ pub fn P<T: 'static>(value: T) -> P<T> { // table) and a SyntaxContext to track renaming and // macro expansion per Flatt et al., "Macros // That Work Together" -#[deriving(Clone, Hash, ToStr, TotalEq, TotalOrd, Show)] +#[deriving(Clone, Hash, TotalEq, TotalOrd, Show)] pub struct Ident { name: Name, ctxt: SyntaxContext } impl Ident { @@ -182,7 +183,7 @@ pub type CrateNum = u32; pub type NodeId = u32; -#[deriving(Clone, TotalEq, TotalOrd, Eq, Encodable, Decodable, Hash, ToStr, Show)] +#[deriving(Clone, TotalEq, TotalOrd, Eq, Encodable, Decodable, Hash, Show)] pub struct DefId { krate: CrateNum, node: NodeId, @@ -277,7 +278,7 @@ pub enum Def { DefMethod(DefId /* method */, Option<DefId> /* trait */), } -#[deriving(Clone, Eq, Hash, Encodable, Decodable, ToStr)] +#[deriving(Clone, Eq, Hash, Encodable, Decodable, Show)] pub enum DefRegion { DefStaticRegion, DefEarlyBoundRegion(/* index */ uint, /* lifetime decl */ NodeId), @@ -398,12 +399,12 @@ pub enum Sigil { ManagedSigil } -impl ToStr for Sigil { - fn to_str(&self) -> ~str { +impl fmt::Show for Sigil { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - BorrowedSigil => ~"&", - OwnedSigil => ~"~", - ManagedSigil => ~"@" + BorrowedSigil => "&".fmt(f), + OwnedSigil => "~".fmt(f), + ManagedSigil => "@".fmt(f), } } } @@ -768,9 +769,9 @@ pub enum IntTy { TyI64, } -impl ToStr for IntTy { - fn to_str(&self) -> ~str { - ast_util::int_ty_to_str(*self) +impl fmt::Show for IntTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{}", ast_util::int_ty_to_str(*self)) } } @@ -783,9 +784,9 @@ pub enum UintTy { TyU64, } -impl ToStr for UintTy { - fn to_str(&self) -> ~str { - ast_util::uint_ty_to_str(*self) +impl fmt::Show for UintTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{}", ast_util::uint_ty_to_str(*self)) } } @@ -795,9 +796,9 @@ pub enum FloatTy { TyF64, } -impl ToStr for FloatTy { - fn to_str(&self) -> ~str { - ast_util::float_ty_to_str(*self) +impl fmt::Show for FloatTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{}", ast_util::float_ty_to_str(*self)) } } @@ -826,11 +827,11 @@ pub enum Onceness { Many } -impl ToStr for Onceness { - fn to_str(&self) -> ~str { +impl fmt::Show for Onceness { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Once => ~"once", - Many => ~"many" + Once => "once".fmt(f), + Many => "many".fmt(f), } } } @@ -939,12 +940,12 @@ pub enum Purity { ExternFn, // declared with "extern fn" } -impl ToStr for Purity { - fn to_str(&self) -> ~str { +impl fmt::Show for Purity { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - ImpureFn => ~"impure", - UnsafeFn => ~"unsafe", - ExternFn => ~"extern" + ImpureFn => "impure".fmt(f), + UnsafeFn => "unsafe".fmt(f), + ExternFn => "extern".fmt(f), } } } diff --git a/src/libsyntax/ast_map.rs b/src/libsyntax/ast_map.rs index 26c4b07fc96..9194cfb0694 100644 --- a/src/libsyntax/ast_map.rs +++ b/src/libsyntax/ast_map.rs @@ -22,6 +22,7 @@ use std::logging; use std::cell::RefCell; use std::iter; use std::vec; +use std::fmt; #[deriving(Clone, Eq)] pub enum PathElem { @@ -37,9 +38,10 @@ impl PathElem { } } -impl ToStr for PathElem { - fn to_str(&self) -> ~str { - token::get_name(self.name()).get().to_str() +impl fmt::Show for PathElem { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let slot = token::get_name(self.name()); + write!(f.buf, "{}", slot.get()) } } diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 93edb552bbe..27d1c6fa649 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -341,7 +341,7 @@ pub struct Stability { } /// The available stability levels. -#[deriving(Eq,Ord,Clone,ToStr)] +#[deriving(Eq,Ord,Clone,Show)] pub enum StabilityLevel { Deprecated, Experimental, diff --git a/src/libsyntax/crateid.rs b/src/libsyntax/crateid.rs index 0831f319ce7..2417a6fa1ba 100644 --- a/src/libsyntax/crateid.rs +++ b/src/libsyntax/crateid.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::fmt; + /// CrateIds identify crates and include the crate name and optionally a path /// and version. In the full form, they look like relative URLs. Example: /// `github.com/mozilla/rust#std:1.0` would be a package ID with a path of @@ -26,16 +28,17 @@ pub struct CrateId { version: Option<~str>, } -impl ToStr for CrateId { - fn to_str(&self) -> ~str { +impl fmt::Show for CrateId { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + try!(write!(f.buf, "{}", self.path)); let version = match self.version { None => "0.0", Some(ref version) => version.as_slice(), }; if self.path == self.name || self.path.ends_with(format!("/{}", self.name)) { - format!("{}\\#{}", self.path, version) + write!(f.buf, "\\#{}", version) } else { - format!("{}\\#{}:{}", self.path, self.name, version) + write!(f.buf, "\\#{}:{}", self.name, version) } } } diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 8cf0f128d22..be45008b92a 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -12,8 +12,9 @@ use codemap::{Pos, Span}; use codemap; use std::cell::Cell; -use std::io; +use std::fmt; use std::io::stdio::StdWriter; +use std::io; use std::iter::range; use std::local_data; use term; @@ -162,12 +163,14 @@ pub enum Level { Note, } -impl ToStr for Level { - fn to_str(&self) -> ~str { +impl fmt::Show for Level { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use std::fmt::Show; + match *self { - Fatal | Error => ~"error", - Warning => ~"warning", - Note => ~"note" + Fatal | Error => "error".fmt(f), + Warning => "warning".fmt(f), + Note => "note".fmt(f), } } } diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index 95b9a45b9fc..9c823449d21 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -27,7 +27,6 @@ pub mod encodable; pub mod decodable; pub mod hash; pub mod rand; -pub mod to_str; pub mod show; pub mod zero; pub mod default; @@ -85,7 +84,6 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt, "Rand" => expand!(rand::expand_deriving_rand), - "ToStr" => expand!(to_str::expand_deriving_to_str), "Show" => expand!(show::expand_deriving_show), "Zero" => expand!(zero::expand_deriving_zero), diff --git a/src/libsyntax/ext/deriving/to_str.rs b/src/libsyntax/ext/deriving/to_str.rs deleted file mode 100644 index 5cb81d9e762..00000000000 --- a/src/libsyntax/ext/deriving/to_str.rs +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use ast; -use ast::{MetaItem, Item, Expr}; -use codemap::Span; -use ext::base::ExtCtxt; -use ext::build::AstBuilder; -use ext::deriving::generic::*; -use parse::token::InternedString; -use parse::token; - -pub fn expand_deriving_to_str(cx: &mut ExtCtxt, - span: Span, - mitem: @MetaItem, - item: @Item, - push: |@Item|) { - let trait_def = TraitDef { - span: span, - attributes: ~[], - path: Path::new(~["std", "to_str", "ToStr"]), - additional_bounds: ~[], - generics: LifetimeBounds::empty(), - methods: ~[ - MethodDef { - name: "to_str", - generics: LifetimeBounds::empty(), - explicit_self: borrowed_explicit_self(), - args: ~[], - ret_ty: Ptr(~Literal(Path::new_local("str")), Send), - inline: false, - const_nonmatching: false, - combine_substructure: to_str_substructure - } - ] - }; - trait_def.expand(cx, mitem, item, push) -} - -// It used to be the case that this deriving implementation invoked -// std::repr::repr_to_str, but this isn't sufficient because it -// doesn't invoke the to_str() method on each field. Hence we mirror -// the logic of the repr_to_str() method, but with tweaks to call to_str() -// on sub-fields. -fn to_str_substructure(cx: &mut ExtCtxt, span: Span, substr: &Substructure) - -> @Expr { - let to_str = cx.ident_of("to_str"); - - let doit = |start: &str, - end: InternedString, - name: ast::Ident, - fields: &[FieldInfo]| { - if fields.len() == 0 { - cx.expr_str_uniq(span, token::get_ident(name)) - } else { - let buf = cx.ident_of("buf"); - let interned_str = token::get_ident(name); - let start = - token::intern_and_get_ident(interned_str.get() + start); - let init = cx.expr_str_uniq(span, start); - let mut stmts = ~[cx.stmt_let(span, true, buf, init)]; - let push_str = cx.ident_of("push_str"); - - { - let push = |s: @Expr| { - let ebuf = cx.expr_ident(span, buf); - let call = cx.expr_method_call(span, ebuf, push_str, ~[s]); - stmts.push(cx.stmt_expr(call)); - }; - - for (i, &FieldInfo {name, span, self_, .. }) in fields.iter().enumerate() { - if i > 0 { - push(cx.expr_str(span, InternedString::new(", "))); - } - match name { - None => {} - Some(id) => { - let interned_id = token::get_ident(id); - let name = interned_id.get() + ": "; - push(cx.expr_str(span, - token::intern_and_get_ident(name))); - } - } - push(cx.expr_method_call(span, self_, to_str, ~[])); - } - push(cx.expr_str(span, end)); - } - - cx.expr_block(cx.block(span, stmts, Some(cx.expr_ident(span, buf)))) - } - }; - - return match *substr.fields { - Struct(ref fields) => { - if fields.len() == 0 || fields[0].name.is_none() { - doit("(", - InternedString::new(")"), - substr.type_ident, - *fields) - } else { - doit("{", - InternedString::new("}"), - substr.type_ident, - *fields) - } - } - - EnumMatching(_, variant, ref fields) => { - match variant.node.kind { - ast::TupleVariantKind(..) => - doit("(", - InternedString::new(")"), - variant.node.name, - *fields), - ast::StructVariantKind(..) => - doit("{", - InternedString::new("}"), - variant.node.name, - *fields), - } - } - - _ => cx.bug("expected Struct or EnumMatching in deriving(ToStr)") - }; -} |
