Skip to content

Commit b9a6cd5

Browse files
authored
Merge pull request #1340 from godot-rust/qol/todos
Remove deprecated symbols, other small cleanups
2 parents 3564cfe + 099e873 commit b9a6cd5

File tree

26 files changed

+138
-230
lines changed

26 files changed

+138
-230
lines changed

godot-codegen/src/conv/type_conversions.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,7 @@ fn to_rust_expr_inner(expr: &str, ty: &RustTy, is_inner: bool) -> TokenStream {
308308
_ => panic!("null not representable in target type {ty:?}"),
309309
}
310310
}
311-
// empty string appears only for Callable/Rid in 4.0; default ctor syntax in 4.1+
312-
// TODO(v0.4): check if we can remove ""
313-
"" | "RID()" | "Callable()" if !is_inner => {
311+
"RID()" | "Callable()" if !is_inner => {
314312
return match ty {
315313
RustTy::BuiltinIdent { ty: ident, .. } if ident == "Rid" => quote! { Rid::Invalid },
316314
RustTy::BuiltinIdent { ty: ident, .. } if ident == "Callable" => {

godot-codegen/src/generator/enums.rs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn make_enum_engine_trait_impl(enum_: &Enum, enum_bitmask: Option<&RustTy>) -> T
268268
}
269269
});
270270

271-
let str_functions = make_enum_str_functions(enum_);
271+
let str_functions = make_enum_as_str(enum_);
272272
let values_and_constants_functions = make_enum_values_and_constants_functions(enum_);
273273

274274
quote! {
@@ -296,7 +296,7 @@ fn make_enum_engine_trait_impl(enum_: &Enum, enum_bitmask: Option<&RustTy>) -> T
296296
// However, those with masks don't have strict validation when marshalling from integers, and a Debug repr which includes the mask.
297297

298298
let unique_ords = enum_.unique_ords().expect("self is an enum");
299-
let str_functions = make_enum_str_functions(enum_);
299+
let str_functions = make_enum_as_str(enum_);
300300
let values_and_constants_functions = make_enum_values_and_constants_functions(enum_);
301301

302302
// We can technically check against all possible mask values, remove each mask, and then verify it's a valid base-enum value.
@@ -391,49 +391,10 @@ fn make_all_constants_function(enum_: &Enum) -> TokenStream {
391391
}
392392
}
393393

394-
/// Creates the `as_str` and `godot_name` implementations for the enum.
395-
fn make_enum_str_functions(enum_: &Enum) -> TokenStream {
394+
/// Creates the `as_str()` implementation for the enum.
395+
fn make_enum_as_str(enum_: &Enum) -> TokenStream {
396396
let as_str_enumerators = make_enum_to_str_cases(enum_);
397397

398-
// Only enumerations with different godot names are specified.
399-
// `as_str` is called for the rest of them.
400-
let godot_different_cases = {
401-
let enumerators = enum_
402-
.enumerators
403-
.iter()
404-
.filter(|enumerator| enumerator.name != enumerator.godot_name)
405-
.map(|enumerator| {
406-
let Enumerator {
407-
name, godot_name, ..
408-
} = enumerator;
409-
let godot_name_str = godot_name.to_string();
410-
quote! {
411-
Self::#name => #godot_name_str,
412-
}
413-
});
414-
415-
quote! {
416-
#( #enumerators )*
417-
}
418-
};
419-
420-
let godot_name_match = if godot_different_cases.is_empty() {
421-
// If empty, all the Rust names match the Godot ones.
422-
// Remove match statement to avoid `clippy::match_single_binding`.
423-
quote! {
424-
self.as_str()
425-
}
426-
} else {
427-
quote! {
428-
// Many enums have duplicates, thus allow unreachable.
429-
#[allow(unreachable_patterns)]
430-
match *self {
431-
#godot_different_cases
432-
_ => self.as_str(),
433-
}
434-
}
435-
};
436-
437398
quote! {
438399
#[inline]
439400
fn as_str(&self) -> &'static str {
@@ -444,10 +405,6 @@ fn make_enum_str_functions(enum_: &Enum) -> TokenStream {
444405
_ => "",
445406
}
446407
}
447-
448-
fn godot_name(&self) -> &'static str {
449-
#godot_name_match
450-
}
451408
}
452409
}
453410

