diff --git a/rust/src/api/instance.rs b/rust/src/api/instance.rs index cfe67d1..958985a 100644 --- a/rust/src/api/instance.rs +++ b/rust/src/api/instance.rs @@ -231,17 +231,13 @@ impl<'a> ChainableInstance<'a> { } /// Returns the Rust representation of the provided instance - pub fn to_rust(self) -> errors::Result - where - T: DeserializeOwned, + pub fn to_rust(self) -> errors::Result { self.jvm.to_rust(self.instance) } /// Returns the Rust representation of the provided instance, boxed - pub fn to_rust_boxed(self) -> errors::Result> - where - T: DeserializeOwned, + pub fn to_rust_boxed(self) -> errors::Result> { self.jvm.to_rust_boxed(self.instance) } diff --git a/rust/src/api/invocation_arg.rs b/rust/src/api/invocation_arg.rs index 0486d60..070f316 100644 --- a/rust/src/api/invocation_arg.rs +++ b/rust/src/api/invocation_arg.rs @@ -627,7 +627,7 @@ impl<'a> TryFrom<&'a u16> for InvocationArg { } } -impl<'a, 'b> TryFrom<&'a i32> for InvocationArg { +impl<'a> TryFrom<&'a i32> for InvocationArg { type Error = errors::J4RsError; fn try_from(arg: &'a i32) -> errors::Result { InvocationArg::new_2( diff --git a/rust/src/api/mod.rs b/rust/src/api/mod.rs index c90d2c3..2a2b366 100644 --- a/rust/src/api/mod.rs +++ b/rust/src/api/mod.rs @@ -1631,9 +1631,8 @@ impl Jvm { pub fn select(instance_receivers: &[&InstanceReceiver]) -> errors::Result<(usize, Instance)> { loop { for (index, ir) in instance_receivers.iter().enumerate() { - let res = ir.rx.try_recv(); - if res.is_ok() { - return Ok((index, res.unwrap())); + if let Ok(res) = ir.rx.try_recv() { + return Ok((index, res)); } } thread::yield_now(); @@ -1653,9 +1652,8 @@ impl Jvm { let start = time::Instant::now(); loop { for (index, ir) in instance_receivers.iter().enumerate() { - let res = ir.rx.try_recv(); - if res.is_ok() { - return Ok((index, res.unwrap())); + if let Ok(res) = ir.rx.try_recv() { + return Ok((index, res)); } } if &start.elapsed() > timeout { @@ -1836,7 +1834,7 @@ impl<'a> JvmBuilder<'a> { self.classpath_entries .iter() .fold(".".to_string(), |all, elem| { - format!("{}{}{}", all, utils::classpath_sep(), elem.to_string()) + format!("{}{}{}", all, utils::classpath_sep(), elem) }) } else { // The default classpath contains all the jars in the jassets directory @@ -1866,7 +1864,7 @@ impl<'a> JvmBuilder<'a> { self.classpath_entries .iter() .fold(default_class_path, |all, elem| { - format!("{}{}{}", all, utils::classpath_sep(), elem.to_string()) + format!("{}{}{}", all, utils::classpath_sep(), elem) }) }; info(&format!("Setting classpath to {}", classpath)); @@ -2058,9 +2056,9 @@ impl<'a> ClasspathEntry<'a> { } } -impl<'a> ToString for ClasspathEntry<'a> { - fn to_string(&self) -> String { - self.0.to_string() +impl<'a> std::fmt::Display for ClasspathEntry<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) } } @@ -2074,9 +2072,9 @@ impl<'a> JavaOpt<'a> { } } -impl<'a> ToString for JavaOpt<'a> { - fn to_string(&self) -> String { - self.0.to_string() +impl<'a> std::fmt::Display for JavaOpt<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) } } diff --git a/rust/src/cache.rs b/rust/src/cache.rs index 8635fd6..9d9747e 100644 --- a/rust/src/cache.rs +++ b/rust/src/cache.rs @@ -346,14 +346,14 @@ macro_rules! get_cached { } else { None }; - if jopt.is_none() { + if let Some(jopt) = jopt { + Ok(jopt) + } else { let j = { $do_retrieve }; if CLASS_CACHING_ENABLED { $setter_name(j); } Ok(j) - } else { - Ok(jopt.unwrap()) } }}; } diff --git a/rust/src/errors.rs b/rust/src/errors.rs index aa8fd15..743e097 100644 --- a/rust/src/errors.rs +++ b/rust/src/errors.rs @@ -32,15 +32,6 @@ pub(crate) fn opt_to_res(opt: Option) -> Result { opt.ok_or(J4RsError::RustError("Option was found None while converting to result".to_string())) } -#[allow(unused)] -pub(crate) fn res_to_opt(res: Result) -> Option { - if res.is_err() { - None - } else { - Some(res.unwrap()) - } -} - #[derive(Debug, PartialEq, Eq, Clone)] pub enum J4RsError { GeneralError(String), diff --git a/rust/src/jfx.rs b/rust/src/jfx.rs index bde40a7..e5e0a3f 100644 --- a/rust/src/jfx.rs +++ b/rust/src/jfx.rs @@ -86,8 +86,8 @@ impl JavaFxSupport for Jvm { instance, "addEventHandler", &[ - InvocationArg::try_from(event_type_instance)?, - InvocationArg::try_from(j4rs_event_handler)?, + InvocationArg::from(event_type_instance), + InvocationArg::from(j4rs_event_handler), ], )?; Ok(btn_action_channel) @@ -102,7 +102,7 @@ impl JavaFxSupport for Jvm { self.invoke( stage, "setOnCloseRequest", - &[InvocationArg::try_from(j4rs_event_handler)?], + &[InvocationArg::from(j4rs_event_handler)], )?; Ok(action_channel) } @@ -165,7 +165,7 @@ impl JavaFxSupport for Jvm { let controller = self.invoke_static( CLASS_J4RS_FXML_LOADER, "loadFxml", - &[InvocationArg::try_from(cloned)?, InvocationArg::try_from(path_str)?], + &[InvocationArg::from(cloned), InvocationArg::try_from(path_str)?], )?; Ok(FxController::new(controller)) } @@ -200,7 +200,7 @@ impl FxController { jvm.invoke( &self.controller, "addControllerInitializedCallback", - &[InvocationArg::try_from(channel_support)?], + &[InvocationArg::from(channel_support)], )?; instance_receiver } @@ -221,8 +221,8 @@ impl FxController { "addEventHandler", &[ InvocationArg::try_from(node_id)?, - InvocationArg::try_from(j4rs_event_handler)?, - InvocationArg::try_from(event_type_instance)?, + InvocationArg::from(j4rs_event_handler), + InvocationArg::from(event_type_instance), ], )?; Ok(event_channel) diff --git a/rust/src/jni_utils.rs b/rust/src/jni_utils.rs index 80d065d..b9a0668 100644 --- a/rust/src/jni_utils.rs +++ b/rust/src/jni_utils.rs @@ -280,7 +280,7 @@ pub(crate) fn global_jobject_from_i8(a: &i8, jni_env: *mut JNIEnv) -> errors::Re jni_env, cache::get_byte_class()?, cache::get_byte_constructor_method()?, - tmp as *const i8, + tmp, ); create_global_ref_from_local_ref(o, jni_env) } @@ -308,7 +308,7 @@ pub(crate) fn global_jobject_from_i16(a: &i16, jni_env: *mut JNIEnv) -> errors:: jni_env, cache::get_short_class()?, cache::get_short_constructor_method()?, - tmp as *const i16, + tmp, ); create_global_ref_from_local_ref(o, jni_env) } @@ -321,7 +321,7 @@ pub(crate) fn global_jobject_from_u16(a: &u16, jni_env: *mut JNIEnv) -> errors:: jni_env, cache::get_character_class()?, cache::get_character_constructor_method()?, - tmp as *const u16, + tmp, ); create_global_ref_from_local_ref(o, jni_env) } @@ -364,7 +364,7 @@ pub(crate) fn global_jobject_from_i32(a: &i32, jni_env: *mut JNIEnv) -> errors:: jni_env, cache::get_integer_class()?, cache::get_integer_constructor_method()?, - tmp as *const i32, + tmp, ); create_global_ref_from_local_ref(o, jni_env) } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index d3636a0..5b07e6c 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -125,7 +125,7 @@ pub extern "C" fn Java_org_astonbitecode_j4rs_api_invocation_NativeCallbackToRus let tx = unsafe { Box::from_raw(p) }; let result = tx.send(Ok(instance)); - if let Err(_) = result { + if result.is_err() { panic!("Could not send to the defined callback channel to complete the future"); } } else { @@ -150,7 +150,7 @@ pub unsafe extern "C" fn Java_org_astonbitecode_j4rs_api_invocation_NativeCallba let tx = unsafe { Box::from_raw(p) }; let result = tx.send(Err(errors::J4RsError::JavaError(st))); - if let Err(_) = result { + if result.is_err() { panic!("Could not send to the defined callback channel to fail a future"); } } else {