Skip to content

Commit 28ad27e

Browse files
author
Lenny222
committed
---
yaml --- r: 61341 b: refs/heads/try c: 017df98 h: refs/heads/master i: 61339: 1163bcd v: v3
1 parent df2297f commit 28ad27e

File tree

21 files changed

+182
-477
lines changed

21 files changed

+182
-477
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: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2d28d645422c1617be58c8ca7ad9a457264ca850
5-
refs/heads/try: cda3ac905a56dc6580429eea259143d30a7f3c02
5+
refs/heads/try: 017df987b8f7877f5be04bc137ea830a9c82343e
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/doc/tutorial-borrowed-ptr.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ point, but allocated in a different place:
4242
~~~
4343
# struct Point {x: float, y: float}
4444
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};
45+
let shared_box : @Point = @Point {x: 5.0, y: 1.0};
46+
let unique_box : ~Point = ~Point {x: 7.0, y: 9.0};
4747
~~~
4848

4949
Suppose we wanted to write a procedure that computed the distance between any
5050
two points, no matter where they were stored. For example, we might like to
51-
compute the distance between `on_the_stack` and `managed_box`, or between
52-
`managed_box` and `owned_box`. One option is to define a function that takes
51+
compute the distance between `on_the_stack` and `shared_box`, or between
52+
`shared_box` and `unique_box`. One option is to define a function that takes
5353
two arguments of type `Point`—that is, it takes the points by value. But if we
5454
define it this way, calling the function will cause the points to be
5555
copied. For points, this is probably not so bad, but often copies are
@@ -73,11 +73,11 @@ Now we can call `compute_distance()` in various ways:
7373
~~~
7474
# struct Point {x: float, y: float}
7575
# 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};
76+
# let shared_box : @Point = @Point{x: 5.0, y: 1.0};
77+
# let unique_box : ~Point = ~Point{x: 7.0, y: 9.0};
7878
# fn compute_distance(p1: &Point, p2: &Point) -> float { 0f }
79-
compute_distance(&on_the_stack, managed_box);
80-
compute_distance(managed_box, owned_box);
79+
compute_distance(&on_the_stack, shared_box);
80+
compute_distance(shared_box, unique_box);
8181
~~~
8282

8383
Here, the `&` operator takes the address of the variable
@@ -87,11 +87,11 @@ value. We also call this _borrowing_ the local variable
8787
`on_the_stack`, because we have created an alias: that is, another
8888
name for the same data.
8989

90-
In contrast, we can pass the boxes `managed_box` and `owned_box` to
90+
In contrast, we can pass the boxes `shared_box` and `unique_box` to
9191
`compute_distance` directly. The compiler automatically converts a box like
9292
`@Point` or `~Point` to a borrowed pointer like `&Point`. This is another form
93-
of borrowing: in this case, the caller lends the contents of the managed or
94-
owned box to the callee.
93+
of borrowing: in this case, the caller lends the contents of the shared or
94+
unique box to the callee.
9595

9696
Whenever a caller lends data to a callee, there are some limitations on what
9797
the caller can do with the original. For example, if the contents of a
@@ -155,7 +155,7 @@ let rect_stack = &Rectangle {origin: Point {x: 1f, y: 2f},
155155
size: Size {w: 3f, h: 4f}};
156156
let rect_managed = @Rectangle {origin: Point {x: 3f, y: 4f},
157157
size: Size {w: 3f, h: 4f}};
158-
let rect_owned = ~Rectangle {origin: Point {x: 5f, y: 6f},
158+
let rect_unique = ~Rectangle {origin: Point {x: 5f, y: 6f},
159159
size: Size {w: 3f, h: 4f}};
160160
~~~
161161

@@ -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: 1f, y: 2f}, size: Size {w: 3f, h: 4f}};
170170
# let rect_managed = @Rectangle {origin: Point {x: 3f, y: 4f}, size: Size {w: 3f, h: 4f}};
171-
# let rect_owned = ~Rectangle {origin: Point {x: 5f, y: 6f}, size: Size {w: 3f, h: 4f}};
171+
# let rect_unique = ~Rectangle {origin: Point {x: 5f, y: 6f}, size: Size {w: 3f, h: 4f}};
172172
# fn compute_distance(p1: &Point, p2: &Point) -> float { 0f }
173173
compute_distance(&rect_stack.origin, &rect_managed.origin);
174174
~~~
@@ -179,7 +179,7 @@ as well as from the managed box, and then compute the distance between them.
179179
# Borrowing managed boxes and rooting
180180

