Skip to content

Commit 8dbbd7e

Browse files
committed
---
yaml --- r: 82751 b: refs/heads/auto c: 5d24a1a h: refs/heads/master i: 82749: c34af30 82747: d829e4e 82743: 2d79766 82735: 6810e2b 82719: 9657ce4 82687: e6af581 v: v3
1 parent 1c70998 commit 8dbbd7e

File tree

8 files changed

+136
-128
lines changed

8 files changed

+136
-128
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 6c8e6aad7344a62d91d5cf10e0dd1769602a5257
16+
refs/heads/auto: 5d24a1ae45189b8222ce84084eaae3ff8b7d67d0
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/doc/tutorial.md

Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ fn area(sh: Shape) -> float {
737737
match sh {
738738
Circle { radius: radius, _ } => float::consts::pi * square(radius),
739739
Rectangle { top_left: top_left, bottom_right: bottom_right } => {
740-
(bottom_right.x - top_left.x) * (bottom_right.y - top_left.y)
740+
(bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
741741
}
742742
}
743743
}
@@ -923,60 +923,22 @@ custom destructors.
923923

924924
# Boxes
925925

926-
A value in Rust is stored directly inside the owner. If a `struct` contains
927-
four `int` fields, it will be four times as large as a single `int`. The
928-
following `struct` type is invalid, as it would have an infinite size:
926+
Many modern languages represent values as pointers to heap memory by
927+
default. In contrast, Rust, like C and C++, represents such types directly.
928+
Another way to say this is that aggregate data in Rust are *unboxed*. This
929+
means that if you `let x = Point { x: 1f, y: 1f };`, you are creating a struct
930+
on the stack. If you then copy it into a data structure, you copy the entire
931+
struct, not just a pointer.
929932

930-
~~~~ {.xfail-test}
931-
struct List {
932-
next: Option<List>,
933-
data: int
934-
}
935-
~~~~
936-
937-
> ***Note:*** The `Option` type is an enum representing an *optional* value.
938-
> It's comparable to a nullable pointer in many other languages, but stores the
939-
> contained value unboxed.
940-
941-
An *owned box* (`~`) uses a heap allocation to provide the invariant of always
942-
being the size of a pointer, regardless of the contained type. This can be
943-
leveraged to create a valid recursive `struct` type with a finite size:
944-
945-
~~~~
946-
struct List {
947-
next: Option<~List>,
948-
data: int
949-
}
950-
~~~~
951-
952-
Since an owned box has a single owner, they are limited to representing
953-
tree-like data structures.
954-
955-
The most common use case for owned boxes is creating recursive data structures
956-
like a binary search tree. Rust's trait-based generics system (covered later in
957-
the tutorial) is usually used for static dispatch, but also provides dynamic
958-
dispatch via boxing. Values of different types may have different sizes, but a
959-
box is able to *erase* the difference via the layer of indirection they
960-
provide.
933+
For small structs like `Point`, this is usually more efficient than allocating
934+
memory and indirecting through a pointer. But for big structs, or mutable
935+
state, it can be useful to have a single copy on the stack or on the heap, and
936+
refer to that through a pointer.
961937

962-
In uncommon cases, the indirection can provide a performance gain or memory
963-
reduction by making values smaller. However, unboxed values should almost
964-
always be preferred.
938+
## Owned boxes
965939

966-
Note that returning large unboxed values via boxes is unnecessary. A large
967-
value is returned via a hidden output parameter, and the decision on where to
968-
place the return value should be left to the caller:
969-
970-
~~~~
971-
fn foo() -> (int, int, int, int, int, int) {
972-
(5, 5, 5, 5, 5, 5)
973-
}
974-
975-
let x = ~foo(); // allocates, and writes the integers directly to it
976-
~~~~
977-
978-
Beyond the properties granted by the size, an owned box behaves as a regular
979-
value by inheriting the mutability and lifetime of the owner:
940+
An owned box (`~`) is a uniquely owned allocation on the heap. It inherits the
941+
mutability and lifetime of the owner as it would if there was no box:
980942

981943
~~~~
982944
let x = 5; // immutable
@@ -988,33 +950,35 @@ let mut y = ~5; // mutable
988950
*y += 2; // the * operator is needed to access the contained value
989951
~~~~
990952

991-
As covered earlier, an owned box has a destructor to clean up the allocated
992-
memory. This makes it more restricted than an unboxed type with no destructor
993-
by introducing *move semantics*.
953+
The purpose of an owned box is to add a layer of indirection in order to create
954+
recursive data structures or cheaply pass around an object larger than a
955+
pointer. Since an owned box has a unique owner, it can only be used to
956+
represent a tree data structure.
994957

995-
# Move semantics
958+
The following struct won't compile, because the lack of indirection would mean
959+
it has an infinite size:
996960

997-
Rust uses a shallow copy for parameter passing, assignment and returning from
998-
functions. This is considered a move of ownership for types with destructors.
999-
After a value has been moved, it can no longer be used from the source location
1000-
and will not be destroyed when the source goes out of scope.
1001-
1002-
~~~~
1003-
let x = ~5;
1004-
let y = x.clone(); // y is a newly allocated box
1005-
let z = x; // no new memory allocated, x can no longer be used
961+
~~~~ {.xfail-test}
962+
struct Foo {
963+
child: Option<Foo>
964+
}
1006965
~~~~
1007966

