Skip to content

Commit bb2be22

Browse files
authored
Merge pull request #389 from lilizoey/cleanup/minor-stuffs
Cleanup some minor things
2 parents 88d22f8 + 07f3966 commit bb2be22

File tree

6 files changed

+53
-17
lines changed

6 files changed

+53
-17
lines changed

godot-codegen/src/class_generator.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,28 @@ fn make_class(class: &Class, class_name: &TyName, ctx: &mut Context) -> Generate
524524
(TokenStream::new(), TokenStream::new())
525525
};
526526

527+
// The base_ty of `Object` is `()`, and we dont want every engine class to deref to `()`.
528+
let deref_impl = if class_name.rust_ty != "Object" {
529+
quote! {
530+
impl std::ops::Deref for #class_name {
531+
type Target = #base_ty;
532+
533+
fn deref(&self) -> &Self::Target {
534+
// SAFETY: same assumptions as `impl Deref for Gd<T>`, see there for comments
535+
unsafe { std::mem::transmute::<&Self, &Self::Target>(self) }
536+
}
537+
}
538+
impl std::ops::DerefMut for #class_name {
539+
fn deref_mut(&mut self) -> &mut Self::Target {
540+
// SAFETY: see above
541+
unsafe { std::mem::transmute::<&mut Self, &mut Self::Target>(self) }
542+
}
543+
}
544+
}
545+
} else {
546+
TokenStream::new()
547+
};
548+
527549
let all_bases = ctx.inheritance_tree().collect_all_bases(class_name);
528550
let (notification_enum, notification_enum_name) =
529551
make_notification_enum(class_name, &all_bases, ctx);
@@ -613,20 +635,7 @@ fn make_class(class: &Class, class_name: &TyName, ctx: &mut Context) -> Generate
613635

614636
#exportable_impl
615637

616-
impl std::ops::Deref for #class_name {
617-
type Target = #base_ty;
618-
619-
fn deref(&self) -> &Self::Target {
620-
// SAFETY: same assumptions as `impl Deref for Gd<T>`, see there for comments
621-
unsafe { std::mem::transmute::<&Self, &Self::Target>(self) }
622-
}
623-
}
624-
impl std::ops::DerefMut for #class_name {
625-
fn deref_mut(&mut self) -> &mut Self::Target {
626-
// SAFETY: see above
627-
unsafe { std::mem::transmute::<&mut Self, &mut Self::Target>(self) }
628-
}
629-
}
638+
#deref_impl
630639

631640
#[macro_export]
632641
#[allow(non_snake_case)]
@@ -708,7 +717,7 @@ fn make_notification_enum(
708717
all_bases: &Vec<TyName>,
709718
ctx: &mut Context,
710719
) -> (Option<TokenStream>, Ident) {
711-
let Some(all_constants) = ctx.notification_constants(class_name) else {
720+
let Some(all_constants) = ctx.notification_constants(class_name) else {
712721
// Class has no notification constants: reuse (direct/indirect) base enum
713722
return (None, ctx.notification_enum_name(class_name).name);
714723
};

godot-core/src/builtin/variant/variant_traits.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,28 @@ pub enum VariantConversionError {
6060

6161
/// Variant value is missing a value for the target type
6262
MissingValue,
63+
64+
/// Variant value is null but expected to be non-null
65+
VariantIsNil,
6366
}
67+
68+
impl std::fmt::Display for VariantConversionError {
69+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70+
match self {
71+
VariantConversionError::BadType => {
72+
f.write_str("Variant type does not match expected type")
73+
}
74+
VariantConversionError::BadValue => {
75+
f.write_str("Variant value cannot be represented in target type")
76+
}
77+
VariantConversionError::MissingValue => {
78+
f.write_str("Variant value is missing a value for the target type")
79+
}
80+
VariantConversionError::VariantIsNil => {
81+
f.write_str("Variant value is null but expected to be non-null")
82+
}
83+
}
84+
}
85+
}
86+
87+
impl std::error::Error for VariantConversionError {}

godot-core/src/obj/gd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ impl<T: GodotClass> FromVariant for Gd<T> {
788788
// TODO(#234) remove this cast when Godot stops allowing illegal conversions
789789
// (See https://github.com/godot-rust/gdext/issues/158)
790790
.and_then(|obj| obj.owned_cast().ok())
791-
.ok_or(VariantConversionError::BadType)
791+
.ok_or(VariantConversionError::VariantIsNil)
792792
}
793793
}
794794

godot-ffi/src/plugins.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ macro_rules! plugin_foreach_inner {
8787
.unwrap();
8888

8989
for e in guard.iter() {
90+
#[allow(clippy::redundant_closure_call)]
9091
$closure(e);
9192
}
9293
};

itest/rust/src/object_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn object_engine_convert_variant_nil() {
336336

337337
assert_eq!(
338338
Gd::<Area2D>::try_from_variant(&nil),
339-
Err(VariantConversionError::BadType),
339+
Err(VariantConversionError::VariantIsNil),
340340
"try_from_variant(&nil)"
341341
);
342342

itest/rust/src/option_ffi_test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fn option_some_sys_conversion() {
1717
let v2 = unsafe { Option::<Gd<Object>>::from_sys(ptr) };
1818
assert_eq!(v2, v);
1919

20+
// We're testing this behavior.
21+
#[allow(clippy::unnecessary_literal_unwrap)]
2022
v.unwrap().free();
2123
}
2224

0 commit comments

Comments
 (0)