1use crate::Map;
22use crate::{IntoValues, Values};
23use std::marker::PhantomData;
24
25impl<'a, V: Clone + 'a> Iterator for Values<'a, V> {
26 type Item = &'a V;
27
28 #[inline]
29 #[must_use]
30 fn next(&mut self) -> Option<Self::Item> {
31 while self.pos < self.max {
32 let opt = unsafe { &*self.head.add(self.pos) };
33 self.pos += 1;
34 if opt.is_some() {
35 return opt.as_ref();
36 }
37 }
38 None
39 }
40}
41
42impl<V: Copy> Iterator for IntoValues<V> {
43 type Item = V;
44
45 #[inline]
46 #[must_use]
47 fn next(&mut self) -> Option<Self::Item> {
48 while self.pos < self.max {
49 let opt = unsafe { &*self.head.add(self.pos) };
50 self.pos += 1;
51 if opt.is_some() {
52 return *opt;
53 }
54 }
55 None
56 }
57}
58
59impl<V: Clone> Map<V> {
60 #[inline]
66 #[must_use]
67 pub const fn values(&self) -> Values<V> {
68 #[cfg(debug_assertions)]
69 assert!(self.initialized, "Can't values() non-initialized Map");
70 Values {
71 max: self.max,
72 pos: 0,
73 head: self.head,
74 _marker: PhantomData,
75 }
76 }
77
78 #[inline]
84 #[must_use]
85 pub const fn into_values(&self) -> IntoValues<V> {
86 #[cfg(debug_assertions)]
87 assert!(self.initialized, "Can't into_values() non-initialized Map");
88 IntoValues {
89 max: self.max,
90 pos: 0,
91 head: self.head,
92 }
93 }
94}
95
96#[test]
97fn empty_values() {
98 let m: Map<u32> = Map::with_capacity_none(16);
99 assert!(m.values().next().is_none());
100}
101
102#[test]
103fn insert_and_jump_over_next() {
104 let mut m: Map<&str> = Map::with_capacity_none(16);
105 m.insert(0, "foo");
106 let mut values = m.into_values();
107 assert_eq!("foo", values.next().unwrap());
108 assert!(values.next().is_none());
109}
110
111#[test]
112fn count_them_all() {
113 let mut m: Map<&str> = Map::with_capacity_none(16);
114 m.insert(0, "one");
115 m.insert(1, "two");
116 m.insert(2, "three");
117 assert_eq!(3, m.into_values().count());
118}