Skip to content

Commit 1204fad

Browse files
committed
---
yaml --- r: 63561 b: refs/heads/snap-stage3 c: 30d7559 h: refs/heads/master i: 63559: 3783355 v: v3
1 parent a2642db commit 1204fad

File tree

10 files changed

+119
-62
lines changed

10 files changed

+119
-62
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 9b2d9a9539d77695e915ebed189575d1249e7064
4+
refs/heads/snap-stage3: 30d755957a0f2cc3be3b355671da79cdf34fd50a
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/doc/rust.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,9 @@ Supported traits for `deriving` are:
15621562
* `IterBytes`, to iterate over the bytes in a data type.
15631563
* `Rand`, to create a random instance of a data type.
15641564
* `ToStr`, to convert to a string. For a type with this instance,
1565-
`obj.to_str()` has the same output as `fmt!("%?", obj)`.
1565+
`obj.to_str()` has similar output as `fmt!("%?", obj)`, but it differs in that
1566+
each constituent field of the type must also implement `ToStr` and will have
1567+
`field.to_str()` invoked to build up the result.
15661568

15671569
# Statements and expressions
15681570

branches/snap-stage3/src/libextra/arc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* ~~~ {.rust}
2121
* extern mod std;
22-
* use extra::arc;
22+
* use std::arc;
2323
* let numbers=vec::from_fn(100, |ind| (ind as float)*rand::random());
2424
* let shared_numbers=arc::ARC(numbers);
2525
*

