Skip to content

Commit 6a5bd8b

Browse files
committed
---
yaml --- r: 32628 b: refs/heads/dist-snap c: 864cca1 h: refs/heads/master v: v3
1 parent 4ad2806 commit 6a5bd8b

File tree

6 files changed

+183
-31
lines changed

6 files changed

+183
-31
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: 82e79f765ce81442f7dd3e2c878f055e95d2a34f
10+
refs/heads/dist-snap: 864cca14ee00082f67bbf92b60802195ab1c4d38
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/configure

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ do
505505
make_dir $h/test/perf
506506
make_dir $h/test/pretty
507507
make_dir $h/test/doc-tutorial
508+
make_dir $h/test/doc-tutorial-ffi
509+
make_dir $h/test/doc-tutorial-macros
510+
make_dir $h/test/doc-tutorial-borrowed-ptr
508511
make_dir $h/test/doc-ref
509512
done
510513

branches/dist-snap/doc/tutorial-borrowed-ptr.md

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ example, in this code, each of these three local variables contains a
4040
point, but allocated in a different place:
4141

4242
~~~
43+
# type point = {x: float, y: float};
4344
let on_the_stack : point = {x: 3.0, y: 4.0};
4445
let shared_box : @point = @{x: 5.0, y: 1.0};
4546
let unique_box : ~point = ~{x: 7.0, y: 9.0};
@@ -58,6 +59,8 @@ define a function that takes the points by pointer. We can use
5859
borrowed pointers to do this:
5960

