Skip to content

Commit 5106416

Browse files
committed
Rename #[method] -> #[godot], document new attributes
1 parent a63f643 commit 5106416

File tree

3 files changed

+93
-60
lines changed

3 files changed

+93
-60
lines changed

gdnative-core/src/export/macros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ macro_rules! deprecated_reference_return {
3737

3838
#[doc(hidden)]
3939
#[macro_export]
40-
#[deprecated = "#[export] is deprecated and will be removed in the next major version. Use #[godot] instead."]
40+
#[deprecated = "\n#[export] is deprecated and will be removed in godot-rust 0.11. Use #[godot] instead.\n\
41+
For more information, see https://godot-rust.github.io/docs/gdnative/derive/derive.NativeClass.html."]
4142
macro_rules! deprecated_export_syntax {
4243
() => {};
4344
}

gdnative-derive/src/lib.rs

Lines changed: 86 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
205205
///
206206
/// - `path = "my_category/my_property_name"`
207207
///
208-
/// Puts the property under the `my_category` category and renames it to
209-
/// `my_property_name` in the inspector and for GDScript.
208+
/// Puts the property under the `my_category` category and renames it to
209+
/// `my_property_name` in the inspector and for GDScript.
210210
///
211211
/// - `default = 42.0`
212212
///
213-
/// Sets the default value *in the inspector* for this property. The setter is *not*
214-
/// guaranteed to be called by the engine with the value.
213+
/// Sets the default value *in the inspector* for this property. The setter is *not*
214+
/// guaranteed to be called by the engine with the value.
215215
///
216216
/// - `before_get` / `after_get` / `before_set` / `after_set` `= "Self::hook_method"`
217217
///
@@ -220,22 +220,22 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
220220
///
221221
/// - `get` / `get_ref` / `set`
222222
///
223-
/// Configure getter/setter for property. All of them can accept a path to specify a custom
224-
/// property accessor. For example, `#[property(get = "Self::my_getter")]` will use
225-
/// `Self::my_getter` as the getter.
223+
/// Configure getter/setter for property. All of them can accept a path to specify a custom
224+
/// property accessor. For example, `#[property(get = "Self::my_getter")]` will use
225+
/// `Self::my_getter` as the getter.
226226
///
227-
/// The difference of `get` and `get_ref` is that `get` will register the getter with
228-
/// `with_getter` function, which means your getter should return an owned value `T`, but
229-
/// `get_ref` use `with_ref_getter` to register getter. In this case, your custom getter
230-
/// should return a shared reference `&T`.
227+
/// The difference of `get` and `get_ref` is that `get` will register the getter with
228+
/// `with_getter` function, which means your getter should return an owned value `T`, but
229+
/// `get_ref` use `with_ref_getter` to register getter. In this case, your custom getter
230+
/// should return a shared reference `&T`.
231231
///
232-
/// Situations with custom getters/setters and no backing fields require the use of the
233-
/// type [`Property<T>`][gdnative::export::Property]. Consult its documentation for
234-
/// a deeper elaboration of property exporting.
232+
/// Situations with custom getters/setters and no backing fields require the use of the
233+
/// type [`Property<T>`][gdnative::export::Property]. Consult its documentation for
234+
/// a deeper elaboration of property exporting.
235235
///
236236
/// - `no_editor`
237237
///
238-
/// Hides the property from the editor. Does not prevent it from being sent over network or saved in storage.
238+
/// Hides the property from the editor. Does not prevent it from being sent over network or saved in storage.
239239
///
240240
/// ### `#[methods]`
241241
/// Adds the necessary information to a an `impl` block to register the properties and methods with Godot.
@@ -244,102 +244,139 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
244244
///
245245
/// For additional details about how `#[methods]` expands, please refer to [gdnative::methods](macro@methods)
246246
///
247-
/// ### `#[export]`
247+
/// ### `#[godot]`
248248
/// Registers the attributed function signature to be used by Godot.
249+
///
250+
/// This attribute was formerly called `#[export]`, but is not directly related to the concept of
251+
/// [exporting](https://docs.godotengine.org/en/stable/tutorials/export/exporting_basics.html) in GDScript.
252+
///
249253
/// A valid function signature must have:
250254
/// - `&self` or `&mut self` as its first parameter
251-
/// - `&T` or `TRef<T>` where T refers to the type declared in `#[inherit(T)]` attribute as it's second parameter; this is typically called the `owner`.
252-
/// - Optionally, any number of additional parameters, which must have the type `Variant` or must implement the `FromVariant` trait. `FromVariant` is implemented for most common types.
255+
/// - Optionally, `&T` or `TRef<T>` where T refers to the type declared in `#[inherit(T)]` attribute as it's second parameter;
256+
/// this is typically called the _base_. The parameter must be attributed with `#[base]`.
257+
/// - Any number of required parameters, which must have the type `Variant` or must implement the `FromVariant` trait.
258+
/// `FromVariant` is implemented for most common types.
259+
/// - Any number of optional parameters annotated with `#[opt]`. Same rules as for required parameters apply.
260+
/// Optional parameters must appear at the end of the parameter list.
253261
/// - Return values must implement the `OwnedToVariant` trait (automatically implemented by `ToVariant`)
254262
/// or be a `Variant` type.
255263
///
256264
/// ```ignore
257-
/// #[export]
258-
/// fn foo(&self, owner: &Reference);
265+
/// // No access to base parameter
266+
/// #[godot]
267+
/// fn foo(&self);
268+
///
269+
/// // Access base parameter as &T
270+
/// #[godot]
271+
/// fn foo(&self, #[base] base: &Reference);
272+
///
273+
/// // Access base parameter as TRef<T>
274+
/// #[godot]
275+
/// fn foo(&self, #[base] base: TRef<Reference>);
259276
/// ```
260-
/// **Note**: Marking a function with `#[export]` does not have any effect unless inside an `impl` block that has the `#[methods]` attribute.
261277
///
262-
/// Possible arguments for this attribute are
278+
/// **Note**: Marking a function with `#[godot]` does not have any effect unless inside an `impl` block that has the `#[methods]` attribute.
279+
///
280+
/// Possible arguments for this attribute are:
281+
///
282+
/// - `name = "overridden_function_name"`
263283
///
264-
/// - `name` = "overridden_function_name"
284+
/// Overrides the function name as the method name to be registered in Godot.
265285
///
266-
/// Overrides the function name as the method name to be registered in Godot.
286+
/// - `rpc = "selected_rpc"`
267287
///
268-
/// - `rpc` = "selected_rpc"
269-
/// - "selected_rpc" must be one of the following values, which refer to the associated [Multiplayer API RPC Mode](https://docs.godotengine.org/en/stable/classes/class_multiplayerapi.html?highlight=RPC#enumerations)
270-
/// - "disabled" - `RPCMode::RPC_MODE_DISABLED`
271-
/// - "remote" - `RPCMode::RPC_MODE_REMOTE`
272-
/// - "remote_sync" - `RPCMode::RPC_MODE_REMOTE_SYMC`
273-
/// - "master" - `RPCMode::RPC_MODE_MASTER`
274-
/// - "puppet" - `RPCMode::RPC_MODE_PUPPET`
275-
/// - "master_sync" - `RPCMode::RPC_MODE_MASTERSYNC`
276-
/// - "puppet_sync" - `RPCMode::RPC_MODE_PUPPETSYNC`
288+
/// `"selected_rpc"` must be one of the following values, which refer to the associated [Multiplayer API RPC Mode](https://docs.godotengine.org/en/stable/classes/class_multiplayerapi.html?highlight=RPC#enumerations).
289+
/// See also the Rust type [`export::RpcMode`].
290+
/// - `"disabled"`
291+
/// - `"remote"`
292+
/// - `"remote_sync"`
293+
/// - `"master"`
294+
/// - `"master_sync"`
295+
/// - `"puppet"`
296+
/// - `"puppet_sync"`
297+
///
298+
/// This enables you to set the [Multiplayer API RPC Mode](https://docs.godotengine.org/en/stable/classes/class_multiplayerapi.html?highlight=RPC#enumerations) for the function.
299+
/// Refer to [Godot's Remote Procedure documentation](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#rpc) for more details.
300+
///
301+
/// - `deref_return`
302+
///
303+
/// Allows you to return a type using its `Deref` representation. This can avoid extra intermediate copies for larger objects, by explicitly
304+
/// returning a reference (or in general, a type that dereferences to something that can be exported).
305+
///
306+
/// For example:
307+
/// ```ignore
308+
/// #[godot(deref_return)]
309+
/// fn get_numbers(&self) -> std::cell::Ref<Vec<i32>> {
310+
/// // Assume self.cell is std::cell::RefCell<Vec<i32>>
311+
/// self.cell.borrow()
312+
/// }
313+
/// ```
277314
///
278-
/// This enables you to set the [Multiplayer API RPC Mode](https://docs.godotengine.org/en/stable/classes/class_multiplayerapi.html?highlight=RPC#enumerations) for the function.
279315
///
280-
/// Refer to [Godot's Remote Procedure documentation](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#rpc) for more details.
281316
/// #### `Node` virtual functions
282317
///
283318
/// This is a list of common Godot virtual functions that are automatically called via [notifications](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-notification).
284319
///
320+
/// It is assumed that every method is exported via `#[godot]` attribute. The parameter `#[base] base: &Node` can be omitted if you don't need it.
321+
///
285322
/// ```ignore
286-
/// fn _ready(&self, owner: &Node);
323+
/// fn _ready(&self, #[base] base: &Node);
287324
/// ```
288325
/// Called when both the node and its children have entered the scene tree.
289326
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-ready) for more information._
290327
/// <br><br>
291328
///
292329
/// ```ignore
293-
/// fn _enter_tree(&self, owner: &Node);
330+
/// fn _enter_tree(&self, #[base] base: &Node);
294331
/// ```
295332
/// Called when the node enters the scene tree.
296333
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-enter-tree) for more information._
297334
/// <br><br>
298335
///
299336
/// ```ignore
300-
/// fn _exit_tree(&self, owner: &Node);
337+
/// fn _exit_tree(&self, #[base] base: &Node);
301338
/// ```
302339
/// Called when the node is removed from the scene tree.
303340
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-exit-tree) for more information._
304341
/// <br><br>
305342
///
306343
/// ```ignore
307-
/// fn _get_configuration_warning(&self, owner: &Node) -> GodotString;
344+
/// fn _get_configuration_warning(&self, #[base] base: &Node) -> GodotString;
308345
/// ```
309346
/// The string returned from this method is displayed as a warning in the Scene Dock if the script that overrides it is a tool script.
310347
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-get-configuration-warning) for more information._
311348
/// <br><br>
312349
///
313350
/// ```ignore
314-
/// fn _process(&mut self, owner: &Node, delta: f64);
351+
/// fn _process(&mut self, #[base] base: &Node, delta: f64);
315352
/// ```
316353
/// Called during processing step of the main loop.
317354
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-process) for more information._
318355
/// <br><br>
319356
///
320357
/// ```ignore
321-
/// fn _physics_process(&self, owner: &Node, delta: f64);
358+
/// fn _physics_process(&self, #[base] base: &Node, delta: f64);
322359
/// ```
323360
/// Called during physics update, with a fixed timestamp.
324361
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-physics-process) for more information._
325362
/// <br><br>
326363
///
327364
/// ```ignore
328-
/// fn _input(&self, owner: &Node, event: Ref<InputEvent>);
365+
/// fn _input(&self, #[base] base: &Node, event: Ref<InputEvent>);
329366
/// ```
330367
/// Called when there is an input event.
331368
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-input) for more information._
332369
/// <br><br>
333370
///
334371
/// ```ignore
335-
/// fn _unhandled_input(&self, owner: &Node, event: Ref<InputEvent>);
372+
/// fn _unhandled_input(&self, #[base] base: &Node, event: Ref<InputEvent>);
336373
/// ```
337374
/// Called when an `InputEvent` hasn't been consumed by `_input()` or any GUI.
338375
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-unhandled-input) for more information._
339376
/// <br><br>
340377
///
341378
/// ```ignore
342-
/// fn _unhandled_key_input (&self, owner: &Node, event: Ref<InputKeyEvent>);
379+
/// fn _unhandled_key_input (&self, #[base] base: &Node, event: Ref<InputKeyEvent>);
343380
/// ```
344381
/// Called when an `InputEventKey` hasn't been consumed by `_input()` or any GUI.
345382
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-unhandled-key-input) for more information._
@@ -350,43 +387,35 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
350387
/// This is a list of common Godot virtual functions that are automatically called via [notifications](https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-notification).
351388
///
352389
/// ```ignore
353-
/// fn _clips_input(&self, owner: &Control) -> bool;
390+
/// fn _clips_input(&self, #[base] base: &Control) -> bool;
354391
/// ```
355392
/// Returns whether `_gui_input()` should not be called for children controls outside this control's rectangle.
356393
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-clips-input) for more information._
357394
/// <br><br>
358395
///
359396
/// ```ignore
360-
/// fn _get_minimum_size(&self, owner: &Control) -> Vector2;
397+
/// fn _get_minimum_size(&self, #[base] base: &Control) -> Vector2;
361398
/// ```
362399
/// Returns the minimum size for this control.
363400
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-get-minimum-size) for more information._
364401
/// <br><br>
365402
///
366403
/// ```ignore
367-
/// fn _gui_input(&self, owner: &Control, event: Ref<InputEvent>);
404+
/// fn _gui_input(&self, #[base] base: &Control, event: Ref<InputEvent>);
368405
/// ```
369406
/// Use this method to process and accept inputs on UI elements.
370407
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-gui-input) for more information._
371408
/// <br><br>
372409
///
373410
/// ```ignore
374-
/// fn _make_custom_tooltip(&self, owner: &Control, for_text: String) -> Ref<Control>;
411+
/// fn _make_custom_tooltip(&self, #[base] base: &Control, for_text: String) -> Ref<Control>;
375412
/// ```
376413
/// Returns a `Control` node that should be used as a tooltip instead of the default one.
377414
/// _See [Godot docs](https://docs.godotengine.org/en/stable/classes/class_control.html#class-control-method-make-custom-tooltip) for more information._
378415
/// <br><br>
379416
#[proc_macro_derive(
380417
NativeClass,
381-
attributes(
382-
inherit,
383-
export,
384-
opt,
385-
user_data,
386-
property,
387-
register_with,
388-
no_constructor
389-
)
418+
attributes(inherit, register_with, no_constructor, user_data, property)
390419
)]
391420
pub fn derive_native_class(input: TokenStream) -> TokenStream {
392421
// Converting the proc_macro::TokenStream into non proc_macro types so that tests

gdnative-derive/src/methods.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
232232
let (is_export, is_old_syntax) = if let Some("export") = last_seg.as_deref()
233233
{
234234
(true, true)
235-
} else if let Some("method") = last_seg.as_deref() {
235+
} else if let Some("godot") = last_seg.as_deref() {
236236
(true, false)
237237
} else {
238238
(false, false)
@@ -304,7 +304,10 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) {
304304
} else {
305305
errors.push(syn::Error::new(
306306
nested_meta.span(),
307-
format!("unexpected value for `rpc`: {}", value),
307+
format!(
308+
"unexpected value for `rpc`: {}",
309+
value
310+
),
308311
));
309312
}
310313
}

0 commit comments

Comments
 (0)