Skip to content

Commit b05369b

Browse files
committed
---
yaml --- r: 16285 b: refs/heads/try c: 0218418 h: refs/heads/master i: 16283: 880246b v: v3
1 parent bffeb23 commit b05369b

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 704a5a8c681a7e4cb044da9f7b1118baf79b1ff6
5+
refs/heads/try: 0218418428a6d21656d5ccf247cca30e47255b97
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/core.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export comm, task, future;
4343
export extfmt;
4444
export tuple;
4545
export to_str;
46+
export swappable;
4647
export dvec, dvec_iter;
4748

4849
// NDM seems to be necessary for resolve to work
@@ -163,6 +164,7 @@ mod option_iter {
163164
}
164165
mod result;
165166
mod to_str;
167+
mod swappable;
166168
mod dvec;
167169
#[path="iter-trait"]
168170
mod dvec_iter {

branches/try/src/libcore/swappable.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
export swappable;
2+
export unwrap;
3+
export methods;
4+
5+
#[doc = "
6+
A value that may be swapped out temporarily while it is being processed
7+
and then replaced. Swappables are most useful when working with unique
8+
values, which often cannot be mutated unless they are stored in the local
9+
stack frame to ensure memory safety.
10+
11+
The type guarantees the invariant that the value is always \"swapped in\"
12+
except during the execution of the `swap()` and `with()` methods.
13+
"]
14+
type swappable<A> = {
15+
mut o_t: option<A>
16+
};
17+
18+
#[doc = "Create a swappable swapped in with a given initial value"]
19+
fn swappable<A>(+t: A) -> swappable<A> {
20+
{mut o_t: some(t)}
21+
}
22+
23+
#[doc = "Consumes a swappable and returns its contents without copying"]
24+
fn unwrap<A>(-s: swappable<A>) -> A {
25+
let {o_t: o_t} <- s;
26+
option::unwrap(o_t)
27+
}
28+
29+
impl methods<A> for swappable<A> {
30+
#[doc = "
31+
Overwrites the contents of the swappable
32+
"]
33+
fn set(+a: A) {
34+
self.o_t <- some(a);
35+
}
36+
37+
#[doc = "
38+
Invokes `f()` with the current value but replaces the
39+
current value when complete. Returns the result of `f()`.
40+
41+
Attempts to read or access the receiver while `f()` is executing
42+
will fail dynamically.
43+
"]
44+
fn with<B>(f: fn(A) -> B) -> B {
45+
let mut o_u = none;
46+
self.swap { |t| o_u <- some(f(t)); t }
47+
option::unwrap(o_u)
48+
}
49+
50+
#[doc = "
51+
Invokes `f()` with the current value and then replaces the
52+
current value with the result of `f()`.
53+
54+
Attempts to read or access the receiver while `f()` is executing
55+
will fail dynamically.
56+
"]
57+
fn swap(f: fn(-A) -> A) {
58+
alt self.o_t {
59+
none { fail "no value present---already swapped?"; }
60+
some(_) {}
61+
}
62+
63+
let mut o_t = none;
64+
o_t <-> self.o_t;
65+
self.o_t <- some(f(option::unwrap(o_t)));
66+
}
67+
68+
#[doc = "True if there is a value present in this swappable"]
69+
fn is_present() -> bool {
70+
alt self.o_t {
71+
none {false}
72+
some(_) {true}
73+
}
74+
}
75+
76+
#[doc = "
77+
Removes the value from the swappable. Any further attempts
78+
to use the swapabble without first invoking `set()` will fail.
79+
"]
80+
fn take() -> A {
81+
alt self.o_t {
82+
none { fail "swapped out"; }
83+
some(_) {}
84+
}
85+
86+
let mut o_t = none;
87+
option::unwrap(o_t)
88+
}
89+
}
90+
91+
impl methods<A:copy> for swappable<A> {
92+
#[doc = "
93+
Copies out the contents of the swappable
94+
"]
95+
fn get() -> A {
96+
self.o_t.get()
97+
}
98+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import swappable::{swappable, methods};
2+
3+
fn main() {
4+
let d = swappable(3);
5+
assert d.get() == 3;
6+
d.set(4);
7+
assert d.get() == 4;
8+
d.swap { |i| i + 1 };
9+
assert d.get() == 5;
10+
assert d.with { |i| i + 1 } == 6;
11+
assert d.get() == 5;
12+
assert swappable::unwrap(d) == 5;
13+
}

0 commit comments

Comments
 (0)