Skip to content

Commit 6fc5b68

Browse files
author
Jakub Wieczorek
committed
---
yaml --- r: 138164 b: refs/heads/try c: c2e8f3b h: refs/heads/master v: v3
1 parent 6c4c549 commit 6fc5b68

File tree

19 files changed

+290
-410
lines changed

19 files changed

+290
-410
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: b6e0d3a5bf4c88650a22f605f822e02c6b163580
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
5-
refs/heads/try: e4761c85b52ac9953a7ac1f89d9e09a79389b4fd
5+
refs/heads/try: c2e8f3b481fd41f801c3e79d43861e8e86cd1a44
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: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,102 +1496,87 @@ low-level details matter, they really matter. Just remember that `String`s
14961496
allocate memory and control their data, while `&str`s are a reference to
14971497
another string, and you'll be all set.
14981498

1499-
# Arrays, Vectors, and Slices
1499+
# Vectors
15001500

1501-
Like many programming languages, Rust has list types to represent a sequence of
1502-
things. The most basic is the **array**, a fixed-size list of elements of the
1503-
same type. By default, arrays are immutable.
1501+
Like many programming languages, Rust has a list type for when you want a list
1502+
of things. But similar to strings, Rust has different types to represent this
1503+
idea: `Vec<T>` (a 'vector'), `[T, .. N]` (an 'array'), and `&[T]` (a 'slice').
1504+
Whew!
15041505

1505-
```{rust}
1506-
let a = [1i, 2i, 3i];
1507-
let mut m = [1i, 2i, 3i];
1508-
```
1509-
1510-
You can create an array with a given number of elements, all initialized to the
1511-
same value, with `[val, ..N]` syntax. The compiler ensures that arrays are
1512-
always initialized.
1506+
Vectors are similar to `String`s: they have a dynamic length, and they
1507+
allocate enough memory to fit. You can create a vector with the `vec!` macro:
15131508

15141509
```{rust}
1515-
let a = [0i, ..20]; // Shorthand for array of 20 elements all initialized to 0
1510+
let nums = vec![1i, 2i, 3i];
15161511
```
15171512

1518-
Arrays have type `[T,..N]`. We'll talk about this `T` notation later, when we
1519-
cover generics.
1513+
Notice that unlike the `println!` macro we've used in the past, we use square
1514+
brackets (`[]`) with `vec!`. Rust allows you to use either in either situation,
1515+
this is just convention.
15201516

1521-
You can get the number of elements in an array `a` with `a.len()`, and use
1522-
`a.iter()` to iterate over them with a for loop. This code will print each
1523-
number in order:
1517+
You can create an array with just square brackets:
15241518

15251519
```{rust}
1526-
let a = [1i, 2, 3]; // Only the first item needs a type suffix
1527-
1528-
println!("a has {} elements", a.len());
1529-
for e in a.iter() {
1530-
println!("{}", e);
1531-
}
1520+
let nums = [1i, 2i, 3i];
1521+
let nums = [1i, ..20]; // Shorthand for an array of 20 elements all initialized to 1
15321522
```
15331523

1534-
You can access a particular element of an array with **subscript notation**:
1524+
So what's the difference? An array has a fixed size, so you can't add or
1525+
subtract elements:
15351526

