@@ -14,6 +14,9 @@ pub trait PartialReflectExt {
1414 /// If the type is an option, returns either the inner value or None if the option is None.
1515 /// Errors if the type is not an option.
1616 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 > ;
1720}
1821
1922impl < T : PartialReflect + ?Sized > PartialReflectExt for T {
@@ -39,8 +42,6 @@ impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
3942 Ok ( ( ) )
4043 }
4144
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.
4445 fn as_option ( & self ) -> Result < Option < & dyn PartialReflect > , ScriptError > {
4546 self . expect_type ( Some ( "core" ) , "Option" ) ?;
4647
@@ -54,6 +55,19 @@ impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
5455
5556 unreachable ! ( "core::Option is an enum with a tuple variant" )
5657 }
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+ }
5771}
5872
5973#[ cfg( test) ]
0 commit comments