提交 7f9c5aae 编写于 作者: E Erick Tryzelaar

std: Restore Option::chain{,_mut}_ref as and_then{,_mut}_ref

上级 7c08abb0
......@@ -505,10 +505,7 @@ fn info(&self) -> Option<NodeInfo> {
impl get_node_info for Option<@ast::Expr> {
fn info(&self) -> Option<NodeInfo> {
match *self {
Some(ref s) => s.info(),
None => None,
}
self.and_then_ref(|s| s.info())
}
}
......
......@@ -745,17 +745,10 @@ pub fn ty_of_closure<AC:AstConv,RS:RegionScope + Clone + 'static>(
RegionParamNames(bound_lifetime_names.clone()));
let input_tys = do decl.inputs.iter().enumerate().map |(i, a)| {
let expected_arg_ty = match expected_sig {
Some(ref e) => {
// no guarantee that the correct number of expected args
// were supplied
if i < e.inputs.len() {
Some(e.inputs[i])
} else {
None
}
}
None => None,
let expected_arg_ty = do expected_sig.and_then_ref |e| {
// no guarantee that the correct number of expected args
// were supplied
if i < e.inputs.len() {Some(e.inputs[i])} else {None}
};
ty_of_arg(this, &rb, a, expected_arg_ty)
}.collect();
......
......@@ -1504,12 +1504,7 @@ fn next(&mut self) -> Option<B> {
}
}
match self.iter.next().map_move(|x| (self.f)(x)) {
None => {
return match self.backiter {
Some(ref mut it) => it.next(),
None => None,
};
}
None => return self.backiter.and_then_mut_ref(|it| it.next()),
next => self.frontiter = next,
}
}
......@@ -1541,12 +1536,7 @@ fn next_back(&mut self) -> Option<B> {
}
}
match self.iter.next_back().map_move(|x| (self.f)(x)) {
None => {
return match self.frontiter {
Some(ref mut it) => it.next_back(),
None => None,
};
}
None => return self.frontiter.and_then_mut_ref(|it| it.next_back()),
next => self.backiter = next,
}
}
......
......@@ -138,8 +138,8 @@ pub fn and(self, optb: Option<T>) -> Option<T> {
}
}
/// Returns `None` if the option is `None`, otherwise calls and returns the
/// value of `f`.
/// Returns `None` if the option is `None`, otherwise calls `f` with the
/// wrapped value and returns the result.
#[inline]
pub fn and_then<U>(self, f: &fn(T) -> Option<U>) -> Option<U> {
match self {
......@@ -148,6 +148,26 @@ pub fn and_then<U>(self, f: &fn(T) -> Option<U>) -> Option<U> {
}
}
/// Returns `None` if the option is `None`, otherwise calls `f` with a
/// reference to the wrapped value and returns the result.
#[inline]
pub fn and_then_ref<'a, U>(&'a self, f: &fn(&'a T) -> Option<U>) -> Option<U> {
match *self {
Some(ref x) => f(x),
None => None
}
}
/// Returns `None` if the option is `None`, otherwise calls `f` with a
/// mutable reference to the wrapped value and returns the result.
#[inline]
pub fn and_then_mut_ref<'a, U>(&'a mut self, f: &fn(&'a mut T) -> Option<U>) -> Option<U> {
match *self {
Some(ref mut x) => f(x),
None => None
}
}
/// Returns the option if it contains a value, otherwise returns `optb`.
#[inline]
pub fn or(self, optb: Option<T>) -> Option<T> {
......@@ -157,8 +177,8 @@ pub fn or(self, optb: Option<T>) -> Option<T> {
}
}
/// Returns the option if it contains a value, otherwise calls and returns the
/// value of `f`.
/// Returns the option if it contains a value, otherwise calls `f` and
/// returns the result.
#[inline]
pub fn or_else(self, f: &fn() -> Option<T>) -> Option<T> {
match self {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册