@@ -14,6 +14,9 @@ pub trait PartialReflectExt {
14
14
/// If the type is an option, returns either the inner value or None if the option is None.
15
15
/// Errors if the type is not an option.
16
16
fn as_option ( & self ) -> Result < Option < & dyn PartialReflect > , ScriptError > ;
17
+
18
+ /// If the type is an iterable list-like type, returns an iterator over the elements.
19
+ fn as_list ( & self ) -> Result < impl Iterator < Item = & dyn PartialReflect > , ScriptError > ;
17
20
}
18
21
19
22
impl < T : PartialReflect + ?Sized > PartialReflectExt for T {
@@ -39,8 +42,6 @@ impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
39
42
Ok ( ( ) )
40
43
}
41
44
42
- /// If the type is an option, returns either the inner value or None if the option is None.
43
- /// Errors if the type is not an option.
44
45
fn as_option ( & self ) -> Result < Option < & dyn PartialReflect > , ScriptError > {
45
46
self . expect_type ( Some ( "core" ) , "Option" ) ?;
46
47
@@ -54,6 +55,19 @@ impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
54
55
55
56
unreachable ! ( "core::Option is an enum with a tuple variant" )
56
57
}
58
+
59
+ fn as_list ( & self ) -> Result < impl Iterator < Item = & dyn PartialReflect > , ScriptError > {
60
+ if let bevy:: reflect:: ReflectRef :: List ( l) = self . reflect_ref ( ) {
61
+ Ok ( l. iter ( ) )
62
+ } else {
63
+ Err ( ScriptError :: new_runtime_error ( format ! (
64
+ "Expected list-like type from crate core, but got {}" ,
65
+ self . get_represented_type_info( )
66
+ . map( |ti| ti. type_path( ) )
67
+ . unwrap_or_else( || "dynamic type with no type information" )
68
+ ) ) )
69
+ }
70
+ }
57
71
}
58
72
59
73
#[ cfg( test) ]
0 commit comments