use super::*; use windows_core::*; #[implement(IMapView, IIterable>)] struct StockMapView where K: RuntimeType + 'static, V: RuntimeType + 'static, K::Default: Clone + Ord, V::Default: Clone, { map: std::collections::BTreeMap, } impl IIterable_Impl> for StockMapView_Impl where K: RuntimeType, V: RuntimeType, K::Default: Clone + Ord, V::Default: Clone, { fn First(&self) -> Result>> { Ok(ComObject::new(StockMapViewIterator:: { _owner: self.to_object(), current: std::sync::RwLock::new(self.map.iter()), }) .into_interface()) } } impl IMapView_Impl for StockMapView_Impl where K: RuntimeType, V: RuntimeType, K::Default: Clone + Ord, V::Default: Clone, { fn Lookup(&self, key: Ref) -> Result { let value = self.map.get(&*key).ok_or_else(|| Error::from(E_BOUNDS))?; V::from_default(value) } fn Size(&self) -> Result { Ok(self.map.len().try_into()?) } fn HasKey(&self, key: Ref) -> Result { Ok(self.map.contains_key(&*key)) } fn Split(&self, first: OutRef>, second: OutRef>) -> Result<()> { _ = first.write(None); _ = second.write(None); Ok(()) } } #[implement(IIterator>)] struct StockMapViewIterator<'a, K, V> where K: RuntimeType + 'static, V: RuntimeType + 'static, K::Default: Clone + Ord, V::Default: Clone, { _owner: ComObject>, current: std::sync::RwLock>, } impl IIterator_Impl> for StockMapViewIterator_Impl<'_, K, V> where K: RuntimeType, V: RuntimeType, K::Default: Clone + Ord, V::Default: Clone, { fn Current(&self) -> Result> { let mut current = self.current.read().unwrap().clone().peekable(); if let Some((key, value)) = current.peek() { Ok(ComObject::new(StockKeyValuePair { key: (*key).clone(), value: (*value).clone(), }) .into_interface()) } else { Err(Error::from(E_BOUNDS)) } } fn HasCurrent(&self) -> Result { let mut current = self.current.read().unwrap().clone().peekable(); Ok(current.peek().is_some()) } fn MoveNext(&self) -> Result { let mut current = self.current.write().unwrap(); current.next(); Ok(current.clone().peekable().peek().is_some()) } fn GetMany(&self, pairs: &mut [Option>]) -> Result { let mut current = self.current.write().unwrap(); let mut actual = 0; for pair in pairs { if let Some((key, value)) = current.next() { *pair = Some( ComObject::new(StockKeyValuePair { key: (*key).clone(), value: (*value).clone(), }) .into_interface(), ); actual += 1; } else { break; } } Ok(actual) } } #[implement(IKeyValuePair)] struct StockKeyValuePair where K: RuntimeType + 'static, V: RuntimeType + 'static, K::Default: Clone, V::Default: Clone, { key: K::Default, value: V::Default, } impl IKeyValuePair_Impl for StockKeyValuePair_Impl where K: RuntimeType, V: RuntimeType, K::Default: Clone, V::Default: Clone, { fn Key(&self) -> Result { K::from_default(&self.key) } fn Value(&self) -> Result { V::from_default(&self.value) } } impl From> for IMapView where K: RuntimeType, V: RuntimeType, K::Default: Clone + Ord, V::Default: Clone, { fn from(map: std::collections::BTreeMap) -> Self { StockMapView { map }.into() } }