Add some methods for helping with iterators

This commit is contained in:
Bradlee Speice 2019-05-08 14:04:30 -04:00
parent 1c15eb14d6
commit d41f578c83

View File

@ -9,6 +9,7 @@ pub enum Needed {
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum KError<'a> { pub enum KError<'a> {
Incomplete(Needed), Incomplete(Needed),
EmptyIterator,
Encoding { expected: &'static str }, Encoding { expected: &'static str },
MissingInstanceValue, MissingInstanceValue,
MissingRoot, MissingRoot,
@ -274,6 +275,30 @@ impl<'a> KStream for BytesReader<'a> {
} }
} }
macro_rules! kf_max {
($i: ident, $t: ty) => (
pub fn $i<'a>(first: Option<&'a $t>, second: &'a $t) -> Option<&'a $t> {
if second.is_nan() { first }
else if first.is_none() { Some(second) }
else { if first.unwrap() < second { Some(second) } else { first } }
}
)
}
kf_max!(kf32_max, f32);
kf_max!(kf64_max, f64);
macro_rules! kf_min {
($i: ident, $t: ty) => (
pub fn $i<'a>(first: Option<&'a $t>, second: &'a $t) -> Option<&'a $t> {
if second.is_nan() { first }
else if first.is_none() { Some(second) }
else { if first.unwrap() < second { first } else { Some(second) } }
}
)
}
kf_min!(kf32_min, f32);
kf_min!(kf64_min, f64);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;