Allow View<T> to be hashed and compared

Antonio Scandurra created

Change summary

crates/gpui2/src/view.rs | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

Detailed changes

crates/gpui2/src/view.rs 🔗

@@ -4,7 +4,12 @@ use crate::{
     Size, ViewContext, VisualContext, WeakModel, WindowContext,
 };
 use anyhow::{Context, Result};
-use std::{any::TypeId, marker::PhantomData, sync::Arc};
+use std::{
+    any::TypeId,
+    hash::{Hash, Hasher},
+    marker::PhantomData,
+    sync::Arc,
+};
 
 pub trait Render: 'static + Sized {
     type Element: Element<Self> + 'static + Send;
@@ -76,6 +81,20 @@ impl<V> Clone for View<V> {
     }
 }
 
+impl<V> Hash for View<V> {
+    fn hash<H: Hasher>(&self, state: &mut H) {
+        self.model.hash(state);
+    }
+}
+
+impl<V> PartialEq for View<V> {
+    fn eq(&self, other: &Self) -> bool {
+        self.model == other.model
+    }
+}
+
+impl<V> Eq for View<V> {}
+
 impl<V: Render, ParentViewState: 'static> Component<ParentViewState> for View<V> {
     fn render(self) -> AnyElement<ParentViewState> {
         AnyElement::new(EraseViewState {