diff --git a/Cargo.toml b/Cargo.toml index 7f63f7d..3b90e09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,12 +4,13 @@ description = "Small byte-sized generic key-value map type" keywords = ["map", "table", "small", "key", "value"] repository = "https://github.com/notflan/smallmap" homepage= "https://git.flanchan.moe/flanchan/smallmap" -version = "1.1.3" +version = "1.1.4" authors = ["Avril "] edition = "2018" license = "MIT" [dependencies] +serde = {version = "1.0.116", features = ["derive"], optional = true} [build-dependencies] rustc_version = "0.2" diff --git a/src/iter.rs b/src/iter.rs index 92c2a5d..7b3be41 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -44,7 +44,13 @@ impl<'a, K, V> Iterator for PageElements<'a,K,V> type Item = &'a (K,V); #[inline] fn next(&mut self) -> Option { - self.0.next().map(Option::as_ref).flatten() + //self.0.next().map(Option::as_ref).flatten() + while let Some(next) = self.0.next() { + if let Some(next) = next.as_ref() { + return Some(next); + } + } + None } fn size_hint(&self) -> (usize, Option) { @@ -61,7 +67,12 @@ impl<'a, K, V> Iterator for PageElementsMut<'a,K,V> type Item = &'a mut (K,V); #[inline] fn next(&mut self) -> Option { - self.0.next().map(Option::as_mut).flatten() + while let Some(next) = self.0.next() { + if let Some(next) = next.as_mut() { + return Some(next); + } + } + None } fn size_hint(&self) -> (usize, Option) { @@ -103,15 +114,23 @@ where K: Collapse { type Item = &'a (K,V); fn next(&mut self) -> Option { + println!("Start loop"); loop { if let Some(ref mut page) = self.0 { if let Some(elem) = page.next() { + println!("!!!!! Get"); return Some(elem); + } else { + println!("NO ENTRY IN PAGE"); } + } else { + println!("NO PAGE"); } if let Some(next_page) = self.1.next() { + println!("REPLACE"); self.0.replace(next_page.iter()); } else { + println!("NONE"); return None; } } @@ -153,7 +172,7 @@ impl<'a, K: Collapse, V> std::iter::FusedIterator for IterMut<'a, K,V>{} pub struct IntoIter(pub(crate) Option>, pub(crate) std::vec::IntoIter>); impl Iterator for IntoIter - where K: Collapse +where K: Collapse { type Item = (K,V); fn next(&mut self) -> Option { @@ -177,3 +196,44 @@ impl Iterator for IntoIter } impl std::iter::FusedIterator for IntoIter{} + +#[cfg(test)] +mod tests +{ + use crate::*; + #[test] + fn iter() + { + let mut map = Map::new(); + map.insert('<', ()); + map.insert('>', ()); + map.insert('|', ()); + + let string: String = map.iter().copied().map(|(x, _)| x).collect(); + assert_eq!("<>|", &string[..]) + } + + #[test] + fn iter_mut() + { + let mut map = Map::new(); + map.insert('<', ()); + map.insert('>', ()); + map.insert('|', ()); + + let string: String = map.iter_mut().map(|&mut (x, _)| x).collect(); + assert_eq!("<>|", &string[..]) + } + #[test] + fn into_iter() + { + let mut map = Map::new(); + map.insert('<', ()); + map.insert('>', ()); + map.insert('|', ()); + + let string: String = map.into_iter().map(|(x, _)| x).collect(); + assert_eq!("<>|", &string[..]) + } + +} diff --git a/src/lib.rs b/src/lib.rs index 24ea784..ef4fba4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,6 +143,7 @@ where K: Collapse /// A small hashtable-like map with byte sized key indecies. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +#[cfg_attr(feature="serde", derive(serde::Serialize))] pub struct Map(Vec>); impl Map diff --git a/src/page_impls.rs b/src/page_impls.rs index eb1cf43..64c5012 100644 --- a/src/page_impls.rs +++ b/src/page_impls.rs @@ -45,3 +45,18 @@ impl hash::Hash for Page { (&self.0[..]).hash(state) } } + +#[cfg(feature="serde")] +const _: () = { + use serde::*; + impl serde::Serialize for Page + where K:Serialize, V: Serialize + { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_slice(&self.0[..]) + } + } +};