diff --git a/packages/yew/src/html/component/scope.rs b/packages/yew/src/html/component/scope.rs index d93f48586d6..6229f3b3a57 100644 --- a/packages/yew/src/html/component/scope.rs +++ b/packages/yew/src/html/component/scope.rs @@ -113,24 +113,35 @@ 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() + }) } - pub(crate) fn find_parent_scope(&self) -> Option> { + /// Attempts to find a parent scope of a certain type + /// + /// Returns [`None`] if no parent scope with the specified type was found. + 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)