Skip to content

feat!: re-design GetTypeDependencies trait & add GetTypeDependencies derive macro #369

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 2 commits into from
Mar 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::{ffi::OsString, path::PathBuf};

use crate::{
bindings::{ScriptValue, ReflectReference},
docgen::TypedThrough,
bindings::{ReflectReference, ScriptValue},
docgen::TypedThrough, error::InteropError,
};

use super::{
Expand Down Expand Up @@ -77,6 +77,8 @@ impl<T> ArgMeta for Val<T> {}
impl<T> ArgMeta for Ref<'_, T> {}
impl<T> ArgMeta for Mut<'_, T> {}

impl<T> ArgMeta for Result<T, InteropError> {}

impl<T> ArgMeta for Option<T> {
fn default_value() -> Option<ScriptValue> {
Some(ScriptValue::Unit)
Expand Down
19 changes: 19 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 @@ -469,6 +469,17 @@ where
pub struct Union<T1, T2>(Result<T1, T2>);

impl<T1, T2> Union<T1, T2> {
/// Create a new union with the left value.
pub fn new_left(value: T1) -> Self {
Union(Ok(value))
}

/// Create a new union with the right value.
pub fn new_right(value: T2) -> Self {
Union(Err(value))
}


/// Try interpret the union as the left type
pub fn into_left(self) -> Result<T1, T2> {
match self.0 {
Expand All @@ -484,6 +495,14 @@ impl<T1, T2> Union<T1, T2> {
Ok(l) => Err(l),
}
}

/// Map the union to another type
pub fn map_both<U1, U2, F: Fn(T1) -> U1, G: Fn(T2) -> U2>(self, f: F, g: G) -> Union<U1, U2> {
match self.0 {
Ok(t) => Union(Ok(f(t))),
Err(t) => Union(Err(g(t))),
}
}
}

impl<T1: FromScript, T2: FromScript> FromScript for Union<T1, T2>
Expand Down
25 changes: 14 additions & 11 deletions crates/bevy_mod_scripting_core/src/bindings/function/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::{borrow::Cow, collections::HashMap, ffi::OsString, path::PathBuf};
use bevy::reflect::Reflect;

use crate::{bindings::{ReflectReference, ScriptValue, WorldGuard}, error::InteropError, private::self_type_dependency_only};
use super::{DynamicScriptFunction, DynamicScriptFunctionMut, Val};
use crate::{bindings::{ReflectReference, ScriptValue, WorldGuard}, error::InteropError};
use super::{DynamicScriptFunction, DynamicScriptFunctionMut, Union, Val};

/// Converts a value into a [`ScriptValue`].
pub trait IntoScript {
Expand All @@ -26,14 +26,13 @@ impl IntoScript for ScriptValue {
}
}

self_type_dependency_only!(ScriptValue);

impl IntoScript for () {
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
Ok(ScriptValue::Unit)
}
}
self_type_dependency_only!(());


impl IntoScript for DynamicScriptFunctionMut {
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
Expand All @@ -47,14 +46,12 @@ impl IntoScript for DynamicScriptFunction {
}
}

self_type_dependency_only!(DynamicScriptFunctionMut, DynamicScriptFunction);

impl IntoScript for bool {
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
Ok(ScriptValue::Bool(self))
}
}
self_type_dependency_only!(bool);

macro_rules! impl_into_with_downcast {
($variant:tt as $cast:ty [$($ty:ty),*]) => {
Expand All @@ -71,9 +68,7 @@ macro_rules! impl_into_with_downcast {

impl_into_with_downcast!(Integer as i64 [i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize]);
impl_into_with_downcast!(Float as f64 [f32, f64]);
self_type_dependency_only!(
i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize, f32, f64
);


macro_rules! impl_into_stringlike {
($id:ident,[ $(($ty:ty => $conversion:expr)),*]) => {
Expand All @@ -99,15 +94,14 @@ impl_into_stringlike!(
]
);

self_type_dependency_only!(String, char, PathBuf, OsString);

impl IntoScript for &'static str {
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
Ok(ScriptValue::String(Cow::Borrowed(self)))
}
}

self_type_dependency_only!(&'static str);


impl IntoScript for ReflectReference {
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
Expand Down Expand Up @@ -156,6 +150,15 @@ impl<T: IntoScript, const N: usize> IntoScript for [T; N] {
}
}

impl <T1: IntoScript, T2: IntoScript> IntoScript for Union<T1,T2> {
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
match self.into_left() {
Ok(left) => left.into_script(world),
Err(right) => right.into_script(world),
}
}
}

impl<V: IntoScript> IntoScript for HashMap<String, V> {
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
let mut map = HashMap::new();
Expand Down
40 changes: 23 additions & 17 deletions crates/bevy_mod_scripting_core/src/bindings/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ mod test {
use bevy::reflect::{FromReflect, GetTypeRegistration, Reflect, Typed};
use bevy_mod_scripting_derive::script_bindings;

use crate::{
bindings::{
use crate::bindings::{
function::{
from::{Ref, Union, Val},
namespace::IntoNamespace,
script_function::AppScriptFunctionRegistry,
},
script_value::ScriptValue,
},
docgen::typed_through::TypedThrough,
};
}, script_value::ScriptValue
};

use super::arg_meta::{ScriptArgument, ScriptReturn, TypedScriptArgument, TypedScriptReturn};

Expand Down Expand Up @@ -137,19 +133,29 @@ mod test {
test_is_valid_arg::<Ref<'_, T>>();
}

fn test_union<T>()
where
T: TypedScriptArgument + TypedScriptReturn,
T::Underlying: FromReflect + Typed + GetTypeRegistration,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<Union<T, T>>();
test_is_valid_arg_and_return::<Union<T, Union<T, T>>>();
}

fn test_array<T, const N: usize>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + TypedThrough + Typed,
T: TypedScriptArgument + TypedScriptReturn + 'static,
T::Underlying: FromReflect + Typed + GetTypeRegistration,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<[T; N]>();
}

fn test_tuple<T>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + TypedThrough + Typed,
T: TypedScriptArgument + TypedScriptReturn + 'static,
T::Underlying: FromReflect + Typed + GetTypeRegistration,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<()>();
Expand All @@ -160,26 +166,26 @@ mod test {

fn test_option<T>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + Typed + TypedThrough,
T: TypedScriptArgument + TypedScriptReturn,
T::Underlying: FromReflect + Typed + GetTypeRegistration,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<Option<T>>();
}

fn test_vec<T>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + Typed + TypedThrough,
T: TypedScriptArgument + TypedScriptReturn + 'static,
T::Underlying: FromReflect + Typed + GetTypeRegistration,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<Vec<T>>();
}

fn test_hashmap<V>()
where
V: ScriptArgument + ScriptReturn,
V: GetTypeRegistration + FromReflect + Typed + TypedThrough,
V: TypedScriptArgument + TypedScriptReturn + 'static,
V::Underlying: FromReflect + Typed + GetTypeRegistration + Eq,
for<'a> V::This<'a>: Into<V>,
{
test_is_valid_arg_and_return::<std::collections::HashMap<String, V>>();
Expand Down
Loading
Loading