Skip to content

Commit 42dbf39

Browse files
committed
---
yaml --- r: 14405 b: refs/heads/try c: 910a32c h: refs/heads/master i: 14403: a048809 v: v3
1 parent 941204f commit 42dbf39

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-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: 9d20ed7bf97e533e0cc5d7be2c3ec5d5dfd30e98
5+
refs/heads/try: 910a32c7c777296be0992bf0d6f2d66261c407d6
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/option.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,57 @@ fn may<T>(opt: t<T>, f: fn(T)) {
8484
alt opt { none {/* nothing */ } some(t) { f(t); } }
8585
}
8686

87+
/*
88+
Function: unwrap
89+
90+
Moves a value out of an option type and returns it. Useful primarily
91+
for getting strings, vectors and unique pointers out of option types
92+
without copying them.
93+
*/
94+
fn unwrap<T>(-opt: t<T>) -> T unsafe {
95+
let addr = alt opt {
96+
some(x) { ptr::addr_of(x) }
97+
none { fail "option none" }
98+
};
99+
let liberated_value = unsafe::reinterpret_cast(*addr);
100+
unsafe::leak(opt);
101+
ret liberated_value;
102+
}
103+
104+
#[test]
105+
fn test_unwrap_ptr() {
106+
let x = ~0;
107+
let addr_x = ptr::addr_of(*x);
108+
let opt = some(x);
109+
let y = unwrap(opt);
110+
let addr_y = ptr::addr_of(*y);
111+
assert addr_x == addr_y;
112+
}
113+
114+
#[test]
115+
fn test_unwrap_str() {
116+
let x = "test";
117+
let addr_x = str::as_buf(x) {|buf| ptr::addr_of(buf) };
118+
let opt = some(x);
119+
let y = unwrap(opt);
120+
let addr_y = str::as_buf(y) {|buf| ptr::addr_of(buf) };
121+
assert addr_x == addr_y;
122+
}
123+
124+
#[test]
125+
fn test_unwrap_resource() {
126+
resource r(i: @mutable int) {
127+
*i += 1;
128+
}
129+
let i = @mutable 0;
130+
{
131+
let x = r(i);
132+
let opt = some(x);
133+
let y = unwrap(opt);
134+
}
135+
assert *i == 1;
136+
}
137+
87138
// Local Variables:
88139
// mode: rust;
89140
// fill-column: 78;

0 commit comments

Comments
 (0)