Skip to content

Commit c03a8f6

Browse files
authored
clean up clippy lints and compiler warnings (#2)
1 parent b4638f8 commit c03a8f6

File tree

14 files changed

+97
-39
lines changed

14 files changed

+97
-39
lines changed

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ darling = { version = "0.20.3" }
2222
proc-macro2 = "1.0.68"
2323
quote = "1.0.33"
2424
syn = "2.0.38"
25+
process_path = "0.1"
2526

26-
godot-rust-script-derive = { path = "derive" }
27+
godot-rust-script-derive = { path = "derive" }
28+
tests-scripts-lib = { path = "tests-scripts-lib" }

derive/src/attribute_ops.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl FieldExportOps {
6363
let parsed_params = exp_list
6464
.elems
6565
.iter()
66-
.map(|item| ExpEasingOpts::from_expr(item))
66+
.map(ExpEasingOpts::from_expr)
6767
.collect::<Result<Vec<_>, _>>()
6868
.map_err(|err| err.write_errors())?;
6969

@@ -92,7 +92,7 @@ impl FieldExportOps {
9292
let filters = list
9393
.elems
9494
.iter()
95-
.map(|item| String::from_expr(item))
95+
.map(String::from_expr)
9696
.collect::<Result<Vec<_>, _>>()
9797
.map_err(|err| err.write_errors())?;
9898

@@ -169,7 +169,7 @@ impl FieldExportOps {
169169
let types = list
170170
.elems
171171
.iter()
172-
.map(|item| String::from_expr(item))
172+
.map(String::from_expr)
173173
.collect::<Result<Vec<_>, _>>()
174174
.map_err(|err| err.write_errors())?;
175175

derive/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn rust_to_variant_type(ty: &syn::Type) -> Result<TokenStream, TokenStream> {
132132
)
133133
.into_compile_error()),
134134
T::Tuple(tuple) => {
135-
if tuple.elems.len() > 0 {
135+
if !tuple.elems.is_empty() {
136136
return Err(syn::Error::new(
137137
ty.span(),
138138
format!("\"{}\" is not a supported type", quote!(#tuple)),
@@ -159,9 +159,9 @@ fn rust_to_variant_type(ty: &syn::Type) -> Result<TokenStream, TokenStream> {
159159
fn derive_default_with_base(field_opts: &[FieldOpts]) -> TokenStream {
160160
let godot_types = godot_types();
161161
let fields: TokenStream = field_opts
162-
.into_iter()
162+
.iter()
163163
.filter_map(|field| match field.ident.as_ref() {
164-
Some(ident) if ident.to_string() == "base" => {
164+
Some(ident) if *ident == "base" => {
165165
Some(quote_spanned!(ident.span() => #ident: base.cast(),))
166166
}
167167
Some(ident) => Some(quote_spanned!(ident.span() => #ident: Default::default(),)),

rust-script/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ itertools.workspace = true
1313
abi_stable.workspace = true
1414
rand.workspace = true
1515
cfg-if.workspace = true
16+
process_path.workspace = true
1617

1718
godot-rust-script-derive.workspace = true
19+
20+
[dev-dependencies]
21+
tests-scripts-lib = { path = "../tests-scripts-lib" }

rust-script/src/apply.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub trait Apply: Sized {
2-
fn apply<F: FnOnce(&mut Self) -> ()>(mut self, cb: F) -> Self {
2+
fn apply<F: FnOnce(&mut Self)>(mut self, cb: F) -> Self {
33
cb(&mut self);
44
self
55
}

rust-script/src/library.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ macro_rules! setup_library {
7272
}
7373
}
7474

75-
let lock = crate::__godot_rust_plugin___SCRIPT_REGISTRY.lock().expect("unable to aquire mutex lock");
75+
let lock = __godot_rust_plugin___SCRIPT_REGISTRY.lock().expect("unable to aquire mutex lock");
7676

7777
$crate::assemble_metadata(lock.iter())
7878
}
@@ -112,8 +112,7 @@ impl RustScriptPropDesc {
112112
RemoteScriptPropertyInfo {
113113
variant_type: self.ty.into(),
114114
class_name: RStr::from_str(class_name),
115-
property_name: RString::with_capacity(self.name.len())
116-
.apply(|s| s.push_str(&self.name)),
115+
property_name: RString::with_capacity(self.name.len()).apply(|s| s.push_str(self.name)),
117116
usage: if self.exported {
118117
(PropertyUsageFlags::PROPERTY_USAGE_EDITOR
119118
| PropertyUsageFlags::PROPERTY_USAGE_STORAGE)
@@ -167,11 +166,11 @@ pub fn assemble_metadata<'a>(
167166
})
168167
.unzip();
169168

170-
let methods: BTreeMap<_, _> = methods.into_iter().filter_map(|x| x).collect();
169+
let methods: BTreeMap<_, _> = methods.into_iter().flatten().collect();
171170

172171
entries
173172
.into_iter()
174-
.filter_map(|x| x)
173+
.flatten()
175174
.map(|class| {
176175
let props = (class.properties)()
177176
.into_iter()

rust-script/src/runtime/hot_reloader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl HotReloader {
6060

6161
Self {
6262
channel: receiver,
63-
ffi_init_fn: ffi_init_fn,
63+
ffi_init_fn,
6464
base,
6565
}
6666
}

rust-script/src/runtime/mod.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod rust_script;
66
mod rust_script_instance;
77
mod rust_script_language;
88

9-
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::RwLock};
9+
use std::{collections::HashMap, rc::Rc, sync::RwLock};
1010

1111
use abi_stable::std_types::RVec;
1212
use cfg_if::cfg_if;
@@ -16,7 +16,7 @@ use godot::{
1616
prelude::{
1717
godot_print,
1818
meta::{MethodInfo, PropertyInfo},
19-
Array, Dictionary, Gd, StringName,
19+
Array, Dictionary, Gd,
2020
},
2121
};
2222

@@ -26,7 +26,7 @@ use crate::{
2626
script_registry::{RemoteScriptMetaData, ScriptMetaData},
2727
};
2828

29-
use self::{rust_script_instance::RustScriptInstanceId, rust_script_language::RustScriptLanguage};
29+
use self::rust_script_language::RustScriptLanguage;
3030

3131
#[cfg(debug_assertions)]
3232
use hot_reloader::{HotReloadEntry, HotReloader};
@@ -35,7 +35,7 @@ use hot_reloader::{HotReloadEntry, HotReloader};
3535
macro_rules! setup {
3636
($lib_crate:tt) => {
3737
#[cfg(debug_assertions)]
38-
#[$crate::private_export::hot_module(dylib = stringify!($lib_crate), lib_dir = env!("CARGO_TARGET"))]
38+
#[$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())]
3939
mod scripts_lib {
4040
use $crate::private_export::RVec;
4141

@@ -57,8 +57,6 @@ macro_rules! setup {
5757
#[cfg(not(debug_assertions))]
5858
mod scripts_lib {
5959
pub use ::$lib_crate::{__godot_rust_script_init, __GODOT_RUST_SCRIPT_SRC_ROOT};
60-
61-
pub fn subscribe() {}
6260
}
6361
};
6462
}
@@ -67,17 +65,18 @@ macro_rules! setup {
6765
macro_rules! init {
6866
() => {
6967
$crate::RustScriptExtensionLayer::new(
70-
crate::scripts_lib::__godot_rust_script_init,
71-
crate::scripts_lib::subscribe,
72-
crate::scripts_lib::__GODOT_RUST_SCRIPT_SRC_ROOT,
68+
scripts_lib::__godot_rust_script_init,
69+
scripts_lib::__GODOT_RUST_SCRIPT_SRC_ROOT,
70+
#[cfg(debug_assertions)]
71+
scripts_lib::subscribe,
7372
)
7473
};
7574
}
7675

7776
thread_local! {
7877
static SCRIPT_REGISTRY: RwLock<HashMap<String, ScriptMetaData>> = RwLock::default();
7978
#[cfg(debug_assertions)]
80-
static HOT_RELOAD_BRIDGE: RefCell<HashMap<RustScriptInstanceId, RefCell<HotReloadEntry>>> = RefCell::default();
79+
static HOT_RELOAD_BRIDGE: std::cell::RefCell<HashMap<rust_script_instance::RustScriptInstanceId, std::cell::RefCell<HotReloadEntry>>> = std::cell::RefCell::default();
8180
}
8281

8382
pub type BindingInit = godot::sys::GodotBinding;
@@ -89,36 +88,39 @@ impl<F> RustScriptLibInit for F where F: Fn(Option<BindingInit>) -> RVec<RemoteS
8988
cfg_if! {
9089
if #[cfg(debug_assertions)] {
9190
type HotReloadSubscribe = fn() -> hot_lib_reloader::LibReloadObserver;
92-
} else {
93-
type HotReloadSubscribe = fn() -> ();
9491
}
9592
}
9693

9794
pub struct RustScriptExtensionLayer {
9895
lib_init_fn: ::std::rc::Rc<dyn RustScriptLibInit>,
99-
hot_reload_subscribe: HotReloadSubscribe,
10096
lang: Option<Gd<RustScriptLanguage>>,
10197
res_saver: Option<Gd<RustScriptResourceSaver>>,
10298
res_loader: Option<Gd<RustScriptResourceLoader>>,
10399
scripts_src_dir: Option<&'static str>,
104100

101+
#[cfg(debug_assertions)]
102+
hot_reload_subscribe: HotReloadSubscribe,
103+
105104
#[cfg(debug_assertions)]
106105
hot_reloader: Option<Gd<HotReloader>>,
107106
}
108107

109108
impl RustScriptExtensionLayer {
110109
pub fn new<F: RustScriptLibInit + 'static + Clone>(
111110
lib_init_fn: F,
112-
hot_reload_subscribe: HotReloadSubscribe,
113111
scripts_src_dir: &'static str,
112+
#[cfg(debug_assertions)] hot_reload_subscribe: HotReloadSubscribe,
114113
) -> Self {
115114
Self {
116115
lib_init_fn: Rc::new(lib_init_fn),
117-
hot_reload_subscribe,
118116
lang: None,
119117
res_saver: None,
120118
res_loader: None,
121119
scripts_src_dir: Some(scripts_src_dir),
120+
121+
#[cfg(debug_assertions)]
122+
hot_reload_subscribe,
123+
122124
#[cfg(debug_assertions)]
123125
hot_reloader: None,
124126
}
@@ -133,6 +135,8 @@ impl RustScriptExtensionLayer {
133135

134136
cfg_if! {
135137
if #[cfg(debug_assertions)] {
138+
use godot::prelude::StringName;
139+
136140
let mut hot_reloader = Gd::with_base(|base| HotReloader::new((self.hot_reload_subscribe)(), self.lib_init_fn.clone(), base));
137141

138142
hot_reloader.call_deferred(StringName::from("register"), &[]);
@@ -235,3 +239,17 @@ impl ToDictionary for MethodInfo {
235239
})
236240
}
237241
}
242+
243+
#[cfg(test)]
244+
mod test {
245+
mod macros_test {
246+
crate::setup!(tests_scripts_lib);
247+
248+
#[test]
249+
fn verify_macros() {
250+
let inst = crate::init!();
251+
252+
assert_eq!(inst.lang, None);
253+
}
254+
}
255+
}

rust-script/src/runtime/rust_script_instance.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use std::{collections::HashMap, rc::Rc};
2+
3+
#[cfg(debug_assertions)]
14
use std::{
25
cell::RefCell,
3-
collections::HashMap,
46
ops::{Deref, DerefMut},
5-
rc::Rc,
67
};
78

89
use abi_stable::std_types::{RBox, RString};
@@ -70,9 +71,11 @@ pub(super) struct RustScriptInstance {
7071
script: Gd<RustScript>,
7172
}
7273

74+
#[cfg(debug_assertions)]
7375
#[derive(Hash, PartialEq, Eq, Clone, Copy)]
7476
pub(super) struct RustScriptInstanceId(usize);
7577

78+
#[cfg(debug_assertions)]
7679
impl RustScriptInstanceId {
7780
pub fn new() -> Self {
7881
Self(rand::random())
@@ -198,7 +201,7 @@ impl ScriptInstance for RustScriptInstance {
198201
godot_print!("calling {}::{}", self.class_name(), method);
199202
let method =
200203
RString::with_capacity(method.len()).apply(|s| s.push_str(&method.to_string()));
201-
let rargs = args.into_iter().map(|v| RemoteValueRef::new(v)).collect();
204+
let rargs = args.iter().map(|v| RemoteValueRef::new(v)).collect();
202205

203206
self.with_data_mut(move |data| data.call(method, rargs))
204207
.map(Into::into)
@@ -221,13 +224,12 @@ impl ScriptInstance for RustScriptInstance {
221224
lock.read()
222225
.expect("script registry is not accessible")
223226
.get(class_name)
224-
.map(|meta| {
227+
.and_then(|meta| {
225228
meta.methods()
226229
.iter()
227230
.find(|m| m.method_name == method)
228231
.map(|_| ())
229232
})
230-
.flatten()
231233
.is_some()
232234
})
233235
}
@@ -251,7 +253,7 @@ impl ScriptInstance for RustScriptInstance {
251253
fn property_state(&self) -> Vec<(StringName, Variant)> {
252254
self.property_list()
253255
.as_slice()
254-
.into_iter()
256+
.iter()
255257
.map(|prop| &prop.property_name)
256258
.filter_map(|name| {
257259
self.get(name.to_owned())

0 commit comments

Comments
 (0)