Motivation
These functions were written very much in C, but not in Rust. Let me know if I've gone overboard, however now I at least understand the order of errors and calculations, as well as the error context being conveyed a bit better.
Overview
NonZeroUsize
Use NonZeroUsize type for mismatch count in errors
SomeToErr
An auxiliary some-to-err crate is used to shorten the construct
if let Some(err) = expr {
return Err(err);
}
to
expr.err_or(())?
CountToNonZero
An auxiliary count-to-non-zero crate is used to shorten the construct
let count = iter.count();
if count != 0 {
return Err(Error::Mismatch {
count: NonZeroUsize::new(count).unwrap(),
});
}
to
iter
.count_to_non_zero()
.map(|count| Error::Mismatch { count })
.err_or(())?;
Motivation These functions were written very much in C, but not in Rust. Let me know if I've gone overboard, however now I at least understand the order of errors and calculations, as well as the error context being conveyed a bit better.
Overview
NonZeroUsize
Use
NonZeroUsize
type for mismatch count in errorsSomeToErr
An auxiliary
some-to-err
crate is used to shorten the constructto
CountToNonZero
An auxiliary
count-to-non-zero
crate is used to shorten the constructto
Better collect of par iter
to
We save memory with this approach and don't do a double pass