Skip to content

Commit 16e5847

Browse files
bors[bot]chitoyuu
andauthored
Merge #999
999: Implement automatic `NativeClass` registration via inventory. Implement mix-ins. r=chitoyuu a=chitoyuu ## Implement automatic NativeClass registration via inventory This adds the optional `inventory` feature, which allows `NativeClass` types to be automatically registered on supported platforms (everything that OSS Godot currently supports except WASM). Run-time diagnostic functions are added to help debug missing registration problems that are highly likely to arise when porting `inventory`-enabled projects to WASM. An internal `cfg_ex` attribute macro is added to help manage cfg conditions. Close #350. Note that the standalone registration attribute syntax proposed by the original issue isn't implemented, for the limited usefulness -- there are much fewer cases where manual `NativeClass` impls are necessary thanks to all the improvements since the original issue. ## Implement mix-in `#[methods]` blocks Adds the `#[methods(mixin = "Name")]` syntax for declaring mix-in blocks. Mix-in blocks have a many-to-many relationship with `NativeClass` types. Both `impl Type` and `impl Trait for Type` blocks are accepted. The argument name is changed from `as` in the original proposal to `mixin`, because we might still want to keep universal `#[methods]` blocks in the future for ease of use with WASM. `#[methods(mixin)]` makes a lot more sense for a future auto-registered mixin block than `#[methods(as /* as what? */)]`. All mix-in blocks have to be manually registered for gdnative v0.11.x. Some difficulty was encountered when trying to make auto-mixins compatible with existing code. It might still be possible with some tricks like autoref-specialization, but that might be too much effort given that we likely want to re-design much of the hierarchy for 0.12. Close #984. ## Allow `#[register_with]` for `#[monomorphize]` Enables `#[monomorphize]` to take the same standalone `#[register_with]` attribute as `#[derive(NativeClass)]`. This is chosen for short term consistency, but will probably change in a later version w/ #848, which might not still get implemented for a fair bit of time. Co-authored-by: Chitose Yuuzaki <chitoyuu@potatoes.gay>
2 parents f920000 + adad1a6 commit 16e5847

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1154
-119
lines changed

.github/workflows/full-ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,20 +277,38 @@ jobs:
277277
godot: "3.5.1-stable"
278278
postfix: ' (ptrcall)'
279279
build_args: '--features ptrcall'
280+
- rust: stable
281+
godot: "3.5.1-stable"
282+
postfix: ' (inventory)'
283+
build_args: '--features inventory'
284+
- rust: stable
285+
godot: "3.5.1-stable"
286+
postfix: ' (inventory)'
287+
# Limiting no-manual-register tests to stable as to not slow down CI too far -- if inventory is
288+
# working across all 3 Rust versions, this is likely to be as well.
289+
build_args: '--features inventory,no-manual-register'
280290
- rust: nightly
281291
godot: "3.5.1-stable"
282292
postfix: ' (nightly)'
283293
- rust: nightly
284294
godot: "3.5.1-stable"
285295
postfix: ' (nightly, ptrcall)'
286296
build_args: '--features ptrcall'
297+
- rust: nightly
298+
godot: "3.5.1-stable"
299+
postfix: ' (nightly, inventory)'
300+
build_args: '--features inventory'
287301
- rust: '1.63'
288302
godot: "3.5.1-stable"
289303
postfix: ' (msrv 1.63)'
290304
- rust: '1.63'
291305
godot: "3.5.1-stable"
292306
postfix: ' (msrv 1.63, ptrcall)'
293307
build_args: '--features ptrcall'
308+
- rust: '1.63'
309+
godot: "3.5.1-stable"
310+
postfix: ' (msrv 1.63, inventory)'
311+
build_args: '--features inventory'
294312

295313
# Test with oldest supported engine version
296314
- rust: stable

check.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ function findGodot() {
6262
fi
6363
}
6464

65-
features="gdnative/async,gdnative/serde"
65+
features="gdnative/async,gdnative/serde,gdnative/inventory"
66+
itest_toggled_features="no-manual-register"
6667
cmds=()
6768

6869
for arg in "${args[@]}"; do
@@ -81,6 +82,9 @@ for arg in "${args[@]}"; do
8182
cmds+=("cargo build --manifest-path test/Cargo.toml --features $features")
8283
cmds+=("cp target/debug/*gdnative_test* test/project/lib/")
8384
cmds+=("$godotBin --path test/project")
85+
cmds+=("cargo build --manifest-path test/Cargo.toml --features $features,$itest_toggled_features")
86+
cmds+=("cp target/debug/*gdnative_test* test/project/lib/")
87+
cmds+=("$godotBin --path test/project")
8488
;;
8589
doc)
8690
cmds+=("cargo doc --lib -p gdnative --no-deps --features $features")

