From d41f578c8344e96524c5553fc74b3b9bdd574e64 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 8 May 2019 14:04:30 -0400 Subject: [PATCH] Add some methods for helping with iterators --- src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 46aa805..d083f0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub enum Needed { #[derive(Debug, PartialEq, Eq, Clone)] pub enum KError<'a> { Incomplete(Needed), + EmptyIterator, Encoding { expected: &'static str }, MissingInstanceValue, 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)] mod tests { use super::*;