From ea43b6352f93b5c4d3f0f2c0d9295d2554b55764 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Wed, 12 Oct 2022 22:36:16 +0200 Subject: [PATCH 1/2] Fixed ToVariant/FromVariant derive for generic types with bounds Example: enum MyEnum was expanded to: impl ToVariant for MyEnum instead of: impl ToVariant for MyEnum --- gdnative-derive/src/variant/bounds.rs | 20 +++++++++++++++++++- gdnative-derive/src/variant/from.rs | 4 +++- gdnative-derive/src/variant/to.rs | 4 +++- test/src/test_derive.rs | 10 ++++++---- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/gdnative-derive/src/variant/bounds.rs b/gdnative-derive/src/variant/bounds.rs index 3f30d28e4..0907e40db 100644 --- a/gdnative-derive/src/variant/bounds.rs +++ b/gdnative-derive/src/variant/bounds.rs @@ -1,5 +1,6 @@ +use syn::punctuated::Punctuated; use syn::visit::Visit; -use syn::Generics; +use syn::{GenericParam, Generics}; use crate::extend_bounds::{with_visitor, BoundsVisitor}; @@ -50,3 +51,20 @@ pub(crate) fn extend_bounds( } }) } + +pub(crate) fn remove_bounds(mut generics: Generics) -> Generics { + for param in generics.params.iter_mut() { + match param { + GenericParam::Type(ty) => { + ty.colon_token = None; + ty.bounds = Punctuated::new(); + } + GenericParam::Lifetime(lt) => { + lt.colon_token = None; + lt.bounds = Punctuated::new(); + } + GenericParam::Const(_) => {} + } + } + generics +} diff --git a/gdnative-derive/src/variant/from.rs b/gdnative-derive/src/variant/from.rs index a726ff737..3054c8967 100644 --- a/gdnative-derive/src/variant/from.rs +++ b/gdnative-derive/src/variant/from.rs @@ -1,5 +1,6 @@ use proc_macro2::{Literal, Span, TokenStream as TokenStream2}; +use crate::variant::bounds; use syn::Ident; use super::repr::Repr; @@ -106,11 +107,12 @@ pub(crate) fn expand_from_variant(derive_data: DeriveData) -> Result ::std::result::Result { diff --git a/gdnative-derive/src/variant/to.rs b/gdnative-derive/src/variant/to.rs index 01f3b106d..ae16f841f 100644 --- a/gdnative-derive/src/variant/to.rs +++ b/gdnative-derive/src/variant/to.rs @@ -1,3 +1,4 @@ +use crate::variant::bounds; use proc_macro2::{Literal, TokenStream as TokenStream2}; use super::repr::Repr; @@ -69,11 +70,12 @@ pub(crate) fn expand_to_variant( } }; + let generics_no_bounds = bounds::remove_bounds(generics.clone()); let where_clause = &generics.where_clause; let result = quote! { #derived - impl #generics #trait_path for #ident #generics #where_clause { + impl #generics #trait_path for #ident #generics_no_bounds #where_clause { fn #to_variant_fn(#to_variant_receiver) -> ::gdnative::core_types::Variant { use #trait_path; use ::gdnative::core_types::FromVariant; diff --git a/test/src/test_derive.rs b/test/src/test_derive.rs index 8139b22ef..e0447df7a 100644 --- a/test/src/test_derive.rs +++ b/test/src/test_derive.rs @@ -40,9 +40,8 @@ pub(crate) fn register(handle: InitHandle) { crate::godot_itest! { test_derive_to_variant { #[derive(Clone, Eq, PartialEq, Debug, ToVariant, FromVariant)] - struct ToVar + struct ToVar where - T: Associated, R: Default, { foo: T::A, @@ -55,7 +54,7 @@ crate::godot_itest! { test_derive_to_variant { } #[derive(Clone, Eq, PartialEq, Debug, ToVariant, FromVariant)] - enum ToVarEnum { + enum ToVarEnum { Foo(T), Bar, Baz { baz: u8 }, @@ -67,9 +66,12 @@ crate::godot_itest! { test_derive_to_variant { T: Associated, R: Default; + trait Bound {} + impl Bound for bool {} + trait Associated { type A; - type B; + type B : Bound; } impl Associated for f64 { From 2d5bf2e0b5fd254092aadcd98967041991224541 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Wed, 12 Oct 2022 23:05:21 +0200 Subject: [PATCH 2/2] Clippy nightly --- gdnative-core/src/core_types/float32_array.rs | 2 +- gdnative-core/src/core_types/int32_array.rs | 2 +- gdnative-core/src/core_types/string.rs | 2 +- gdnative-core/src/export/property.rs | 6 +++--- gdnative-core/src/globalscope.rs | 7 ++----- gdnative-core/src/lib.rs | 2 +- gdnative-sys/src/lib.rs | 4 ++-- test/src/lib.rs | 2 +- 8 files changed, 12 insertions(+), 15 deletions(-) diff --git a/gdnative-core/src/core_types/float32_array.rs b/gdnative-core/src/core_types/float32_array.rs index 9cebcb961..c3f9ee9fb 100644 --- a/gdnative-core/src/core_types/float32_array.rs +++ b/gdnative-core/src/core_types/float32_array.rs @@ -30,7 +30,7 @@ godot_test!( } for i in 0..8 { - assert_relative_eq!(i as f32 * 2., cow_arr.get(i as i32)); + assert_relative_eq!(i as f32 * 2., cow_arr.get(i)); } // the write shouldn't have affected the original array diff --git a/gdnative-core/src/core_types/int32_array.rs b/gdnative-core/src/core_types/int32_array.rs index 4fde65c00..d3be887e7 100644 --- a/gdnative-core/src/core_types/int32_array.rs +++ b/gdnative-core/src/core_types/int32_array.rs @@ -28,7 +28,7 @@ godot_test!( } for i in 0..8 { - assert_eq!(i * 2, cow_arr.get(i as i32)); + assert_eq!(i * 2, cow_arr.get(i)); } // the write shouldn't have affected the original array diff --git a/gdnative-core/src/core_types/string.rs b/gdnative-core/src/core_types/string.rs index d0ca76843..fbc23cea1 100644 --- a/gdnative-core/src/core_types/string.rs +++ b/gdnative-core/src/core_types/string.rs @@ -411,7 +411,7 @@ impl TryFrom for char { 1 => std::char::from_u32(c.0 as u32).ok_or(GodotCharError::IncompleteSequence), 4 => std::char::from_u32(c.0 as u32).ok_or(GodotCharError::InvalidCodePoint), 2 => { - let mut iter = std::char::decode_utf16(std::iter::once(c.0 as u16)); + let mut iter = std::char::decode_utf16(std::iter::once(c.0)); let c = iter .next() .ok_or(GodotCharError::InvalidCodePoint)? diff --git a/gdnative-core/src/export/property.rs b/gdnative-core/src/export/property.rs index d89904815..42367fc60 100644 --- a/gdnative-core/src/export/property.rs +++ b/gdnative-core/src/export/property.rs @@ -310,9 +310,9 @@ bitflags::bitflags! { const ANIMATE_AS_TRIGGER = sys::godot_property_usage_flags_GODOT_PROPERTY_USAGE_ANIMATE_AS_TRIGGER as u32; const UPDATE_ALL_IF_MODIFIED = sys::godot_property_usage_flags_GODOT_PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED as u32; - const DEFAULT = Self::STORAGE.bits | Self::EDITOR.bits | Self::NETWORK.bits as u32; - const DEFAULT_INTL = Self::DEFAULT.bits | Self::INTERNATIONALIZED.bits as u32; - const NOEDITOR = Self::STORAGE.bits | Self::NETWORK.bits as u32; + const DEFAULT = Self::STORAGE.bits | Self::EDITOR.bits | Self::NETWORK.bits; + const DEFAULT_INTL = Self::DEFAULT.bits | Self::INTERNATIONALIZED.bits; + const NOEDITOR = Self::STORAGE.bits | Self::NETWORK.bits; } } diff --git a/gdnative-core/src/globalscope.rs b/gdnative-core/src/globalscope.rs index c04829bf1..a523fc486 100644 --- a/gdnative-core/src/globalscope.rs +++ b/gdnative-core/src/globalscope.rs @@ -145,11 +145,8 @@ pub fn move_toward(range: RangeInclusive, delta: f32) -> f32 { /// ![Image](https://raw.githubusercontent.com/godotengine/godot-docs/3.4/img/ease_cheatsheet.png) #[inline] pub fn ease(mut s: f32, curve: f32) -> f32 { - if s < 0.0 { - s = 0.0; - } else if s > 1.0 { - s = 1.0; - } + s = s.clamp(0.0, 1.0); + if curve > 0.0 { if curve < 1.0 { 1.0 - (1.0 - s).powf(1.0 / curve) diff --git a/gdnative-core/src/lib.rs b/gdnative-core/src/lib.rs index f9219e97e..452da02b2 100644 --- a/gdnative-core/src/lib.rs +++ b/gdnative-core/src/lib.rs @@ -25,7 +25,7 @@ clippy::missing_safety_doc, clippy::non_send_fields_in_send_ty )] -#![cfg_attr(feature = "gd-test", allow(clippy::blacklisted_name))] +#![cfg_attr(feature = "gd-test", allow(clippy::disallowed_names))] #[doc(hidden)] pub extern crate gdnative_sys as sys; diff --git a/gdnative-sys/src/lib.rs b/gdnative-sys/src/lib.rs index 2f21d7e2b..8d1ed6ecb 100644 --- a/gdnative-sys/src/lib.rs +++ b/gdnative-sys/src/lib.rs @@ -42,11 +42,11 @@ unsafe fn find_version( version_minor: u32, ) -> Option> { let mut got = None; - if (*api).type_ as u32 == api_type as u32 { + if (*api).type_ == api_type as u32 { while !api.is_null() { // The boolean expression below SHOULD always be true; // we will double check to be safe. - if (*api).type_ as u32 == api_type as u32 { + if (*api).type_ == api_type as u32 { let (major, minor) = ((*api).version.major, (*api).version.minor); if major == version_major && minor == version_minor { return Some(Ok(api)); diff --git a/test/src/lib.rs b/test/src/lib.rs index 3ceba0399..bb4fad914 100644 --- a/test/src/lib.rs +++ b/test/src/lib.rs @@ -1,4 +1,4 @@ -#![allow(clippy::blacklisted_name)] +#![allow(clippy::disallowed_names)] #![allow(deprecated)] use gdnative::prelude::*;