Skip to content

Commit 1970870

Browse files
committed
more typed handles + removed duplicated associated type
1 parent f9b5b04 commit 1970870

File tree

3 files changed

+60
-51
lines changed

3 files changed

+60
-51
lines changed

fyrox-graph/src/lib.rs

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ impl<N> Default for LinkScheme<N> {
672672

673673
/// SceneGraph is a trait for all scene graphs to implement.
674674
pub trait SceneGraph: 'static {
675-
type ObjectType: Sized;
676675
type Prefab: PrefabData<Graph = Self>;
677676
type NodeContainer: PayloadContainer<Element = Self::Node>;
678677
type Node: SceneGraphNode<SceneGraph = Self, ResourceData = Self::Prefab>;
@@ -695,10 +694,10 @@ pub trait SceneGraph: 'static {
695694
/// Sets the new root of the graph. If used incorrectly, it may create isolated sub-graphs.
696695
fn set_root(&mut self, root: Handle<Self::Node>);
697696

698-
/// Tries to borrow a node, returns Some(node) if the handle is valid, None - otherwise.
697+
/// Tries to borrow a node, returns Ok(node) if the handle is valid, Err(...) - otherwise.
699698
fn try_get_node(&self, handle: Handle<Self::Node>) -> Result<&Self::Node, PoolError>;
700699

701-
/// Tries to borrow a node, returns Some(node) if the handle is valid, None - otherwise.
700+
/// Tries to borrow a node, returns Ok(node) if the handle is valid, Err(...) - otherwise.
702701
fn try_get_node_mut(
703702
&mut self,
704703
handle: Handle<Self::Node>,
@@ -727,16 +726,32 @@ pub trait SceneGraph: 'static {
727726
/// graph.
728727
fn isolate_node(&mut self, node_handle: Handle<impl ObjectOrVariant<Self::Node>>);
729728

729+
/// Creates new iterator that iterates over internal collection giving (handle; node) pairs.
730+
fn pair_iter(&self) -> impl Iterator<Item = (Handle<Self::Node>, &Self::Node)>;
731+
732+
/// Creates an iterator that has linear iteration order over internal collection
733+
/// of nodes. It does *not* perform any tree traversal!
734+
fn linear_iter(&self) -> impl Iterator<Item = &Self::Node>;
735+
736+
/// Creates an iterator that has linear iteration order over internal collection
737+
/// of nodes. It does *not* perform any tree traversal!
738+
fn linear_iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Node>;
739+
740+
fn try_get<U: ObjectOrVariant<Self::Node>>(&self, handle: Handle<U>) -> Result<&U, PoolError>;
741+
742+
fn try_get_mut<U: ObjectOrVariant<Self::Node>>(
743+
&mut self,
744+
handle: Handle<U>,
745+
) -> Result<&mut U, PoolError>;
746+
730747
/// Borrows a node by its handle.
731748
fn node(&self, handle: Handle<Self::Node>) -> &Self::Node {
732-
self.try_get_node(handle)
733-
.expect("The handle must be valid!")
749+
self.try_get_node(handle).unwrap()
734750
}
735751

736752
/// Borrows a node by its handle.
737753
fn node_mut(&mut self, handle: Handle<Self::Node>) -> &mut Self::Node {
738-
self.try_get_node_mut(handle)
739-
.expect("The handle must be valid!")
754+
self.try_get_node_mut(handle).unwrap()
740755
}
741756

742757
/// Reorders the node hierarchy so the `new_root` becomes the root node for the entire hierarchy
@@ -767,9 +782,12 @@ pub trait SceneGraph: 'static {
767782
#[allow(clippy::unnecessary_to_owned)] // False-positive
768783
fn change_hierarchy_root(
769784
&mut self,
770-
prev_root: Handle<Self::Node>,
771-
new_root: Handle<Self::Node>,
785+
prev_root: Handle<impl ObjectOrVariant<Self::Node>>,
786+
new_root: Handle<impl ObjectOrVariant<Self::Node>>,
772787
) -> LinkScheme<Self::Node> {
788+
let prev_root = prev_root.to_base();
789+
let new_root = new_root.to_base();
790+
773791
let mut scheme = LinkScheme::default();
774792

775793
if prev_root == new_root {
@@ -819,35 +837,14 @@ pub trait SceneGraph: 'static {
819837

820838
/// Removes all the nodes from the given slice.
821839
#[inline]
822-
fn remove_nodes(&mut self, nodes: &[Handle<Self::Node>]) {
840+
fn remove_nodes(&mut self, nodes: &[Handle<impl ObjectOrVariant<Self::Node>>]) {
823841
for &node in nodes {
824842
if self.is_valid_handle(node) {
825843
self.remove_node(node)
826844
}
827845
}
828846
}
829847

830-
/// Creates new iterator that iterates over internal collection giving (handle; node) pairs.
831-
fn pair_iter(&self) -> impl Iterator<Item = (Handle<Self::Node>, &Self::Node)>;
832-
833-
/// Creates an iterator that has linear iteration order over internal collection
834-
/// of nodes. It does *not* perform any tree traversal!
835-
fn linear_iter(&self) -> impl Iterator<Item = &Self::Node>;
836-
837-
/// Creates an iterator that has linear iteration order over internal collection
838-
/// of nodes. It does *not* perform any tree traversal!
839-
fn linear_iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Node>;
840-
841-
fn try_get<U: ObjectOrVariant<Self::ObjectType>>(
842-
&self,
843-
handle: Handle<U>,
844-
) -> Result<&U, PoolError>;
845-
846-
fn try_get_mut<U: ObjectOrVariant<Self::ObjectType>>(
847-
&mut self,
848-
handle: Handle<U>,
849-
) -> Result<&mut U, PoolError>;
850-
851848
/// Tries to borrow a node and fetch its component of specified type.
852849
#[inline]
853850
fn try_get_of_type<T>(&self, handle: Handle<Self::Node>) -> Result<&T, PoolError>
@@ -885,25 +882,26 @@ pub trait SceneGraph: 'static {
885882
/// Tries to borrow a node by the given handle and checks if it has a component of the specified
886883
/// type.
887884
#[inline]
888-
fn has_component<T>(&self, handle: Handle<Self::Node>) -> bool
885+
fn has_component<T>(&self, handle: Handle<impl ObjectOrVariant<Self::Node>>) -> bool
889886
where
890887
T: 'static,
891888
{
892-
self.try_get_of_type::<T>(handle).is_ok()
889+
self.try_get_of_type::<T>(handle.to_base()).is_ok()
893890
}
894891

895892
/// Searches for a node down the tree starting from the specified node using the specified closure. Returns a tuple
896893
/// with a handle and a reference to the mapped value. If nothing is found, it returns [`None`].
897894
#[inline]
898895
fn find_map<C, T>(
899896
&self,
900-
root_node: Handle<Self::Node>,
897+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
901898
cmp: &mut C,
902899
) -> Option<(Handle<Self::Node>, &T)>
903900
where
904901
C: FnMut(&Self::Node) -> Option<&T>,
905902
T: ?Sized,
906903
{
904+
let root_node = root_node.to_base();
907905
self.try_get_node(root_node).ok().and_then(|root| {
908906
if let Some(x) = cmp(root) {
909907
Some((root_node, x))
@@ -918,13 +916,13 @@ pub trait SceneGraph: 'static {
918916
#[inline]
919917
fn find_up<C>(
920918
&self,
921-
root_node: Handle<Self::Node>,
919+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
922920
cmp: &mut C,
923921
) -> Option<(Handle<Self::Node>, &Self::Node)>
924922
where
925923
C: FnMut(&Self::Node) -> bool,
926924
{
927-
let mut handle = root_node;
925+
let mut handle = root_node.to_base();
928926
while let Ok(node) = self.try_get_node(handle) {
929927
if cmp(node) {
930928
return Some((handle, node));
@@ -937,7 +935,11 @@ pub trait SceneGraph: 'static {
937935
/// The same as [`Self::find_up`], but only returns node handle which will be [`Handle::NONE`]
938936
/// if nothing is found.
939937
#[inline]
940-
fn find_handle_up<C>(&self, root_node: Handle<Self::Node>, cmp: &mut C) -> Handle<Self::Node>
938+
fn find_handle_up<C>(
939+
&self,
940+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
941+
cmp: &mut C,
942+
) -> Handle<Self::Node>
941943
where
942944
C: FnMut(&Self::Node) -> bool,
943945
{
@@ -949,7 +951,7 @@ pub trait SceneGraph: 'static {
949951
#[inline]
950952
fn find_component_up<T>(
951953
&self,
952-
node_handle: Handle<Self::Node>,
954+
node_handle: Handle<impl ObjectOrVariant<Self::Node>>,
953955
) -> Option<(Handle<Self::Node>, &T)>
954956
where
955957
T: 'static,
@@ -961,7 +963,10 @@ pub trait SceneGraph: 'static {
961963
}
962964

963965
#[inline]
964-
fn find_component<T>(&self, node_handle: Handle<Self::Node>) -> Option<(Handle<Self::Node>, &T)>
966+
fn find_component<T>(
967+
&self,
968+
node_handle: Handle<impl ObjectOrVariant<Self::Node>>,
969+
) -> Option<(Handle<Self::Node>, &T)>
965970
where
966971
T: 'static,
967972
{
@@ -976,14 +981,14 @@ pub trait SceneGraph: 'static {
976981
#[inline]
977982
fn find_up_map<C, T>(
978983
&self,
979-
root_node: Handle<Self::Node>,
984+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
980985
cmp: &mut C,
981986
) -> Option<(Handle<Self::Node>, &T)>
982987
where
983988
C: FnMut(&Self::Node) -> Option<&T>,
984989
T: ?Sized,
985990
{
986-
let mut handle = root_node;
991+
let mut handle = root_node.to_base();
987992
while let Ok(node) = self.try_get_node(handle) {
988993
if let Some(x) = cmp(node) {
989994
return Some((handle, x));
@@ -998,7 +1003,7 @@ pub trait SceneGraph: 'static {
9981003
#[inline]
9991004
fn find_by_name(
10001005
&self,
1001-
root_node: Handle<Self::Node>,
1006+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
10021007
name: &str,
10031008
) -> Option<(Handle<Self::Node>, &Self::Node)> {
10041009
self.find(root_node, &mut |node| node.name() == name)
@@ -1009,7 +1014,7 @@ pub trait SceneGraph: 'static {
10091014
#[inline]
10101015
fn find_up_by_name(
10111016
&self,
1012-
root_node: Handle<Self::Node>,
1017+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
10131018
name: &str,
10141019
) -> Option<(Handle<Self::Node>, &Self::Node)> {
10151020
self.find_up(root_node, &mut |node| node.name() == name)
@@ -1045,12 +1050,13 @@ pub trait SceneGraph: 'static {
10451050
#[inline]
10461051
fn find<C>(
10471052
&self,
1048-
root_node: Handle<Self::Node>,
1053+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
10491054
cmp: &mut C,
10501055
) -> Option<(Handle<Self::Node>, &Self::Node)>
10511056
where
10521057
C: FnMut(&Self::Node) -> bool,
10531058
{
1059+
let root_node = root_node.to_base();
10541060
self.try_get_node(root_node).ok().and_then(|root| {
10551061
if cmp(root) {
10561062
Some((root_node, root))
@@ -1063,7 +1069,11 @@ pub trait SceneGraph: 'static {
10631069
/// The same as [`Self::find`], but only returns node handle which will be [`Handle::NONE`]
10641070
/// if nothing is found.
10651071
#[inline]
1066-
fn find_handle<C>(&self, root_node: Handle<Self::Node>, cmp: &mut C) -> Handle<Self::Node>
1072+
fn find_handle<C>(
1073+
&self,
1074+
root_node: Handle<impl ObjectOrVariant<Self::Node>>,
1075+
cmp: &mut C,
1076+
) -> Handle<Self::Node>
10671077
where
10681078
C: FnMut(&Self::Node) -> bool,
10691079
{
@@ -1087,9 +1097,10 @@ pub trait SceneGraph: 'static {
10871097
#[inline]
10881098
fn relative_position(
10891099
&self,
1090-
child: Handle<Self::Node>,
1100+
child: Handle<impl ObjectOrVariant<Self::Node>>,
10911101
offset: isize,
10921102
) -> Option<(Handle<Self::Node>, usize)> {
1103+
let child = child.to_base();
10931104
let parents_parent_handle = self.try_get_node(child).ok()?.parent();
10941105
let parents_parent_ref = self.try_get_node(parents_parent_handle).ok()?;
10951106
let position = parents_parent_ref.child_position(child)?;
@@ -1103,8 +1114,9 @@ pub trait SceneGraph: 'static {
11031114
#[inline]
11041115
fn traverse_iter(
11051116
&self,
1106-
from: Handle<Self::Node>,
1117+
from: Handle<impl ObjectOrVariant<Self::Node>>,
11071118
) -> impl Iterator<Item = (Handle<Self::Node>, &Self::Node)> {
1119+
let from = from.to_base();
11081120
self.try_get_node(from).expect("Handle must be valid!");
11091121
GraphTraverseIterator {
11101122
graph: self,
@@ -1116,7 +1128,7 @@ pub trait SceneGraph: 'static {
11161128
#[inline]
11171129
fn traverse_handle_iter(
11181130
&self,
1119-
from: Handle<Self::Node>,
1131+
from: Handle<impl ObjectOrVariant<Self::Node>>,
11201132
) -> impl Iterator<Item = Handle<Self::Node>> {
11211133
self.traverse_iter(from).map(|(handle, _)| handle)
11221134
}
@@ -1792,7 +1804,6 @@ mod test {
17921804
}
17931805

17941806
impl SceneGraph for Graph {
1795-
type ObjectType = Node;
17961807
type Prefab = Graph;
17971808
type NodeContainer = NodeContainer;
17981809
type Node = Node;

fyrox-impl/src/scene/graph/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,6 @@ impl Visit for Graph {
19121912
}
19131913

19141914
impl SceneGraph for Graph {
1915-
type ObjectType = Node;
19161915
type Prefab = Model;
19171916
type NodeContainer = NodeContainer;
19181917
type Node = Node;

fyrox-ui/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3504,7 +3504,6 @@ impl PrefabData for UserInterface {
35043504
}
35053505

35063506
impl SceneGraph for UserInterface {
3507-
type ObjectType = UiNode;
35083507
type Prefab = Self;
35093508
type NodeContainer = WidgetContainer;
35103509
type Node = UiNode;

0 commit comments

Comments
 (0)