summaryrefslogtreecommitdiffstats
path: root/pure/src/widget/tree.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-02-16 15:44:50 +0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-02-16 15:44:50 +0700
commit35e9b75e415ef3b9124051696b60628ef56afe47 (patch)
treedc81a8bfa59634072d2e439d32bdf263dd4f610a /pure/src/widget/tree.rs
parentcff891833be68c0e2d4919d4475daf23da821f9b (diff)
downloadiced-35e9b75e415ef3b9124051696b60628ef56afe47.tar.gz
iced-35e9b75e415ef3b9124051696b60628ef56afe47.tar.bz2
iced-35e9b75e415ef3b9124051696b60628ef56afe47.zip
Introduce `Tag` and `State` opaque types in `iced_pure::widget::tree`
Diffstat (limited to 'pure/src/widget/tree.rs')
-rw-r--r--pure/src/widget/tree.rs52
1 files changed, 44 insertions, 8 deletions
diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs
index 3a5f4433..33f5693a 100644
--- a/pure/src/widget/tree.rs
+++ b/pure/src/widget/tree.rs
@@ -3,7 +3,7 @@ use crate::widget::Element;
use std::any::{self, Any};
pub struct Tree {
- pub tag: any::TypeId,
+ pub tag: Tag,
pub state: State,
pub children: Vec<Tree>,
}
@@ -11,8 +11,8 @@ pub struct Tree {
impl Tree {
pub fn empty() -> Self {
Self {
- tag: any::TypeId::of::<()>(),
- state: State(Box::new(())),
+ tag: Tag::stateless(),
+ state: State::None,
children: Vec::new(),
}
}
@@ -22,8 +22,8 @@ impl Tree {
) -> Self {
Self {
tag: element.as_widget().tag(),
- state: State(element.as_widget().state()),
- children: element.as_widget().children_state(),
+ state: element.as_widget().state(),
+ children: element.as_widget().children(),
}
}
@@ -60,20 +60,56 @@ impl Tree {
}
}
-pub struct State(Box<dyn Any>);
+#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
+pub struct Tag(any::TypeId);
+
+impl Tag {
+ pub fn of<T>() -> Self
+ where
+ T: 'static,
+ {
+ Self(any::TypeId::of::<T>())
+ }
+
+ pub fn stateless() -> Self {
+ Self::of::<()>()
+ }
+}
+
+pub enum State {
+ None,
+ Some(Box<dyn Any>),
+}
impl State {
+ pub fn new<T>(state: T) -> Self
+ where
+ T: 'static,
+ {
+ State::Some(Box::new(state))
+ }
+
pub fn downcast_ref<T>(&self) -> &T
where
T: 'static,
{
- self.0.downcast_ref().expect("Downcast widget state")
+ match self {
+ State::None => panic!("Downcast on stateless state"),
+ State::Some(state) => {
+ state.downcast_ref().expect("Downcast widget state")
+ }
+ }
}
pub fn downcast_mut<T>(&mut self) -> &mut T
where
T: 'static,
{
- self.0.downcast_mut().expect("Downcast widget state")
+ match self {
+ State::None => panic!("Downcast on stateless state"),
+ State::Some(state) => {
+ state.downcast_mut().expect("Downcast widget state")
+ }
+ }
}
}