Commit 36058d4 1 parent 6fc9ef4 commit 36058d4 Copy full SHA for 36058d4
File tree 1 file changed +24
-13
lines changed
packages/yew/src/html/component
1 file changed +24
-13
lines changed Original file line number Diff line number Diff line change @@ -113,24 +113,35 @@ impl AnyScope {
113
113
}
114
114
115
115
/// Attempts to downcast into a typed scope
116
+ ///
117
+ /// # Panics
118
+ ///
119
+ /// If the self value can't be cast into the target type.
116
120
pub fn downcast < COMP : BaseComponent > ( self ) -> Scope < COMP > {
121
+ self . try_downcast :: < COMP > ( ) . unwrap ( )
122
+ }
123
+
124
+ /// Attempts to downcast into a typed scope
125
+ ///
126
+ /// Returns [`None`] if the self value can't be cast into the target type.
127
+ pub fn try_downcast < COMP : BaseComponent > ( self ) -> Option < Scope < COMP > > {
117
128
let state = self . state . borrow ( ) ;
118
129
119
- state
120
- . as_ref ( )
121
- . map ( |m| {
122
- m. inner
123
- . as_any ( )
124
- . downcast_ref :: < CompStateInner < COMP > > ( )
125
- . unwrap ( )
126
- . context
127
- . link ( )
128
- . clone ( )
129
- } )
130
- . unwrap ( )
130
+ state. as_ref ( ) . map ( |m| {
131
+ m. inner
132
+ . as_any ( )
133
+ . downcast_ref :: < CompStateInner < COMP > > ( )
134
+ . unwrap ( )
135
+ . context
136
+ . link ( )
137
+ . clone ( )
138
+ } )
131
139
}
132
140
133
- pub ( crate ) fn find_parent_scope < C : BaseComponent > ( & self ) -> Option < Scope < C > > {
141
+ /// Attempts to find a parent scope of a certain type
142
+ ///
143
+ /// Returns [`None`] if no parent scope with the specified type was found.
144
+ pub fn find_parent_scope < C : BaseComponent > ( & self ) -> Option < Scope < C > > {
134
145
let expected_type_id = TypeId :: of :: < C > ( ) ;
135
146
iter:: successors ( Some ( self ) , |scope| scope. get_parent ( ) )
136
147
. filter ( |scope| scope. get_type_id ( ) == & expected_type_id)
You can’t perform that action at this time.
0 commit comments