From 2e2c150ed12560b5f1bc6719b59414f751631df4 Mon Sep 17 00:00:00 2001 From: Aaron Erhardt Date: Tue, 8 Feb 2022 10:58:16 +0100 Subject: [PATCH 1/2] Improve AnyScope API Signed-off-by: Aaron Erhardt --- packages/yew/src/html/component/scope.rs | 35 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/yew/src/html/component/scope.rs b/packages/yew/src/html/component/scope.rs index d93f48586d6..0ccce4104c0 100644 --- a/packages/yew/src/html/component/scope.rs +++ b/packages/yew/src/html/component/scope.rs @@ -113,23 +113,34 @@ impl AnyScope { } /// Attempts to downcast into a typed scope + /// + /// # Panics + /// + /// If the self value can't be cast into the target type. pub fn downcast(self) -> Scope { + self.try_downcast::().unwrap() + } + + /// Attempts to downcast into a typed scope + /// + /// Returns [`None`] if the self value can't be cast into the target type. + pub fn try_downcast(self) -> Option> { let state = self.state.borrow(); - state - .as_ref() - .map(|m| { - m.inner - .as_any() - .downcast_ref::>() - .unwrap() - .context - .link() - .clone() - }) - .unwrap() + state.as_ref().map(|m| { + m.inner + .as_any() + .downcast_ref::>() + .unwrap() + .context + .link() + .clone() + }) } + /// Attempts to find a parent scope of a certain type + /// + /// Returns [`None`] if no parent scope with the specified type was found. pub(crate) fn find_parent_scope(&self) -> Option> { let expected_type_id = TypeId::of::(); iter::successors(Some(self), |scope| scope.get_parent()) From ae73adfc5f95c9694281ffc41b7588045f350cc5 Mon Sep 17 00:00:00 2001 From: Aaron Erhardt Date: Tue, 8 Feb 2022 15:24:58 +0100 Subject: [PATCH 2/2] Make find_parent_scope public Signed-off-by: Aaron Erhardt --- packages/yew/src/html/component/scope.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/yew/src/html/component/scope.rs b/packages/yew/src/html/component/scope.rs index 0ccce4104c0..6229f3b3a57 100644 --- a/packages/yew/src/html/component/scope.rs +++ b/packages/yew/src/html/component/scope.rs @@ -141,7 +141,7 @@ impl AnyScope { /// Attempts to find a parent scope of a certain type /// /// Returns [`None`] if no parent scope with the specified type was found. - pub(crate) fn find_parent_scope(&self) -> Option> { + pub fn find_parent_scope(&self) -> Option> { let expected_type_id = TypeId::of::(); iter::successors(Some(self), |scope| scope.get_parent()) .filter(|scope| scope.get_type_id() == &expected_type_id)