1008-
The mutability of a value may be changed by moving it to a new owner:
967+
> ***Note:*** The `Option` type is an enum that represents an *optional* value.
968+
> It's comparable to a nullable pointer in many other languages, but stores the
969+
> contained value unboxed.
970+
971+
Adding indirection with an owned pointer allocates the child outside of the
972+
struct on the heap, which makes it a finite size and won't result in a
973+
compile-time error:
1009974

1010975
~~~~
1011-
let r = ~13;
1012-
let mut s = r; // box becomes mutable
1013-
*s += 1;
1014-
let t = s; // box becomes immutable
976+
struct Foo {
977+
child: Option<~Foo>
978+
}
1015979
~~~~
1016980

1017-
# Managed boxes
981+
## Managed boxes
1018982

1019983
A managed box (`@`) is a heap allocation with the lifetime managed by a
1020984
task-local garbage collector. It will be destroyed at some point after there
@@ -1059,6 +1023,30 @@ d = b; // box type is the same, okay
10591023
c = b; // error
10601024
~~~~
10611025

1026+
# Move semantics
1027+
1028+
Rust uses a shallow copy for parameter passing, assignment and returning values
1029+
from functions. A shallow copy is considered a move of ownership if the
1030+
ownership tree of the copied value includes an owned box or a type with a
1031+
custom destructor. After a value has been moved, it can no longer be used from
1032+
the source location and will not be destroyed there.
1033+
1034+
~~~~
1035+
let x = ~5;
1036+
let y = x.clone(); // y is a newly allocated box
1037+
let z = x; // no new memory allocated, x can no longer be used
1038+
~~~~
1039+
1040+
Since in owned boxes mutability is a property of the owner, not the
1041+
box, mutable boxes may become immutable when they are moved, and vice-versa.
1042+
1043+
~~~~
1044+
let r = ~13;
1045+
let mut s = r; // box becomes mutable
1046+
*s += 1;
1047+
let t = s; // box becomes immutable
1048+
~~~~
1049+
10621050
# Borrowed pointers
10631051

10641052
Rust's borrowed pointers are a general purpose reference type. In contrast with

branches/auto/src/etc/emacs/rust-mode-tests.el

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,35 @@ This is some more text. Fee fie fo fum. Humpty dumpty sat on a wall.
196196
*very very very long string
197197
*/"))
198198

199+
(ert-deftest fill-paragraph-single-line-style-with-code-before ()
200+
(test-fill-paragraph
201+
"fn foo() { }
202+
/// This is my comment. This is more of my comment. This is even more."
203+
"fn foo() { }
204+
/// This is my comment. This is
205+
/// more of my comment. This is
206+
/// even more." 14))
207+
208+
(ert-deftest fill-paragraph-single-line-style-with-code-after ()
209+
(test-fill-paragraph
210+
"/// This is my comment. This is more of my comment. This is even more.
211+
fn foo() { }"
212+
"/// This is my comment. This is
213+
/// more of my comment. This is
214+
/// even more.
215+
fn foo() { }" 1 73))
216+
217+
(ert-deftest fill-paragraph-single-line-style-code-before-and-after ()
218+
(test-fill-paragraph
219+
"fn foo() { }
220+
/// This is my comment. This is more of my comment. This is even more.
221+
fn bar() { }"
222+
"fn foo() { }
223+
/// This is my comment. This is
224+
/// more of my comment. This is
225+
/// even more.
226+
fn bar() { }" 14 67))
227+
199228
(defun test-auto-fill (initial position inserted expected)
200229
(rust-test-manip-code
201230
initial

branches/auto/src/etc/emacs/rust-mode.el

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@
300300
(let
301301
((fill-paragraph-function
302302
(if (not (eq fill-paragraph-function 'rust-fill-paragraph))
303-
fill-paragraph-function)))
303+
fill-paragraph-function))
304+
(fill-paragraph-handle-comment t))
304305
(apply 'fill-paragraph args)
305306
t))))))
306307

branches/auto/src/librustc/middle/check_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,12 @@ pub fn specialize(cx: &MatchCheckCtxt,
773773
let num_elements = before.len() + after.len();
774774
if num_elements < arity && slice.is_some() {
775775
Some(vec::append(
776-
vec::concat(&[
776+
[
777777
before,
778778
vec::from_elem(
779779
arity - num_elements, wild()),
780780
after
781-
]),
781+
].concat_vec(),
782782
r.tail()
783783
))
784784
} else if num_elements == arity {

branches/auto/src/librustdoc/clean.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ impl Clean<Item> for doctree::Module {
173173
visibility: self.vis.clean(),
174174
id: self.id,
175175
inner: ModuleItem(Module {
176-
items: std::vec::concat(&[self.structs.clean(),
177-
self.enums.clean(), self.fns.clean(),
178-
std::vec::concat(self.foreigns.clean()),
179-
self.mods.clean(), self.typedefs.clean(),
180-
self.statics.clean(), self.traits.clean(),
181-
self.impls.clean(), self.view_items.clean()])
176+
items: [self.structs.clean(), self.enums.clean(),
177+
self.fns.clean(), self.foreigns.clean().concat_vec(),
178+
self.mods.clean(), self.typedefs.clean(),
179+
self.statics.clean(), self.traits.clean(),
180+
self.impls.clean(), self.view_items.clean()].concat_vec()
182181
})
183182
}
184183
}

branches/auto/src/libstd/rt/sched.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,6 @@ mod test {
12801280
// FIXME: #9407: xfail-test
12811281
fn dont_starve_1() {
12821282
use rt::comm::oneshot;
1283-
use unstable::running_on_valgrind;
12841283

12851284
do stress_factor().times {
12861285
do run_in_mt_newsched_task {

0 commit comments

Comments
 (0)