Skip to content

Commit

Permalink
Some clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
qm3ster committed Apr 23, 2021
1 parent 798b151 commit 5b74f77
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 99 deletions.
2 changes: 1 addition & 1 deletion examples/rpc_math_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! - Using MQTT v5 properties
//! - Publishing RPC request messages
//! - Using asynchronous tokens
//! - Subscribing to reply topic
//! - Subscribing to reply topic
//

/*******************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion examples/rpc_math_srvr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//

/*******************************************************************************
Expand Down
59 changes: 25 additions & 34 deletions src/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,20 +1155,16 @@ mod tests {
let data = cli.user_data();
assert!(data.is_some());

if let Some(lock) = data.unwrap().downcast_ref::<Mutex<Vec<&str>>>() {
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::<Mutex<Vec<&str>>>().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]
Expand All @@ -1185,28 +1181,23 @@ mod tests {
assert!(data.is_some());
let data = data.unwrap();

if let Some(lock) = data.downcast_ref::<RwLock<Vec<&str>>>() {
// 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::<RwLock<Vec<&str>>>().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.
Expand Down
25 changes: 12 additions & 13 deletions src/client_persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use std::{
ffi::{CString, CStr},
os::raw::{c_void, c_char, c_int},
};
use libc;

use crate::{
ffi,
Expand Down Expand Up @@ -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<dyn ClientPersistence> = mem::transmute(context);
let persist = &mut *(context as *mut std::boxed::Box<dyn ClientPersistence>);

if let Ok(_) = persist.open(client_id, server_uri) {
if persist.open(client_id, server_uri).is_ok() {
*handle = context;
return PERSISTENCE_SUCCESS;
}
Expand All @@ -148,7 +147,7 @@ impl UserPersistence
return PERSISTENCE_ERROR;
}

let persist: &mut Box<dyn ClientPersistence> = mem::transmute(handle);
let persist = &mut *(handle as *mut std::boxed::Box<dyn ClientPersistence>);

match persist.close() {
Ok(_) => PERSISTENCE_SUCCESS,
Expand All @@ -170,7 +169,7 @@ impl UserPersistence
if bufcount == 0 {
return PERSISTENCE_SUCCESS;
}
let persist: &mut Box<dyn ClientPersistence> = mem::transmute(handle);
let persist = &mut *(handle as *mut std::boxed::Box<dyn ClientPersistence>);
let key = CStr::from_ptr(key).to_str().unwrap();

let mut bufs: Vec<&[u8]> = Vec::new();
Expand All @@ -197,7 +196,7 @@ impl UserPersistence
buffer.is_null() || buflen.is_null() {
return PERSISTENCE_ERROR;
}
let persist: &mut Box<dyn ClientPersistence> = mem::transmute(handle);
let persist = &mut *(handle as *mut std::boxed::Box<dyn ClientPersistence>);
let key = CStr::from_ptr(key).to_str().unwrap();

match persist.get(key) {
Expand All @@ -221,7 +220,7 @@ impl UserPersistence
if handle.is_null() || key.is_null() {
return PERSISTENCE_ERROR;
}
let persist: &mut Box<dyn ClientPersistence> = mem::transmute(handle);
let persist = &mut *(handle as *mut std::boxed::Box<dyn ClientPersistence>);
let key = CStr::from_ptr(key).to_str().unwrap();

match persist.remove(key) {
Expand All @@ -240,7 +239,7 @@ impl UserPersistence
return PERSISTENCE_ERROR;
}

let persist: &mut Box<dyn ClientPersistence> = mem::transmute(handle);
let persist = &mut *(handle as *mut std::boxed::Box<dyn ClientPersistence>);

*keys = ptr::null_mut();
*nkeys = 0;
Expand All @@ -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::<usize>()) 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;
Expand All @@ -276,7 +275,7 @@ impl UserPersistence
if handle.is_null() {
return PERSISTENCE_ERROR;
}
let persist: &mut Box<dyn ClientPersistence> = mem::transmute(handle);
let persist = &mut *(handle as *mut std::boxed::Box<dyn ClientPersistence>);

match persist.clear() {
Ok(_) => PERSISTENCE_SUCCESS,
Expand All @@ -292,7 +291,7 @@ impl UserPersistence
if handle.is_null() || key.is_null() {
return PERSISTENCE_ERROR;
}
let persist: &mut Box<dyn ClientPersistence> = mem::transmute(handle);
let persist = &mut *(handle as *mut std::boxed::Box<dyn ClientPersistence>);
let key = CStr::from_ptr(key).to_str().unwrap();

if persist.contains_key(key) { 1 } else { 0 }
Expand Down
8 changes: 4 additions & 4 deletions src/connect_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
)
}
Expand Down Expand Up @@ -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()
)
}
Expand Down Expand Up @@ -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());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/create_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ mod tests {

match opts.persistence {
PersistenceType::None => (),
_ => assert!(false),
_ => unreachable!(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/disconnect_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down Expand Up @@ -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()
)
}
Expand Down Expand Up @@ -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()
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, }
}

Expand All @@ -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.
Expand Down Expand Up @@ -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()
)
}
Expand Down
12 changes: 6 additions & 6 deletions src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -471,9 +471,9 @@ impl Property {

/// Creates a property from a C lib MQTTProperty struct.
fn from_c_property(cprop: &ffi::MQTTProperty) -> Result<Property> {
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())
};
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/reason_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use std::{
fmt,
ffi::CStr,
};
use ffi;

/// MQTT v5 single-byte reason codes.
#[repr(u8)]
Expand Down
4 changes: 2 additions & 2 deletions src/response_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
Expand Down Expand Up @@ -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()
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/server_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 5b74f77

Please sign in to comment.