Skip to content

Commit bf52605

Browse files
authored
feat: add TypedThrough abstraction to function meta, and refactor (#272)
* feat: add `TypeKind` to function meta, and refactor * add `TypedThrough` abstraction * add some more tests
1 parent 49d50b0 commit bf52605

File tree

6 files changed

+406
-88
lines changed

6 files changed

+406
-88
lines changed

crates/bevy_mod_scripting_core/src/bindings/function/arg_meta.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
33
use std::{ffi::OsString, path::PathBuf};
44

5-
use crate::bindings::{script_value::ScriptValue, ReflectReference};
5+
use crate::{
6+
bindings::{script_value::ScriptValue, ReflectReference},
7+
docgen::typed_through::TypedThrough,
8+
};
69

710
use super::{
811
from::{FromScript, Mut, Ref, Val},
@@ -15,10 +18,18 @@ use super::{
1518
pub trait ScriptArgument: ArgMeta + FromScript + GetTypeDependencies {}
1619
impl<T: ArgMeta + FromScript + GetTypeDependencies> ScriptArgument for T {}
1720

21+
/// Marker trait for types that can be used as arguments to a script function. And contain type information.
22+
pub trait TypedScriptArgument: TypedThrough + ScriptArgument {}
23+
impl<T: TypedThrough + ScriptArgument> TypedScriptArgument for T {}
24+
1825
/// Marker trait for types that can be used as return values from a script function.
1926
pub trait ScriptReturn: IntoScript + GetTypeDependencies {}
2027
impl<T: IntoScript + GetTypeDependencies> ScriptReturn for T {}
2128

29+
/// Marker trait for types that can be used as return values from a script function. And contain type information.
30+
pub trait TypedScriptReturn: TypedThrough + ScriptReturn {}
31+
impl<T: TypedThrough + ScriptReturn> TypedScriptReturn for T {}
32+
2233
/// Describes an argument to a script function. Provides necessary information for the function to handle dispatch.
2334
pub trait ArgMeta {
2435
/// The default value for the argument. Used when the argument is not provided.
@@ -37,13 +48,29 @@ macro_rules! impl_arg_info {
3748
};
3849
}
3950

40-
impl_arg_info!(bool, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, f32, f64, usize, isize);
41-
42-
impl_arg_info!(String, PathBuf, OsString);
43-
44-
impl_arg_info!(char);
45-
46-
impl_arg_info!(ReflectReference);
51+
impl_arg_info!(
52+
bool,
53+
i8,
54+
i16,
55+
i32,
56+
i64,
57+
i128,
58+
u8,
59+
u16,
60+
u32,
61+
u64,
62+
u128,
63+
f32,
64+
f64,
65+
usize,
66+
isize,
67+
String,
68+
PathBuf,
69+
OsString,
70+
char,
71+
ReflectReference,
72+
&'static str
73+
);
4774

4875
impl<T> ArgMeta for Val<T> {}
4976
impl<T> ArgMeta for Ref<'_, T> {}

crates/bevy_mod_scripting_core/src/bindings/function/mod.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ mod test {
1616
use bevy_mod_scripting_derive::script_bindings;
1717

1818
use crate::{
19-
bindings::function::{
20-
from::{Ref, Val},
21-
namespace::IntoNamespace,
22-
script_function::AppScriptFunctionRegistry,
19+
bindings::{
20+
function::{
21+
from::{Ref, Val},
22+
namespace::IntoNamespace,
23+
script_function::AppScriptFunctionRegistry,
24+
},
25+
script_value::ScriptValue,
2326
},
24-
error::InteropError,
27+
docgen::typed_through::TypedThrough,
2528
};
2629

27-
use super::arg_meta::{ScriptArgument, ScriptReturn};
30+
use super::arg_meta::{ScriptArgument, ScriptReturn, TypedScriptArgument, TypedScriptReturn};
2831

2932
#[test]
3033
fn test_macro_generates_correct_registrator_function() {
@@ -66,14 +69,14 @@ mod test {
6669
assert_eq!(test_fn.info.arg_info[1].name, Some("_arg1".into()));
6770

6871
assert_eq!(
69-
test_fn.info.return_info.type_id,
72+
test_fn.info.return_info.as_ref().unwrap().type_id,
7073
std::any::TypeId::of::<()>()
7174
);
7275
}
7376

74-
fn test_is_valid_return<T: ScriptReturn>() {}
75-
fn test_is_valid_arg<T: ScriptArgument>() {}
76-
fn test_is_valid_arg_and_return<T: ScriptArgument + ScriptReturn>() {}
77+
fn test_is_valid_return<T: TypedScriptReturn>() {}
78+
fn test_is_valid_arg<T: TypedScriptArgument>() {}
79+
fn test_is_valid_arg_and_return<T: TypedScriptReturn + TypedScriptArgument>() {}
7780

7881
#[test]
7982
fn primitives_are_valid_args() {
@@ -92,6 +95,7 @@ mod test {
9295
test_is_valid_arg_and_return::<f64>();
9396
test_is_valid_arg_and_return::<usize>();
9497
test_is_valid_arg_and_return::<isize>();
98+
test_is_valid_arg_and_return::<ScriptValue>();
9599
}
96100

97101
#[test]
@@ -100,14 +104,15 @@ mod test {
100104
test_is_valid_arg_and_return::<std::path::PathBuf>();
101105
test_is_valid_arg_and_return::<std::ffi::OsString>();
102106
test_is_valid_arg_and_return::<char>();
107+
test_is_valid_return::<&'static str>();
103108
}
104109

105110
#[test]
106111
fn composites_are_valid_args() {
107112
fn test_val<T>()
108113
where
109114
T: ScriptArgument + ScriptReturn,
110-
T: GetTypeRegistration + FromReflect,
115+
T: GetTypeRegistration + FromReflect + Typed,
111116
{
112117
test_is_valid_arg_and_return::<Val<T>>();
113118
}
@@ -128,12 +133,10 @@ mod test {
128133
test_is_valid_arg::<Ref<'_, T>>();
129134
}
130135

131-
test_is_valid_return::<InteropError>();
132-
133136
fn test_array<T, const N: usize>()
134137
where
135138
T: ScriptArgument + ScriptReturn,
136-
T: GetTypeRegistration + FromReflect + Typed,
139+
T: GetTypeRegistration + FromReflect + TypedThrough + Typed,
137140
for<'a> T::This<'a>: Into<T>,
138141
{
139142
test_is_valid_arg_and_return::<[T; N]>();
@@ -142,7 +145,7 @@ mod test {
142145
fn test_tuple<T>()
143146
where
144147
T: ScriptArgument + ScriptReturn,
145-
T: GetTypeRegistration + FromReflect + Typed,
148+
T: GetTypeRegistration + FromReflect + TypedThrough + Typed,
146149
for<'a> T::This<'a>: Into<T>,
147150
{
148151
test_is_valid_arg_and_return::<()>();
@@ -154,7 +157,7 @@ mod test {
154157
fn test_option<T>()
155158
where
156159
T: ScriptArgument + ScriptReturn,
157-
T: GetTypeRegistration + FromReflect + Typed,
160+
T: GetTypeRegistration + FromReflect + Typed + TypedThrough,
158161
for<'a> T::This<'a>: Into<T>,
159162
{
160163
test_is_valid_arg_and_return::<Option<T>>();
@@ -163,7 +166,7 @@ mod test {
163166
fn test_vec<T>()
164167
where
165168
T: ScriptArgument + ScriptReturn,
166-
T: GetTypeRegistration + FromReflect + Typed,
169+
T: GetTypeRegistration + FromReflect + Typed + TypedThrough,
167170
for<'a> T::This<'a>: Into<T>,
168171
{
169172
test_is_valid_arg_and_return::<Vec<T>>();
@@ -172,7 +175,7 @@ mod test {
172175
fn test_hashmap<V>()
173176
where
174177
V: ScriptArgument + ScriptReturn,
175-
V: GetTypeRegistration + FromReflect + Typed,
178+
V: GetTypeRegistration + FromReflect + Typed + TypedThrough,
176179
for<'a> V::This<'a>: Into<V>,
177180
{
178181
test_is_valid_arg_and_return::<std::collections::HashMap<String, V>>();

crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ pub struct DynamicScriptFunction {
7373
>,
7474
}
7575

76-
impl PartialEq for DynamicScriptFunction {
77-
fn eq(&self, other: &Self) -> bool {
78-
self.info == other.info
79-
}
80-
}
81-
8276
#[derive(Clone, Reflect)]
8377
#[reflect(opaque)]
8478
/// A dynamic mutable script function.
@@ -96,12 +90,6 @@ pub struct DynamicScriptFunctionMut {
9690
>,
9791
}
9892

99-
impl PartialEq for DynamicScriptFunctionMut {
100-
fn eq(&self, other: &Self) -> bool {
101-
self.info == other.info
102-
}
103-
}
104-
10593
impl DynamicScriptFunction {
10694
/// Call the function with the given arguments and caller context.
10795
///
@@ -215,6 +203,18 @@ impl DynamicScriptFunctionMut {
215203
}
216204
}
217205

206+
impl PartialEq for DynamicScriptFunction {
207+
fn eq(&self, other: &Self) -> bool {
208+
std::ptr::addr_eq(self as *const Self, other as *const Self)
209+
}
210+
}
211+
212+
impl PartialEq for DynamicScriptFunctionMut {
213+
fn eq(&self, other: &Self) -> bool {
214+
std::ptr::addr_eq(self as *const Self, other as *const Self)
215+
}
216+
}
217+
218218
impl std::fmt::Debug for DynamicScriptFunction {
219219
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
220220
f.debug_struct("DynamicScriptFunction")

0 commit comments

Comments
 (0)