Skip to content

Commit ad1bc6f

Browse files
committed
---
yaml --- r: 31585 b: refs/heads/dist-snap c: 9facb15 h: refs/heads/master i: 31583: 264aa4d v: v3
1 parent 1b75469 commit ad1bc6f

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
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: 7d4742e101f476de072834cb68b6c53dfb59ed07
10+
refs/heads/dist-snap: 9facb15c49488901e0a1a91af016b9516b5a124f
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/src/libcore/option.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pure fn get<T: copy>(opt: option<T>) -> T {
2323
* Fails if the value equals `none`
2424
*/
2525

26-
alt opt { some(x) { return x; } none { fail ~"option none"; } }
26+
alt opt { some(x) { return x; } none { fail ~"option::get none"; } }
2727
}
2828

2929
pure fn expect<T: copy>(opt: option<T>, reason: ~str) -> T {
@@ -112,14 +112,21 @@ pure fn unwrap<T>(-opt: option<T>) -> T {
112112
unsafe {
113113
let addr = alt opt {
114114
some(x) { ptr::addr_of(x) }
115-
none { fail ~"option none" }
115+
none { fail ~"option::unwrap none" }
116116
};
117117
let liberated_value = unsafe::reinterpret_cast(*addr);
118118
unsafe::forget(opt);
119119
return liberated_value;
120120
}
121121
}
122122

123+
/// The ubiquitous option dance.
124+
#[inline(always)]
125+
fn swap_unwrap<T>(opt: &mut option<T>) -> T {
126+
if opt.is_none() { fail ~"option::swap_unwrap none" }
127+
unwrap(util::replace(opt, none))
128+
}
129+
123130
pure fn unwrap_expect<T>(-opt: option<T>, reason: ~str) -> T {
124131
//! As unwrap, but with a specified failure message.
125132
if opt.is_none() { fail reason; }
@@ -204,6 +211,24 @@ fn test_unwrap_resource() {
204211
assert *i == 1;
205212
}
206213

214+
#[test]
215+
fn test_option_dance() {
216+
let x = some(());
217+
let mut y = some(5);
218+
let mut y2 = 0;
219+
do x.iter |_x| {
220+
y2 = swap_unwrap(&mut y);
221+
}
222+
assert y2 == 5;
223+
assert y.is_none();
224+
}
225+
#[test] #[should_fail] #[ignore(cfg(windows))]
226+
fn test_option_too_much_dance() {
227+
let mut y = some(util::noncopyable());
228+
let _y2 = swap_unwrap(&mut y);
229+
let _y3 = swap_unwrap(&mut y);
230+
}
231+
207232
#[test]
208233
fn test_option_while_some() {
209234
let mut i = 0;

branches/dist-snap/src/libcore/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pure fn id<T>(+x: T) -> T { x }
99
* Swap the values at two mutable locations of the same type, without
1010
* deinitialising or copying either one.
1111
*/
12+
#[inline(always)]
1213
fn swap<T>(x: &mut T, y: &mut T) {
1314
*x <-> *y;
1415
}
@@ -17,6 +18,7 @@ fn swap<T>(x: &mut T, y: &mut T) {
1718
* Replace the value at a mutable location with a new one, returning the old
1819
* value, without deinitialising or copying either one.
1920
*/
21+
#[inline(always)]
2022
fn replace<T>(dest: &mut T, +src: T) -> T {
2123
let mut tmp = src;
2224
swap(dest, &mut tmp);

0 commit comments

Comments
 (0)