1
1
use crate :: bindings:: function:: script_function:: {
2
2
AppScriptFunctionRegistry , DynamicScriptFunction , GetFunctionTypeDependencies , ScriptFunction ,
3
- ScriptFunctionRegistry ,
4
3
} ;
5
4
use bevy:: {
6
5
prelude:: { AppTypeRegistry , World } ,
@@ -53,10 +52,14 @@ pub trait GetNamespacedFunction {
53
52
}
54
53
}
55
54
55
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Default ) ]
56
56
pub enum Namespace {
57
- /// The function is registered in the global namespace, i.e. with no namespace
57
+ /// The function is registered in the global namespace, i.e. with no namespace.
58
+ /// In practice functions in this namespace should be callable directly by their name, i.e. `my_function()`
59
+ #[ default]
58
60
Global ,
59
- /// The function is registered in the namespace corresponding to the given type
61
+ /// The function is registered in the namespace corresponding to the given type.
62
+ /// In practice functions in this namespace should be callable by their qualified name, i.e. `MyType.my_function()`
60
63
OnType ( TypeId ) ,
61
64
}
62
65
@@ -79,63 +82,61 @@ impl Namespace {
79
82
}
80
83
81
84
/// Returns the fully qualified name of a function in this namespace
82
- pub fn function_name ( self , name : Cow < ' static , str > ) -> Cow < ' static , str > {
85
+ pub fn function_name < I : Into < Cow < ' static , str > > > ( self , name : I ) -> Cow < ' static , str > {
83
86
match self {
84
- Namespace :: Global => name,
85
- Namespace :: OnType ( type_id) => Cow :: Owned ( format ! ( "{:?}::{}" , type_id, name) ) ,
87
+ Namespace :: Global => name. into ( ) ,
88
+ Namespace :: OnType ( type_id) => Cow :: Owned ( format ! ( "{:?}::{}" , type_id, name. into ( ) ) ) ,
86
89
}
87
90
}
88
91
}
89
92
90
- impl RegisterNamespacedFunction for ScriptFunctionRegistry {
91
- fn register_namespaced_function < S , N , F , M > ( & mut self , name : N , function : F )
92
- where
93
- N : Into < Cow < ' static , str > > ,
94
- S : IntoNamespace ,
95
- F : ScriptFunction < ' static , M > ,
96
- {
97
- let cow: Cow < ' static , str > = name. into ( ) ;
98
- let function_name = S :: into_namespace ( ) . function_name ( cow) ;
99
- self . register ( function_name, function) ;
100
- }
101
- }
102
-
103
- impl GetNamespacedFunction for ScriptFunctionRegistry {
104
- fn iter_overloads_namespaced < N > (
105
- & self ,
106
- name : N ,
107
- namespace : Namespace ,
108
- ) -> impl Iterator < Item = & DynamicScriptFunction >
109
- where
110
- N : Into < Cow < ' static , str > > ,
111
- {
112
- let cow: Cow < ' static , str > = name. into ( ) ;
113
- let function_name = namespace. function_name ( cow) ;
114
- self . iter_overloads ( function_name)
115
- }
116
-
117
- fn get_namespaced_function < N > (
118
- & self ,
119
- name : N ,
120
- namespace : Namespace ,
121
- ) -> Option < & DynamicScriptFunction >
122
- where
123
- N : Into < Cow < ' static , str > > ,
124
- {
125
- let cow: Cow < ' static , str > = name. into ( ) ;
126
- let function_name = namespace. function_name ( cow) ;
127
- self . get_first ( & function_name)
128
- }
129
-
130
- fn has_namespaced_function < N > ( & self , name : N , namespace : Namespace ) -> bool
131
- where
132
- N : Into < Cow < ' static , str > > ,
133
- {
134
- let cow: Cow < ' static , str > = name. into ( ) ;
135
- let function_name = namespace. function_name ( cow) ;
136
- self . contains ( & function_name)
137
- }
138
- }
93
+ // impl RegisterNamespacedFunction for ScriptFunctionRegistry {
94
+ // fn register_namespaced_function<S, N, F, M>(&mut self, name: N, function: F)
95
+ // where
96
+ // N: Into<Cow<'static, str>>,
97
+ // S: IntoNamespace,
98
+ // F: ScriptFunction<'static, M>,
99
+ // {
100
+ // self.register(S::into_namespace(), name, function);
101
+ // }
102
+ // }
103
+
104
+ // impl GetNamespacedFunction for ScriptFunctionRegistry {
105
+ // fn iter_overloads_namespaced<N>(
106
+ // &self,
107
+ // name: N,
108
+ // namespace: Namespace,
109
+ // ) -> impl Iterator<Item = &DynamicScriptFunction>
110
+ // where
111
+ // N: Into<Cow<'static, str>>,
112
+ // {
113
+ // let cow: Cow<'static, str> = name.into();
114
+ // let function_name = namespace.function_name(cow);
115
+ // self.iter_overloads(function_name)
116
+ // }
117
+
118
+ // fn get_namespaced_function<N>(
119
+ // &self,
120
+ // name: N,
121
+ // namespace: Namespace,
122
+ // ) -> Option<&DynamicScriptFunction>
123
+ // where
124
+ // N: Into<Cow<'static, str>>,
125
+ // {
126
+ // let cow: Cow<'static, str> = name.into();
127
+ // let function_name = namespace.function_name(cow);
128
+ // self.get_first(&function_name)
129
+ // }
130
+
131
+ // fn has_namespaced_function<N>(&self, name: N, namespace: Namespace) -> bool
132
+ // where
133
+ // N: Into<Cow<'static, str>>,
134
+ // {
135
+ // let cow: Cow<'static, str> = name.into();
136
+ // let function_name = namespace.function_name(cow);
137
+ // self.contains(&function_name)
138
+ // }
139
+ // }
139
140
140
141
pub struct NamespaceBuilder < ' a , N > {
141
142
namespace : PhantomData < N > ,
@@ -179,7 +180,7 @@ impl<'a, S: IntoNamespace> NamespaceBuilder<'a, S> {
179
180
. world
180
181
. get_resource_or_init :: < AppScriptFunctionRegistry > ( ) ;
181
182
let mut registry = registry. write ( ) ;
182
- registry. register_namespaced_function :: < S , _ , F , M > ( name, function) ;
183
+ registry. register ( S :: into_namespace ( ) , name, function) ;
183
184
}
184
185
{
185
186
let type_registry = self . world . get_resource_or_init :: < AppTypeRegistry > ( ) ;
0 commit comments