Skip to content

Commit d9ceda7

Browse files
committed
Improve ClassBuilder doc and enable doc-tests
1 parent 56db2d6 commit d9ceda7

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

gdnative-core/Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ type_tag_fallback = []
1818

1919
[dependencies]
2020
gdnative-sys = { path = "../gdnative-sys", version = "0.9.3" }
21-
libc = "0.2.98"
21+
gdnative-impl-proc-macros = { path = "../impl/proc_macros", version = "=0.9.3" }
22+
ahash = "0.7.4"
2223
approx = "0.5.0"
24+
atomic-take = "1.0.0"
25+
bitflags = { version = "1.2.1", optional = true }
2326
glam = "0.18.0"
2427
indexmap = "1.7.0"
25-
ahash = "0.7.4"
28+
libc = "0.2.98"
2629
once_cell = "1.8.0"
30+
parking_lot = { version = "0.11.1", optional = true }
2731
serde = { version = "1", features = ["derive"], optional = true }
2832

29-
gdnative-impl-proc-macros = { path = "../impl/proc_macros", version = "=0.9.3" }
30-
31-
bitflags = { version = "1.2.1", optional = true }
32-
parking_lot = { version = "0.11.1", optional = true }
33-
atomic-take = "1.0.0"
33+
[dev-dependencies]
34+
gdnative = { path = "../gdnative", version = "0.9.3" } # for doc-tests

gdnative-core/src/nativescript/init.rs

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
//! Types and functionalities to declare and initialize gdnative classes.
1+
//! Low-level API to register and export GDNative classes, methods and properties.
22
//!
3-
//! ## API endpoints
3+
//! ## Init and exit hooks
44
//!
55
//! Three endpoints are automatically invoked by the engine during startup and shutdown:
66
//!
7-
//! - [`godot_gdnative_init`](macro.godot_gdnative_init.html),
8-
//! - [`godot_nativescript_init`](macro.godot_nativescript_init.html),
9-
//! - [`godot_gdnative_terminate`](macro.godot_gdnative_terminate.html),
7+
//! - [`godot_gdnative_init`],
8+
//! - [`godot_nativescript_init`],
9+
//! - [`godot_gdnative_terminate`],
1010
//!
1111
//! All three must be present. To quickly define all three endpoints using the default names,
12-
//! use [`godot_init`](macro.godot_init.html).
12+
//! use [`godot_init`].
1313
//!
1414
//! ## Registering script classes
1515
//!
16-
//! To register script classes, call `InitHandle::add_class` or `InitHandle::add_tool_class`
17-
//! in your `godot_nativescript_init` or `godot_init` callback:
16+
//! [`InitHandle`] is the registry of all your exported symbols.
17+
//! To register script classes, call [`InitHandle::add_class`] or [`InitHandle::add_tool_class`]
18+
//! in your [`godot_nativescript_init`] or [`godot_init`] callback:
1819
//!
1920
//! ```ignore
20-
//! // - snip -
21+
//! use gdnative::prelude::*;
2122
//!
22-
//! fn init(handle: gdnative::init::InitHandle) {
23+
//! fn init(handle: InitHandle) {
2324
//! handle.add_class::<HelloWorld>();
2425
//! }
2526
//!
2627
//! godot_init!(init);
27-
//!
28-
//! // - snip -
2928
//! ```
3029
//!
3130
//! For full examples, see [`examples`](https://github.com/godot-rust/godot-rust/tree/master/examples)
@@ -291,15 +290,39 @@ impl<C: NativeClass> ClassBuilder<C> {
291290
/// # Examples
292291
///
293292
/// Basic usage:
293+
/// ```
294+
/// use gdnative::prelude::*;
295+
/// use gdnative::nativescript::init::{RpcMode, Varargs};
296+
///
297+
/// #[derive(NativeClass)]
298+
/// #[register_with(Self::my_register)]
299+
/// #[no_constructor]
300+
/// struct MyType {}
294301
///
295-
/// ```ignore
296-
/// // `Bar` is a stateful method implementing `Method<C>`
302+
/// // Note: no #[methods] required
303+
/// impl MyType {
304+
/// fn my_method(&self) -> i64 { 42 }
297305
///
298-
/// builder
299-
/// .build_method("foo", Bar { baz: 42 })
300-
/// .with_rpc_mode(RpcMode::RemoteSync)
301-
/// .done();
306+
/// fn my_register(builder: &ClassBuilder<MyType>) {
307+
/// builder
308+
/// .build_method("my_method", MyMethod)
309+
/// .with_rpc_mode(RpcMode::RemoteSync)
310+
/// .done();
311+
/// }
312+
/// }
313+
///
314+
/// // Now, wrap the method (this can do anything and does not need to actually call a method)
315+
/// struct MyMethod;
316+
/// impl Method<MyType> for MyMethod {
317+
/// fn call(&self, this: RefInstance<'_, MyType, Shared>, _args: Varargs<'_>) -> Variant {
318+
/// this.map(|obj: &MyType, _| {
319+
/// let result = obj.my_method();
320+
/// Variant::from_i64(result)
321+
/// }).expect("method call succeeds")
322+
/// }
323+
/// }
302324
/// ```
325+
///
303326
#[inline]
304327
pub fn build_method<'a, F: Method<C>>(
305328
&'a self,
@@ -316,14 +339,32 @@ impl<C: NativeClass> ClassBuilder<C> {
316339
///
317340
/// Basic usage:
318341
///
319-
/// ```ignore
320-
/// builder
321-
/// .add_property("foo")
322-
/// .with_default(0.0)
323-
/// .with_hint((-10.0..=30.0).into())
324-
/// .with_getter(MyType::get_foo)
325-
/// .with_setter(MyType::set_foo)
326-
/// .done();
342+
/// ```
343+
/// use gdnative::prelude::*;
344+
///
345+
/// #[derive(NativeClass)]
346+
/// #[inherit(Node)]
347+
/// #[register_with(Self::my_register)]
348+
/// #[no_constructor]
349+
/// struct MyType {
350+
/// foo: i32,
351+
/// }
352+
///
353+
/// // Note: no #[methods] required
354+
/// impl MyType {
355+
/// pub fn get_foo(&self, _owner: TRef<Node>) -> i32 { self.foo }
356+
/// pub fn set_foo(&mut self, _owner: TRef<Node>, val: i32) { self.foo = val; }
357+
///
358+
/// fn my_register(builder: &ClassBuilder<MyType>) {
359+
/// builder
360+
/// .add_property("foo")
361+
/// .with_default(5)
362+
/// .with_hint((-10..=30).into())
363+
/// .with_getter(MyType::get_foo)
364+
/// .with_setter(MyType::set_foo)
365+
/// .done();
366+
/// }
367+
/// }
327368
/// ```
328369
#[inline]
329370
pub fn add_property<'a, T>(&'a self, name: &'a str) -> PropertyBuilder<'a, C, T>

gdnative-derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
163163
///
164164
/// ```
165165
/// use gdnative::prelude::*;
166-
/// use gdnative::nativescript::property::{RangeHint, FloatHint};
166+
/// use gdnative::nativescript::init::property::{RangeHint, FloatHint};
167167
///
168168
/// #[derive(NativeClass)]
169169
/// #[inherit(Reference)]

0 commit comments

Comments
 (0)