Skip to content

Commit

Permalink
clean up clippy lints and compiler warnings (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano committed Nov 1, 2023
1 parent b4638f8 commit c03a8f6
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 39 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
godot-rust-script-derive = { path = "derive" }
tests-scripts-lib = { path = "tests-scripts-lib" }
6 changes: 3 additions & 3 deletions derive/src/attribute_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FieldExportOps {
let parsed_params = exp_list
.elems
.iter()
.map(|item| ExpEasingOpts::from_expr(item))
.map(ExpEasingOpts::from_expr)
.collect::<Result<Vec<_>, _>>()
.map_err(|err| err.write_errors())?;

Expand Down Expand Up @@ -92,7 +92,7 @@ impl FieldExportOps {
let filters = list
.elems
.iter()
.map(|item| String::from_expr(item))
.map(String::from_expr)
.collect::<Result<Vec<_>, _>>()
.map_err(|err| err.write_errors())?;

Expand Down Expand Up @@ -169,7 +169,7 @@ impl FieldExportOps {
let types = list
.elems
.iter()
.map(|item| String::from_expr(item))
.map(String::from_expr)
.collect::<Result<Vec<_>, _>>()
.map_err(|err| err.write_errors())?;

Expand Down
6 changes: 3 additions & 3 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn rust_to_variant_type(ty: &syn::Type) -> Result<TokenStream, TokenStream> {
)
.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)),
Expand All @@ -159,9 +159,9 @@ fn rust_to_variant_type(ty: &syn::Type) -> Result<TokenStream, TokenStream> {
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(),)),
Expand Down
4 changes: 4 additions & 0 deletions rust-script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion rust-script/src/apply.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub trait Apply: Sized {
fn apply<F: FnOnce(&mut Self) -> ()>(mut self, cb: F) -> Self {
fn apply<F: FnOnce(&mut Self)>(mut self, cb: F) -> Self {
cb(&mut self);
self
}
Expand Down
9 changes: 4 additions & 5 deletions rust-script/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion rust-script/src/runtime/hot_reloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl HotReloader {

Self {
channel: receiver,
ffi_init_fn: ffi_init_fn,
ffi_init_fn,
base,
}
}
Expand Down
48 changes: 33 additions & 15 deletions rust-script/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +16,7 @@ use godot::{
prelude::{
godot_print,
meta::{MethodInfo, PropertyInfo},
Array, Dictionary, Gd, StringName,
Array, Dictionary, Gd,
},
};

Expand All @@ -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};
Expand All @@ -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;

Expand All @@ -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() {}
}
};
}
Expand All @@ -67,17 +65,18 @@ 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,
)
};
}

thread_local! {
static SCRIPT_REGISTRY: RwLock<HashMap<String, ScriptMetaData>> = RwLock::default();
#[cfg(debug_assertions)]
static HOT_RELOAD_BRIDGE: RefCell<HashMap<RustScriptInstanceId, RefCell<HotReloadEntry>>> = RefCell::default();
static HOT_RELOAD_BRIDGE: std::cell::RefCell<HashMap<rust_script_instance::RustScriptInstanceId, std::cell::RefCell<HotReloadEntry>>> = std::cell::RefCell::default();
}

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

pub struct RustScriptExtensionLayer {
lib_init_fn: ::std::rc::Rc<dyn RustScriptLibInit>,
hot_reload_subscribe: HotReloadSubscribe,
lang: Option<Gd<RustScriptLanguage>>,
res_saver: Option<Gd<RustScriptResourceSaver>>,
res_loader: Option<Gd<RustScriptResourceLoader>>,
scripts_src_dir: Option<&'static str>,

#[cfg(debug_assertions)]
hot_reload_subscribe: HotReloadSubscribe,

#[cfg(debug_assertions)]
hot_reloader: Option<Gd<HotReloader>>,
}

impl RustScriptExtensionLayer {
pub fn new<F: RustScriptLibInit + 'static + Clone>(
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,
}
Expand All @@ -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"), &[]);
Expand Down Expand Up @@ -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);
}
}
}
14 changes: 8 additions & 6 deletions rust-script/src/runtime/rust_script_instance.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -70,9 +71,11 @@ pub(super) struct RustScriptInstance {
script: Gd<RustScript>,
}

#[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())
Expand Down Expand Up @@ -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)
Expand All @@ -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()
})
}
Expand All @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions rust-script/src/runtime/rust_script_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions rust-script/src/script_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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,
}
}

Expand Down
Loading

0 comments on commit c03a8f6

Please sign in to comment.