Skip to content

Commit ef6daf9

Browse files
committed
auto merge of #13958 : pcwalton/rust/detilde, r=pcwalton
for `~str`/`~[]`. Note that `~self` still remains, since I forgot to add support for `Box<self>` before the snapshot. r? @brson or @alexcrichton or whoever
2 parents 4a5d390 + 090040b commit ef6daf9

File tree

495 files changed

+2247
-1892
lines changed

Some content is hidden

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

495 files changed

+2247
-1892
lines changed

src/doc/guide-ffi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ extern {
285285
286286
fn main() {
287287
// Create the object that will be referenced in the callback
288-
let mut rust_object = ~RustObject{ a: 5 };
288+
let mut rust_object = box RustObject { a: 5 };
289289
290290
unsafe {
291291
register_callback(&mut *rust_object, callback);

src/doc/guide-lifetimes.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ point, but allocated in a different place:
4141

4242
~~~
4343
# struct Point {x: f64, y: f64}
44-
let on_the_stack : Point = Point {x: 3.0, y: 4.0};
45-
let managed_box : @Point = @Point {x: 5.0, y: 1.0};
46-
let owned_box : ~Point = ~Point {x: 7.0, y: 9.0};
44+
let on_the_stack : Point = Point {x: 3.0, y: 4.0};
45+
let managed_box : @Point = @Point {x: 5.0, y: 1.0};
46+
let owned_box : Box<Point> = box Point {x: 7.0, y: 9.0};
4747
~~~
4848

4949
Suppose we wanted to write a procedure that computed the distance between any
@@ -72,9 +72,9 @@ Now we can call `compute_distance()` in various ways:
7272

7373
~~~
7474
# struct Point {x: f64, y: f64}
75-
# let on_the_stack : Point = Point{x: 3.0, y: 4.0};
76-
# let managed_box : @Point = @Point{x: 5.0, y: 1.0};
77-
# let owned_box : ~Point = ~Point{x: 7.0, y: 9.0};
75+
# let on_the_stack : Point = Point{x: 3.0, y: 4.0};
76+
# let managed_box : @Point = @Point{x: 5.0, y: 1.0};
77+
# let owned_box : Box<Point> = box Point{x: 7.0, y: 9.0};
7878
# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
7979
compute_distance(&on_the_stack, managed_box);
8080
compute_distance(managed_box, owned_box);
@@ -151,12 +151,12 @@ Now, as before, we can define rectangles in a few different ways:
151151
# struct Point {x: f64, y: f64}
152152
# struct Size {w: f64, h: f64} // as before
153153
# struct Rectangle {origin: Point, size: Size}
154-
let rect_stack = &Rectangle {origin: Point {x: 1.0, y: 2.0},
155-
size: Size {w: 3.0, h: 4.0}};
156-
let rect_managed = @Rectangle {origin: Point {x: 3.0, y: 4.0},
157-
size: Size {w: 3.0, h: 4.0}};
158-
let rect_owned = ~Rectangle {origin: Point {x: 5.0, y: 6.0},
159-
size: Size {w: 3.0, h: 4.0}};
154+
let rect_stack = &Rectangle {origin: Point {x: 1.0, y: 2.0},
155+
size: Size {w: 3.0, h: 4.0}};
156+
let rect_managed = @Rectangle {origin: Point {x: 3.0, y: 4.0},
157+
size: Size {w: 3.0, h: 4.0}};
158+
let rect_owned = box Rectangle {origin: Point {x: 5.0, y: 6.0},
159+
size: Size {w: 3.0, h: 4.0}};
160160
~~~
161161

162162
In each case, we can extract out individual subcomponents with the `&`
@@ -168,7 +168,7 @@ operator. For example, I could write:
168168
# struct Rectangle {origin: Point, size: Size}
169169
# let rect_stack = &Rectangle {origin: Point {x: 1.0, y: 2.0}, size: Size {w: 3.0, h: 4.0}};
170170
# let rect_managed = @Rectangle {origin: Point {x: 3.0, y: 4.0}, size: Size {w: 3.0, h: 4.0}};
171-
# let rect_owned = ~Rectangle {origin: Point {x: 5.0, y: 6.0}, size: Size {w: 3.0, h: 4.0}};
171+
# let rect_owned = box Rectangle {origin: Point {x: 5.0, y: 6.0}, size: Size {w: 3.0, h: 4.0}};
172172
# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
173173
compute_distance(&rect_stack.origin, &rect_managed.origin);
174174
~~~
@@ -276,12 +276,12 @@ the following function is legal:
276276
# fn some_condition() -> bool { true }
277277
# struct Foo { f: int }
278278
fn example3() -> int {
279-
let mut x = ~Foo {f: 3};
279+
let mut x = box Foo {f: 3};
280280
if some_condition() {
281281
let y = &x.f; // -+ L
282282
return *y; // |
283283
} // -+
284-
x = ~Foo {f: 4};
284+
x = box Foo {f: 4};
285285
// ...
286286
# return 0;
287287
}
@@ -301,9 +301,9 @@ rejected by the compiler):
301301

302302
~~~ {.ignore}
303303
fn example3() -> int {
304-
let mut x = ~X {f: 3};
304+
let mut x = box X {f: 3};
305305
let y = &x.f;
306-
x = ~X {f: 4}; // Error reported here.
306+
x = box X {f: 4}; // Error reported here.
307307
*y
308308
}
309309
~~~
@@ -314,27 +314,27 @@ memory immediately before the re-assignment of `x`:
314314
~~~ {.notrust}
315315
Stack Exchange Heap
316316
317-
x +----------+
318-
| ~{f:int} | ----+
319-
y +----------+ |
320-
| &int | ----+
321-
+----------+ | +---------+
322-
+--> | f: 3 |
323-
+---------+
317+
x +-------------+
318+
| box {f:int} | ----+
319+
y +-------------+ |
320+
| &int | ----+
321+
+-------------+ | +---------+
322+
+--> | f: 3 |
323+
+---------+
324324
~~~
325325

326326
Once the reassignment occurs, the memory will look like this:
327327

328328
~~~ {.notrust}
329329
Stack Exchange Heap
330330
331-
x +----------+ +---------+
332-
| ~{f:int} | -------> | f: 4 |
333-
y +----------+ +---------+
334-
| &int | ----+
335-
+----------+ | +---------+
336-
+--> | (freed) |
337-
+---------+
331+
x +-------------+ +---------+
332+
| box {f:int} | -------> | f: 4 |
333+
y +-------------+ +---------+
334+
| &int | ----+
335+
+-------------+ | +---------+
336+
+--> | (freed) |
337+
+---------+
338338
~~~
339339

340340
Here you can see that the variable `y` still points at the old box,
@@ -349,12 +349,12 @@ mutations:
349349
~~~ {.ignore}
350350
fn example3() -> int {
351351
struct R { g: int }
352-
struct S { f: ~R }
352+
struct S { f: Box<R> }
353353
354-
let mut x = ~S {f: ~R {g: 3}};
354+
let mut x = box S {f: box R {g: 3}};
355355
let y = &x.f.g;
356-
x = ~S {f: ~R {g: 4}}; // Error reported here.
357-
x.f = ~R {g: 5}; // Error reported here.
356+
x = box S {f: box R {g: 4}}; // Error reported here.
357+
x.f = box R {g: 5}; // Error reported here.
358358
*y
359359
}
360360
~~~

src/doc/guide-pointers.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,22 @@ trait. Therefore, unboxed traits don't make any sense, and aren't allowed.
182182
Sometimes, you need a recursive data structure. The simplest is known as a 'cons list':
183183

184184
~~~rust
185+
185186
enum List<T> {
186187
Nil,
187-
Cons(T, ~List<T>),
188+
Cons(T, Box<List<T>>),
188189
}
189190

190191
fn main() {
191-
let list: List<int> = Cons(1, ~Cons(2, ~Cons(3, ~Nil)));
192+
let list: List<int> = Cons(1, box Cons(2, box Cons(3, box Nil)));
192193
println!("{:?}", list);
193194
}
194195
~~~
195196

196197
This prints:
197198

198199
~~~ {.notrust}
199-
Cons(1, ~Cons(2, ~Cons(3, ~Nil)))
200+
Cons(1, box Cons(2, box Cons(3, box Nil)))
200201
~~~
201202

202203
The inner lists _must_ be an owned pointer, because we can't know how many
@@ -237,7 +238,7 @@ struct Point {
237238
}
238239

239240
fn main() {
240-
let a = ~Point { x: 10, y: 20 };
241+
let a = box Point { x: 10, y: 20 };
241242
spawn(proc() {
242243
println!("{}", a.x);
243244
});
@@ -268,7 +269,7 @@ struct Point {
268269
}
269270
270271
fn main() {
271-
let a = ~Point { x: 10, y: 20 };
272+
let a = box Point { x: 10, y: 20 };
272273
let b = a;
273274
println!("{}", b.x);
274275
println!("{}", a.x);
@@ -285,7 +286,7 @@ note: in expansion of format_args!
285286
<std-macros>:158:27: 158:81 note: expansion site
286287
<std-macros>:157:5: 159:6 note: in expansion of println!
287288
test.rs:10:5: 10:25 note: expansion site
288-
test.rs:8:9: 8:10 note: `a` moved here because it has type `~Point`, which is moved by default (use `ref` to override)
289+
test.rs:8:9: 8:10 note: `a` moved here because it has type `Box<Point>`, which is moved by default (use `ref` to override)
289290
test.rs:8 let b = a;
290291
^
291292
~~~
@@ -345,8 +346,8 @@ fn compute_distance(p1: &Point, p2: &Point) -> f32 {
345346
}
346347

347348
fn main() {
348-
let origin = @Point { x: 0.0, y: 0.0 };
349-
let p1 = ~Point { x: 5.0, y: 3.0 };
349+
let origin = @Point { x: 0.0, y: 0.0 };
350+
let p1 = box Point { x: 5.0, y: 3.0 };
350351

351352
println!("{:?}", compute_distance(origin, p1));
352353
}
@@ -381,7 +382,7 @@ duration a 'lifetime'. Let's try a more complex example:
381382

382383
~~~rust
383384
fn main() {
384-
let mut x = ~5;
385+
let mut x = box 5;
385386
if *x < 10 {
386387
let y = &x;
387388
println!("Oh no: {:?}", y);
@@ -398,7 +399,7 @@ mutated, and therefore, lets us pass. This wouldn't work:
398399

399400
~~~rust{.ignore}
400401
fn main() {
401-
let mut x = ~5;
402+
let mut x = box 5;
402403
if *x < 10 {
403404
let y = &x;
404405
*x -= 1;
@@ -437,39 +438,39 @@ is best.
437438
What does that mean? Don't do this:
438439

439440
~~~rust
440-
fn foo(x: ~int) -> ~int {
441-
return ~*x;
441+
fn foo(x: Box<int>) -> Box<int> {
442+
return box *x;
442443
}
443444

444445
fn main() {
445-
let x = ~5;
446+
let x = box 5;
446447
let y = foo(x);
447448
}
448449
~~~
449450

450451
Do this:
451452

452453
~~~rust
453-
fn foo(x: ~int) -> int {
454+
fn foo(x: Box<int>) -> int {
454455
return *x;
455456
}
456457

457458
fn main() {
458-
let x = ~5;
459-
let y = ~foo(x);
459+
let x = box 5;
460+
let y = box foo(x);
460461
}
461462
~~~
462463

463464
This gives you flexibility, without sacrificing performance. For example, this will
464465
also work:
465466

466467
~~~rust
467-
fn foo(x: ~int) -> int {
468+
fn foo(x: Box<int>) -> int {
468469
return *x;
469470
}
470471

471472
fn main() {
472-
let x = ~5;
473+
let x = box 5;
473474
let y = @foo(x);
474475
}
475476
~~~

src/doc/guide-unsafe.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ impl<T: Send> Drop for Unique<T> {
258258
}
259259
}
260260
261-
// A comparison between the built-in ~ and this reimplementation
261+
// A comparison between the built-in `Box` and this reimplementation
262262
fn main() {
263263
{
264-
let mut x = ~5;
264+
let mut x = box 5;
265265
*x = 10;
266266
} // `x` is freed here
267267

src/doc/intro.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,13 @@ That's a great example for stack memory,
127127
but what about heap memory?
128128
Rust has a second kind of pointer,
129129
an 'owned box',
130-
that you can create with a `~`.
130+
that you can create with the `box` operator.
131131
Check it out:
132132

133133
```
134-
fn dangling() -> ~int {
135-
let i = ~1234;
134+
135+
fn dangling() -> Box<int> {
136+
let i = box 1234;
136137
return i;
137138
}
138139
@@ -143,15 +144,15 @@ fn add_one() -> int {
143144
```
144145

145146
Now instead of a stack allocated `1234`,
146-
we have a heap allocated `~1234`.
147+
we have a heap allocated `box 1234`.
147148
Whereas `&` borrows a pointer to existing memory,
148149
creating an owned box allocates memory on the heap and places a value in it,
149150
giving you the sole pointer to that memory.
150151
You can roughly compare these two lines:
151152

152153
```
153154
// Rust
154-
let i = ~1234;
155+
let i = box 1234;
155156
```
156157

157158
```notrust

0 commit comments

Comments
 (0)