branches/snap-stage3/src/libextra/base64.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ impl<'self> ToBase64 for &'self [u8] {
3636
* # Example
3737
*
3838
* ~~~ {.rust}
39-
* extern mod extra;
40-
* use extra::base64::ToBase64;
39+
* extern mod std;
40+
* use std::base64::ToBase64;
4141
*
4242
* fn main () {
4343
* let str = [52,32].to_base64();
@@ -99,8 +99,8 @@ impl<'self> ToBase64 for &'self str {
9999
* # Example
100100
*
101101
* ~~~ {.rust}
102-
* extern mod extra;
103-
* use extra::base64::ToBase64;
102+
* extern mod std;
103+
* use std::base64::ToBase64;
104104
*
105105
* fn main () {
106106
* let str = "Hello, World".to_base64();
@@ -127,9 +127,9 @@ impl<'self> FromBase64 for &'self [u8] {
127127
* # Example
128128
*
129129
* ~~~ {.rust}
130-
* extern mod extra;
131-
* use extra::base64::ToBase64;
132-
* use extra::base64::FromBase64;
130+
* extern mod std;
131+
* use std::base64::ToBase64;
132+
* use std::base64::FromBase64;
133133
*
134134
* fn main () {
135135
* let str = [52,32].to_base64();
@@ -207,9 +207,9 @@ impl<'self> FromBase64 for &'self str {
207207
* This converts a string literal to base64 and back.
208208
*
209209
* ~~~ {.rust}
210-
* extern mod extra;
211-
* use extra::base64::ToBase64;
212-
* use extra::base64::FromBase64;
210+
* extern mod std;
211+
* use std::base64::ToBase64;
212+
* use std::base64::FromBase64;
213213
* use core::str;
214214
*
215215
* fn main () {

branches/snap-stage3/src/libextra/future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* ~~~ {.rust}
1818
* # fn fib(n: uint) -> uint {42};
1919
* # fn make_a_sandwich() {};
20-
* let mut delayed_fib = extra::future::spawn (|| fib(5000) );
20+
* let mut delayed_fib = std::future::spawn (|| fib(5000) );
2121
* make_a_sandwich();
2222
* println(fmt!("fib(5000) = %?", delayed_fib.get()))
2323
* ~~~

branches/snap-stage3/src/libextra/net_ip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct ParseAddrErr {
5555
*
5656
* # Arguments
5757
*
58-
* * ip - a `extra::net::ip::IpAddr`
58+
* * ip - a `std::net::ip::IpAddr`
5959
*/
6060
pub fn format_addr(ip: &IpAddr) -> ~str {
6161
match *ip {
@@ -80,7 +80,7 @@ pub fn format_addr(ip: &IpAddr) -> ~str {
8080
* Get the associated port
8181
*
8282
* # Arguments
83-
* * ip - a `extra::net::ip::IpAddr`
83+
* * ip - a `std::net::ip::IpAddr`
8484
*/
8585
pub fn get_port(ip: &IpAddr) -> uint {
8686
match *ip {

branches/snap-stage3/src/libstd/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,5 +1716,5 @@ mod tests {
17161716
assert!(!os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
17171717
}
17181718

1719-
// More recursive_mkdir tests are in extra::tempfile
1719+
// More recursive_mkdir tests are in std::tempfile
17201720
}

branches/snap-stage3/src/libstd/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Strings are a packed UTF-8 representation of text, stored as null
1515
* terminated buffers of u8 bytes. Strings should be indexed in bytes,
1616
* for efficiency, but UTF-8 unsafe operations should be avoided. For
17-
* some heavy-duty uses, try extra::rope.
17+
* some heavy-duty uses, try std::rope.
1818
*/
1919

2020
use at_vec;

branches/snap-stage3/src/libsyntax/ext/deriving/to_str.rs

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use core::prelude::*;
1212

13+
use ast;
1314
use ast::{meta_item, item, expr};
1415
use codemap::span;
1516
use ext::base::ExtCtxt;
@@ -40,16 +41,68 @@ pub fn expand_deriving_to_str(cx: @ExtCtxt,
4041
trait_def.expand(cx, span, mitem, in_items)
4142
}
4243

43-
fn to_str_substructure(cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr {
44-
match substr.self_args {
45-
[self_obj] => {
46-
let self_addr = cx.expr_addr_of(span, self_obj);
47-
cx.expr_call_global(span,
48-
~[cx.ident_of("std"),
49-
cx.ident_of("sys"),
50-
cx.ident_of("log_str")],
51-
~[self_addr])
44+
// It used to be the case that this deriving implementation invoked
45+
// std::sys::log_str, but this isn't sufficient because it doesn't invoke the
46+
// to_str() method on each field. Hence we mirror the logic of the log_str()
47+
// method, but with tweaks to call to_str() on sub-fields.
48+
fn to_str_substructure(cx: @ExtCtxt, span: span,
49+
substr: &Substructure) -> @expr {
50+
let to_str = cx.ident_of("to_str");
51+
52+
let doit = |start: &str, end: @str, name: ast::ident,
53+
fields: &[(Option<ast::ident>, @expr, ~[@expr])]| {
54+
if fields.len() == 0 {
55+
cx.expr_str_uniq(span, cx.str_of(name))
56+
} else {
57+
let buf = cx.ident_of("buf");
58+
let start = cx.str_of(name) + start;
59+
let init = cx.expr_str_uniq(span, start.to_managed());
60+
let mut stmts = ~[cx.stmt_let(span, true, buf, init)];
61+
let push_str = cx.ident_of("push_str");
62+
63+
let push = |s: @expr| {
64+
let ebuf = cx.expr_ident(span, buf);
65+
let call = cx.expr_method_call(span, ebuf, push_str, ~[s]);
66+
stmts.push(cx.stmt_expr(call));
67+
};
68+
69+
for fields.iter().enumerate().advance |(i, &(name, e, _))| {
70+
if i > 0 {
71+
push(cx.expr_str(span, @", "));
72+
}
73+
match name {
74+
None => {}
75+
Some(id) => {
76+
let name = cx.str_of(id) + ": ";
77+
push(cx.expr_str(span, name.to_managed()));
78+
}
79+
}
80+
push(cx.expr_method_call(span, e, to_str, ~[]));
81+
}
82+
push(cx.expr_str(span, end));
83+
84+
cx.expr_blk(cx.blk(span, stmts, Some(cx.expr_ident(span, buf))))
85+
}
86+
};
87+
88+
return match *substr.fields {
89+
Struct(ref fields) => {
90+
if fields.len() == 0 || fields[0].n0_ref().is_none() {
91+
doit("(", @")", substr.type_ident, *fields)
92+
} else {
93+
doit("{", @"}", substr.type_ident, *fields)
94+
}
5295
}
53-
_ => cx.span_bug(span, "Invalid number of arguments in `deriving(ToStr)`")
54-
}
96+
97+
EnumMatching(_, variant, ref fields) => {
98+
match variant.node.kind {
99+
ast::tuple_variant_kind(*) =>
100+
doit("(", @")", variant.node.name, *fields),
101+
ast::struct_variant_kind(*) =>
102+
doit("{", @"}", variant.node.name, *fields),
103+
}
104+
}
105+
106+
_ => cx.bug("expected Struct or EnumMatching in deriving(ToStr)")
107+
};
55108
}
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// xfail-fast #6330
2-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
32
// file at the top-level directory of this distribution and at
43
// http://rust-lang.org/COPYRIGHT.
54
//
@@ -9,39 +8,42 @@
98
// option. This file may not be copied, modified, or distributed
109
// except according to those terms.
1110

12-
use std::rand;
11+
#[deriving(ToStr)]
12+
enum A {}
13+
#[deriving(ToStr)]
14+
enum B { B1, B2, B3 }
15+
#[deriving(ToStr)]
16+
enum C { C1(int), C2(B), C3(~str) }
17+
#[deriving(ToStr)]
18+
enum D { D1{ a: int } }
19+
#[deriving(ToStr)]
20+
struct E;
21+
#[deriving(ToStr)]
22+
struct F(int);
23+
#[deriving(ToStr)]
24+
struct G(int, int);
25+
#[deriving(ToStr)]
26+
struct H { a: int }
27+
#[deriving(ToStr)]
28+
struct I { a: int, b: int }
29+
#[deriving(ToStr)]
30+
struct J(Custom);
1331

14-
#[deriving(Rand,ToStr)]
15-
struct A;
16-
17-
#[deriving(Rand,ToStr)]
18-
struct B(int, int);
19-
20-
#[deriving(Rand,ToStr)]
21-
struct C {
22-
x: f64,
23-
y: (u8, u8)
24-
}
25-
26-
#[deriving(Rand,ToStr)]
27-
enum D {
28-
D0,
29-
D1(uint),
30-
D2 { x: (), y: () }
32+
struct Custom;
33+
impl ToStr for Custom {
34+
fn to_str(&self) -> ~str { ~"yay" }
3135
}
3236

3337
fn main() {
34-
macro_rules! t(
35-
($ty:ty) => {{
36-
let x =rand::random::<$ty>();
37-
assert_eq!(x.to_str(), fmt!("%?", x));
38-
}}
39-
);
40-
41-
for 20.times {
42-
t!(A);
43-
t!(B);
44-
t!(C);
45-
t!(D);
46-
}
38+
assert_eq!(B1.to_str(), ~"B1");
39+
assert_eq!(B2.to_str(), ~"B2");
40+
assert_eq!(C1(3).to_str(), ~"C1(3)");
41+
assert_eq!(C2(B2).to_str(), ~"C2(B2)");
42+
assert_eq!(D1{ a: 2 }.to_str(), ~"D1{a: 2}");
43+
assert_eq!(E.to_str(), ~"E");
44+
assert_eq!(F(3).to_str(), ~"F(3)");
45+
assert_eq!(G(3, 4).to_str(), ~"G(3, 4)");
46+
assert_eq!(G(3, 4).to_str(), ~"G(3, 4)");
47+
assert_eq!(I{ a: 2, b: 4 }.to_str(), ~"I{a: 2, b: 4}");
48+
assert_eq!(J(Custom).to_str(), ~"J(yay)");
4749
}

0 commit comments

Comments
 (0)