From 9368be018dff51fa4df046cb4f438b0e076d3678 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 14 Aug 2024 09:57:41 +0200 Subject: [PATCH] impl Reflect for VecDeque --- crates/mirror-mirror/src/foreign_impls/mod.rs | 1 + .../src/foreign_impls/vec_deque.rs | 184 ++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 crates/mirror-mirror/src/foreign_impls/vec_deque.rs diff --git a/crates/mirror-mirror/src/foreign_impls/mod.rs b/crates/mirror-mirror/src/foreign_impls/mod.rs index f8fde72..8da4c54 100644 --- a/crates/mirror-mirror/src/foreign_impls/mod.rs +++ b/crates/mirror-mirror/src/foreign_impls/mod.rs @@ -15,6 +15,7 @@ mod hash_map; mod kollect; mod vec; mod via_scalar; +mod vec_deque; #[cfg(feature = "glam")] mod glam; diff --git a/crates/mirror-mirror/src/foreign_impls/vec_deque.rs b/crates/mirror-mirror/src/foreign_impls/vec_deque.rs new file mode 100644 index 0000000..677d3b1 --- /dev/null +++ b/crates/mirror-mirror/src/foreign_impls/vec_deque.rs @@ -0,0 +1,184 @@ +use alloc::boxed::Box; +use alloc::collections::VecDeque; +use alloc::vec::Vec; +use core::any::Any; + +use crate::array::Array; +use crate::list::ListError; +use crate::type_info::graph::ListNode; +use crate::type_info::graph::NodeId; +use crate::type_info::graph::TypeGraph; +use crate::DescribeType; +use crate::FromReflect; +use crate::List; +use crate::Reflect; +use crate::ReflectMut; +use crate::ReflectOwned; +use crate::ReflectRef; +use crate::Value; + +impl List for VecDeque +where + T: FromReflect + DescribeType, +{ + fn try_push(&mut self, element: &dyn Reflect) -> Result<(), ListError> { + if let Some(value) = T::from_reflect(element) { + VecDeque::push_back(self, value); + Ok(()) + } else { + Err(ListError) + } + } + + fn pop(&mut self) -> Option> { + let value = VecDeque::pop_back(self)?; + Some(Box::new(value)) + } + + fn try_remove(&mut self, index: usize) -> Option> { + if index < self.len() { + let value = VecDeque::remove(self, index); + Some(Box::new(value)) + } else { + None + } + } + + fn try_insert(&mut self, index: usize, element: &dyn Reflect) -> Result<(), ListError> { + if let Some(element) = T::from_reflect(element) { + VecDeque::insert(self, index, element); + Ok(()) + } else { + Err(ListError) + } + } +} + +impl Array for VecDeque +where + T: FromReflect + DescribeType, +{ + fn get(&self, index: usize) -> Option<&dyn Reflect> { + VecDeque::get(self, index).map(|value| value.as_reflect()) + } + + fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> { + VecDeque::get_mut(self, index).map(|value| value.as_reflect_mut()) + } + + fn len(&self) -> usize { + VecDeque::len(self) + } + + fn is_empty(&self) -> bool { + VecDeque::is_empty(self) + } + + fn iter(&self) -> crate::array::Iter<'_> { + crate::array::Iter::new(self) + } + + fn iter_mut(&mut self) -> crate::iter::ValueIterMut<'_> { + let iter = VecDeque::iter_mut(self).map(|value| value.as_reflect_mut()); + Box::new(iter) + } + + fn swap(&mut self, a: usize, b: usize) { + VecDeque::swap(self, a, b); + } +} + +impl Reflect for VecDeque +where + T: FromReflect + DescribeType, +{ + trivial_reflect_methods!(); + + fn patch(&mut self, value: &dyn Reflect) { + if let Some(list) = value.reflect_ref().as_list() { + for (idx, new_value) in list.iter().enumerate() { + if let Some(value) = self.get_mut(idx) { + value.patch(new_value); + } + } + } + } + + fn to_value(&self) -> Value { + let data = self.iter().map(Reflect::to_value).collect(); + Value::List(data) + } + + fn clone_reflect(&self) -> Box { + let value = self.to_value(); + Box::new(Self::from_reflect(&value).unwrap()) + } + + fn debug(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_list().entries(Array::iter(self)).finish() + } + + fn reflect_owned(self: Box) -> ReflectOwned { + ReflectOwned::List(self) + } + + fn reflect_ref(&self) -> ReflectRef<'_> { + ReflectRef::List(self) + } + + fn reflect_mut(&mut self) -> ReflectMut<'_> { + ReflectMut::List(self) + } + + fn as_array(&self) -> Option<&dyn Array> { + Some(self) + } + + fn as_array_mut(&mut self) -> Option<&mut dyn Array> { + Some(self) + } + + fn into_array(self: Box) -> Option> { + Some(self) + } +} + +impl FromReflect for VecDeque +where + T: FromReflect + DescribeType, +{ + fn from_reflect(reflect: &dyn Reflect) -> Option { + let iter = match reflect.reflect_ref() { + ReflectRef::Array(array) => array.iter(), + ReflectRef::List(list) => list.iter(), + _ => return None, + }; + let mut out = VecDeque::with_capacity(iter.len()); + for value in iter { + out.push_back(T::from_reflect(value)?); + } + Some(out) + } +} + +impl From> for Value +where + T: Reflect, +{ + fn from(list: VecDeque) -> Self { + let list = list + .into_iter() + .map(|value| value.to_value()) + .collect::>(); + Value::List(list) + } +} + +impl DescribeType for VecDeque +where + T: DescribeType, +{ + fn build(graph: &mut TypeGraph) -> NodeId { + graph.get_or_build_node_with::(|graph| ListNode::new::(graph)) + } +}