6061
~~~
62+
# type point = {x: float, y: float};
63+
# fn sqrt(f: float) -> float { 0f }
6164
fn compute_distance(p1: &point, p2: &point) -> float {
6265
let x_d = p1.x - p2.x;
6366
let y_d = p1.y - p2.y;
@@ -67,7 +70,12 @@ fn compute_distance(p1: &point, p2: &point) -> float {
6770

6871
Now we can call `compute_distance()` in various ways:
6972

70-
~~~
73+
~~~ {.xfail-test}
74+
# type point = {x: float, y: float};
75+
# let on_the_stack : point = {x: 3.0, y: 4.0};
76+
# let shared_box : @point = @{x: 5.0, y: 1.0};
77+
# let unique_box : ~point = ~{x: 7.0, y: 9.0};
78+
# fn compute_distance(p1: &point, p2: &point) -> float { 0f }
7179
compute_distance(&on_the_stack, shared_box)
7280
compute_distance(shared_box, unique_box)
7381
~~~
@@ -100,6 +108,7 @@ it again.
100108
In the previous example, the value `on_the_stack` was defined like so:
101109

102110
~~~
111+
# type point = {x: float, y: float};
103112
let on_the_stack : point = {x: 3.0, y: 4.0};
104113
~~~
105114

@@ -109,13 +118,15 @@ pointer. Sometimes however it is more convenient to move the &
109118
operator into the definition of `on_the_stack`:
110119

111120
~~~
121+
# type point = {x: float, y: float};
112122
let on_the_stack2 : &point = &{x: 3.0, y: 4.0};
113123
~~~
114124

115125
Applying `&` to an rvalue (non-assignable location) is just a convenient
116126
shorthand for creating a temporary and taking its address:
117127

118128
~~~
129+
# type point = {x: float, y: float};
119130
let tmp = {x: 3.0, y: 4.0};
120131
let on_the_stack2 : &point = &tmp;
121132
~~~
@@ -144,7 +155,14 @@ let rect_unique = ~{origin: {x: 5, y: 6}, size: {w: 3, h: 4}};
144155
In each case I can use the `&` operator to extact out individual
145156
subcomponents. For example, I could write:
146157

147-
~~~
158+
~~~ {.xfail-test}
159+
# type point = {x: float, y: float};
160+
# type size = {w: float, h: float}; // as before
161+
# type rectangle = {origin: point, size: size};
162+
# let rect_stack = &{origin: {x: 1, y: 2}, size: {w: 3, h: 4}};
163+
# let rect_shared = @{origin: {x: 3, y: 4}, size: {w: 3, h: 4}};
164+
# let rect_unique = ~{origin: {x: 5, y: 6}, size: {w: 3, h: 4}};
165+
# fn compute_distance(p1: &point, p2: &point) -> float { 0f }
148166
compute_distance(&rect_stack.origin, &rect_shared.origin);
149167
~~~
150168

@@ -238,14 +256,16 @@ mean that the unique box is stored in immutable memory. For example,
238256
the following function is legal:
239257

240258
~~~
259+
# fn some_condition() -> bool { true }
241260
fn example3() -> int {
242261
let mut x = ~{f: 3};
243-
if some_condition {
262+
if some_condition() {
244263
let y = &x.f; // -+ L
245-
ret *y; // |
264+
return *y; // |
246265
} // -+
247266
x = ~{f: 4};
248267
...
268+
# return 0;
249269
}
250270
~~~
251271

@@ -261,7 +281,7 @@ _as soon as their owning reference is changed or goes out of
261281
scope_. Therefore, a program like this is illegal (and would be
262282
rejected by the compiler):
263283

264-
~~~
284+
~~~ {.xfail-test}
265285
fn example3() -> int {
266286
let mut x = ~{f: 3};
267287
let y = &x.f;
@@ -308,7 +328,7 @@ frame_. So we could modify the previous example to introduce
308328
additional unique pointers and records, and the compiler will still be
309329
able to detect possible mutations:
310330

311-
~~~
331+
~~~ {.xfail-test}
312332
fn example3() -> int {
313333
let mut x = ~{mut f: ~{g: 3}};
314334
let y = &x.f.g;
@@ -326,8 +346,8 @@ Things get tricker when the unique box is not uniquely owned by the
326346
stack frame (or when the compiler doesn’t know who the owner
327347
is). Consider a program like this:
328348

329-
~~~
330-
fn example5a(x: @{mut f: ~{g: int}}, ...) -> int {
349+
~~~ {.xfail-test}
350+
fn example5a(x: @{mut f: ~{g: int}} ...) -> int {
331351
let y = &x.f.g; // Error reported here.
332352
...
333353
}
@@ -359,9 +379,10 @@ unique found in aliasable memory is to ensure that it is stored within
359379
unique fields, as in the following example:
360380

361381
~~~
362-
fn example5b(x: @{f: ~{g: int}}, ...) -> int {
382+
fn example5b(x: @{f: ~{g: int}}) -> int {
363383
let y = &x.f.g;
364384
...
385+
# return 0;
365386
}
366387
~~~
367388

@@ -373,13 +394,15 @@ If you do have a unique box in a mutable field, and you wish to borrow
373394
it, one option is to use the swap operator to bring that unique box
374395
onto your stack:
375396

376-
~~~
377-
fn example5c(x: @{mut f: ~int}, ...) -> int {
397+
~~~ {.xfail-test}
398+
fn example5c(x: @{mut f: ~int}) -> int {
378399
let mut v = ~0;
379400
v <-> x.f; // Swap v and x.f
380401
let y = &v;
381402
...
382403
x.f <- v; // Replace x.f
404+
...
405+
# return 0;
383406
}
384407
~~~
385408

@@ -412,8 +435,15 @@ function takes a borrowed pointer to a shape to avoid the need of
412435
copying them.
413436

414437
~~~
438+
# type point = {x: float, y: float}; // as before
439+
# type size = {w: float, h: float}; // as before
440+
# enum shape {
441+
# circle(point, float), // origin, radius
442+
# rectangle(point, size) // upper-left, dimensions
443+
# }
444+
# const tau: float = 6.28f;
415445
fn compute_area(shape: &shape) -> float {
416-
alt *shape {
446+
match *shape {
417447
circle(_, radius) => 0.5 * tau * radius * radius,
418448
rectangle(_, ref size) => size.w * size.h
419449
}
@@ -502,7 +532,7 @@ but as we’ll see this is more limited.
502532

503533
For example, we could write a subroutine like this:
504534

505-
~~~
535+
~~~ {.xfail-test}
506536
type point = {x: float, y: float};
507537
fn get_x(p: &point) -> &float { &p.x }
508538
~~~
@@ -535,7 +565,7 @@ the compiler is satisfied with the function `get_x()`.
535565
To drill in this point, let’s look at a variation on the example, this
536566
time one which does not compile:
537567

538-
~~~
568+
~~~ {.xfail-test}
539569
type point = {x: float, y: float};
540570
fn get_x_sh(p: @point) -> &float {
541571
&p.x // Error reported here
@@ -574,7 +604,14 @@ pointer. However, sometimes if a function takes many parameters, it is
574604
useful to be able to group those parameters by lifetime. For example,
575605
consider this function:
576606

577-
~~~
607+
~~~ {.xfail-test}
608+
# type point = {x: float, y: float}; // as before
609+
# type size = {w: float, h: float}; // as before
610+
# enum shape {
611+
# circle(point, float), // origin, radius
612+
# rectangle(point, size) // upper-left, dimensions
613+
# }
614+
# fn compute_area(shape: &shape) -> float { 0f }
578615
fn select<T>(shape: &shape, threshold: float,
579616
a: &T, b: &T) -> &T {
580617
if compute_area(shape) > threshold {a} else {b}
@@ -588,7 +625,19 @@ lifetime of the returned value will be the intersection of the
588625
lifetime of the three region parameters. This may be overloy
589626
conservative, as in this example:
590627

591-
~~~
628+
~~~ {.xfail-test}
629+
# type point = {x: float, y: float}; // as before
630+
# type size = {w: float, h: float}; // as before
631+
# enum shape {
632+
# circle(point, float), // origin, radius
633+
# rectangle(point, size) // upper-left, dimensions
634+
# }
635+
# fn compute_area(shape: &shape) -> float { 0f }
636+
# fn select<T>(shape: &shape, threshold: float,
637+
# a: &T, b: &T) -> &T {
638+
# if compute_area(shape) > threshold {a} else {b}
639+
# }
640+
592641
// -+ L
593642
fn select_based_on_unit_circle<T>( // |-+ B
594643
threshold: float, a: &T, b: &T) -> &T { // | |
@@ -618,7 +667,14 @@ second lifetime parameter for the function; named lifetime parameters
618667
do not need to be declared, you just use them. Here is how the new
619668
`select()` might look:
620669

621-
~~~
670+
~~~ {.xfail-test}
671+
# type point = {x: float, y: float}; // as before
672+
# type size = {w: float, h: float}; // as before
673+
# enum shape {
674+
# circle(point, float), // origin, radius
675+
# rectangle(point, size) // upper-left, dimensions
676+
# }
677+
# fn compute_area(shape: &shape) -> float { 0f }
622678
fn select<T>(shape: &tmp/shape, threshold: float,
623679
a: &T, b: &T) -> &T {
624680
if compute_area(shape) > threshold {a} else {b}
@@ -632,7 +688,14 @@ lifetime parameter.
632688
You could also write `select()` using all named lifetime parameters,
633689
which might look like:
634690

635-
~~~
691+
~~~ {.xfail-test}
692+
# type point = {x: float, y: float}; // as before
693+
# type size = {w: float, h: float}; // as before
694+
# enum shape {
695+
# circle(point, float), // origin, radius
696+
# rectangle(point, size) // upper-left, dimensions
697+
# }
698+
# fn compute_area(shape: &shape) -> float { 0f }
636699
fn select<T>(shape: &tmp/shape, threshold: float,
637700
a: &r/T, b: &r/T) -> &r/T {
638701
if compute_area(shape) > threshold {a} else {b}
@@ -658,7 +721,7 @@ a unique box found in an aliasable, mutable location, only now we’ve
658721
replaced the `...` with some specific code:
659722

660723
~~~
661-
fn example5a(x: @{mut f: ~{g: int}}, ...) -> int {
724+
fn example5a(x: @{mut f: ~{g: int}} ...) -> int {
662725
let y = &x.f.g; // Unsafe
663726
*y + 1
664727
}
@@ -676,8 +739,9 @@ fn add_one(x: &int) -> int { *x + 1 }
676739

677740
We can now update `example5a()` to use `add_one()`:
678741

679-
~~~
680-
fn example5a(x: @{mut f: ~{g: int}}, ...) -> int {
742+
~~~ {.xfail-test}
743+
# fn add_one(x: &int) -> int { *x + 1 }
744+
fn example5a(x: @{mut f: ~{g: int}} ...) -> int {
681745
let y = &x.f.g;
682746
add_one(y) // Error reported here
683747
}

branches/dist-snap/doc/tutorial-ffi.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fn as_hex(data: ~[u8]) -> ~str {
2828
2929
fn sha1(data: ~str) -> ~str unsafe {
3030
let bytes = str::to_bytes(data);
31-
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
31+
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
3232
vec::len(bytes) as c_uint, ptr::null());
33-
return as_hex(vec::unsafe::from_buf(hash, 20u));
33+
return as_hex(vec::raw::from_buf(hash, 20u));
3434
}
3535
3636
fn main(args: ~[~str]) {
@@ -128,9 +128,9 @@ The `sha1` function is the most obscure part of the program.
128128
fn sha1(data: ~str) -> ~str {
129129
unsafe {
130130
let bytes = str::to_bytes(data);
131-
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
131+
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
132132
vec::len(bytes), ptr::null());
133-
return as_hex(vec::unsafe::from_buf(hash, 20u));
133+
return as_hex(vec::raw::from_buf(hash, 20u));
134134
}
135135
}
136136
~~~~
@@ -171,15 +171,15 @@ Let's look at our `sha1` function again.
171171
# fn x(data: ~str) -> ~str {
172172
# unsafe {
173173
let bytes = str::to_bytes(data);
174-
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
174+
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
175175
vec::len(bytes), ptr::null());
176-
return as_hex(vec::unsafe::from_buf(hash, 20u));
176+
return as_hex(vec::raw::from_buf(hash, 20u));
177177
# }
178178
# }
179179
~~~~
180180

181181
The `str::to_bytes` function is perfectly safe: it converts a string to
182-
a `[u8]`. This byte array is then fed to `vec::unsafe::to_ptr`, which
182+
a `[u8]`. This byte array is then fed to `vec::raw::to_ptr`, which
183183
returns an unsafe pointer to its contents.
184184

185185
This pointer will become invalid as soon as the vector it points into
@@ -193,7 +193,7 @@ unsafe null pointer of the correct type (Rust generics are awesome
193193
like that—they can take the right form depending on the type that they
194194
are expected to return).
195195

196-
Finally, `vec::unsafe::from_buf` builds up a new `[u8]` from the
196+
Finally, `vec::raw::from_buf` builds up a new `[u8]` from the
197197
unsafe pointer that was returned by `SHA1`. SHA1 digests are always
198198
twenty bytes long, so we can pass `20u` for the length of the new
199199
vector.

0 commit comments

Comments
 (0)