2011-06-15 13:19:50 -05:00
|
|
|
// lib/option::rs
|
2011-08-09 19:36:07 -05:00
|
|
|
|
2011-08-12 08:37:10 -05:00
|
|
|
tag t<@T> { none; some(T); }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get<@T>(opt: t<T>) -> T {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt opt { some(x) { x } none. { fail "option none" } }
|
2011-08-09 19:36:07 -05:00
|
|
|
}
|
2010-11-03 19:10:37 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn map<@T, @U>(f: block(T) -> U, opt: t<T>) -> t<U> {
|
2011-08-09 19:36:07 -05:00
|
|
|
alt opt { some(x) { some(f(x)) } none. { none } }
|
2010-11-03 19:10:37 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn is_none<@T>(opt: t<T>) -> bool {
|
2011-08-09 19:36:07 -05:00
|
|
|
alt opt { none. { true } some(_) { false } }
|
2011-03-17 13:21:11 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn is_some<@T>(opt: t<T>) -> bool { !is_none(opt) }
|
2011-06-16 12:52:43 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn from_maybe<@T>(def: T, opt: t<T>) -> T {
|
2011-08-09 19:36:07 -05:00
|
|
|
alt opt { some(x) { x } none. { def } }
|
2011-04-06 19:56:44 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn maybe<@T, @U>(def: U, f: block(T) -> U, opt: t<T>) -> U {
|
2011-08-09 19:36:07 -05:00
|
|
|
alt opt { none. { def } some(t) { f(t) } }
|
Handle nested items correctly in typestate_check
Summary says it all. Actually, only nested objects and functions
are handled, but that's better than before. The fold that I was using
before to traverse a crate wasn't working correctly, because annotations
have to reflect the number of local variables of the nearest enclosing
function (in turn, because annotations are represented as bit vectors).
The fold was traversing the AST in the wrong order, first filling in
the annotations correctly, but then re-traversing them with the bit
vector length for any outer nested functions, and so on.
Remedying this required writing a lot of tedious boilerplate code
because I scrapped the idea of using a fold altogether.
I also made typestate_check handle unary, field, alt, and fail.
Also, some miscellaneous changes:
* added annotations to blocks in typeck
* fix pprust so it can handle spawn
* added more logging functions in util.common
* fixed _vec.or
* added maybe and from_maybe in option
* removed fold_block field from ast_fold, since it was never used
2011-04-18 17:33:10 -05:00
|
|
|
}
|
2011-05-17 22:11:25 -05:00
|
|
|
|
|
|
|
// Can be defined in terms of the above when/if we have const bind.
|
2011-09-12 04:27:30 -05:00
|
|
|
fn may<@T>(f: block(T), opt: t<T>) {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt opt { none. {/* nothing */ } some(t) { f(t); } }
|
2011-05-17 22:11:25 -05:00
|
|
|
}
|
2011-08-09 19:36:07 -05:00
|
|
|
|
2010-11-03 19:10:37 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-11-03 19:10:37 -05:00
|
|
|
// End:
|