1536-
```{rust}
1537-
let names = ["Graydon", "Brian", "Niko"];
1527+
```{rust,ignore}
1528+
let mut nums = vec![1i, 2i, 3i];
1529+
nums.push(4i); // works
15381530
1539-
println!("The second name is: {}", names[1]);
1531+
let mut nums = [1i, 2i, 3i];
1532+
nums.push(4i); // error: type `[int, .. 3]` does not implement any method
1533+
// in scope named `push`
15401534
```
15411535

1542-
Subscripts start at zero, like in most programming languages, so the first name
1543-
is `names[0]` and the second name is `names[1]`. The above example prints
1544-
`The second name is: Brian`. If you try to use a subscript that is not in the
1545-
array, you will get an error: array access is bounds-checked at run-time. Such
1546-
errant access is the source of many bugs in other systems programming
1547-
languages.
1536+
The `push()` method lets you append a value to the end of the vector. But
1537+
since arrays have fixed sizes, adding an element doesn't make any sense.
1538+
You can see how it has the exact type in the error message: `[int, .. 3]`.
1539+
An array of `int`s, with length 3.
15481540

1549-
A **vector** is a dynamic or "growable" array, implemented as the standard
1550-
library type [`Vec<T>`](std/vec/) (we'll talk about what the `<T>` means
1551-
later). Vectors are to arrays what `String` is to `&str`. You can create them
1552-
with the `vec!` macro:
1541+
Similar to `&str`, a slice is a reference to another array. We can get a
1542+
slice from a vector by using the `as_slice()` method:
15531543

15541544
```{rust}
1555-
let v = vec![1i, 2, 3];
1545+
let vec = vec![1i, 2i, 3i];
1546+
let slice = vec.as_slice();
15561547
```
15571548

1558-
(Notice that unlike the `println!` macro we've used in the past, we use square
1559-
brackets `[]` with `vec!`. Rust allows you to use either in either situation,
1560-
this is just convention.)
1561-
1562-
You can get the length of, iterate over, and subscript vectors just like
1563-
arrays. In addition, (mutable) vectors can grow automatically:
1549+
All three types implement an `iter()` method, which returns an iterator. We'll
1550+
talk more about the details of iterators later, but for now, the `iter()` method
1551+
allows you to write a `for` loop that prints out the contents of a vector, array,
1552+
or slice:
15641553

15651554
```{rust}
1566-
let mut nums = vec![1i, 2, 3];
1567-
nums.push(4);
1568-
println!("The length of nums is now {}", nums.len()); // Prints 4
1555+
let vec = vec![1i, 2i, 3i];
1556+
1557+
for i in vec.iter() {
1558+
println!("{}", i);
1559+
}
15691560
```
15701561

1571-
Vectors have many more useful methods.
1562+
This code will print each number in order, on its own line.
15721563

1573-
A **slice** is a reference to (or "view" into) an array. They are useful for
1574-
allowing safe, efficient access to a portion of an array without copying. For
1575-
example, you might want to reference just one line of a file read into memory.
1576-
By nature, a slice is not created directly, but from an existing variable.
1577-
Slices have a length, can be mutable or not, and in many ways behave like
1578-
arrays:
1564+
You can access a particular element of a vector, array, or slice by using
1565+
**subscript notation**:
15791566

15801567
```{rust}
1581-
let a = [0i, 1, 2, 3, 4];
1582-
let middle = a.slice(1, 4); // A slice of a: just the elements [1,2,3]
1568+
let names = ["Graydon", "Brian", "Niko"];
15831569
1584-
for e in middle.iter() {
1585-
println!("{}", e); // Prints 1, 2, 3
1586-
}
1570+
println!("The second name is: {}", names[1]);
15871571
```
15881572

1589-
You can also take a slice of a vector, `String`, or `&str`, because they are
1590-
backed by arrays. Slices have type `&[T]`, which we'll talk about when we cover
1591-
generics.
1573+
These subscripts start at zero, like in most programming languages, so the
1574+
first name is `names[0]` and the second name is `names[1]`. The above example
1575+
prints `The second name is: Brian`.
15921576

1593-
We have now learned all of the most basic Rust concepts. We're ready to start
1594-
building our guessing game, we just need to know one last thing: how to get
1577+
There's a whole lot more to vectors, but that's enough to get started. We have
1578+
now learned all of the most basic Rust concepts. We're ready to start building
1579+
our guessing game, but we need to know how to do one last thing first: get
15951580
input from the keyboard. You can't have a guessing game without the ability to
15961581
guess!
15971582

branches/try/src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub mod middle {
9797
pub mod intrinsicck;
9898
pub mod lang_items;
9999
pub mod liveness;
100+
pub mod macros;
100101
pub mod mem_categorization;
101102
pub mod pat_util;
102103
pub mod privacy;

branches/try/src/librustc/metadata/encoder.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,13 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext,
407407
method_did.def_id());
408408
match impl_item {
409409
ty::MethodTraitItem(ref m) => {
410-
encode_reexported_static_method(rbml_w,
411-
exp,
412-
m.def_id,
413-
m.ident);
410+
if m.explicit_self ==
411+
ty::StaticExplicitSelfCategory {
412+
encode_reexported_static_method(rbml_w,
413+
exp,
414+
m.def_id,
415+
m.ident);
416+
}
414417
}
415418
ty::TypeTraitItem(_) => {}
416419
}
@@ -431,7 +434,8 @@ fn encode_reexported_static_trait_methods(ecx: &EncodeContext,
431434
Some(trait_items) => {
432435
for trait_item in trait_items.iter() {
433436
match *trait_item {
434-
ty::MethodTraitItem(ref m) => {
437+
ty::MethodTraitItem(ref m) if m.explicit_self ==
438+
ty::StaticExplicitSelfCategory => {
435439
encode_reexported_static_method(rbml_w,
436440
exp,
437441
m.def_id,
@@ -1404,16 +1408,18 @@ fn encode_info_for_item(ecx: &EncodeContext,
14041408
encode_family(rbml_w,
14051409
fn_style_static_method_family(
14061410
method_ty.fty.fn_style));
1411+
1412+
let pty = ty::lookup_item_type(tcx,
1413+
method_def_id);
1414+
encode_bounds_and_type(rbml_w, ecx, &pty);
14071415
}
1416+
14081417
_ => {
14091418
encode_family(rbml_w,
14101419
style_fn_family(
14111420
method_ty.fty.fn_style));
14121421
}
14131422
}
1414-
let pty = ty::lookup_item_type(tcx,
1415-
method_def_id);
1416-
encode_bounds_and_type(rbml_w, ecx, &pty);
14171423

14181424
is_nonstatic_method = method_ty.explicit_self !=
14191425
ty::StaticExplicitSelfCategory;

branches/try/src/librustc/middle/astencode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ impl tr for def::Def {
453453
},
454454
p)
455455
}
456-
def::DefMethod(did0, did1, p) => {
457-
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)), p)
456+
def::DefMethod(did0, did1) => {
457+
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)))
458458
}
459459
def::DefSelfTy(nid) => { def::DefSelfTy(dcx.tr_id(nid)) }
460460
def::DefMod(did) => { def::DefMod(did.tr(dcx)) }

branches/try/src/librustc/middle/def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum Def {
4646
DefTyParamBinder(ast::NodeId), /* struct, impl or trait with ty params */
4747
DefRegion(ast::NodeId),
4848
DefLabel(ast::NodeId),
49-
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
49+
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */),
5050
}
5151

5252
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
@@ -62,7 +62,7 @@ impl Def {
6262
DefForeignMod(id) | DefStatic(id, _) |
6363
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(id) |
6464
DefTyParam(_, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
65-
DefMethod(id, _, _) | DefConst(id) => {
65+
DefMethod(id, _) | DefConst(id) => {
6666
id
6767
}
6868
DefLocal(id) |
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
#![macro_escape]
12+
13+
macro_rules! memoize_expand_block(
14+
($cache_map:expr, $cache_key:expr, $($param_name:ident: $param_ty:ty),*) => { {
15+
match ($cache_map).borrow().find(&$cache_key) {
16+
Some(ref result) => return (*result).clone(),
17+
None => {}
18+
}
19+
let result = inner($($param_name), *);
20+
($cache_map).borrow_mut().insert($cache_key, result.clone());
21+
result
22+
} }
23+
)
24+
25+
/// Memoizes a function using a cache that is available by evaluating the
26+
/// `$cache_map` exression in the context of the function's arguments.
27+
/// `$cache_key` is the expression that will be used to compute the cache key
28+
/// for each function invocation.
29+
///
30+
/// The macro assumes the cache to be a RefCell containing a HashMap,
31+
/// which is in practice how most caching in rustc is currently carried out.
32+
///
33+
/// # Example
34+
///
35+
/// ```
36+
/// struct Context {
37+
/// fibonacci_cache: RefCell<HashMap<uint, uint>>
38+
/// }
39+
///
40+
/// memoize!(context.fibonacci_cache, n,
41+
/// fn fibonacci(context: &Context, n: uint) -> uint {
42+
/// match n {
43+
/// 0 | 1 => n,
44+
/// _ => fibonacci(n - 2) + fibonacci(n - 1)
45+
/// }
46+
/// }
47+
/// )
48+
/// ```
49+
macro_rules! memoize(
50+
($cache_map:expr, $cache_key:expr,
51+
fn $name:ident(
52+
$($param_name:ident: $param_ty:ty),*
53+
) -> $output_ty:ty $block:block
54+
) => {
55+
fn $name($($param_name: $param_ty), *) -> $output_ty {
56+
fn inner($($param_name: $param_ty), *) -> $output_ty $block
57+
memoize_expand_block!($cache_map, $cache_key, $($param_name: $param_ty), *)
58+
}
59+
};
60+
61+
($cache_map:expr, $cache_key:expr,
62+
pub fn $name:ident(
63+
$($param_name:ident: $param_ty:ty),*
64+
) -> $output_ty:ty $block:block
65+
) => {
66+
pub fn $name($($param_name: $param_ty), *) -> $output_ty {
67+
fn inner($($param_name: $param_ty), *) -> $output_ty $block
68+
memoize_expand_block!($cache_map, $cache_key, $($param_name: $param_ty), *)
69+
}
70+
}
71+
)

branches/try/src/librustc/middle/privacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
811811
def::DefTy(_, true) => ck("enum"),
812812
def::DefTrait(..) => ck("trait"),
813813
def::DefStruct(..) => ck("struct"),
814-
def::DefMethod(_, Some(..), _) => ck("trait method"),
814+
def::DefMethod(_, Some(..)) => ck("trait method"),
815815
def::DefMethod(..) => ck("method"),
816816
def::DefMod(..) => ck("module"),
817817
_ => {}

0 commit comments

Comments
 (0)