Skip to content

Commit 1dea883

Browse files
committed
auto merge of #14364 : alexcrichton/rust/libdebug, r=brson
This commit moves reflection (as well as the {:?} format modifier) to a new libdebug crate, all of which is marked experimental. This is a breaking change because it now requires the debug crate to be explicitly linked if the :? format qualifier is used. This means that any code using this feature will have to add `extern crate debug;` to the top of the crate. Any code relying on reflection will also need to do this. Closes #12019 [breaking-change]
2 parents 73dac7e + b53454e commit 1dea883

File tree

156 files changed

+1545
-147
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+1545
-147
lines changed

mk/crates.mk

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,25 @@
5151

5252
TARGET_CRATES := libc std green rustuv native flate arena glob term semver \
5353
uuid serialize sync getopts collections num test time rand \
54-
workcache url log regex graphviz core rlibc alloc
54+
workcache url log regex graphviz core rlibc alloc debug
5555
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros
5656
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5757
TOOLS := compiletest rustdoc rustc
5858

5959
DEPS_core :=
6060
DEPS_rlibc :=
6161
DEPS_alloc := core libc native:jemalloc
62+
DEPS_debug := std
6263
DEPS_std := core libc alloc native:rustrt native:backtrace
6364
DEPS_graphviz := std
6465
DEPS_green := std rand native:context_switch
6566
DEPS_rustuv := std native:uv native:uv_support
6667
DEPS_native := std
67-
DEPS_syntax := std term serialize collections log fmt_macros
68+
DEPS_syntax := std term serialize collections log fmt_macros debug
6869
DEPS_rustc := syntax native:rustllvm flate arena serialize sync getopts \
69-
collections time log graphviz
70+
collections time log graphviz debug
7071
DEPS_rustdoc := rustc native:hoedown serialize sync getopts collections \
71-
test time
72+
test time debug
7273
DEPS_flate := std native:miniz
7374
DEPS_arena := std collections
7475
DEPS_graphviz := std
@@ -79,7 +80,7 @@ DEPS_semver := std
7980
DEPS_uuid := std serialize rand
8081
DEPS_sync := std alloc
8182
DEPS_getopts := std
82-
DEPS_collections := std rand
83+
DEPS_collections := std rand debug
8384
DEPS_fourcc := syntax std
8485
DEPS_hexfloat := syntax std
8586
DEPS_num := std rand