181181
We’ve seen a few examples so far of borrowing heap boxes, both managed
182-
and owned. Up till this point, we’ve glossed over issues of
182+
and unique. Up till this point, we’ve glossed over issues of
183183
safety. As stated in the introduction, at runtime a borrowed pointer
184184
is simply a pointer, nothing more. Therefore, avoiding C's problems
185185
with dangling pointers requires a compile-time safety check.
@@ -258,18 +258,18 @@ fn example2() {
258258
Now if `x` is reassigned, the pointer `y` will still remain valid. This
259259
process is called *rooting*.
260260

261-
# Borrowing owned boxes
261+
# Borrowing unique boxes
262262

263263
The previous example demonstrated *rooting*, the process by which the
264264
compiler ensures that managed boxes remain live for the duration of a
265-
borrow. Unfortunately, rooting does not work for borrows of owned
266-
boxes, because it is not possible to have two references to a owned
265+
borrow. Unfortunately, rooting does not work for borrows of unique
266+
boxes, because it is not possible to have two references to a unique
267267
box.
268268

269-
For owned boxes, therefore, the compiler will only allow a borrow *if
270-
the compiler can guarantee that the owned box will not be reassigned
269+
For unique boxes, therefore, the compiler will only allow a borrow *if
270+
the compiler can guarantee that the unique box will not be reassigned
271271
or moved for the lifetime of the pointer*. This does not necessarily
272-
mean that the owned box is stored in immutable memory. For example,
272+
mean that the unique box is stored in immutable memory. For example,
273273
the following function is legal:
274274

275275
~~~
@@ -294,7 +294,7 @@ and `x` is declared as mutable. However, the compiler can prove that
294294
and in fact is mutated later in the function.
295295

296296
It may not be clear why we are so concerned about mutating a borrowed
297-
variable. The reason is that the runtime system frees any owned box
297+
variable. The reason is that the runtime system frees any unique box
298298
_as soon as its owning reference changes or goes out of
299299
scope_. Therefore, a program like this is illegal (and would be
300300
rejected by the compiler):
@@ -342,7 +342,7 @@ which has been freed.
342342

343343
In fact, the compiler can apply the same kind of reasoning to any
344344
memory that is _(uniquely) owned by the stack frame_. So we could
345-
modify the previous example to introduce additional owned pointers
345+
modify the previous example to introduce additional unique pointers
346346
and structs, and the compiler will still be able to detect possible
347347
mutations:
348348

@@ -366,7 +366,7 @@ invalidate the pointer `y`.
366366
# Borrowing and enums
367367

368368
The previous example showed that the type system forbids any borrowing
369-
of owned boxes found in aliasable, mutable memory. This restriction
369+
of unique boxes found in aliasable, mutable memory. This restriction
370370
prevents pointers from pointing into freed memory. There is one other
371371
case where the compiler must be very careful to ensure that pointers
372372
remain valid: pointers into the interior of an `enum`.
@@ -462,14 +462,14 @@ of a `float` as if it were a struct with two fields would be a memory
462462
safety violation.
463463

464464
So, in fact, for every `ref` binding, the compiler will impose the
465-
same rules as the ones we saw for borrowing the interior of a owned
465+
same rules as the ones we saw for borrowing the interior of a unique
466466
box: it must be able to guarantee that the `enum` will not be
467467
overwritten for the duration of the borrow. In fact, the compiler
468468
would accept the example we gave earlier. The example is safe because
469469
the shape pointer has type `&Shape`, which means "borrowed pointer to
470470
immutable memory containing a `shape`". If, however, the type of that
471471
pointer were `&mut Shape`, then the ref binding would be ill-typed.
472-
Just as with owned boxes, the compiler will permit `ref` bindings
472+
Just as with unique boxes, the compiler will permit `ref` bindings
473473
into data owned by the stack frame even if the data are mutable,
474474
but otherwise it requires that the data reside in immutable memory.
475475

@@ -550,7 +550,7 @@ guarantees; in fact, it cannot guarantee that the pointer will remain
550550
valid at all once it returns, as the parameter `p` may or may not be
551551
live in the caller. Therefore, the compiler will report an error here.
552552

553-
In general, if you borrow a managed (or owned) box to create a
553+
In general, if you borrow a managed (or unique) box to create a
554554
borrowed pointer, the pointer will only be valid within the function
555555
and cannot be returned. This is why the typical way to return borrowed
556556
pointers is to take borrowed pointers as input (the only other case in

branches/try/doc/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ to a borrowed pointer.
17791779
# fn draw_value(self) { ... }
17801780
# }
17811781
# let s = Circle(Point { x: 1f, y: 2f }, 3f);
1782-
// As with typical function arguments, managed and owned pointers
1782+
// As with typical function arguments, managed and unique pointers
17831783
// are automatically converted to borrowed pointers
17841784
17851785
(@s).draw_borrowed();

branches/try/src/libcore/cell.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,10 @@ Similar to a mutable option type, but friendlier.
2121
*/
2222

2323
#[mutable]
24-
#[deriving(Clone)]
2524
pub struct Cell<T> {
2625
priv value: Option<T>
2726
}
2827

29-
impl<T: DeepClone> DeepClone for Cell<T> {
30-
fn deep_clone(&self) -> Cell<T> {
31-
Cell{value: self.value.deep_clone()}
32-
}
33-
}
34-
3528
impl<T:cmp::Eq> cmp::Eq for Cell<T> {
3629
fn eq(&self, other: &Cell<T>) -> bool {
3730
(self.value) == (other.value)

branches/try/src/libcore/clone.rs

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ by convention implementing the `Clone` trait and calling the
2323
*/
2424

2525
pub trait Clone {
26-
/// Return a deep copy of the owned object tree. Types with shared ownership like managed boxes
27-
/// are cloned with a shallow copy.
26+
/// Return a deep copy of the owned object tree. Managed boxes are cloned with a shallow copy.
2827
fn clone(&self) -> Self;
2928
}
3029

31-
impl<T: Clone> Clone for ~T {
30+
impl Clone for () {
31+
/// Return a copy of the value.
32+
#[inline(always)]
33+
fn clone(&self) -> () { () }
34+
}
35+
36+
impl<T:Clone> Clone for ~T {
3237
/// Return a deep copy of the owned box.
3338
#[inline(always)]
3439
fn clone(&self) -> ~T { ~(**self).clone() }
@@ -49,7 +54,7 @@ impl<T> Clone for @mut T {
4954
macro_rules! clone_impl(
5055
($t:ty) => {
5156
impl Clone for $t {
52-
/// Return a deep copy of the value.
57+
/// Return a copy of the value.
5358
#[inline(always)]
5459
fn clone(&self) -> $t { *self }
5560
}
@@ -72,53 +77,9 @@ clone_impl!(float)
7277
clone_impl!(f32)
7378
clone_impl!(f64)
7479

75-
clone_impl!(())
7680
clone_impl!(bool)
7781
clone_impl!(char)
7882

79-
pub trait DeepClone {
80-
/// Return a deep copy of the object tree. Types with shared ownership are also copied via a
81-
/// deep copy, unlike `Clone`. Note that this is currently unimplemented for managed boxes, as
82-
/// it would need to handle cycles.
83-
fn deep_clone(&self) -> Self;
84-
}
85-
86-
macro_rules! deep_clone_impl(
87-
($t:ty) => {
88-
impl DeepClone for $t {
89-
/// Return a deep copy of the value.
90-
#[inline(always)]
91-
fn deep_clone(&self) -> $t { *self }
92-
}
93-
}
94-
)
95-
96-
impl<T: DeepClone> DeepClone for ~T {
97-
/// Return a deep copy of the owned box.
98-
#[inline(always)]
99-
fn deep_clone(&self) -> ~T { ~(**self).deep_clone() }
100-
}
101-
102-
deep_clone_impl!(int)
103-
deep_clone_impl!(i8)
104-
deep_clone_impl!(i16)
105-
deep_clone_impl!(i32)
106-
deep_clone_impl!(i64)
107-
108-
deep_clone_impl!(uint)
109-
deep_clone_impl!(u8)
110-
deep_clone_impl!(u16)
111-
deep_clone_impl!(u32)
112-
deep_clone_impl!(u64)
113-
114-
deep_clone_impl!(float)
115-
deep_clone_impl!(f32)
116-
deep_clone_impl!(f64)
117-
118-
deep_clone_impl!(())
119-
deep_clone_impl!(bool)
120-
deep_clone_impl!(char)
121-
12283
#[test]
12384
fn test_owned_clone() {
12485
let a: ~int = ~5i;

branches/try/src/libcore/option.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use num::Zero;
4949
use old_iter::{BaseIter, MutableIter, ExtendedIter};
5050
use old_iter;
5151
use str::StrSlice;
52-
use clone::DeepClone;
5352

5453
#[cfg(test)] use str;
5554

@@ -60,15 +59,6 @@ pub enum Option<T> {
6059
Some(T),
6160
}
6261

63-
impl<T: DeepClone> DeepClone for Option<T> {
64-
fn deep_clone(&self) -> Option<T> {
65-
match *self {
66-
Some(ref x) => Some(x.deep_clone()),
67-
None => None
68-
}
69-
}
70-
}
71-
7262
impl<T:Ord> Ord for Option<T> {
7363
fn lt(&self, other: &Option<T>) -> bool {
7464
match (self, other) {

branches/try/src/libcore/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use io::{print, println};
2727

2828
/* Reexported types and traits */
2929

30-
pub use clone::{Clone, DeepClone};
30+
pub use clone::Clone;
3131
pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
3232
pub use container::{Container, Mutable, Map, Set};
3333
pub use hash::Hash;

branches/try/src/libcore/vec.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,8 +2071,6 @@ pub trait ImmutableVector<'self, T> {
20712071
fn initn(&self, n: uint) -> &'self [T];
20722072
fn last(&self) -> &'self T;
20732073
fn last_opt(&self) -> Option<&'self T>;
2074-
fn position(&self, f: &fn(t: &T) -> bool) -> Option<uint>;
2075-
fn rposition(&self, f: &fn(t: &T) -> bool) -> Option<uint>;
20762074
#[cfg(stage0)]
20772075
fn each_reverse(&self, blk: &fn(&T) -> bool);
20782076
#[cfg(not(stage0))]
@@ -2140,30 +2138,6 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
21402138
#[inline]
21412139
fn last_opt(&self) -> Option<&'self T> { last_opt(*self) }
21422140

2143-
/**
2144-
* Find the first index matching some predicate
2145-
*
2146-
* Apply function `f` to each element of `v`. When function `f` returns
2147-
* true then an option containing the index is returned. If `f` matches no
2148-
* elements then none is returned.
2149-
*/
2150-
#[inline]
2151-
fn position(&self, f: &fn(t: &T) -> bool) -> Option<uint> {
2152-
position(*self, f)
2153-
}
2154-
2155-
/**
2156-
* Find the last index matching some predicate
2157-
*
2158-
* Apply function `f` to each element of `v` in reverse order. When
2159-
* function `f` returns true then an option containing the index is
2160-
* returned. If `f` matches no elements then none is returned.
2161-
*/
2162-
#[inline]
2163-
fn rposition(&self, f: &fn(t: &T) -> bool) -> Option<uint> {
2164-
rposition(*self, f)
2165-
}
2166-
21672141
/// Iterates over a vector's elements in reverse.
21682142
#[inline]
21692143
#[cfg(stage0)]
@@ -2256,17 +2230,43 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
22562230
}
22572231

22582232
pub trait ImmutableEqVector<T:Eq> {
2233+
fn position(&self, f: &fn(t: &T) -> bool) -> Option<uint>;
22592234
fn position_elem(&self, t: &T) -> Option<uint>;
2235+
fn rposition(&self, f: &fn(t: &T) -> bool) -> Option<uint>;
22602236
fn rposition_elem(&self, t: &T) -> Option<uint>;
22612237
}
22622238

22632239
impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
2240+
/**
2241+
* Find the first index matching some predicate
2242+
*
2243+
* Apply function `f` to each element of `v`. When function `f` returns
2244+
* true then an option containing the index is returned. If `f` matches no
2245+
* elements then none is returned.
2246+
*/
2247+
#[inline]
2248+
fn position(&self, f: &fn(t: &T) -> bool) -> Option<uint> {
2249+
position(*self, f)
2250+
}
2251+
22642252
/// Find the first index containing a matching value
22652253
#[inline]
22662254
fn position_elem(&self, x: &T) -> Option<uint> {
22672255
position_elem(*self, x)
22682256
}
22692257

2258+
/**
2259+
* Find the last index matching some predicate
2260+
*
2261+
* Apply function `f` to each element of `v` in reverse order. When
2262+
* function `f` returns true then an option containing the index is
2263+
* returned. If `f` matches no elements then none is returned.
2264+
*/
2265+
#[inline]
2266+
fn rposition(&self, f: &fn(t: &T) -> bool) -> Option<uint> {
2267+
rposition(*self, f)
2268+
}
2269+
22702270
/// Find the last index containing a matching value
22712271
#[inline]
22722272
fn rposition_elem(&self, t: &T) -> Option<uint> {

0 commit comments

Comments
 (0)