Skip to content

Commit c38a65b

Browse files
committed
---
yaml --- r: 127467 b: refs/heads/try c: 50ffe0c h: refs/heads/master i: 127465: bb83e60 127463: 943ae72 v: v3
1 parent f9efa37 commit c38a65b

File tree

15 files changed

+190
-10
lines changed

15 files changed

+190
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: c4a63fabe345967e7d6b4570752ea081cae21785
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 12e0f72f517516ac4fce2aed85e6142e9b874bce
5-
refs/heads/try: 86decf638e95f93b60f2325263005c60e7d3183e
5+
refs/heads/try: 50ffe0ccabd9b67ded2116e6b428dfbfedce9f23
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/doc/guide.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,94 @@ guide](http://doc.rust-lang.org/guide-pointers.html#rc-and-arc).
36143614

36153615
# Patterns
36163616

3617+
# Method Syntax
3618+
3619+
Functions are great, but if you want to call a bunch of them on some data, it
3620+
can be awkward. Consider this code:
3621+
3622+
```{rust,ignore}
3623+
baz(bar(foo(x)));
3624+
```
3625+
3626+
We would read this left-to right, and so we see 'baz bar foo.' But this isn't the
3627+
order that the functions would get called in, that's inside-out: 'foo bar baz.'
3628+
Wouldn't it be nice if we could do this instead?
3629+
3630+
```{rust,ignore}
3631+
x.foo().bar().baz();
3632+
```
3633+
3634+
Luckily, as you may have guessed with the leading question, you can! Rust provides
3635+
the ability to use this **method call syntax** via the `impl` keyword.
3636+
3637+
Here's how it works:
3638+
3639+
```
3640+
struct Circle {
3641+
x: f64,
3642+
y: f64,
3643+
radius: f64,
3644+
}
3645+
3646+
impl Circle {
3647+
fn area(&self) -> f64 {
3648+
std::f64::consts::PI * (self.radius * self.radius)
3649+
}
3650+
}
3651+
3652+
fn main() {
3653+
let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
3654+
println!("{}", c.area());
3655+
}
3656+
```
3657+
3658+
This will print `12.566371`.
3659+
3660+
We've made a struct that represents a circle. We then write an `impl` block,
3661+
and inside it, define a method, `area`. Methods take a special first
3662+
parameter, `&self`. There are three variants: `self`, `&self`, and `&mut self`.
3663+
You can think of this first parameter as being the `x` in `x.foo()`. The three
3664+
variants correspond to the three kinds of thing `x` could be: `self` if it's
3665+
just a value on the stack, `&self` if it's a reference, and `&mut self` if it's
3666+
a mutable reference. We should default to using `&self`, as it's the most
3667+
common.
3668+
3669+
Finally, as you may remember, the value of the area of a circle is `π*r²`.
3670+
Because we took the `&self` parameter to `area`, we can use it just like any
3671+
other parameter. Because we know it's a `Circle`, we can access the `radius`
3672+
just like we would with any other struct. An import of π and some
3673+
multiplications later, and we have our area.
3674+
3675+
You can also define methods that do not take a `self` parameter. Here's a
3676+
pattern that's very common in Rust code:
3677+
3678+
```
3679+
struct Circle {
3680+
x: f64,
3681+
y: f64,
3682+
radius: f64,
3683+
}
3684+
3685+
impl Circle {
3686+
fn new(x: f64, y: f64, radius: f64) -> Circle {
3687+
Circle {
3688+
x: x,
3689+
y: y,
3690+
radius: radius,
3691+
}
3692+
}
3693+
}
3694+
3695+
fn main() {
3696+
let c = Circle::new(0.0, 0.0, 2.0);
3697+
}
3698+
```
3699+
3700+
This **static method** builds a new `Circle` for us. Note that static methods
3701+
are called with the `Struct::method()` syntax, rather than the `ref.method()`
3702+
syntax.
3703+
3704+
36173705
# Closures
36183706

36193707
So far, we've made lots of functions in Rust. But we've given them all names.

branches/try/src/libcore/char.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use mem::transmute;
1919
use option::{None, Option, Some};
2020
use iter::range_step;
2121

22+
#[cfg(stage0)]
23+
use iter::Iterator; // NOTE(stage0): Remove after snapshot.
24+
2225
// UTF-8 ranges and tags for encoding characters
2326
static TAG_CONT: u8 = 0b1000_0000u8;
2427
static TAG_TWO_B: u8 = 0b1100_0000u8;

branches/try/src/libcore/failure.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@
3333
use fmt;
3434
use intrinsics;
3535

36+
#[cfg(stage0)]
37+
#[cold] #[inline(never)] // this is the slow path, always
38+
#[lang="fail_"]
39+
fn fail_(expr: &'static str, file: &'static str, line: uint) -> ! {
40+
format_args!(|args| -> () {
41+
begin_unwind(args, &(file, line));
42+
}, "{}", expr);
43+
44+
unsafe { intrinsics::abort() }
45+
}
46+
47+
#[cfg(stage0)]
48+
#[cold]
49+
#[lang="fail_bounds_check"]
50+
fn fail_bounds_check(file: &'static str, line: uint,
51+
index: uint, len: uint) -> ! {
52+
format_args!(|args| -> () {
53+
begin_unwind(args, &(file, line));
54+
}, "index out of bounds: the len is {} but the index is {}", len, index);
55+
unsafe { intrinsics::abort() }
56+
}
57+
58+
#[cfg(not(stage0))]
3659
#[cold] #[inline(never)] // this is the slow path, always
3760
#[lang="fail_"]
3861
fn fail_(expr_file_line: &(&'static str, &'static str, uint)) -> ! {
@@ -45,6 +68,7 @@ fn fail_(expr_file_line: &(&'static str, &'static str, uint)) -> ! {
4568
unsafe { intrinsics::abort() }
4669
}
4770

71+
#[cfg(not(stage0))]
4872
#[cold] #[inline(never)]
4973
#[lang="fail_bounds_check"]
5074
fn fail_bounds_check(file_line: &(&'static str, uint),

branches/try/src/libcore/fmt/float.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ use slice::{ImmutableVector, MutableVector};
2121
use slice;
2222
use str::StrSlice;
2323

24+
#[cfg(stage0)]
25+
use iter::Iterator; // NOTE(stage0): Remove after snapshot.
26+
#[cfg(stage0)]
27+
use option::{Some, None}; // NOTE(stage0): Remove after snapshot.
28+
2429
/// A flag that specifies whether to use exponential (scientific) notation.
2530
pub enum ExponentFormat {
2631
/// Do not use exponential notation.

branches/try/src/libcore/fmt/num.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ use iter::DoubleEndedIterator;
2020
use num::{Int, cast, zero};
2121
use slice::{ImmutableVector, MutableVector};
2222

23+
#[cfg(stage0)]
24+
use iter::Iterator; // NOTE(stage0): Remove after snapshot.
25+
#[cfg(stage0)]
26+
use option::{Some, None}; // NOTE(stage0): Remove after snapshot.
27+
2328
/// A type that represents a specific radix
2429
#[doc(hidden)]
2530
trait GenericRadix {

branches/try/src/libcore/ops.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ pub trait FnOnce<Args,Result> {
771771

772772
macro_rules! def_fn_mut(
773773
($($args:ident)*) => (
774+
#[cfg(not(stage0))]
774775
impl<Result$(,$args)*>
775776
FnMut<($($args,)*),Result>
776777
for extern "Rust" fn($($args: $args,)*) -> Result {

branches/try/src/libcore/ptr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ use option::{Some, None, Option};
9595

9696
use cmp::{PartialEq, Eq, PartialOrd, Equiv, Ordering, Less, Equal, Greater};
9797

98+
#[cfg(stage0)]
99+
use iter::Iterator; // NOTE(stage0): Remove after snapshot.
100+
98101
pub use intrinsics::copy_memory;
99102
pub use intrinsics::copy_nonoverlapping_memory;
100103
pub use intrinsics::set_memory;

branches/try/src/libcore/str.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,9 @@ pub mod traits {
10301030
use option::{Option, Some};
10311031
use str::{Str, StrSlice, eq_slice};
10321032

1033+
#[cfg(stage0)]
1034+
use option::None; // NOTE(stage0): Remove after snapshot.
1035+
10331036
impl<'a> Ord for &'a str {
10341037
#[inline]
10351038
fn cmp(&self, other: & &'a str) -> Ordering {

branches/try/src/librustrt/unwind.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,22 @@ pub fn begin_unwind_fmt(msg: &fmt::Arguments, file_line: &(&'static str, uint))
523523
}
524524

525525
/// This is the entry point of unwinding for fail!() and assert!().
526+
#[cfg(stage0)]
527+
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
528+
pub fn begin_unwind<M: Any + Send>(msg: M, file: &'static str, line: uint) -> ! {
529+
// Note that this should be the only allocation performed in this code path.
530+
// Currently this means that fail!() on OOM will invoke this code path,
531+
// but then again we're not really ready for failing on OOM anyway. If
532+
// we do start doing this, then we should propagate this allocation to
533+
// be performed in the parent of this task instead of the task that's
534+
// failing.
535+
536+
// see below for why we do the `Any` coercion here.
537+
begin_unwind_inner(box msg, &(file, line))
538+
}
539+
540+
/// This is the entry point of unwinding for fail!() and assert!().
541+
#[cfg(not(stage0))]
526542
#[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible
527543
pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, uint)) -> ! {
528544
// Note that this should be the only allocation performed in this code path.

branches/try/src/libstd/io/tempfile.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use path::{Path, GenericPath};
2121
use result::{Ok, Err};
2222
use sync::atomic;
2323

24+
#[cfg(stage0)]
25+
use iter::Iterator; // NOTE(stage0): Remove after snapshot.
26+
2427
/// A wrapper for a path to temporary directory implementing automatic
2528
/// scope-based deletion.
2629
pub struct TempDir {

branches/try/src/libstd/macros.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
/// fail!("this is a {} {message}", "fancy", message = "message");
3838
/// ```
3939
#[macro_export]
40+
#[cfg(not(stage0))]
4041
macro_rules! fail(
4142
() => ({
4243
fail!("explicit failure")
@@ -67,6 +68,39 @@ macro_rules! fail(
6768
});
6869
)
6970

71+
#[macro_export]
72+
#[cfg(stage0)]
73+
macro_rules! fail(
74+
() => ({
75+
fail!("explicit failure")
76+
});
77+
($msg:expr) => ({
78+
// static requires less code at runtime, more constant data
79+
static FILE_LINE: (&'static str, uint) = (file!(), line!());
80+
let (file, line) = FILE_LINE;
81+
::std::rt::begin_unwind($msg, file, line)
82+
});
83+
($fmt:expr, $($arg:tt)*) => ({
84+
// a closure can't have return type !, so we need a full
85+
// function to pass to format_args!, *and* we need the
86+
// file and line numbers right here; so an inner bare fn
87+
// is our only choice.
88+
//
89+
// LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
90+
// is #[cold] and #[inline(never)] and because this is flagged as cold
91+
// as returning !. We really do want this to be inlined, however,
92+
// because it's just a tiny wrapper. Small wins (156K to 149K in size)
93+
// were seen when forcing this to be inlined, and that number just goes
94+
// up with the number of calls to fail!()
95+
#[inline(always)]
96+
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
97+
static FILE_LINE: (&'static str, uint) = (file!(), line!());
98+
::std::rt::begin_unwind_fmt(fmt, &FILE_LINE)
99+
}
100+
format_args!(run_fmt, $fmt, $($arg)*)
101+
});
102+
)
103+
70104
/// Ensure that a boolean expression is `true` at runtime.
71105
///
72106
/// This will invoke the `fail!` macro if the provided expression cannot be

branches/try/src/libstd/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Useful synchronization primitives
1212
//!
13-
//! This module contains useful safe and unsafe synchronization primitives.
13+
//! This modules contains useful safe and unsafe synchronization primitives.
1414
//! Most of the primitives in this module do not provide any sort of locking
1515
//! and/or blocking at all, but rather provide the necessary tools to build
1616
//! other types of concurrent primitives.

branches/try/src/libunicode/normalize.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub fn decompose_canonical(c: char, i: |char|) { d(c, i, false); }
3939
pub fn decompose_compatible(c: char, i: |char|) { d(c, i, true); }
4040

4141
fn d(c: char, i: |char|, k: bool) {
42+
#[cfg(stage0)]
43+
use core::iter::Iterator;
44+
4245
// 7-bit ASCII never decomposes
4346
if c <= '\x7f' { i(c); return; }
4447

branches/try/src/snapshots.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
S 2014-08-07 12e0f72
2-
freebsd-x86_64 e55055a876ebbde0d3ed3bcb97579afab9264def
3-
linux-i386 2665e45879f2ef77ce0c9015f971642fe424ac33
4-
linux-x86_64 51ed1f4cf0707585a136bb149a443394067c074c
5-
macos-i386 78f1996954a6e0718d684a3756b4870a6f8771ee
6-
macos-x86_64 216f46f65866207a9f41c3ed654f5c1e085cb7f3
7-
winnt-i386 95a9b8a8bf587761ae954392aee2ccee3758a533
8-
91
S 2014-07-17 9fc8394
102
freebsd-x86_64 5a4b645e2b42ae06224cc679d4a43b3d89be1482
113
linux-i386 a5e1bb723020ac35173d49600e76b0935e257a6a

0 commit comments

Comments
 (0)