From 5b74f77210719c38f3e6c6f539e68b465872c83f Mon Sep 17 00:00:00 2001 From: qm3ster Date: Fri, 23 Apr 2021 03:31:30 +0300 Subject: [PATCH 1/4] Some clippy fixes --- examples/rpc_math_cli.rs | 2 +- examples/rpc_math_srvr.rs | 2 +- src/async_client.rs | 59 +++++++++++++++++---------------------- src/client_persistence.rs | 25 ++++++++--------- src/connect_options.rs | 8 +++--- src/create_options.rs | 2 +- src/disconnect_options.rs | 6 ++-- src/message.rs | 6 ++-- src/properties.rs | 12 ++++---- src/reason_code.rs | 1 - src/response_options.rs | 4 +-- src/server_response.rs | 6 ++-- src/ssl_options.rs | 14 +++++----- src/subscribe_options.rs | 2 +- src/token.rs | 26 ++++++++--------- src/types.rs | 2 -- src/will_options.rs | 4 +-- 17 files changed, 82 insertions(+), 99 deletions(-) diff --git a/examples/rpc_math_cli.rs b/examples/rpc_math_cli.rs index 74a3482..93e8cb6 100644 --- a/examples/rpc_math_cli.rs +++ b/examples/rpc_math_cli.rs @@ -12,7 +12,7 @@ //! - Using MQTT v5 properties //! - Publishing RPC request messages //! - Using asynchronous tokens -//! - Subscribing to reply topic +//! - Subscribing to reply topic // /******************************************************************************* diff --git a/examples/rpc_math_srvr.rs b/examples/rpc_math_srvr.rs index 3114303..d109978 100644 --- a/examples/rpc_math_srvr.rs +++ b/examples/rpc_math_srvr.rs @@ -12,7 +12,7 @@ //! - Using MQTT v5 properties //! - Receiving RPC request messages, and sending replies. //! - Using asynchronous tokens -//! - Subscribing to request topic +//! - Subscribing to request topic // /******************************************************************************* diff --git a/src/async_client.rs b/src/async_client.rs index 6a1f90a..a3c39e6 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -1155,20 +1155,16 @@ mod tests { let data = cli.user_data(); assert!(data.is_some()); - if let Some(lock) = data.unwrap().downcast_ref::>>() { - let mut v = lock.lock().unwrap(); - assert_eq!(3, v.len()); - assert_eq!("zero", v[0]); - assert_eq!("one", v[1]); - assert_eq!("two", v[2]); - - v.push("three"); - assert_eq!(4, v.len()); - assert_eq!("three", v[3]); - } - else { - assert!(false); - } + let lock = data.unwrap().downcast_ref::>>().unwrap(); + let mut v = lock.lock().unwrap(); + assert_eq!(3, v.len()); + assert_eq!("zero", v[0]); + assert_eq!("one", v[1]); + assert_eq!("two", v[2]); + + v.push("three"); + assert_eq!(4, v.len()); + assert_eq!("three", v[3]); } #[test] @@ -1185,28 +1181,23 @@ mod tests { assert!(data.is_some()); let data = data.unwrap(); - if let Some(lock) = data.downcast_ref::>>() { - // Try reading - { - let v = lock.read().unwrap(); - assert_eq!(3, v.len()); - assert_eq!("zero", v[0]); - assert_eq!("one", v[1]); - assert_eq!("two", v[2]); - } - - // Now try writing - { - let mut v = lock.write().unwrap(); - v.push("three"); - assert_eq!(4, v.len()); - assert_eq!("three", v[3]); - } - } - else { - assert!(false); + let lock = data.downcast_ref::>>().unwrap(); + // Try reading + { + let v = lock.read().unwrap(); + assert_eq!(3, v.len()); + assert_eq!("zero", v[0]); + assert_eq!("one", v[1]); + assert_eq!("two", v[2]); } + // Now try writing + { + let mut v = lock.write().unwrap(); + v.push("three"); + assert_eq!(4, v.len()); + assert_eq!("three", v[3]); + } } // Determine that a client can be sent across threads. diff --git a/src/client_persistence.rs b/src/client_persistence.rs index 29e497a..4d07c21 100644 --- a/src/client_persistence.rs +++ b/src/client_persistence.rs @@ -26,7 +26,6 @@ use std::{ ffi::{CString, CStr}, os::raw::{c_void, c_char, c_int}, }; -use libc; use crate::{ ffi, @@ -131,9 +130,9 @@ impl UserPersistence let client_id = CStr::from_ptr(client_id).to_str().unwrap(); let server_uri = CStr::from_ptr(server_uri).to_str().unwrap(); - let persist: &mut Box = mem::transmute(context); + let persist = &mut *(context as *mut std::boxed::Box); - if let Ok(_) = persist.open(client_id, server_uri) { + if persist.open(client_id, server_uri).is_ok() { *handle = context; return PERSISTENCE_SUCCESS; } @@ -148,7 +147,7 @@ impl UserPersistence return PERSISTENCE_ERROR; } - let persist: &mut Box = mem::transmute(handle); + let persist = &mut *(handle as *mut std::boxed::Box); match persist.close() { Ok(_) => PERSISTENCE_SUCCESS, @@ -170,7 +169,7 @@ impl UserPersistence if bufcount == 0 { return PERSISTENCE_SUCCESS; } - let persist: &mut Box = mem::transmute(handle); + let persist = &mut *(handle as *mut std::boxed::Box); let key = CStr::from_ptr(key).to_str().unwrap(); let mut bufs: Vec<&[u8]> = Vec::new(); @@ -197,7 +196,7 @@ impl UserPersistence buffer.is_null() || buflen.is_null() { return PERSISTENCE_ERROR; } - let persist: &mut Box = mem::transmute(handle); + let persist = &mut *(handle as *mut std::boxed::Box); let key = CStr::from_ptr(key).to_str().unwrap(); match persist.get(key) { @@ -221,7 +220,7 @@ impl UserPersistence if handle.is_null() || key.is_null() { return PERSISTENCE_ERROR; } - let persist: &mut Box = mem::transmute(handle); + let persist = &mut *(handle as *mut std::boxed::Box); let key = CStr::from_ptr(key).to_str().unwrap(); match persist.remove(key) { @@ -240,7 +239,7 @@ impl UserPersistence return PERSISTENCE_ERROR; } - let persist: &mut Box = mem::transmute(handle); + let persist = &mut *(handle as *mut std::boxed::Box); *keys = ptr::null_mut(); *nkeys = 0; @@ -251,14 +250,14 @@ impl UserPersistence if n != 0 { // TODO OPTIMIZE: This does a lot of copying let ckeys = libc::malloc(n * mem::size_of::()) as *mut *mut c_char; - for i in 0..n { - let s = CString::new(k[i].clone()).unwrap(); + for (i, s) in k.into_iter().enumerate() { + let s = CString::new(s).unwrap(); let sb = s.as_bytes_with_nul(); let sn = sb.len(); let cbuf = libc::malloc(sn) as *mut c_char; ptr::copy(sb.as_ptr(), cbuf as *mut u8, sn); - *ckeys.offset(i as isize) = cbuf; + *ckeys.add(i) = cbuf; } *keys = ckeys; *nkeys = n as c_int; @@ -276,7 +275,7 @@ impl UserPersistence if handle.is_null() { return PERSISTENCE_ERROR; } - let persist: &mut Box = mem::transmute(handle); + let persist = &mut *(handle as *mut std::boxed::Box); match persist.clear() { Ok(_) => PERSISTENCE_SUCCESS, @@ -292,7 +291,7 @@ impl UserPersistence if handle.is_null() || key.is_null() { return PERSISTENCE_ERROR; } - let persist: &mut Box = mem::transmute(handle); + let persist = &mut *(handle as *mut std::boxed::Box); let key = CStr::from_ptr(key).to_str().unwrap(); if persist.contains_key(key) { 1 } else { 0 } diff --git a/src/connect_options.rs b/src/connect_options.rs index ff10d7a..7eaa17b 100644 --- a/src/connect_options.rs +++ b/src/connect_options.rs @@ -182,7 +182,7 @@ impl ConnectOptions { /// done with it, we must recover and drop it (i.e. in the completion /// callback). pub fn set_token(&mut self, tok: ConnectToken) { - let tok: Token = tok.into(); + let tok: Token = tok; if self.copts.MQTTVersion < ffi::MQTTVERSION_5 as i32 { self.copts.onSuccess = Some(TokenInner::on_success); @@ -208,7 +208,7 @@ impl Default for ConnectOptions { impl Clone for ConnectOptions { fn clone(&self) -> Self { Self::from_data( - self.copts.clone(), + self.copts, (&*self.data).clone() ) } @@ -483,7 +483,7 @@ impl ConnectOptionsBuilder { /// Finalize the builder to create the connect options. pub fn finalize(&self) -> ConnectOptions { ConnectOptions::from_data( - self.copts.clone(), + self.copts, self.data.clone() ) } @@ -623,7 +623,7 @@ mod tests { // Compare the strings to the C-arrays in copts for (i, ref svr) in servers.iter().enumerate() { - let s = unsafe { CStr::from_ptr(*opts.copts.serverURIs.offset(i as isize)) }; + let s = unsafe { CStr::from_ptr(*opts.copts.serverURIs.add(i)) }; assert_eq!(&svr[..], s.to_str().unwrap()); } } diff --git a/src/create_options.rs b/src/create_options.rs index 5dea9f6..23973d7 100644 --- a/src/create_options.rs +++ b/src/create_options.rs @@ -434,7 +434,7 @@ mod tests { match opts.persistence { PersistenceType::None => (), - _ => assert!(false), + _ => unreachable!(), } } diff --git a/src/disconnect_options.rs b/src/disconnect_options.rs index 7285241..cdcd766 100644 --- a/src/disconnect_options.rs +++ b/src/disconnect_options.rs @@ -50,7 +50,7 @@ impl DisconnectOptions { // Ensures that the underlying C struct points to cached values fn from_data(mut copts: ffi::MQTTAsync_disconnectOptions, props: Properties) -> Self { - copts.properties = props.cprops.clone(); + copts.properties = props.cprops; Self { copts, props } } @@ -87,7 +87,7 @@ impl Default for DisconnectOptions { impl Clone for DisconnectOptions { fn clone(&self) -> Self { Self::from_data( - self.copts.clone(), + self.copts, self.props.clone() ) } @@ -157,7 +157,7 @@ impl DisconnectOptionsBuilder { /// Finalize the builder to create the disconnect options. pub fn finalize(&self) -> DisconnectOptions { DisconnectOptions::from_data( - self.copts.clone(), + self.copts, self.props.clone() ) } diff --git a/src/message.rs b/src/message.rs index dbe476d..ec79dda 100644 --- a/src/message.rs +++ b/src/message.rs @@ -115,7 +115,7 @@ impl Message { let data = Box::pin(data); cmsg.payload = data.payload.as_ptr() as *const _ as *mut c_void; cmsg.payloadlen = data.payload.len() as i32; - cmsg.properties = data.props.cprops.clone(); + cmsg.properties = data.props.cprops; Self { cmsg, data, } } @@ -134,7 +134,7 @@ impl Message { props: Properties::from_c_struct(&cmsg.properties), }; - Self::from_data(cmsg.clone(), data) + Self::from_data(*cmsg, data) } /// Gets the topic for the message. @@ -188,7 +188,7 @@ impl Default for Message { impl Clone for Message { fn clone(&self) -> Self { Self::from_data( - self.cmsg.clone(), + self.cmsg, (&*self.data).clone() ) } diff --git a/src/properties.rs b/src/properties.rs index 8f566e2..8f45deb 100644 --- a/src/properties.rs +++ b/src/properties.rs @@ -253,7 +253,7 @@ impl Property { Self::new_binary(code, v.clone()) } else if let Some(v) = rval.downcast_ref::<&[u8]>() { - return Self::new_binary(code, *v); + Self::new_binary(code, *v) } else if let Some(v) = rval.downcast_ref::<&[u8 ; 1]>() { Self::new_binary(code, v.to_vec()) @@ -355,7 +355,7 @@ impl Property { Self::new_string(code, &*v) } else if let Some(v) = rval.downcast_ref::<&str>() { - return Self::new_string(code, v); + Self::new_string(code, v) } else if let Some(v) = rval.downcast_ref::<(String,String)>() { Self::new_string_pair(code, &v.0, &v.1) @@ -471,9 +471,9 @@ impl Property { /// Creates a property from a C lib MQTTProperty struct. fn from_c_property(cprop: &ffi::MQTTProperty) -> Result { - let mut cprop = cprop.clone(); + let mut cprop = *cprop; let typ = match PropertyCode::new(cprop.identifier) - .and_then(|c| Some(c.property_type())) { + .map(|c| c.property_type()) { Some(typ) => typ, None => return Err(INVALID_PROPERTY_ID.into()) }; @@ -749,7 +749,7 @@ impl Clone for Property { /// For string any binary properties, this also clones the heap memory /// so that each property is managing separate allocations. fn clone(&self) -> Self { - let mut cprop = self.cprop.clone(); + let mut cprop = self.cprop; unsafe { match self.property_type() { @@ -1311,7 +1311,7 @@ mod tests { let val = "replies/myqueue"; let org_prop = Property::new_string(PropertyCode::ResponseTopic, val).unwrap(); - let prop = org_prop.clone(); + let prop = org_prop; unsafe { assert_eq!(prop.cprop.identifier, PropertyCode::ResponseTopic as Code); diff --git a/src/reason_code.rs b/src/reason_code.rs index 449d479..03b85d8 100644 --- a/src/reason_code.rs +++ b/src/reason_code.rs @@ -25,7 +25,6 @@ use std::{ fmt, ffi::CStr, }; -use ffi; /// MQTT v5 single-byte reason codes. #[repr(u8)] diff --git a/src/response_options.rs b/src/response_options.rs index cdb69b6..74e0f8f 100644 --- a/src/response_options.rs +++ b/src/response_options.rs @@ -104,7 +104,7 @@ impl ResponseOptions { ) -> Self { let mut data = Box::pin(data); - copts.properties = data.props.cprops.clone(); + copts.properties = data.props.cprops; let (p, n) = match data.sub_opts { Some(ref mut sub_opts) => ( @@ -179,7 +179,7 @@ impl ResponseOptionsBuilder { /// Create the response options from the builder. pub fn finalize(&self) -> ResponseOptions { ResponseOptions::from_data( - self.copts.clone(), + self.copts, self.data.clone() ) } diff --git a/src/server_response.rs b/src/server_response.rs index 3e19469..a886d01 100644 --- a/src/server_response.rs +++ b/src/server_response.rs @@ -140,7 +140,7 @@ impl ServerResponse { } else if !rsp.alt.qosList.is_null() { for i in 0..n { - qosv.push(*rsp.alt.qosList.offset(i as isize)); + qosv.push(*rsp.alt.qosList.add(i)); } } debug!("Subscribed to {} topics w/ QoS: {:?}", qosv.len(), qosv); @@ -183,7 +183,7 @@ impl ServerResponse { } else if !rsp.alt.sub.reasonCodes.is_null() { for i in 0..n { - qosv.push(rsp.alt.sub.reasonCodes.offset(i as isize) as i32); + qosv.push(rsp.alt.sub.reasonCodes.add(i) as i32); } } debug!("Subscribed to {} topics w/ QoS: {:?}", qosv.len(), qosv); @@ -202,7 +202,7 @@ impl ServerResponse { } else if !rsp.alt.sub.reasonCodes.is_null() { for i in 0..n { - qosv.push(rsp.alt.unsub.reasonCodes.offset(i as isize) as i32); + qosv.push(rsp.alt.unsub.reasonCodes.add(i) as i32); } } debug!("Subscribed to {} topics w/ Qos: {:?}", qosv.len(), qosv); diff --git a/src/ssl_options.rs b/src/ssl_options.rs index 9fd0c68..c0c17a6 100644 --- a/src/ssl_options.rs +++ b/src/ssl_options.rs @@ -92,7 +92,7 @@ impl SslOptions { // The C library expects unset values in the SSL options struct to be // NULL rather than empty string. fn c_str(str: &CString) -> *const c_char { - if str.to_bytes().len() == 0 { ptr::null() } else { str.as_ptr() } + if str.to_bytes().is_empty() { ptr::null() } else { str.as_ptr() } } // Converts the list of ALPN protocol strings into the wire format for @@ -190,7 +190,7 @@ impl Default for SslOptions { impl Clone for SslOptions { fn clone(&self) -> Self { Self::from_data( - self.copts.clone(), + self.copts, (&*self.data).clone() ) } @@ -224,7 +224,7 @@ impl SslOptionsBuilder { { self.data.trust_store = CString::new( trust_store.as_ref().to_str() - .ok_or(io::Error::new(io::ErrorKind::Other, "Path string error"))? + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Path string error"))? )?; Ok(self) } @@ -238,7 +238,7 @@ impl SslOptionsBuilder { { self.data.key_store = CString::new( key_store.as_ref().to_str() - .ok_or(io::Error::new(io::ErrorKind::Other, "Path string error"))? + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Path string error"))? )?; Ok(self) } @@ -250,7 +250,7 @@ impl SslOptionsBuilder { { self.data.private_key = CString::new( private_key.as_ref().to_str() - .ok_or(io::Error::new(io::ErrorKind::Other, "Path string error"))? + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Path string error"))? )?; Ok(self) } @@ -308,7 +308,7 @@ impl SslOptionsBuilder { { self.data.ca_path = CString::new( ca_path.as_ref().to_str() - .ok_or(io::Error::new(io::ErrorKind::Other, "Path string error"))? + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Path string error"))? )?; Ok(self) } @@ -331,7 +331,7 @@ impl SslOptionsBuilder { /// Create the SSL options from the builder. pub fn finalize(&self) -> SslOptions { SslOptions::from_data( - self.copts.clone(), + self.copts, self.data.clone() ) } diff --git a/src/subscribe_options.rs b/src/subscribe_options.rs index 4bceaf9..bd57009 100644 --- a/src/subscribe_options.rs +++ b/src/subscribe_options.rs @@ -161,7 +161,7 @@ impl SubscribeOptionsBuilder { /// Finalizes the builder to create the subscribe options. pub fn finalize(&self) -> SubscribeOptions { - SubscribeOptions { copts: self.copts.clone(), } + SubscribeOptions { copts: self.copts } } } diff --git a/src/token.rs b/src/token.rs index 734872a..88e4b82 100644 --- a/src/token.rs +++ b/src/token.rs @@ -306,11 +306,9 @@ impl TokenInner { cb(cli, msgid); } } - else { - if let Some(ref cb) = self.on_failure { - trace!("Invoking TokenInner::on_failure callback"); - cb(cli, msgid, rc); - } + else if let Some(ref cb) = self.on_failure { + trace!("Invoking TokenInner::on_failure callback"); + cb(cli, msgid, rc); } } @@ -351,11 +349,9 @@ impl TokenInner { cb(cli, msgid); } } - else { - if let Some(ref cb) = self.on_failure { - trace!("Invoking TokenInner::on_failure callback"); - cb(cli, msgid, rc); - } + else if let Some(ref cb) = self.on_failure { + trace!("Invoking TokenInner::on_failure callback"); + cb(cli, msgid, rc); } } @@ -576,14 +572,14 @@ impl DeliveryToken { unsafe impl Send for DeliveryToken {} -impl Into for DeliveryToken { - fn into(self) -> Message { self.msg } +impl From for Message { + fn from(v: DeliveryToken) -> Message { v.msg } } -impl Into for DeliveryToken { +impl From for Token { /// Converts the delivery token into a Token - fn into(self) -> Token { - Token { inner: self.inner } + fn from(v: DeliveryToken) -> Token { + Token { inner: v.inner } } } diff --git a/src/types.rs b/src/types.rs index 0006454..1bc98c6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -23,8 +23,6 @@ //! MQTT support types -use ffi; - /// The default version to connect with. /// First try v3.1.1, and if that fails, try v3.1 pub const MQTT_VERSION_DEFAULT: u32 = ffi::MQTTVERSION_DEFAULT; diff --git a/src/will_options.rs b/src/will_options.rs index 11f5e9d..6f6a96d 100644 --- a/src/will_options.rs +++ b/src/will_options.rs @@ -106,7 +106,7 @@ impl WillOptions { mut copts: ffi::MQTTAsync_willOptions, data: Pin> ) -> Self { - copts.topicName = if data.topic.as_bytes().len() != 0 { + copts.topicName = if !data.topic.as_bytes().is_empty() { data.topic.as_ptr() } else { @@ -172,7 +172,7 @@ impl Clone for WillOptions { /// to them. fn clone(&self) -> Self { Self::from_data( - self.copts.clone(), + self.copts, (&*self.data).clone() ) } From 0fae255cd00a35a41a1899c5d45d3df7c79ffa84 Mon Sep 17 00:00:00 2001 From: qm3ster Date: Fri, 23 Apr 2021 21:23:53 +0300 Subject: [PATCH 2/4] Some more clippy fixes --- src/connect_options.rs | 115 ++++++++++++++-------------------------- src/lib.rs | 1 + src/message.rs | 6 +++ src/response_options.rs | 2 +- src/ssl_options.rs | 36 ++++++------- src/token.rs | 22 ++++---- 6 files changed, 77 insertions(+), 105 deletions(-) diff --git a/src/connect_options.rs b/src/connect_options.rs index 7eaa17b..cb6bdd7 100644 --- a/src/connect_options.rs +++ b/src/connect_options.rs @@ -559,17 +559,11 @@ mod tests { assert!(!opts.copts.ssl.is_null()); - if let Some(ref ssl_opts) = opts.data.ssl { - // TODO: Test that ssl_opts.get_trust_store() is TRUST_STORE? - assert!(true); - assert_eq!(&ssl_opts.copts as *const _, opts.copts.ssl); - let ts = unsafe { CStr::from_ptr((*opts.copts.ssl).trustStore) }; - assert_eq!(TRUST_STORE, ts.to_str().unwrap()); - } - else { - // The SSL option should be set - assert!(false); - }; + let ssl_opts= opts.data.ssl.as_ref().unwrap(); + // TODO: Test that ssl_opts.get_trust_store() is TRUST_STORE? + assert_eq!(&ssl_opts.copts as *const _, opts.copts.ssl); + let ts = unsafe { CStr::from_ptr((*opts.copts.ssl).trustStore) }; + assert_eq!(TRUST_STORE, ts.to_str().unwrap()); } #[test] @@ -581,15 +575,12 @@ mod tests { assert!(!opts.copts.username.is_null()); - if let Some(ref user_name) = opts.data.user_name { - assert_eq!(NAME, user_name.to_str().unwrap()); + let user_name = opts.data.user_name.as_deref().unwrap(); + assert_eq!(NAME, user_name.to_str().unwrap()); - let s = unsafe { CStr::from_ptr(opts.copts.username) }; - assert_eq!(NAME, s.to_str().unwrap()); - } - else { - assert!(false); - }; + let s = unsafe { CStr::from_ptr(opts.copts.username) }; + assert_eq!(NAME, s.to_str().unwrap()); + } #[test] @@ -601,15 +592,11 @@ mod tests { assert!(!opts.copts.password.is_null()); - if let Some(ref password) = opts.data.password { - assert_eq!(PSWD, password.to_str().unwrap()); + let password = opts.data.password.as_deref().unwrap(); + assert_eq!(PSWD, password.to_str().unwrap()); - let s = unsafe { CStr::from_ptr(opts.copts.password) }; - assert_eq!(PSWD, s.to_str().unwrap()); - } - else { - assert!(false); - }; + let s = unsafe { CStr::from_ptr(opts.copts.password) }; + assert_eq!(PSWD, s.to_str().unwrap()); } #[test] @@ -651,15 +638,11 @@ mod tests { assert!(opts.data.https_proxy.is_none()); - if let Some(ref proxy) = opts.data.http_proxy { - assert_eq!(HTTP, proxy.to_str().unwrap()); + let proxy = opts.data.http_proxy.as_deref().unwrap(); + assert_eq!(HTTP, proxy.to_str().unwrap()); - let s = unsafe { CStr::from_ptr(opts.copts.httpProxy) }; - assert_eq!(HTTP, s.to_str().unwrap()); - } - else { - assert!(false); - }; + let s = unsafe { CStr::from_ptr(opts.copts.httpProxy) }; + assert_eq!(HTTP, s.to_str().unwrap()); let opts = ConnectOptionsBuilder::new() @@ -670,15 +653,11 @@ mod tests { assert!(opts.data.http_proxy.is_none()); - if let Some(ref proxy) = opts.data.https_proxy { - assert_eq!(HTTPS, proxy.to_str().unwrap()); + let proxy = opts.data.https_proxy.as_deref().unwrap(); + assert_eq!(HTTPS, proxy.to_str().unwrap()); - let s = unsafe { CStr::from_ptr(opts.copts.httpsProxy) }; - assert_eq!(HTTPS, s.to_str().unwrap()); - } - else { - assert!(false); - }; + let s = unsafe { CStr::from_ptr(opts.copts.httpsProxy) }; + assert_eq!(HTTPS, s.to_str().unwrap()); } @@ -709,18 +688,12 @@ mod tests { assert_eq!(USER_NAME, opts.data.user_name.as_ref().unwrap().to_str().unwrap()); assert_eq!(PASSWORD, opts.data.password.as_ref().unwrap().to_str().unwrap()); - if let Some(ref user_name) = opts.data.user_name { - assert_eq!(user_name.as_ptr(), opts.copts.username) - } - else { - assert!(false) - }; - if let Some(ref password) = opts.data.password { - assert_eq!(password.as_ptr(), opts.copts.password) - } - else { - assert!(false) - }; + let user_name = opts.data.user_name.as_deref().unwrap(); + assert_eq!(user_name.as_ptr(), opts.copts.username); + + let password = opts.data.password.as_deref().unwrap(); + assert_eq!(password.as_ptr(), opts.copts.password); + assert_eq!(CONNECT_TIMEOUT_SECS as i32, opts.copts.connectTimeout); } @@ -736,17 +709,13 @@ mod tests { .mqtt_version(MQTT_VERSION_5) // building options .finalize(); - if let Some(ref props) = opts.data.props { - assert_eq!(1, props.len()); - assert_eq!(Some(60), props.get_int(PropertyCode::SessionExpiryInterval)); + let props = opts.data.props.as_ref().unwrap(); + assert_eq!(1, props.len()); + assert_eq!(Some(60), props.get_int(PropertyCode::SessionExpiryInterval)); - assert!(!opts.copts.connectProperties.is_null()); - assert_eq!(&props.cprops as *const _ as *mut ffi::MQTTProperties, - opts.copts.connectProperties); - } - else { - assert!(false) - }; + assert!(!opts.copts.connectProperties.is_null()); + assert_eq!(&props.cprops as *const _ as *mut ffi::MQTTProperties, + opts.copts.connectProperties); } #[test] @@ -765,17 +734,13 @@ mod tests { .will_message(lwt) .finalize(); - if let Some(ref will_props) = opts.data.will_props { - assert_eq!(1, will_props.len()); - assert_eq!(Some(60), will_props.get_int(PropertyCode::WillDelayInterval)); + let will_props = opts.data.will_props.as_ref().unwrap(); + assert_eq!(1, will_props.len()); + assert_eq!(Some(60), will_props.get_int(PropertyCode::WillDelayInterval)); - assert!(!opts.copts.willProperties.is_null()); - assert_eq!(&will_props.cprops as *const _ as *mut ffi::MQTTProperties, - opts.copts.willProperties); - } - else { - assert!(false) - }; + assert!(!opts.copts.willProperties.is_null()); + assert_eq!(&will_props.cprops as *const _ as *mut ffi::MQTTProperties, + opts.copts.willProperties); } diff --git a/src/lib.rs b/src/lib.rs index 75a537a..da2bbd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::clippy::missing_safety_doc)] // lib.rs // // This file is part of the Eclipse Paho MQTT Rust Client library. diff --git a/src/message.rs b/src/message.rs index ec79dda..244087f 100644 --- a/src/message.rs +++ b/src/message.rs @@ -326,6 +326,12 @@ impl MessageBuilder } } +impl Default for MessageBuilder { + fn default() -> Self { + Self::new() + } +} + ///////////////////////////////////////////////////////////////////////////// // Unit Tests ///////////////////////////////////////////////////////////////////////////// diff --git a/src/response_options.rs b/src/response_options.rs index 74e0f8f..9b019c0 100644 --- a/src/response_options.rs +++ b/src/response_options.rs @@ -111,7 +111,7 @@ impl ResponseOptions { sub_opts.as_mut_ptr(), sub_opts.len() as c_int ), - _ => (ptr::null_mut(), 0 as c_int) + _ => (ptr::null_mut(), 0) }; copts.subscribeOptionsList = p; diff --git a/src/ssl_options.rs b/src/ssl_options.rs index c0c17a6..d32e36d 100644 --- a/src/ssl_options.rs +++ b/src/ssl_options.rs @@ -419,24 +419,24 @@ mod tests { fn test_protos() { let protos = &["spdy/1", "http/1.1"]; - let v = vec![ - 6 as c_uchar, - b's' as c_uchar, - b'p' as c_uchar, - b'd' as c_uchar, - b'y' as c_uchar, - b'/' as c_uchar, - b'1' as c_uchar, - - 8 as c_uchar, - b'h' as c_uchar, - b't' as c_uchar, - b't' as c_uchar, - b'p' as c_uchar, - b'/' as c_uchar, - b'1' as c_uchar, - b'.' as c_uchar, - b'1' as c_uchar + let v: Vec = vec![ + 6, + b's', + b'p', + b'd', + b'y', + b'/', + b'1', + + 8, + b'h', + b't', + b't', + b'p', + b'/', + b'1', + b'.', + b'1' ]; assert_eq!(v, SslOptions::proto_vec(protos)); diff --git a/src/token.rs b/src/token.rs index 88e4b82..30ffd4a 100644 --- a/src/token.rs +++ b/src/token.rs @@ -41,7 +41,6 @@ use { task::{Context, Poll, Waker}, ffi::CStr, os::raw::c_void, - convert::Into, }, futures::{ executor::block_on, @@ -478,6 +477,12 @@ impl Token { } } +impl Default for Token { + fn default() -> Self { + Self::new() + } +} + unsafe impl Send for Token {} impl Future for Token { @@ -688,11 +693,9 @@ mod tests { let mut tok = Token::from_error(ERR_CODE); match tok.try_wait() { - //Some(Err(Error::Paho(ERR_CODE))) => (), - Some(Err(Error::PahoDescr(ERR_CODE, _))) => (), - Some(Err(_)) => assert!(false), - Some(Ok(_)) => assert!(false), - None => assert!(false) + // Some(Err(Error::Paho(ERR_CODE))) => {} + Some(Err(Error::PahoDescr(ERR_CODE, _))) => {} + Some(Err(_)) | Some(Ok(_)) | None => unreachable!() } // An unsignaled token @@ -701,8 +704,7 @@ mod tests { // If it's not done, we should get None match tok.try_wait() { None => (), - Some(Err(_)) => assert!(false), - Some(Ok(_)) => assert!(false), + Some(Err(_)) | Some(Ok(_)) => unreachable!(), } // Complete the token @@ -717,9 +719,7 @@ mod tests { match tok.try_wait() { Some(Err(Error::Paho(ERR_CODE))) => (), //Some(Err(Error::PahoDescr(ERR_CODE, _))) => (), - Some(Err(_)) => assert!(false), - Some(Ok(_)) => assert!(false), - None => assert!(false) + Some(Err(_)) | Some(Ok(_)) | None => unreachable!() } } From 3e06674fcf1a1c1d7a4b5853a334adcb587364e0 Mon Sep 17 00:00:00 2001 From: qm3ster Date: Fri, 23 Apr 2021 21:24:24 +0300 Subject: [PATCH 3/4] Go hog wild in build.rs --- paho-mqtt-sys/build.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/paho-mqtt-sys/build.rs b/paho-mqtt-sys/build.rs index 66ac15b..ac15053 100644 --- a/paho-mqtt-sys/build.rs +++ b/paho-mqtt-sys/build.rs @@ -268,24 +268,22 @@ mod build { // Link in the SSL libraries if configured for it. if cfg!(feature = "ssl") { + let openssl_root_dir = openssl_root_dir(); if cfg!(windows) { println!("cargo:rustc-link-lib=libssl"); println!("cargo:rustc-link-lib=libcrypto"); - if let Some(openssl_root_dir) = openssl_root_dir() { + let openssl_root_dir = openssl_root_dir + .as_deref() + .or_else(|| cfg!(target_arch = "x86").then(||"C:\\OpenSSL-Win32")) + .or_else(|| cfg!(target_arch = "x86_64").then(||"C:\\OpenSSL-Win64")); + if let Some(openssl_root_dir) = openssl_root_dir { println!("cargo:rustc-link-search={}\\lib", openssl_root_dir); } - else { - #[cfg(target_arch = "x86")] - println!("cargo:rustc-link-search={}\\lib", "C:\\OpenSSL-Win32"); - - #[cfg(target_arch = "x86_64")] - println!("cargo:rustc-link-search={}\\lib", "C:\\OpenSSL-Win64"); - }; } else { println!("cargo:rustc-link-lib=ssl"); println!("cargo:rustc-link-lib=crypto"); - if let Some(openssl_root_dir) = openssl_root_dir() { + if let Some(openssl_root_dir) = openssl_root_dir { println!("cargo:rustc-link-search={}/lib", openssl_root_dir); } } From 8b601b4fa4ad8b144f465322a354479613b266c9 Mon Sep 17 00:00:00 2001 From: qm3ster Date: Fri, 23 Apr 2021 22:17:05 +0300 Subject: [PATCH 4/4] Update stale doc comments --- src/message.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/message.rs b/src/message.rs index 244087f..60133e1 100644 --- a/src/message.rs +++ b/src/message.rs @@ -138,13 +138,12 @@ impl Message { } /// Gets the topic for the message. - /// Note that this copies the topic. pub fn topic(&self) -> &str { - self.data.topic.to_str().unwrap() + self.data.topic.to_str().expect("paho.mqtt.c already validated utf8") } /// Gets the payload of the message. - /// This returns the payload as a binary vector. + /// This returns the payload as a slice. pub fn payload(&self) -> &[u8] { self.data.payload.as_slice() } @@ -153,7 +152,7 @@ impl Message { /// /// This utilizes the "lossy" style of conversion from the std library. /// If the contents of the CStr are valid UTF-8 data, this function will - /// return a `Cow::Borrowed([&str])` with the the corresponding `[&str]` slice. + /// return a `Cow::Borrowed(&str)` with the the corresponding `&str` slice. /// Otherwise, it will replace any invalid UTF-8 sequences with U+FFFD /// REPLACEMENT CHARACTER and return a `Cow::Owned(String)` with the result. pub fn payload_str(&self) -> Cow<'_, str> {