src/doc/guide-container.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ for (x, y) in it {
254254
}
255255
256256
// yield and print the last pair from the iterator
257-
println!("last: {:?}", it.next());
257+
println!("last: {}", it.next());
258258
259259
// the iterator is now fully consumed
260260
assert!(it.next().is_none());
@@ -349,9 +349,9 @@ returning another `DoubleEndedIterator` with `next` and `next_back` exchanged.
349349
~~~
350350
let xs = [1, 2, 3, 4, 5, 6];
351351
let mut it = xs.iter();
352-
println!("{:?}", it.next()); // prints `Some(&1)`
353-
println!("{:?}", it.next()); // prints `Some(&2)`
354-
println!("{:?}", it.next_back()); // prints `Some(&6)`
352+
println!("{}", it.next()); // prints `Some(1)`
353+
println!("{}", it.next()); // prints `Some(2)`
354+
println!("{}", it.next_back()); // prints `Some(6)`
355355
356356
// prints `5`, `4` and `3`
357357
for &x in it.rev() {
@@ -367,7 +367,7 @@ let xs = [1, 2, 3, 4];
367367
let ys = [5, 6, 7, 8];
368368
let mut it = xs.iter().chain(ys.iter()).map(|&x| x * 2);
369369
370-
println!("{:?}", it.next()); // prints `Some(2)`
370+
println!("{}", it.next()); // prints `Some(2)`
371371
372372
// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`
373373
for x in it.rev() {
@@ -398,17 +398,17 @@ underlying iterators are.
398398
let xs = [1, 2, 3, 4, 5];
399399
let ys = ~[7, 9, 11];
400400
let mut it = xs.iter().chain(ys.iter());
401-
println!("{:?}", it.idx(0)); // prints `Some(&1)`
402-
println!("{:?}", it.idx(5)); // prints `Some(&7)`
403-
println!("{:?}", it.idx(7)); // prints `Some(&11)`
404-
println!("{:?}", it.idx(8)); // prints `None`
401+
println!("{}", it.idx(0)); // prints `Some(1)`
402+
println!("{}", it.idx(5)); // prints `Some(7)`
403+
println!("{}", it.idx(7)); // prints `Some(11)`
404+
println!("{}", it.idx(8)); // prints `None`
405405
406406
// yield two elements from the beginning, and one from the end
407407
it.next();
408408
it.next();
409409
it.next_back();
410410
411-
println!("{:?}", it.idx(0)); // prints `Some(&3)`
412-
println!("{:?}", it.idx(4)); // prints `Some(&9)`
413-
println!("{:?}", it.idx(6)); // prints `None`
411+
println!("{}", it.idx(0)); // prints `Some(3)`
412+
println!("{}", it.idx(4)); // prints `Some(9)`
413+
println!("{}", it.idx(6)); // prints `None`
414414
~~~

src/doc/guide-pointers.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ you were writing this Rust code:
104104

105105
~~~rust
106106
# fn transform(p: Point) -> Point { p }
107+
#[deriving(Show)]
107108
struct Point {
108109
x: int,
109110
y: int,
@@ -112,7 +113,7 @@ struct Point {
112113
fn main() {
113114
let p0 = Point { x: 5, y: 10};
114115
let p1 = transform(p0);
115-
println!("{:?}", p1);
116+
println!("{}", p1);
116117
}
117118

118119
~~~
@@ -136,6 +137,7 @@ let p1 = transform(&p0);
136137
This does work, but you don't need to create those references! The better way to write this is simply:
137138

138139
~~~rust
140+
#[deriving(Show)]
139141
struct Point {
140142
x: int,
141143
y: int,
@@ -148,7 +150,7 @@ fn transform(p: Point) -> Point {
148150
fn main() {
149151
let p0 = Point { x: 5, y: 10};
150152
let p1 = transform(p0);
151-
println!("{:?}", p1);
153+
println!("{}", p1);
152154
}
153155
~~~
154156

@@ -185,15 +187,15 @@ trait. Therefore, unboxed traits don't make any sense, and aren't allowed.
185187
Sometimes, you need a recursive data structure. The simplest is known as a 'cons list':
186188

187189
~~~rust
188-
190+
#[deriving(Show)]
189191
enum List<T> {
190192
Nil,
191193
Cons(T, Box<List<T>>),
192194
}
193195

194196
fn main() {
195197
let list: List<int> = Cons(1, box Cons(2, box Cons(3, box Nil)));
196-
println!("{:?}", list);
198+
println!("{}", list);
197199
}
198200
~~~
199201

@@ -277,7 +279,7 @@ fn main() {
277279
let origin = &Point { x: 0.0, y: 0.0 };
278280
let p1 = box Point { x: 5.0, y: 3.0 };
279281

280-
println!("{:?}", compute_distance(origin, p1));
282+
println!("{}", compute_distance(origin, p1));
281283
}
282284
~~~
283285

@@ -316,11 +318,11 @@ fn main() {
316318
let mut x = box 5;
317319
if *x < 10 {
318320
let y = &x;
319-
println!("Oh no: {:?}", y);
321+
println!("Oh no: {}", y);
320322
return;
321323
}
322324
*x -= 1;
323-
println!("Oh no: {:?}", x);
325+
println!("Oh no: {}", x);
324326
}
325327
~~~
326328

@@ -335,11 +337,11 @@ fn main() {
335337
let y = &x;
336338
*x -= 1;
337339
338-
println!("Oh no: {:?}", y);
340+
println!("Oh no: {}", y);
339341
return;
340342
}
341343
*x -= 1;
342-
println!("Oh no: {:?}", x);
344+
println!("Oh no: {}", x);
343345
}
344346
~~~
345347

src/doc/guide-tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn fib(n: u64) -> u64 {
280280
281281
let mut delayed_fib = sync::Future::spawn(proc() fib(50));
282282
make_a_sandwich();
283-
println!("fib(50) = {:?}", delayed_fib.get())
283+
println!("fib(50) = {}", delayed_fib.get())
284284
# }
285285
~~~
286286

src/doc/tutorial.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,20 @@ will often see in examples, and its related family of macros: `print!`,
405405
that [printf][pf] has. Unlike printf, `format!` will give you a compile-time
406406
error when the types of the directives don't match the types of the arguments.
407407

408-
~~~~
409-
# let mystery_object = ();
410-
408+
~~~
411409
// `{}` will print the "default format" of a type
412410
println!("{} is {}", "the answer", 43);
411+
~~~
413412

414-
// `{:?}` will conveniently print any type
413+
~~~~
414+
extern crate debug;
415+
416+
# fn main() {
417+
# let mystery_object = ();
418+
// `{:?}` will conveniently print any type,
419+
// but requires the `debug` crate to be linked in
415420
println!("what is this thing: {:?}", mystery_object);
421+
# }
416422
~~~~
417423

418424
[pf]: http://en.cppreference.com/w/cpp/io/c/fprintf
@@ -698,8 +704,8 @@ When an enum has simple integer discriminators, you can apply the `as` cast
698704
operator to convert a variant to its discriminator value as an `int`:
699705

700706
~~~~
701-
# enum Direction { North }
702-
println!( "{:?} => {}", North, North as int );
707+
# #[deriving(Show)] enum Direction { North }
708+
println!( "{} => {}", North, North as int );
703709
~~~~
704710

705711
It is possible to set the discriminator values to chosen constant values:
@@ -2228,7 +2234,7 @@ method.
22282234
~~~~
22292235
# trait Printable { fn print(&self); }
22302236
impl Printable for int {
2231-
fn print(&self) { println!("{:?}", *self) }
2237+
fn print(&self) { println!("{}", *self) }
22322238
}
22332239
22342240
impl Printable for String {
@@ -2253,11 +2259,11 @@ types to be exactly as it is for `int`, above:
22532259
~~~~
22542260
# trait Printable { fn print(&self); }
22552261
impl Printable for f32 {
2256-
fn print(&self) { println!("{:?}", *self) }
2262+
fn print(&self) { println!("{}", *self) }
22572263
}
22582264
22592265
impl Printable for bool {
2260-
fn print(&self) { println!("{:?}", *self) }
2266+
fn print(&self) { println!("{}", *self) }
22612267
}
22622268
22632269
# true.print();
@@ -2270,8 +2276,11 @@ definition of `print` right in the trait definition, instead of just
22702276
giving its signature. That is, we can write the following:
22712277

22722278
~~~~
2279+
extern crate debug;
2280+
2281+
# fn main() {
22732282
trait Printable {
2274-
// Default method implementation
2283+
// Default method implementation
22752284
fn print(&self) { println!("{:?}", *self) }
22762285
}
22772286
@@ -2289,6 +2298,7 @@ impl Printable for f32 {}
22892298
# ("foo".to_string()).print();
22902299
# true.print();
22912300
# 3.14159.print();
2301+
# }
22922302
~~~~
22932303

22942304
Here, the impls of `Printable` for `int`, `bool`, and `f32` don't

src/liballoc/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@
7474
extern crate core;
7575
extern crate libc;
7676

77+
7778
// Allow testing this library
7879

80+
#[cfg(test)] extern crate debug;
7981
#[cfg(test)] extern crate sync;
8082
#[cfg(test)] extern crate native;
8183
#[cfg(test)] #[phase(syntax, link)] extern crate std;

src/libcollections/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![deny(deprecated_owned_vector)]
2626

2727
extern crate rand;
28+
extern crate debug;
2829

2930
#[cfg(test)] extern crate test;
3031
#[cfg(test)] #[phase(syntax, link)] extern crate log;

src/libcore/ops.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* demonstrates adding and subtracting two `Point`s.
2828
*
2929
* ```rust
30+
* #[deriving(Show)]
3031
* struct Point {
3132
* x: int,
3233
* y: int
@@ -44,8 +45,8 @@
4445
* }
4546
* }
4647
* fn main() {
47-
* println!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
48-
* println!("{:?}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
48+
* println!("{}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
49+
* println!("{}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
4950
* }
5051
* ```
5152
*

src/libcore/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ pub trait ImmutableVector<'a, T> {
419419
* ```rust
420420
* let v = &[1,2,3,4];
421421
* for win in v.windows(2) {
422-
* println!("{:?}", win);
422+
* println!("{}", win);
423423
* }
424424
* ```
425425
*
@@ -444,7 +444,7 @@ pub trait ImmutableVector<'a, T> {
444444
* ```rust
445445
* let v = &[1,2,3,4,5];
446446
* for win in v.chunks(2) {
447-
* println!("{:?}", win);
447+
* println!("{}", win);
448448
* }
449449
* ```
450450
*

src/libdebug/fmt.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Implementation of the `{:?}` format qualifier
12+
//!
13+
//! This module contains the `Poly` trait which is used to implement the `{:?}`
14+
//! format expression in formatting macros. This trait is defined for all types
15+
//! automatically, so it is likely not necessary to use this module manually
16+
17+
use std::fmt;
18+
19+
use repr;
20+
21+
/// Format trait for the `?` character
22+
pub trait Poly {
23+
/// Formats the value using the given formatter.
24+
#[experimental]
25+
fn fmt(&self, &mut fmt::Formatter) -> fmt::Result;
26+
}
27+
28+
#[doc(hidden)]
29+
pub fn secret_poly<T: Poly>(x: &T, fmt: &mut fmt::Formatter) -> fmt::Result {
30+
// FIXME #11938 - UFCS would make us able call the this method
31+
// directly Poly::fmt(x, fmt).
32+
x.fmt(fmt)
33+
}
34+
35+
impl<T> Poly for T {
36+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37+
match (f.width, f.precision) {
38+
(None, None) => {
39+
match repr::write_repr(f, self) {
40+
Ok(()) => Ok(()),
41+
Err(..) => Err(fmt::WriteError),
42+
}
43+
}
44+
45+
// If we have a specified width for formatting, then we have to make
46+
// this allocation of a new string
47+
_ => {
48+
let s = repr::repr_to_str(self);
49+
f.pad(s.as_slice())
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)