core: Implement IntoIterator for Option and Result references
Fixes #27996.
This commit is contained in:
parent
ab21fe59e9
commit
a7313a0b89
@ -779,6 +779,26 @@ impl<T> IntoIterator for Option<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(since = "1.4.0", feature = "option_iter")]
|
||||
impl<'a, T> IntoIterator for &'a Option<T> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = Iter<'a, T>;
|
||||
|
||||
fn into_iter(self) -> Iter<'a, T> {
|
||||
self.iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(since = "1.4.0", feature = "option_iter")]
|
||||
impl<'a, T> IntoIterator for &'a mut Option<T> {
|
||||
type Item = &'a mut T;
|
||||
type IntoIter = IterMut<'a, T>;
|
||||
|
||||
fn into_iter(mut self) -> IterMut<'a, T> {
|
||||
self.iter_mut()
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// The Option Iterators
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -815,6 +815,26 @@ impl<T, E> IntoIterator for Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(since = "1.4.0", feature = "result_iter")]
|
||||
impl<'a, T, E> IntoIterator for &'a Result<T, E> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = Iter<'a, T>;
|
||||
|
||||
fn into_iter(self) -> Iter<'a, T> {
|
||||
self.iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(since = "1.4.0", feature = "result_iter")]
|
||||
impl<'a, T, E> IntoIterator for &'a mut Result<T, E> {
|
||||
type Item = &'a mut T;
|
||||
type IntoIter = IterMut<'a, T>;
|
||||
|
||||
fn into_iter(mut self) -> IterMut<'a, T> {
|
||||
self.iter_mut()
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// The Result Iterators
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -180,11 +180,14 @@ fn test_iter() {
|
||||
assert_eq!(it.next(), Some(&val));
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert!(it.next().is_none());
|
||||
|
||||
let mut it = (&x).into_iter();
|
||||
assert_eq!(it.next(), Some(&val));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_iter() {
|
||||
let val = 5;
|
||||
let mut val = 5;
|
||||
let new_val = 11;
|
||||
|
||||
let mut x = Some(val);
|
||||
@ -205,6 +208,10 @@ fn test_mut_iter() {
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
assert_eq!(x, Some(new_val));
|
||||
|
||||
let mut y = Some(val);
|
||||
let mut it = (&mut y).into_iter();
|
||||
assert_eq!(it.next(), Some(&mut val));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -150,3 +150,36 @@ pub fn test_expect_err() {
|
||||
let err: Result<isize, &'static str> = Err("All good");
|
||||
err.expect("Got expected error");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_iter() {
|
||||
let ok: Result<isize, &'static str> = Ok(100);
|
||||
let mut it = ok.iter();
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next(), Some(&100));
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert!(it.next().is_none());
|
||||
assert_eq!((&ok).into_iter().next(), Some(&100));
|
||||
|
||||
let err: Result<isize, &'static str> = Err("error");
|
||||
assert_eq!(err.iter().next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_iter_mut() {
|
||||
let mut ok: Result<isize, &'static str> = Ok(100);
|
||||
for loc in ok.iter_mut() {
|
||||
*loc = 200;
|
||||
}
|
||||
assert_eq!(ok, Ok(200));
|
||||
for loc in &mut ok {
|
||||
*loc = 300;
|
||||
}
|
||||
assert_eq!(ok, Ok(300));
|
||||
|
||||
let mut err: Result<isize, &'static str> = Err("error");
|
||||
for loc in err.iter_mut() {
|
||||
*loc = 200;
|
||||
}
|
||||
assert_eq!(err, Err("error"));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user