Skip to content

feat: add construct global for constructing arbitrary types & Union type #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_mod_scripting_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ bevy_mod_scripting_derive = { workspace = true }
[dev-dependencies]
test_utils = { workspace = true }
tokio = { version = "1", features = ["rt", "macros"] }
pretty_assertions = "1.4"

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};

use super::{
from::{FromScript, Mut, Ref, Val},
from::{FromScript, Mut, Ref, Union, Val},
into::IntoScript,
script_function::{DynamicScriptFunction, DynamicScriptFunctionMut, FunctionCallContext},
type_dependencies::GetTypeDependencies,
Expand Down Expand Up @@ -72,6 +72,7 @@ impl_arg_info!(
&'static str
);

impl<T1, T2> ArgMeta for Union<T1, T2> {}
impl<T> ArgMeta for Val<T> {}
impl<T> ArgMeta for Ref<'_, T> {}
impl<T> ArgMeta for Mut<'_, T> {}
Expand Down
43 changes: 43 additions & 0 deletions crates/bevy_mod_scripting_core/src/bindings/function/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,46 @@ where
}
}
}

/// A union of two or more (by nesting unions) types.
pub struct Union<T1, T2>(Result<T1, T2>);

impl<T1, T2> Union<T1, T2> {
/// Try interpret the union as the left type
pub fn into_left(self) -> Result<T1, T2> {
match self.0 {
Ok(r) => Ok(r),
Err(l) => Err(l),
}
}

/// Try interpret the union as the right type
pub fn into_right(self) -> Result<T2, T1> {
match self.0 {
Err(r) => Ok(r),
Ok(l) => Err(l),
}
}
}

impl<T1: FromScript, T2: FromScript> FromScript for Union<T1, T2>
where
for<'a> T1::This<'a>: Into<T1>,
for<'a> T2::This<'a>: Into<T2>,
{
type This<'w> = Self;
fn from_script(
value: ScriptValue,
world: WorldGuard<'_>,
) -> Result<Self::This<'_>, InteropError> {
let _ = match T1::from_script(value.clone(), world.clone()) {
Ok(v) => return Ok(Union(Ok(v.into()))),
Err(e) => e,
};

match T2::from_script(value, world) {
Ok(v) => Ok(Union(Err(v.into()))),
Err(e) => Err(e),
}
}
}
4 changes: 3 additions & 1 deletion crates/bevy_mod_scripting_core/src/bindings/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod test {
use crate::{
bindings::{
function::{
from::{Ref, Val},
from::{Ref, Union, Val},
namespace::IntoNamespace,
script_function::AppScriptFunctionRegistry,
},
Expand Down Expand Up @@ -109,6 +109,8 @@ mod test {

#[test]
fn composites_are_valid_args() {
test_is_valid_arg::<Union<usize, usize>>();

fn test_val<T>()
where
T: ScriptArgument + ScriptReturn,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module contains the [`GetTypeDependencies`] trait and its implementations for various types.

use super::{
from::{Mut, Ref, Val},
from::{Mut, Ref, Union, Val},
script_function::FunctionCallContext,
};
use crate::{
Expand Down Expand Up @@ -88,6 +88,13 @@ recursive_type_dependencies!(
(HashMap<K,V> where K: GetTypeRegistration;FromReflect;Typed;Hash;Eq, V: GetTypeRegistration;FromReflect;Typed => with Self)
);

impl<T1: GetTypeDependencies, T2: GetTypeDependencies> GetTypeDependencies for Union<T1, T2> {
fn register_type_dependencies(registry: &mut TypeRegistry) {
T1::register_type_dependencies(registry);
T2::register_type_dependencies(registry);
}
}

bevy::utils::all_tuples!(register_tuple_dependencies, 1, 14, T);

/// A trait collecting type dependency information for a whole function. Used to register everything used by a function with the type registry
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_mod_scripting_core/src/bindings/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ impl ScriptResourceRegistration {
pub fn type_registration(&self) -> &ScriptTypeRegistration {
&self.registration
}

/// Convert to a generic [`ScriptTypeRegistration`] ditching the resource information.
pub fn into_type_registration(self) -> ScriptTypeRegistration {
self.registration
}
}

impl ScriptComponentRegistration {
Expand All @@ -101,6 +106,11 @@ impl ScriptComponentRegistration {
pub fn type_registration(&self) -> &ScriptTypeRegistration {
&self.registration
}

/// Convert to a generic [`ScriptTypeRegistration`] ditching the component information.
pub fn into_type_registration(self) -> ScriptTypeRegistration {
self.registration
}
}

impl std::fmt::Debug for ScriptTypeRegistration {
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_mod_scripting_core/src/bindings/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ pub enum TypeIdSource {
}
#[profiling::all_functions]
impl ReflectReference {
/// If this points to a variant of an enum, returns the name of the variant.
pub fn variant_name(&self, world: WorldGuard) -> Result<Option<String>, InteropError> {
self.with_reflect(world, |s| {
s.reflect_ref()
.as_enum()
.ok()
.map(|enum_ref| enum_ref.variant_name().to_owned())
})
}

/// Creates a new infinite iterator. This iterator will keep returning the next element reference forever.
pub fn into_iter_infinite(self) -> ReflectRefIter {
ReflectRefIter::new_indexed(self)
Expand Down
Loading
Loading