This has been superseded by deriving(Show).
cc #9806
///Returns a string representation of a Leaf.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
- if i != 0 { if_ok!(write!(f.buf, " // ")) }
- if_ok!(write!(f.buf, "{}", *s))
+ if i != 0 { try!(write!(f.buf, " // ")) }
+ try!(write!(f.buf, "{}", *s))
}
Ok(())
}
///Returns a string representation of a Branch.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
- if i != 0 { if_ok!(write!(f.buf, " // ")) }
- if_ok!(write!(f.buf, "{}", *s))
+ if i != 0 { try!(write!(f.buf, " // ")) }
+ try!(write!(f.buf, "{}", *s))
}
write!(f.buf, " // rightmost child: ({}) ", *self.rightmost_child)
}
/// Return a string that lists the key-value pairs from most-recently
/// used to least-recently used.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- if_ok!(write!(f.buf, r"\{"));
+ try!(write!(f.buf, r"\{"));
let mut cur = self.head;
for i in range(0, self.len()) {
- if i > 0 { if_ok!(write!(f.buf, ", ")) }
+ if i > 0 { try!(write!(f.buf, ", ")) }
unsafe {
cur = (*cur).next;
match (*cur).key {
// should never print nil
- None => if_ok!(write!(f.buf, "nil")),
- Some(ref k) => if_ok!(write!(f.buf, "{}", *k)),
+ None => try!(write!(f.buf, "nil")),
+ Some(ref k) => try!(write!(f.buf, "{}", *k)),
}
}
- if_ok!(write!(f.buf, ": "));
+ try!(write!(f.buf, ": "));
unsafe {
match (*cur).value {
// should never print nil
- None => if_ok!(write!(f.buf, "nil")),
- Some(ref value) => if_ok!(write!(f.buf, "{}", *value)),
+ None => try!(write!(f.buf, "nil")),
+ Some(ref value) => try!(write!(f.buf, "{}", *value)),
}
}
}
* result in just "http://somehost.com".
*/
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- if_ok!(write!(f.buf, "{}:", self.scheme));
+ try!(write!(f.buf, "{}:", self.scheme));
if !self.host.is_empty() {
- if_ok!(write!(f.buf, "//"));
+ try!(write!(f.buf, "//"));
match self.user {
- Some(ref user) => if_ok!(write!(f.buf, "{}", *user)),
+ Some(ref user) => try!(write!(f.buf, "{}", *user)),
None => {}
}
match self.port {
- Some(ref port) => if_ok!(write!(f.buf, "{}:{}", self.host,
+ Some(ref port) => try!(write!(f.buf, "{}:{}", self.host,
*port)),
- None => if_ok!(write!(f.buf, "{}", self.host)),
+ None => try!(write!(f.buf, "{}", self.host)),
}
}
- if_ok!(write!(f.buf, "{}", self.path));
+ try!(write!(f.buf, "{}", self.path));
if !self.query.is_empty() {
- if_ok!(write!(f.buf, "?{}", query_to_str(&self.query)));
+ try!(write!(f.buf, "?{}", query_to_str(&self.query)));
}
match self.fragment {
impl fmt::Show for Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- if_ok!(write!(f.buf, "{}", self.path));
+ try!(write!(f.buf, "{}", self.path));
if !self.query.is_empty() {
- if_ok!(write!(f.buf, "?{}", self.query))
+ try!(write!(f.buf, "?{}", self.query))
}
match self.fragment {
impl fmt::Show for AbiSet {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- if_ok!(write!(f.buf, "\""));
+ try!(write!(f.buf, "\""));
let mut first = true;
self.each(|abi| {
if first { first = false; }
impl fmt::Show for CrateId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- if_ok!(write!(f.buf, "{}", self.path));
+ try!(write!(f.buf, "{}", self.path));
let version = match self.version {
None => "0.0",
Some(ref version) => version.as_slice(),
pub mod decodable;
pub mod hash;
pub mod rand;
-pub mod to_str;
pub mod show;
pub mod zero;
pub mod default;
"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),
+++ /dev/null
-// 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)")
- };
-}
extern crate test;
extern crate serialize;
-use std::cast::{transmute,transmute_copy};
use std::cast::{transmute,transmute_copy};
use std::char::Char;
-use std::cmp::Eq;
-use std::cmp::Eq;
use std::fmt;
use std::hash::{Hash, sip};
use std::num::FromStrRadix;
use std::rand::Rng;
+use std::rand;
+use std::str;
+use std::vec;
use serialize::{Encoder, Encodable, Decoder, Decodable};
// option. This file may not be copied, modified, or distributed
// except according to those terms.
+// ignore-pretty
+
static a: int =
(((((((((((((((((((((((((((((((((((((((((((((((((((
(((((((((((((((((((((((((((((((((((((((((((((((((((