godot-codegen/src/generator/extension_interface.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use proc_macro2::{Ident, Literal, TokenStream};
1212
use quote::quote;
1313
use regex::Regex;
1414

15-
use crate::util::ident;
15+
use crate::util::{ident, make_load_safety_doc};
1616
use crate::SubmitFn;
1717

1818
pub fn generate_sys_interface_file(
@@ -77,15 +77,14 @@ fn generate_proc_address_funcs(h_path: &Path) -> TokenStream {
7777
}
7878

7979
// Do not derive Copy -- even though the struct is bitwise-copyable, this is rarely needed and may point to an error.
80+
let safety_doc = make_load_safety_doc();
8081
let code = quote! {
8182
pub struct GDExtensionInterface {
8283
#( #fptr_decls )*
8384
}
8485

8586
impl GDExtensionInterface {
86-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
87-
// can cause issues with people assuming they are sufficient.
88-
#[allow(clippy::missing_safety_doc)]
87+
#safety_doc
8988
pub(crate) unsafe fn load(
9089
get_proc_address: crate::GDExtensionInterfaceGetProcAddress,
9190
) -> Self {

godot-codegen/src/generator/method_tables.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::models::domain::{
1616
BuiltinClass, BuiltinMethod, BuiltinVariant, Class, ClassCodegenLevel, ClassLike, ClassMethod,
1717
ExtensionApi, FnDirection, Function, TyName,
1818
};
19-
use crate::util::ident;
19+
use crate::util::{ident, make_load_safety_doc};
2020
use crate::{conv, generator, special_cases, util};
2121

2222
pub fn make_builtin_lifecycle_table(api: &ExtensionApi) -> TokenStream {
@@ -276,6 +276,7 @@ fn make_named_method_table(info: NamedMethodTable) -> TokenStream {
276276

277277
// Assumes that both decls and inits already have a trailing comma.
278278
// This is necessary because some generators emit multiple lines (statements) per element.
279+
let safety_doc = make_load_safety_doc();
279280
quote! {
280281
#imports
281282

@@ -288,9 +289,7 @@ fn make_named_method_table(info: NamedMethodTable) -> TokenStream {
288289
pub const CLASS_COUNT: usize = #class_count;
289290
pub const METHOD_COUNT: usize = #method_count;
290291

291-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
292-
// can cause issues with people assuming they are sufficient.
293-
#[allow(clippy::missing_safety_doc)]
292+
#safety_doc
294293
pub unsafe fn load(
295294
#ctor_parameters
296295
) -> Self {
@@ -374,6 +373,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
374373

375374
// Assumes that inits already have a trailing comma.
376375
// This is necessary because some generators emit multiple lines (statements) per element.
376+
let safety_doc = make_load_safety_doc();
377377
quote! {
378378
#imports
379379

@@ -387,9 +387,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
387387
pub const CLASS_COUNT: usize = #class_count;
388388
pub const METHOD_COUNT: usize = #method_count;
389389

390-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
391-
// can cause issues with people assuming they are sufficient.
392-
#[allow(clippy::missing_safety_doc)]
390+
#safety_doc
393391
#unused_attr
394392
pub unsafe fn load(
395393
#ctor_parameters
@@ -440,6 +438,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
440438

441439
// Assumes that inits already have a trailing comma.
442440
// This is necessary because some generators emit multiple lines (statements) per element.
441+
let safety_doc = make_load_safety_doc();
443442
quote! {
444443
#imports
445444
use crate::StringCache;
@@ -462,9 +461,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
462461
pub const CLASS_COUNT: usize = #class_count;
463462
pub const METHOD_COUNT: usize = #method_count;
464463

465-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
466-
// can cause issues with people assuming they are sufficient.
467-
#[allow(clippy::missing_safety_doc)]
464+
#safety_doc
468465
#unused_attr
469466
pub unsafe fn load() -> Self {
470467
// SAFETY: interface and lifecycle tables are initialized at this point, so we can get 'static references to them.

godot-codegen/src/util.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ pub fn lifetime(s: &str) -> TokenStream {
8585
TokenStream::from_iter([tk_apostrophe, tk_lifetime])
8686
}
8787

88+
pub fn make_load_safety_doc() -> TokenStream {
89+
quote! {
90+
/// # Safety
91+
/// - Must be called exactly once during library initialization.
92+
/// - All parameters (dependencies) must have been initialized and valid.
93+
}
94+
}
95+
8896
// This function is duplicated in godot-macros\src\util\mod.rs
8997
#[rustfmt::skip]
9098
pub fn safe_ident(s: &str) -> Ident {

godot-core/src/builtin/aabb.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ use crate::builtin::{real, Plane, Vector3, Vector3Axis};
2828
/// [`Rect2`]: crate::builtin::Rect2
2929
/// [`Rect2i`]: crate::builtin::Rect2i
3030
///
31-
/// # Godot docs
31+
/// # Soft invariants
32+
/// `Aabb` requires non-negative size for certain operations, which is validated only on a best-effort basis. Violations may
33+
/// cause panics in Debug mode. See also [_Builtin API design_](../__docs/index.html#6-public-fields-and-soft-invariants).
3234
///
35+
/// # Godot docs
3336
/// [`AABB`](https://docs.godotengine.org/en/stable/classes/class_aabb.html)
3437
#[derive(Default, Copy, Clone, PartialEq, Debug)]
3538
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -421,8 +424,7 @@ impl Aabb {
421424
///
422425
/// Most functions will fail to give a correct result if the size is negative.
423426
#[inline]
424-
/// TODO(v0.3): make private, change to debug_assert().
425-
pub fn assert_nonnegative(self) {
427+
fn assert_nonnegative(self) {
426428
assert!(
427429
self.size.x >= 0.0 && self.size.y >= 0.0 && self.size.z >= 0.0,
428430
"size {:?} is negative",

godot-core/src/builtin/callable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ mod custom_callable {
615615
#[allow(clippy::result_unit_err)] // TODO remove once there's a clear error type here.
616616
fn invoke(&mut self, args: &[&Variant]) -> Variant;
617617

618-
// TODO(v0.3): add object_id().
618+
// TODO(v0.5): add object_id().
619619

620620
/// Returns whether the callable is considered valid.
621621
///

godot-core/src/builtin/collections/packed_array.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,6 @@ impl<T: PackedArrayElement + fmt::Display> fmt::Display for PackedArray<T> {
771771
// ----------------------------------------------------------------------------------------------------------------------------------------------
772772
// Specific API for PackedByteArray
773773

774-
impl_builtin_froms!(PackedByteArray; VariantArray => packed_byte_array_from_array);
775-
776774
macro_rules! declare_encode_decode {
777775
// $Via could be inferred, but ensures we have the correct type expectations.
778776
($Ty:ty, $bytes:literal, $encode_fn:ident, $decode_fn:ident, $Via:ty) => {

godot-core/src/builtin/plane.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ use crate::builtin::{real, Vector3};
2323
/// unit length and will panic if this invariant is violated. This is not separately
2424
/// annotated for each method.
2525
///
26-
/// # Godot docs
26+
/// # Soft invariants
27+
/// `Plane` requires that the normal vector has unit length for most operations, which is validated only on a best-effort basis. Violations may
28+
/// cause panics in Debug mode. See also [_Builtin API design_](../__docs/index.html#6-public-fields-and-soft-invariants).
2729
///
30+
/// # Godot docs
2831
/// [Plane (stable)](https://docs.godotengine.org/en/stable/classes/class_plane.html)
2932
#[derive(Copy, Clone, PartialEq, Debug)]
3033
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]

godot-core/src/builtin/rect2.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ use crate::builtin::{real, Rect2i, Side, Vector2};
2727
///
2828
/// [`Aabb`]: crate::builtin::Aabb
2929
///
30-
/// # Godot docs
30+
/// # Soft invariants
31+
/// `Rect2` requires non-negative size for certain operations, which is validated only on a best-effort basis. Violations may
32+
/// cause panics in Debug mode. See also [_Builtin API design_](../__docs/index.html#6-public-fields-and-soft-invariants).
3133
///
34+
/// # Godot docs
3235
/// [`Rect2` (stable)](https://docs.godotengine.org/en/stable/classes/class_rect2.html)
3336
#[derive(Default, Copy, Clone, PartialEq, Debug)]
3437
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]

0 commit comments

Comments
 (0)