diff --git a/Cargo.lock b/Cargo.lock index 8c8e7cc..8581517 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -370,7 +370,9 @@ dependencies = [ "godot-rust-script-derive", "hot-lib-reloader", "itertools", + "process_path", "rand", + "tests-scripts-lib", ] [[package]] @@ -666,6 +668,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "process_path" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f676f11eb0b3e2ea0fbaee218fa6b806689e2297b8c8adc5bf73df465c4f6171" +dependencies = [ + "libc", + "winapi 0.3.9", +] + [[package]] name = "quote" version = "1.0.33" @@ -860,6 +872,13 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tests-scripts-lib" +version = "0.1.0" +dependencies = [ + "godot-rust-script", +] + [[package]] name = "thiserror" version = "1.0.50" diff --git a/Cargo.toml b/Cargo.toml index ca81dc9..81dd04f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,7 @@ darling = { version = "0.20.3" } proc-macro2 = "1.0.68" quote = "1.0.33" syn = "2.0.38" +process_path = "0.1" -godot-rust-script-derive = { path = "derive" } \ No newline at end of file +godot-rust-script-derive = { path = "derive" } +tests-scripts-lib = { path = "tests-scripts-lib" } \ No newline at end of file diff --git a/derive/src/attribute_ops.rs b/derive/src/attribute_ops.rs index 3e8754c..b6e3849 100644 --- a/derive/src/attribute_ops.rs +++ b/derive/src/attribute_ops.rs @@ -63,7 +63,7 @@ impl FieldExportOps { let parsed_params = exp_list .elems .iter() - .map(|item| ExpEasingOpts::from_expr(item)) + .map(ExpEasingOpts::from_expr) .collect::, _>>() .map_err(|err| err.write_errors())?; @@ -92,7 +92,7 @@ impl FieldExportOps { let filters = list .elems .iter() - .map(|item| String::from_expr(item)) + .map(String::from_expr) .collect::, _>>() .map_err(|err| err.write_errors())?; @@ -169,7 +169,7 @@ impl FieldExportOps { let types = list .elems .iter() - .map(|item| String::from_expr(item)) + .map(String::from_expr) .collect::, _>>() .map_err(|err| err.write_errors())?; diff --git a/derive/src/lib.rs b/derive/src/lib.rs index ffe2b11..f5bc4c1 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -132,7 +132,7 @@ fn rust_to_variant_type(ty: &syn::Type) -> Result { ) .into_compile_error()), T::Tuple(tuple) => { - if tuple.elems.len() > 0 { + if !tuple.elems.is_empty() { return Err(syn::Error::new( ty.span(), format!("\"{}\" is not a supported type", quote!(#tuple)), @@ -159,9 +159,9 @@ fn rust_to_variant_type(ty: &syn::Type) -> Result { fn derive_default_with_base(field_opts: &[FieldOpts]) -> TokenStream { let godot_types = godot_types(); let fields: TokenStream = field_opts - .into_iter() + .iter() .filter_map(|field| match field.ident.as_ref() { - Some(ident) if ident.to_string() == "base" => { + Some(ident) if *ident == "base" => { Some(quote_spanned!(ident.span() => #ident: base.cast(),)) } Some(ident) => Some(quote_spanned!(ident.span() => #ident: Default::default(),)), diff --git a/rust-script/Cargo.toml b/rust-script/Cargo.toml index 65393f5..91b02e5 100644 --- a/rust-script/Cargo.toml +++ b/rust-script/Cargo.toml @@ -13,5 +13,9 @@ itertools.workspace = true abi_stable.workspace = true rand.workspace = true cfg-if.workspace = true +process_path.workspace = true godot-rust-script-derive.workspace = true + +[dev-dependencies] +tests-scripts-lib = { path = "../tests-scripts-lib" } diff --git a/rust-script/src/apply.rs b/rust-script/src/apply.rs index c6edb1f..2779a05 100644 --- a/rust-script/src/apply.rs +++ b/rust-script/src/apply.rs @@ -1,5 +1,5 @@ pub trait Apply: Sized { - fn apply ()>(mut self, cb: F) -> Self { + fn apply(mut self, cb: F) -> Self { cb(&mut self); self } diff --git a/rust-script/src/library.rs b/rust-script/src/library.rs index c0b3520..72b38ee 100644 --- a/rust-script/src/library.rs +++ b/rust-script/src/library.rs @@ -72,7 +72,7 @@ macro_rules! setup_library { } } - let lock = crate::__godot_rust_plugin___SCRIPT_REGISTRY.lock().expect("unable to aquire mutex lock"); + let lock = __godot_rust_plugin___SCRIPT_REGISTRY.lock().expect("unable to aquire mutex lock"); $crate::assemble_metadata(lock.iter()) } @@ -112,8 +112,7 @@ impl RustScriptPropDesc { RemoteScriptPropertyInfo { variant_type: self.ty.into(), class_name: RStr::from_str(class_name), - property_name: RString::with_capacity(self.name.len()) - .apply(|s| s.push_str(&self.name)), + property_name: RString::with_capacity(self.name.len()).apply(|s| s.push_str(self.name)), usage: if self.exported { (PropertyUsageFlags::PROPERTY_USAGE_EDITOR | PropertyUsageFlags::PROPERTY_USAGE_STORAGE) @@ -167,11 +166,11 @@ pub fn assemble_metadata<'a>( }) .unzip(); - let methods: BTreeMap<_, _> = methods.into_iter().filter_map(|x| x).collect(); + let methods: BTreeMap<_, _> = methods.into_iter().flatten().collect(); entries .into_iter() - .filter_map(|x| x) + .flatten() .map(|class| { let props = (class.properties)() .into_iter() diff --git a/rust-script/src/runtime/hot_reloader.rs b/rust-script/src/runtime/hot_reloader.rs index f17ced9..3a2180c 100644 --- a/rust-script/src/runtime/hot_reloader.rs +++ b/rust-script/src/runtime/hot_reloader.rs @@ -60,7 +60,7 @@ impl HotReloader { Self { channel: receiver, - ffi_init_fn: ffi_init_fn, + ffi_init_fn, base, } } diff --git a/rust-script/src/runtime/mod.rs b/rust-script/src/runtime/mod.rs index 842b469..1742135 100644 --- a/rust-script/src/runtime/mod.rs +++ b/rust-script/src/runtime/mod.rs @@ -6,7 +6,7 @@ mod rust_script; mod rust_script_instance; mod rust_script_language; -use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::RwLock}; +use std::{collections::HashMap, rc::Rc, sync::RwLock}; use abi_stable::std_types::RVec; use cfg_if::cfg_if; @@ -16,7 +16,7 @@ use godot::{ prelude::{ godot_print, meta::{MethodInfo, PropertyInfo}, - Array, Dictionary, Gd, StringName, + Array, Dictionary, Gd, }, }; @@ -26,7 +26,7 @@ use crate::{ script_registry::{RemoteScriptMetaData, ScriptMetaData}, }; -use self::{rust_script_instance::RustScriptInstanceId, rust_script_language::RustScriptLanguage}; +use self::rust_script_language::RustScriptLanguage; #[cfg(debug_assertions)] use hot_reloader::{HotReloadEntry, HotReloader}; @@ -35,7 +35,7 @@ use hot_reloader::{HotReloadEntry, HotReloader}; macro_rules! setup { ($lib_crate:tt) => { #[cfg(debug_assertions)] - #[$crate::private_export::hot_module(dylib = stringify!($lib_crate), lib_dir = env!("CARGO_TARGET"))] + #[$crate::private_export::hot_module(dylib = stringify!($lib_crate), lib_dir=process_path::get_dylib_path().and_then(|path| path.parent().map(std::path::Path::to_path_buf)).unwrap_or_default())] mod scripts_lib { use $crate::private_export::RVec; @@ -57,8 +57,6 @@ macro_rules! setup { #[cfg(not(debug_assertions))] mod scripts_lib { pub use ::$lib_crate::{__godot_rust_script_init, __GODOT_RUST_SCRIPT_SRC_ROOT}; - - pub fn subscribe() {} } }; } @@ -67,9 +65,10 @@ macro_rules! setup { macro_rules! init { () => { $crate::RustScriptExtensionLayer::new( - crate::scripts_lib::__godot_rust_script_init, - crate::scripts_lib::subscribe, - crate::scripts_lib::__GODOT_RUST_SCRIPT_SRC_ROOT, + scripts_lib::__godot_rust_script_init, + scripts_lib::__GODOT_RUST_SCRIPT_SRC_ROOT, + #[cfg(debug_assertions)] + scripts_lib::subscribe, ) }; } @@ -77,7 +76,7 @@ macro_rules! init { thread_local! { static SCRIPT_REGISTRY: RwLock> = RwLock::default(); #[cfg(debug_assertions)] - static HOT_RELOAD_BRIDGE: RefCell>> = RefCell::default(); + static HOT_RELOAD_BRIDGE: std::cell::RefCell>> = std::cell::RefCell::default(); } pub type BindingInit = godot::sys::GodotBinding; @@ -89,19 +88,19 @@ impl RustScriptLibInit for F where F: Fn(Option) -> RVec hot_lib_reloader::LibReloadObserver; - } else { - type HotReloadSubscribe = fn() -> (); } } pub struct RustScriptExtensionLayer { lib_init_fn: ::std::rc::Rc, - hot_reload_subscribe: HotReloadSubscribe, lang: Option>, res_saver: Option>, res_loader: Option>, scripts_src_dir: Option<&'static str>, + #[cfg(debug_assertions)] + hot_reload_subscribe: HotReloadSubscribe, + #[cfg(debug_assertions)] hot_reloader: Option>, } @@ -109,16 +108,19 @@ pub struct RustScriptExtensionLayer { impl RustScriptExtensionLayer { pub fn new( lib_init_fn: F, - hot_reload_subscribe: HotReloadSubscribe, scripts_src_dir: &'static str, + #[cfg(debug_assertions)] hot_reload_subscribe: HotReloadSubscribe, ) -> Self { Self { lib_init_fn: Rc::new(lib_init_fn), - hot_reload_subscribe, lang: None, res_saver: None, res_loader: None, scripts_src_dir: Some(scripts_src_dir), + + #[cfg(debug_assertions)] + hot_reload_subscribe, + #[cfg(debug_assertions)] hot_reloader: None, } @@ -133,6 +135,8 @@ impl RustScriptExtensionLayer { cfg_if! { if #[cfg(debug_assertions)] { + use godot::prelude::StringName; + let mut hot_reloader = Gd::with_base(|base| HotReloader::new((self.hot_reload_subscribe)(), self.lib_init_fn.clone(), base)); hot_reloader.call_deferred(StringName::from("register"), &[]); @@ -235,3 +239,17 @@ impl ToDictionary for MethodInfo { }) } } + +#[cfg(test)] +mod test { + mod macros_test { + crate::setup!(tests_scripts_lib); + + #[test] + fn verify_macros() { + let inst = crate::init!(); + + assert_eq!(inst.lang, None); + } + } +} diff --git a/rust-script/src/runtime/rust_script_instance.rs b/rust-script/src/runtime/rust_script_instance.rs index 44a2f56..08daba2 100644 --- a/rust-script/src/runtime/rust_script_instance.rs +++ b/rust-script/src/runtime/rust_script_instance.rs @@ -1,8 +1,9 @@ +use std::{collections::HashMap, rc::Rc}; + +#[cfg(debug_assertions)] use std::{ cell::RefCell, - collections::HashMap, ops::{Deref, DerefMut}, - rc::Rc, }; use abi_stable::std_types::{RBox, RString}; @@ -70,9 +71,11 @@ pub(super) struct RustScriptInstance { script: Gd, } +#[cfg(debug_assertions)] #[derive(Hash, PartialEq, Eq, Clone, Copy)] pub(super) struct RustScriptInstanceId(usize); +#[cfg(debug_assertions)] impl RustScriptInstanceId { pub fn new() -> Self { Self(rand::random()) @@ -198,7 +201,7 @@ impl ScriptInstance for RustScriptInstance { godot_print!("calling {}::{}", self.class_name(), method); let method = RString::with_capacity(method.len()).apply(|s| s.push_str(&method.to_string())); - let rargs = args.into_iter().map(|v| RemoteValueRef::new(v)).collect(); + let rargs = args.iter().map(|v| RemoteValueRef::new(v)).collect(); self.with_data_mut(move |data| data.call(method, rargs)) .map(Into::into) @@ -221,13 +224,12 @@ impl ScriptInstance for RustScriptInstance { lock.read() .expect("script registry is not accessible") .get(class_name) - .map(|meta| { + .and_then(|meta| { meta.methods() .iter() .find(|m| m.method_name == method) .map(|_| ()) }) - .flatten() .is_some() }) } @@ -251,7 +253,7 @@ impl ScriptInstance for RustScriptInstance { fn property_state(&self) -> Vec<(StringName, Variant)> { self.property_list() .as_slice() - .into_iter() + .iter() .map(|prop| &prop.property_name) .filter_map(|name| { self.get(name.to_owned()) diff --git a/rust-script/src/runtime/rust_script_language.rs b/rust-script/src/runtime/rust_script_language.rs index 29ee712..5aae568 100644 --- a/rust-script/src/runtime/rust_script_language.rs +++ b/rust-script/src/runtime/rust_script_language.rs @@ -30,10 +30,10 @@ impl RustScriptLanguage { .file_name() .and_then(OsStr::to_str) .unwrap() - .rsplit_once(".") + .rsplit_once('.') .unwrap() .0 - .split("_") + .split('_') .map(|part| { let mut chars = part.chars(); let first = chars.next().unwrap(); diff --git a/rust-script/src/script_registry.rs b/rust-script/src/script_registry.rs index 664d710..4ebfea9 100644 --- a/rust-script/src/script_registry.rs +++ b/rust-script/src/script_registry.rs @@ -53,7 +53,7 @@ where let args: Vec<_> = args.into_iter().map(|vref| vref.as_ref()).collect(); self.call(method.as_str().into(), &args) - .map(|rv| RemoteValue::from(rv)) + .map(RemoteValue::from) .into() } @@ -408,7 +408,7 @@ impl<'a> RemoteValueRef<'a> { pub fn new(value: &'a Variant) -> Self { Self { ptr: value.var_sys(), - lt: PhantomData::default(), + lt: PhantomData, } } diff --git a/tests-scripts-lib/Cargo.toml b/tests-scripts-lib/Cargo.toml new file mode 100644 index 0000000..d062bc5 --- /dev/null +++ b/tests-scripts-lib/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "tests-scripts-lib" +version.workspace = true +edition.workspace = true + +[lib] +crate-type = ["dylib", "rlib"] + +[dependencies] +godot-rust-script = { path = "../rust-script" } + + + diff --git a/tests-scripts-lib/src/lib.rs b/tests-scripts-lib/src/lib.rs new file mode 100644 index 0000000..d9da3c1 --- /dev/null +++ b/tests-scripts-lib/src/lib.rs @@ -0,0 +1 @@ +godot_rust_script::setup_library!();