Skip to content

Commit 9050211

Browse files
bors[bot]chitoyuu
andauthored
Merge #1015 #1020
1015: Add `#[non_exhaustive]` to most public enums r=chitoyuu a=chitoyuu Additionally, utilizes the `clippy::exhaustive_enums` lint to prevent new exhaustive ones from appearing in the public interface. This makes it non-breaking to add new variants to enums that aren't intended to be exhaustively matched on. Close #987 1020: Remove Export impls with surprising behavior r=chitoyuu a=chitoyuu Also added documentation on the rationales behind leaving `Export` unimplemented for standard Rust collections, pointing to alternatives. Close #1009 Co-authored-by: Chitose Yuuzaki <chitoyuu@potatoes.gay>
3 parents bee643e + ad6580b + fa43b85 commit 9050211

File tree

18 files changed

+86
-59
lines changed

18 files changed

+86
-59
lines changed

.github/workflows/full-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
rust: ${{ matrix.rust.toolchain }}
7575
components: clippy
7676
- name: "Check clippy"
77-
run: cargo clippy --workspace --features ${GDRUST_FEATURES} -- -D clippy::style -D clippy::complexity -D clippy::perf -D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented
77+
run: cargo clippy --workspace --features ${GDRUST_FEATURES} -- -D clippy::style -D clippy::complexity -D clippy::perf -D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented -D warnings
7878

7979
cargo-deny-machete:
8080
runs-on: ubuntu-latest

examples/property-export/GDScriptPrinter.gd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ func _ready():
44
var rust = get_node("../PropertyExport")
55

66
print("\n-----------------------------------------------------------------")
7-
print("Print from GDScript (note the lexicographically ordered map/set):")
8-
print(" Vec (name):");
7+
print("Print from GDScript:")
8+
print(" PoolArray<GodotString>:");
99
for name in rust.name_vec:
1010
print(" * %s" % name)
1111

12-
print("\n HashMap (string -> color):")
12+
print("\n Dictionary (string -> color):")
1313
for string in rust.color_map:
1414
var color = rust.color_map[string]
1515
print(" * %s -> #%s" % [string, color.to_html(false)]);
1616

17-
print("\n HashSet (ID):")
17+
print("\n PoolArray<i32>:")
1818
for id in rust.id_set:
1919
print(" * %s" % id)
2020

examples/property-export/Main.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ library = ExtResource( 1 )
1212

1313
[node name="PropertyExport" type="Node" parent="."]
1414
script = SubResource( 1 )
15-
name_vec = [ "Godot", "Godette", "Go ." ]
15+
name_vec = PoolStringArray("Godot", "Godette", "Go .")
1616
color_map = {
1717
"blue": Color( 0.184314, 0.160784, 0.8, 1 ),
1818
"green": Color( 0.0941176, 0.447059, 0.192157, 1 ),
1919
"teal": Color( 0.0941176, 0.423529, 0.564706, 1 )
2020
}
21-
id_set = [ 21, 77, 8, 90 ]
21+
id_set = PoolIntArray(21, 77, 8, 90)
2222

2323
[node name="GDScriptPrinter" type="Node" parent="."]
2424
script = ExtResource( 2 )

examples/property-export/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
use gdnative::prelude::*;
22

3-
use std::collections::{HashMap, HashSet};
4-
53
#[derive(NativeClass, Default)]
64
#[inherit(Node)]
75
pub struct PropertyExport {
86
#[property]
9-
name_vec: Vec<String>,
7+
name_vec: PoolArray<GodotString>,
108

119
#[property]
12-
color_map: HashMap<GodotString, Color>,
10+
color_map: Dictionary,
1311

1412
#[property]
15-
id_set: HashSet<i64>,
13+
id_set: PoolArray<i32>,
1614
}
1715