gdnative-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ libc = "0.2"
3030
once_cell = "1"
3131
parking_lot = "0.12"
3232
serde = { version = "1", features = ["derive"], optional = true }
33+
inventory = { version = "0.3", optional = true }
3334

3435
[dev-dependencies]
3536
gdnative = { path = "../gdnative" } # for doc-tests

gdnative-core/src/core_types/byte_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ godot_test!(
1414
let original_read = {
1515
let read = arr.read();
1616
assert_eq!(&[0, 1, 2, 3, 4, 5, 6, 7], read.as_slice());
17-
read.clone()
17+
read
1818
};
1919

2020
let mut cow_arr = arr.new_ref();

gdnative-core/src/core_types/color_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ godot_test!(
2323
Color::from_rgb(0.0, 1.0, 0.0),
2424
Color::from_rgb(0.0, 0.0, 1.0),
2525
], read.as_slice());
26-
read.clone()
26+
read
2727
};
2828

2929
let mut cow_arr = arr.new_ref();

gdnative-core/src/core_types/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::sys;
22

33
/// Error codes used in various Godot APIs.
4+
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
45
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
56
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67
#[repr(u32)]
@@ -65,6 +66,7 @@ impl GodotError {
6566
/// `err` should be a valid value for `GodotError`.
6667
#[inline]
6768
#[doc(hidden)]
69+
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
6870
pub unsafe fn result_from_sys(err: sys::godot_error) -> Result<(), Self> {
6971
if err == sys::godot_error_GODOT_OK {
7072
return Ok(());

gdnative-core/src/core_types/float32_array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ godot_test!(
1616
for (n, i) in read.as_slice().iter().enumerate() {
1717
assert_relative_eq!(n as f32, i);
1818
}
19-
read.clone()
19+
read
2020
};
2121

2222
let mut cow_arr = arr.new_ref();
@@ -30,7 +30,7 @@ godot_test!(
3030
}
3131

3232
for i in 0..8 {
33-
assert_relative_eq!(i as f32 * 2., cow_arr.get(i as i32));
33+
assert_relative_eq!(i as f32 * 2., cow_arr.get(i));
3434
}
3535

3636
// the write shouldn't have affected the original array

gdnative-core/src/core_types/int32_array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ godot_test!(
1414
let original_read = {
1515
let read = arr.read();
1616
assert_eq!(&[0, 1, 2, 3, 4, 5, 6, 7], read.as_slice());
17-
read.clone()
17+
read
1818
};
1919

2020
let mut cow_arr = arr.new_ref();
@@ -28,7 +28,7 @@ godot_test!(
2828
}
2929

3030
for i in 0..8 {
31-
assert_eq!(i * 2, cow_arr.get(i as i32));
31+
assert_eq!(i * 2, cow_arr.get(i));
3232
}
3333

3434
// the write shouldn't have affected the original array

gdnative-core/src/core_types/string_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ godot_test!(
2323
GodotString::from("bar"),
2424
GodotString::from("baz"),
2525
], read.as_slice());
26-
read.clone()
26+
read
2727
};
2828

2929
let mut cow_arr = arr.new_ref();

gdnative-core/src/core_types/variant.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ decl_variant_type!(
166166
);
167167

168168
impl VariantType {
169+
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
169170
#[doc(hidden)]
170171
#[inline]
171172
pub fn from_sys(v: sys::godot_variant_type) -> VariantType {
@@ -182,6 +183,7 @@ impl VariantType {
182183
}
183184
}
184185

186+
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
185187
#[repr(u32)]
186188
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
187189
pub enum CallError {
@@ -198,6 +200,7 @@ pub enum CallError {
198200
}
199201

200202
impl CallError {
203+
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
201204
#[inline]
202205
fn from_sys(v: sys::godot_variant_call_error_error) -> Result<(), CallError> {
203206
if v == sys::godot_variant_call_error_error_GODOT_CALL_ERROR_CALL_OK {
@@ -230,6 +233,7 @@ impl std::fmt::Display for CallError {
230233
impl std::error::Error for CallError {}
231234

232235
/// Godot variant operator kind.
236+
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
233237
#[repr(u32)]
234238
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
235239
pub enum VariantOperator {
@@ -269,6 +273,7 @@ pub enum VariantOperator {
269273
In = sys::godot_variant_operator_GODOT_VARIANT_OP_IN as u32,
270274
}
271275

276+
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
272277
impl VariantOperator {
273278
const MAX: u32 = sys::godot_variant_operator_GODOT_VARIANT_OP_MAX as u32;
274279

0 commit comments

Comments
 (0)