Skip to content

Commit 7f9c5aa

Browse files
committed
std: Restore Option::chain{,_mut}_ref as and_then{,_mut}_ref
1 parent 7c08abb commit 7f9c5aa

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

src/librustc/middle/trans/common.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,7 @@ impl get_node_info for ast::Block {
505505

506506
impl get_node_info for Option<@ast::Expr> {
507507
fn info(&self) -> Option<NodeInfo> {
508-
match *self {
509-
Some(ref s) => s.info(),
510-
None => None,
511-
}
508+
self.and_then_ref(|s| s.info())
512509
}
513510
}
514511

src/librustc/middle/typeck/astconv.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -745,17 +745,10 @@ pub fn ty_of_closure<AC:AstConv,RS:RegionScope + Clone + 'static>(
745745
RegionParamNames(bound_lifetime_names.clone()));
746746

747747
let input_tys = do decl.inputs.iter().enumerate().map |(i, a)| {
748-
let expected_arg_ty = match expected_sig {
749-
Some(ref e) => {
750-
// no guarantee that the correct number of expected args
751-
// were supplied
752-
if i < e.inputs.len() {
753-
Some(e.inputs[i])
754-
} else {
755-
None
756-
}
757-
}
758-
None => None,
748+
let expected_arg_ty = do expected_sig.and_then_ref |e| {
749+
// no guarantee that the correct number of expected args
750+
// were supplied
751+
if i < e.inputs.len() {Some(e.inputs[i])} else {None}
759752
};
760753
ty_of_arg(this, &rb, a, expected_arg_ty)
761754
}.collect();

src/libstd/iter.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -1504,12 +1504,7 @@ impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for FlatMap<'self,
15041504
}
15051505
}
15061506
match self.iter.next().map_move(|x| (self.f)(x)) {
1507-
None => {
1508-
return match self.backiter {
1509-
Some(ref mut it) => it.next(),
1510-
None => None,
1511-
};
1512-
}
1507+
None => return self.backiter.and_then_mut_ref(|it| it.next()),
15131508
next => self.frontiter = next,
15141509
}
15151510
}
@@ -1541,12 +1536,7 @@ impl<'self,
15411536
}
15421537
}
15431538
match self.iter.next_back().map_move(|x| (self.f)(x)) {
1544-
None => {
1545-
return match self.frontiter {
1546-
Some(ref mut it) => it.next_back(),
1547-
None => None,
1548-
};
1549-
}
1539+
None => return self.frontiter.and_then_mut_ref(|it| it.next_back()),
15501540
next => self.backiter = next,
15511541
}
15521542
}

src/libstd/option.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ impl<T> Option<T> {
138138
}
139139
}
140140

141-
/// Returns `None` if the option is `None`, otherwise calls and returns the
142-
/// value of `f`.
141+
/// Returns `None` if the option is `None`, otherwise calls `f` with the
142+
/// wrapped value and returns the result.
143143
#[inline]
144144
pub fn and_then<U>(self, f: &fn(T) -> Option<U>) -> Option<U> {
145145
match self {
@@ -148,6 +148,26 @@ impl<T> Option<T> {
148148
}
149149
}
150150

151+
/// Returns `None` if the option is `None`, otherwise calls `f` with a
152+
/// reference to the wrapped value and returns the result.
153+
#[inline]
154+
pub fn and_then_ref<'a, U>(&'a self, f: &fn(&'a T) -> Option<U>) -> Option<U> {
155+
match *self {
156+
Some(ref x) => f(x),
157+
None => None
158+
}
159+
}
160+
161+
/// Returns `None` if the option is `None`, otherwise calls `f` with a
162+
/// mutable reference to the wrapped value and returns the result.
163+
#[inline]
164+
pub fn and_then_mut_ref<'a, U>(&'a mut self, f: &fn(&'a mut T) -> Option<U>) -> Option<U> {
165+
match *self {
166+
Some(ref mut x) => f(x),
167+
None => None
168+
}
169+
}
170+
151171
/// Returns the option if it contains a value, otherwise returns `optb`.
152172
#[inline]
153173
pub fn or(self, optb: Option<T>) -> Option<T> {
@@ -157,8 +177,8 @@ impl<T> Option<T> {
157177
}
158178
}
159179

160-
/// Returns the option if it contains a value, otherwise calls and returns the
161-
/// value of `f`.
180+
/// Returns the option if it contains a value, otherwise calls `f` and
181+
/// returns the result.
162182
#[inline]
163183
pub fn or_else(self, f: &fn() -> Option<T>) -> Option<T> {
164184
match self {

0 commit comments

Comments
 (0)