1816
#[methods]
@@ -24,19 +22,20 @@ impl PropertyExport {
2422
#[method]
2523
fn _ready(&self) {
2624
godot_print!("------------------------------------------------------------------");
27-
godot_print!("Print from Rust (note the unordered map/set):");
28-
godot_print!(" Vec (name):");
29-
for name in &self.name_vec {
25+
godot_print!("Print from Rust:");
26+
godot_print!(" PoolArray<GodotString>:");
27+
for name in &*self.name_vec.read() {
3028
godot_print!(" * {}", name);
3129
}
3230

33-
godot_print!("\n HashMap (string -> color):");
31+
godot_print!("\n Dictionary (string -> color):");
3432
for (string, color) in &self.color_map {
33+
let color = Color::from_variant(&color).unwrap();
3534
godot_print!(" * {} -> #{}", string, color.to_html(false));
3635
}
3736

38-
godot_print!("\n HashSet (ID):");
39-
for id in &self.id_set {
37+
godot_print!("\n PoolArray<i32>:");
38+
for id in &*self.id_set.read() {
4039
godot_print!(" * {}", id);
4140
}
4241
}

gdnative-async/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//!
77
//! This crate assumes that all user non-Rust code follow the official threading guidelines.
88
9+
#![warn(clippy::exhaustive_enums)]
10+
911
// Workaround for macros that expect the `gdnative` crate.
1012
extern crate gdnative_core as gdnative;
1113

gdnative-core/src/core_types/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::sys;
55
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
66
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
77
#[repr(u32)]
8+
#[non_exhaustive]
89
pub enum GodotError {
910
Failed = sys::godot_error_GODOT_FAILED as u32,
1011
Unavailable = sys::godot_error_GODOT_ERR_UNAVAILABLE as u32,

gdnative-core/src/core_types/geom/rect2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub struct MarginError(i64);
267267
///
268268
/// [margin]: https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-margin
269269
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
270+
#[non_exhaustive]
270271
pub enum Margin {
271272
Left,
272273
Top,

gdnative-core/src/core_types/string.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ pub struct GodotChar(libc::wchar_t);
390390

391391
/// Error indicating that a `GodotChar` cannot be converted to a `char`.
392392
#[derive(Debug)]
393+
#[non_exhaustive]
393394
pub enum GodotCharError {
394395
/// The character cannot be represented as a Unicode code point.
395396
InvalidCodePoint,

gdnative-core/src/core_types/variant.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ macro_rules! decl_variant_type {
8181
) => {
8282
#[repr(u32)]
8383
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
84+
#[non_exhaustive]
8485
pub enum VariantType {
8586
$(
8687
$variant = $c_const as u32,
@@ -99,6 +100,7 @@ macro_rules! decl_variant_type {
99100
/// For `Variant`s containing objects, the original `Variant` is returned unchanged, due to
100101
/// the limitations of statically-determined memory management.
101102
#[repr(u32)]
103+
#[non_exhaustive]
102104
pub enum VariantDispatch {
103105
$(
104106
$variant $( ($inner) )?,
@@ -186,6 +188,7 @@ impl VariantType {
186188
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
187189
#[repr(u32)]
188190
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
191+
#[non_exhaustive]
189192
pub enum CallError {
190193
InvalidMethod =
191194
sys::godot_variant_call_error_error_GODOT_CALL_ERROR_CALL_ERROR_INVALID_METHOD as u32,
@@ -236,6 +239,7 @@ impl std::error::Error for CallError {}
236239
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
237240
#[repr(u32)]
238241
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
242+
#[non_exhaustive]
239243
pub enum VariantOperator {
240244
// Comparison
241245
Equal = sys::godot_variant_operator_GODOT_VARIANT_OP_EQUAL as u32,
@@ -856,6 +860,7 @@ pub trait CoerceFromVariant: Sized + private::Sealed {
856860
}
857861

858862
#[derive(Clone, PartialEq, Eq, Debug)]
863+
#[non_exhaustive]
859864
/// Error type returned by `FromVariant::from_variant`.
860865
pub enum FromVariantError {
861866
/// An unspecified error.
@@ -924,11 +929,13 @@ pub enum FromVariantError {
924929
}
925930

926931
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
932+
#[non_exhaustive]
927933
pub enum VariantEnumRepr {
928934
ExternallyTagged,
929935
}
930936

931937
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
938+
#[non_exhaustive]
932939
pub enum VariantStructRepr {
933940
Unit,
934941
Tuple,

gdnative-core/src/core_types/vector3.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ pub struct Vector3 {
1515
pub z: f32,
1616
}
1717

18-
#[allow(clippy::unnecessary_cast)] // False positives: casts necessary for cross-platform
18+
#[allow(
19+
clippy::unnecessary_cast, // False positives: casts necessary for cross-platform
20+
clippy::exhaustive_enums, // explicitly exhaustive since there can't be more axes for Vector3
21+
)]
1922
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2023
#[repr(u32)]
2124
pub enum Axis {

0 commit comments

Comments
 (0)