diff --git a/src/ipc.rs b/src/ipc.rs index 353b09b9..c91a381a 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -47,8 +47,8 @@ pub enum IpcError { impl fmt::Display for IpcError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match *self { - IpcError::Bincode(ref err) => write!(fmt, "bincode error: {}", err), - IpcError::Io(ref err) => write!(fmt, "io error: {}", err), + IpcError::Bincode(ref err) => write!(fmt, "bincode error: {err}"), + IpcError::Io(ref err) => write!(fmt, "io error: {err}"), IpcError::Disconnected => write!(fmt, "disconnected"), } } @@ -73,7 +73,7 @@ pub enum TryRecvError { impl fmt::Display for TryRecvError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match *self { - TryRecvError::IpcError(ref err) => write!(fmt, "ipc error: {}", err), + TryRecvError::IpcError(ref err) => write!(fmt, "ipc error: {err}"), TryRecvError::Empty => write!(fmt, "empty"), } } @@ -673,7 +673,7 @@ impl IpcSelectionResult { match self { IpcSelectionResult::MessageReceived(id, message) => (id, message), IpcSelectionResult::ChannelClosed(id) => { - panic!("IpcSelectionResult::unwrap(): channel {} closed", id) + panic!("IpcSelectionResult::unwrap(): channel {id} closed") }, } } diff --git a/src/platform/unix/mod.rs b/src/platform/unix/mod.rs index 9e45f090..c7821366 100644 --- a/src/platform/unix/mod.rs +++ b/src/platform/unix/mod.rs @@ -539,11 +539,10 @@ impl OsIpcReceiverSet { assert!(event.is_readable()); let event_token = event.token(); - let poll_entry = self + let poll_entry = *self .pollfds .get(&event_token) - .expect("Got event for unknown token.") - .clone(); + .expect("Got event for unknown token."); loop { match recv(poll_entry.fd, BlockingMode::Nonblocking) { Ok(ipc_message) => { @@ -590,10 +589,7 @@ impl OsIpcSelectionResult { match self { OsIpcSelectionResult::DataReceived(id, ipc_message) => (id, ipc_message), OsIpcSelectionResult::ChannelClosed(id) => { - panic!( - "OsIpcSelectionResult::unwrap(): receiver ID {} was closed!", - id - ) + panic!("OsIpcSelectionResult::unwrap(): receiver ID {id} was closed!") }, } } @@ -863,6 +859,11 @@ impl Deref for OsIpcSharedMemory { } impl OsIpcSharedMemory { + /// # Safety + /// + /// This is safe if there is only one reader/writer on the data. + /// User can achieve this by not cloning [`IpcSharedMemory`] + /// and serializing/deserializing only once. #[inline] pub unsafe fn deref_mut(&mut self) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(self.ptr, self.length) } diff --git a/src/test.rs b/src/test.rs index 0e356dfe..4f4f3e0a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -93,7 +93,7 @@ impl Wait for libc::pid_t { #[cfg(not(any(feature = "force-inprocess", target_os = "android", target_os = "ios")))] pub fn get_channel_name_arg(which: &str) -> Option { for arg in env::args() { - let arg_str = &*format!("channel_name-{}:", which); + let arg_str = &*format!("channel_name-{which}:"); if let Some(arg) = arg.strip_prefix(arg_str) { return Some(arg.to_owned()); } @@ -110,7 +110,7 @@ pub fn spawn_server(test_name: &str, server_args: &[(&str, &str)]) -> process::C .args( server_args .iter() - .map(|(name, val)| format!("channel_name-{}:{}", name, val)), + .map(|(name, val)| format!("channel_name-{name}:{val}")), ) .stdin(Stdio::null()) .stdout(Stdio::null()) @@ -131,7 +131,7 @@ fn simple() { drop(tx); match rx.recv().unwrap_err() { ipc::IpcError::Disconnected => (), - e => panic!("expected disconnected error, got {:?}", e), + e => panic!("expected disconnected error, got {e:?}"), } } @@ -288,7 +288,7 @@ fn router_simple_global() { // Try the same, with a strongly typed route let message: usize = 42; let (tx, rx) = ipc::channel().unwrap(); - tx.send(message.clone()).unwrap(); + tx.send(message).unwrap(); let (callback_fired_sender, callback_fired_receiver) = crossbeam_channel::unbounded::(); ROUTER.add_typed_route( @@ -448,7 +448,7 @@ fn router_drops_callbacks_on_cloned_sender_shutdown() { #[test] fn router_big_data() { let person = ("Patrick Walton".to_owned(), 29); - let people: Vec<_> = iter::repeat(person).take(64 * 1024).collect(); + let people: Vec<_> = std::iter::repeat_n(person, 64 * 1024).collect(); let (tx, rx) = ipc::channel().unwrap(); let people_for_subthread = people.clone(); let thread = thread::spawn(move || { @@ -550,19 +550,19 @@ fn try_recv() { let (tx, rx) = ipc::channel().unwrap(); match rx.try_recv() { Err(ipc::TryRecvError::Empty) => (), - v => panic!("Expected empty channel err: {:?}", v), + v => panic!("Expected empty channel err: {v:?}"), } tx.send(person.clone()).unwrap(); let received_person = rx.try_recv().unwrap(); assert_eq!(person, received_person); match rx.try_recv() { Err(ipc::TryRecvError::Empty) => (), - v => panic!("Expected empty channel err: {:?}", v), + v => panic!("Expected empty channel err: {v:?}"), } drop(tx); match rx.try_recv() { Err(ipc::TryRecvError::IpcError(ipc::IpcError::Disconnected)) => (), - v => panic!("Expected disconnected err: {:?}", v), + v => panic!("Expected disconnected err: {v:?}"), } } @@ -576,7 +576,7 @@ fn try_recv_timeout() { Err(ipc::TryRecvError::Empty) => { assert!(start_recv.elapsed() >= Duration::from_millis(500)) }, - v => panic!("Expected empty channel err: {:?}", v), + v => panic!("Expected empty channel err: {v:?}"), } tx.send(person.clone()).unwrap(); let start_recv = Instant::now(); @@ -588,12 +588,12 @@ fn try_recv_timeout() { Err(ipc::TryRecvError::Empty) => { assert!(start_recv.elapsed() >= Duration::from_millis(500)) }, - v => panic!("Expected empty channel err: {:?}", v), + v => panic!("Expected empty channel err: {v:?}"), } drop(tx); match rx.try_recv_timeout(timeout) { Err(ipc::TryRecvError::IpcError(ipc::IpcError::Disconnected)) => (), - v => panic!("Expected disconnected err: {:?}", v), + v => panic!("Expected disconnected err: {v:?}"), } } @@ -651,7 +651,7 @@ fn test_so_linger() { let val = match receiver.recv() { Ok(val) => val, Err(e) => { - panic!("err: `{:?}`", e); + panic!("err: `{e:?}`"); }, }; assert_eq!(val, 42);