Skip to content
Permalink
Browse files

Add error conversion to .collect()

  • Loading branch information
emlun committed Dec 15, 2019
1 parent 205c0e6 commit 7e957a0970eec6afc1b2609dcc7e755c376e7c57
@@ -1554,7 +1554,7 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
// FIXME(#11084): This could be replaced with Iterator::scan when this
// performance bug is closed.

iter.into_iter().map(|x| x.ok_or(())).collect::<Result<_, _>>().ok()
iter.into_iter().map(|x| x.ok_or(())).collect::<Result<_, ()>>().ok()
}
}

@@ -1399,7 +1399,7 @@ unsafe impl<A> TrustedLen for IntoIter<A> {}
/////////////////////////////////////////////////////////////////////////////

#[stable(feature = "rust1", since = "1.0.0")]
impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
impl<A, E1, E2: From<E1>, V: FromIterator<A>> FromIterator<Result<A, E1>> for Result<V, E2> {
/// Takes each element in the `Iterator`: if it is an `Err`, no further
/// elements are taken, and the `Err` is returned. Should no `Err` occur, a
/// container with the values of each `Result` is returned.
@@ -1443,11 +1443,11 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
/// Since the third element caused an underflow, no further elements were taken,
/// so the final value of `shared` is 6 (= `3 + 2 + 1`), not 16.
#[inline]
fn from_iter<I: IntoIterator<Item=Result<A, E>>>(iter: I) -> Result<V, E> {
fn from_iter<I: IntoIterator<Item = Result<A, E1>>>(iter: I) -> Result<V, E2> {
// FIXME(#11084): This could be replaced with Iterator::scan when this
// performance bug is closed.

iter::process_results(iter.into_iter(), |i| i.collect())
iter::process_results(iter.into_iter().map(|r| r.map_err(|e| e.into())), |i| i.collect())
}
}

@@ -1475,7 +1475,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Err(OverflowError) => Err(Overflow),
})
.flat_map(Result::transpose)
.collect::<Result<Vec<_>, _>>()?;
.collect::<Result<Vec<_>, SelectionError<'_>>>()?;

debug!(
"winnowed to {} candidates for {:?}: {:?}",
@@ -1103,9 +1103,9 @@ impl Target {
format!("{}.{}: expected a JSON array", name, k)
)?.iter().enumerate()
.map(|(i,s)| {
let s = s.as_string().ok_or_else(||
format!("{}.{}[{}]: expected a JSON string", name, k, i))?;
Ok(s.to_owned())
s.as_string().ok_or_else(||
format!("{}.{}[{}]: expected a JSON string", name, k, i))
.map(|s| s.to_owned())
})
.collect::<Result<Vec<_>, String>>()?;

@@ -255,7 +255,7 @@ pub fn parse(file: &mut dyn io::Read, longnames: bool) -> Result<TermInfo, Strin
Err(e) => Some(Err(e)),
Ok(1) => Some(Ok((bnames[i].to_string(), true))),
Ok(_) => None
}).collect()
}).collect::<Result<_, std::io::Error>>()
};

if (bools_bytes + names_bytes) % 2 == 1 {
@@ -267,13 +267,13 @@ pub fn parse(file: &mut dyn io::Read, longnames: bool) -> Result<TermInfo, Strin
Ok(0xFFFF) => None,
Ok(n) => Some(Ok((nnames[i].to_string(), n))),
Err(e) => Some(Err(e))
}).collect()
}).collect::<Result<_, std::io::Error>>()
};

let string_map: HashMap<String, Vec<u8>> = if string_offsets_count > 0 {
let string_offsets: Vec<u16> = t!((0..string_offsets_count)
.map(|_| read_le_u16(file))
.collect());
.collect::<Result<_, std::io::Error>>());

let mut string_table = Vec::new();
t!(file.take(string_table_bytes as u64).read_to_end(&mut string_table));
@@ -302,7 +302,7 @@ pub fn parse(file: &mut dyn io::Read, longnames: bool) -> Result<TermInfo, Strin
Some(len) => Ok((name.to_string(), string_table[offset..offset + len].to_vec())),
None => Err("invalid file: missing NUL in string_table".to_string()),
}
}).collect())
}).collect::<Result<_, String>>())
} else {
HashMap::new()
};

0 comments on commit 7e957a0

Please sign in to comment.
You can’t perform that action at this time.