From 8b5bb302e3d8dd5c913c9c9bfdc7824155c1d168 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Fri, 4 Oct 2024 18:13:56 +0200 Subject: [PATCH] Start fixing issues identified by clippy (#361) --- src/ipc.rs | 8 ++++---- src/platform/macos/mod.rs | 30 +++++++++++++----------------- src/platform/test.rs | 18 +++++++++--------- src/test.rs | 10 ++++------ 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/ipc.rs b/src/ipc.rs index 823f107a..aa2d7124 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -26,18 +26,18 @@ use std::time::Duration; thread_local! { static OS_IPC_CHANNELS_FOR_DESERIALIZATION: RefCell> = - RefCell::new(Vec::new()) + const { RefCell::new(Vec::new()) } } thread_local! { static OS_IPC_SHARED_MEMORY_REGIONS_FOR_DESERIALIZATION: - RefCell>> = RefCell::new(Vec::new()) + RefCell>> = const { RefCell::new(Vec::new()) } } thread_local! { - static OS_IPC_CHANNELS_FOR_SERIALIZATION: RefCell> = RefCell::new(Vec::new()) + static OS_IPC_CHANNELS_FOR_SERIALIZATION: RefCell> = const { RefCell::new(Vec::new()) } } thread_local! { static OS_IPC_SHARED_MEMORY_REGIONS_FOR_SERIALIZATION: RefCell> = - RefCell::new(Vec::new()) + const { RefCell::new(Vec::new()) } } #[derive(Debug)] diff --git a/src/platform/macos/mod.rs b/src/platform/macos/mod.rs index ad8eff8e..5acc2959 100644 --- a/src/platform/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -31,7 +31,6 @@ use std::ptr; use std::slice; use std::sync::RwLock; use std::time::Duration; -use std::usize; mod mach_sys; @@ -40,7 +39,7 @@ mod mach_sys; const SMALL_MESSAGE_SIZE: usize = 4096; /// A string to prepend to our bootstrap ports. -static BOOTSTRAP_PREFIX: &'static str = "org.rust-lang.ipc-channel."; +static BOOTSTRAP_PREFIX: &str = "org.rust-lang.ipc-channel."; const BOOTSTRAP_NAME_IN_USE: kern_return_t = 1101; const BOOTSTRAP_SUCCESS: kern_return_t = 0; @@ -205,7 +204,7 @@ impl OsIpcReceiver { mach_task_self(), port, MACH_PORT_LIMITS_INFO, - mem::transmute(&limits), + &limits as *const mach_port_limits_t as *mut i32, 1, ) }; @@ -243,7 +242,7 @@ impl OsIpcReceiver { debug_assert!(port != MACH_PORT_NULL); let (right, acquired_right) = mach_port_extract_right(port, MACH_MSG_TYPE_MAKE_SEND as u32)?; - debug_assert!(acquired_right == MACH_MSG_TYPE_PORT_SEND as u32); + debug_assert!(acquired_right == MACH_MSG_TYPE_PORT_SEND); Ok(OsIpcSender::from_name(right)) } @@ -263,7 +262,7 @@ impl OsIpcReceiver { let (right, acquired_right) = mach_port_extract_right(port, MACH_MSG_TYPE_MAKE_SEND as u32)?; - debug_assert!(acquired_right == MACH_MSG_TYPE_PORT_SEND as u32); + debug_assert!(acquired_right == MACH_MSG_TYPE_PORT_SEND); let mut os_result; let mut name; @@ -396,7 +395,7 @@ impl<'a> SendData<'a> { fn inline_data(&self) -> &[u8] { match *self { - SendData::Inline(ref data) => data, + SendData::Inline(data) => data, SendData::OutOfLine(_) => &[], } } @@ -442,7 +441,7 @@ impl Clone for OsIpcSender { impl OsIpcSender { fn from_name(port: mach_port_t) -> OsIpcSender { OsIpcSender { - port: port, + port, nosync_marker: PhantomData, } } @@ -534,7 +533,7 @@ impl OsIpcSender { let padding_count = Message::payload_padding(padding_start as usize); // Zero out padding padding_start.write_bytes(0, padding_count); - let data_size_dest = padding_start.offset(padding_count as isize) as *mut usize; + let data_size_dest = padding_start.add(padding_count) as *mut usize; *data_size_dest = data_size; let data_dest = data_size_dest.offset(1) as *mut u8; @@ -628,7 +627,7 @@ impl OsIpcReceiverSet { pub fn new() -> Result { let port = mach_port_allocate(MACH_PORT_RIGHT_PORT_SET)?; Ok(OsIpcReceiverSet { - port: port, + port, ports: vec![], }) } @@ -796,7 +795,7 @@ fn select( let payload = if has_inline_data { let padding_start = has_inline_data_ptr.offset(1) as *mut u8; let padding_count = Message::payload_padding(padding_start as usize); - let payload_size_ptr = padding_start.offset(padding_count as isize) as *mut usize; + let payload_size_ptr = padding_start.add(padding_count) as *mut usize; let payload_size = *payload_size_ptr; let max_payload_size = message as usize + ((*message).header.msgh_size as usize) - (shared_memory_descriptor as usize); @@ -831,7 +830,7 @@ pub struct OsIpcOneShotServer { impl Drop for OsIpcOneShotServer { fn drop(&mut self) { - let _ = OsIpcReceiver::unregister_global_name(mem::replace(&mut self.name, String::new())); + let _ = OsIpcReceiver::unregister_global_name(std::mem::take(&mut self.name)); deallocate_mach_port(self.registration_port); } } @@ -842,7 +841,7 @@ impl OsIpcOneShotServer { let (registration_port, name) = receiver.register_bootstrap_name()?; Ok(( OsIpcOneShotServer { - receiver: receiver, + receiver, name: name.clone(), registration_port, }, @@ -943,10 +942,7 @@ impl Deref for OsIpcSharedMemory { impl OsIpcSharedMemory { unsafe fn from_raw_parts(ptr: *mut u8, length: usize) -> OsIpcSharedMemory { - OsIpcSharedMemory { - ptr: ptr, - length: length, - } + OsIpcSharedMemory { ptr, length } } pub fn from_byte(byte: u8, length: usize) -> OsIpcSharedMemory { @@ -978,7 +974,7 @@ unsafe fn allocate_vm_pages(length: usize) -> *mut u8 { } unsafe fn setup_receive_buffer(buffer: &mut [u8], port_name: mach_port_t) { - let message: *mut mach_msg_header_t = mem::transmute(&buffer[0]); + let message = buffer.as_mut_ptr() as *mut mach_msg_header_t; (*message).msgh_local_port = port_name; (*message).msgh_size = buffer.len() as u32 } diff --git a/src/platform/test.rs b/src/platform/test.rs index 0f64cd4a..45d8e77c 100644 --- a/src/platform/test.rs +++ b/src/platform/test.rs @@ -161,7 +161,7 @@ fn medium_data() { received_channels, received_shared_memory_regions ), - (&data[..], vec![], vec![]) + (data, vec![], vec![]) ); } @@ -206,7 +206,7 @@ fn check_big_data(size: u32) { received_channels, received_shared_memory_regions ), - (&data[..], vec![], vec![]) + (data, vec![], vec![]) ); thread.join().unwrap(); } @@ -241,7 +241,7 @@ fn big_data_with_sender_transfer() { let data: Vec = (0..1024 * 1024).map(|i| (i % 251) as u8).collect(); let data: &[u8] = &data[..]; assert_eq!(received_data.len(), data.len()); - assert_eq!(&received_data[..], &data[..]); + assert_eq!(&received_data[..], data); assert_eq!(received_channels.len(), 1); assert_eq!(received_shared_memory_regions.len(), 0); @@ -257,7 +257,7 @@ fn big_data_with_sender_transfer() { received_channels, received_shared_memory_regions ), - (&data[..], vec![], vec![]) + (data, vec![], vec![]) ); thread.join().unwrap(); } @@ -464,7 +464,7 @@ fn concurrent_senders() { received_channels, received_shared_memory_regions ), - (&data[..], vec![], vec![]) + (data, vec![], vec![]) ); } assert!(rx.try_recv().is_err()); // There should be no further messages pending. @@ -635,8 +635,8 @@ fn receiver_set_medium_data() { .map(|offset| (offset % 127) as u8 | 0x80) .collect(); - tx0.send(&*data0, vec![], vec![]).unwrap(); - tx1.send(&*data1, vec![], vec![]).unwrap(); + tx0.send(&data0, vec![], vec![]).unwrap(); + tx1.send(&data1, vec![], vec![]).unwrap(); let (mut received0, mut received1) = (false, false); while !received0 || !received1 { for result in rx_set.select().unwrap().into_iter() { @@ -673,11 +673,11 @@ fn receiver_set_big_data() { let (reference_data0, reference_data1) = (data0.clone(), data1.clone()); let thread0 = thread::spawn(move || { - tx0.send(&*data0, vec![], vec![]).unwrap(); + tx0.send(&data0, vec![], vec![]).unwrap(); tx0 // Don't close just yet -- the receiver-side test code below doesn't expect that... }); let thread1 = thread::spawn(move || { - tx1.send(&*data1, vec![], vec![]).unwrap(); + tx1.send(&data1, vec![], vec![]).unwrap(); tx1 }); diff --git a/src/test.rs b/src/test.rs index 74d3e8c7..046aa808 100644 --- a/src/test.rs +++ b/src/test.rs @@ -12,8 +12,6 @@ use crate::ipc::IpcReceiver; use crate::ipc::{self, IpcReceiverSet, IpcSender, IpcSharedMemory}; use crate::router::{RouterProxy, ROUTER}; use crossbeam_channel::{self, Sender}; -#[cfg(not(any(feature = "force-inprocess", target_os = "android", target_os = "ios")))] -use libc; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::cell::RefCell; #[cfg(not(any(feature = "force-inprocess", target_os = "android", target_os = "ios")))] @@ -33,7 +31,7 @@ use std::process::{self, Command, Stdio}; target_os = "windows", )))] use std::ptr; -use std::sync::Arc; +use std::rc::Rc; use std::thread; #[cfg(not(any( @@ -122,7 +120,7 @@ pub fn spawn_server(test_name: &str, server_args: &[(&str, &str)]) -> process::C .args( server_args .iter() - .map(|&(ref name, ref val)| format!("channel_name-{}:{}", name, val)), + .map(|(name, val)| format!("channel_name-{}:{}", name, val)), ) .stdin(Stdio::null()) .stdout(Stdio::null()) @@ -585,7 +583,7 @@ fn try_recv_timeout() { fn multiple_paths_to_a_sender() { let person = ("Patrick Walton".to_owned(), 29); let (sub_tx, sub_rx) = ipc::channel().unwrap(); - let person_and_sender = Arc::new((person.clone(), sub_tx)); + let person_and_sender = Rc::new((person.clone(), sub_tx)); let send_data = vec![ person_and_sender.clone(), person_and_sender.clone(), @@ -644,7 +642,7 @@ fn test_so_linger() { #[derive(Clone, Debug, Eq, PartialEq)] struct HasWeirdSerializer(Option); -thread_local! { static WEIRD_CHANNEL: RefCell>> = RefCell::new(None) } +thread_local! { static WEIRD_CHANNEL: RefCell>> = const { RefCell::new(None) } } impl Serialize for HasWeirdSerializer { fn serialize(&self, serializer: S) -> Result