From 2e5ff38b383de948254ab48b4f929bfa4cb701a9 Mon Sep 17 00:00:00 2001 From: Abraham Zukor Date: Tue, 6 Aug 2024 10:56:14 -0700 Subject: [PATCH] Fix crash due to https://github.com/rust-lang/rust/pull/124210 on rust 180 --- src/corebluetooth/l2cap_channel.rs | 37 +++++++++++------------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/corebluetooth/l2cap_channel.rs b/src/corebluetooth/l2cap_channel.rs index 3ed821d..2412b8e 100644 --- a/src/corebluetooth/l2cap_channel.rs +++ b/src/corebluetooth/l2cap_channel.rs @@ -5,7 +5,7 @@ use std::{ task::{Context, Poll}, }; -use objc_foundation::INSData; +use objc_foundation::{INSData, NSData}; use objc_id::{Id, Shared}; use tokio::{ io::{AsyncRead, AsyncWrite, ReadBuf}, @@ -33,8 +33,7 @@ pub struct Channel { enum ChannelCreationError { FileDescriptorPropertyNotValid, - InputFileDescriptorBytesWrongSize, - OutputFileDescriptorBytesWrongSize, + FileDescriptorBytesWrongSize, FileDescriptorsNotIdentical, SetNonBlockingModeFailed(std::io::Error), TokioStreamCreation(std::io::Error), @@ -45,27 +44,21 @@ impl Channel { let input_stream = channel.input_stream(); let output_stream = channel.output_stream(); - let in_stream_prop = input_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }); - let out_stream_prop = output_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }); + let in_stream_prop = input_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }).map(NSData::bytes); + let out_stream_prop = output_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }).map(NSData::bytes); let (Some(in_data), Some(out_data)) = (in_stream_prop, out_stream_prop) else { return Err(ChannelCreationError::FileDescriptorPropertyNotValid.into()); }; - let in_bytes = in_data - .bytes() - .try_into() - .map_err(|_| ChannelCreationError::InputFileDescriptorBytesWrongSize)?; - let in_fd = RawFd::from_ne_bytes(in_bytes); - - let out_bytes = out_data - .bytes() - .try_into() - .map_err(|_| ChannelCreationError::OutputFileDescriptorBytesWrongSize)?; - let out_fd = RawFd::from_ne_bytes(out_bytes); - - if in_fd != out_fd { + + if in_data != out_data { return Err(ChannelCreationError::FileDescriptorsNotIdentical.into()); }; + + let in_fd = RawFd::from_ne_bytes( + in_data.try_into() + .map_err(|_| ChannelCreationError::FileDescriptorBytesWrongSize)? + ); let stream = unsafe { std::os::unix::net::UnixStream::from_raw_fd(in_fd) }; stream @@ -106,12 +99,9 @@ impl From for Error { fn from(value: ChannelCreationError) -> Self { let message = match &value { ChannelCreationError::FileDescriptorPropertyNotValid => "File descriptor property not valid.", - ChannelCreationError::InputFileDescriptorBytesWrongSize => { + ChannelCreationError::FileDescriptorBytesWrongSize => { "Input file descriptor bytes are an invalid size." } - ChannelCreationError::OutputFileDescriptorBytesWrongSize => { - "Output file descriptor bytes are an invalid size." - } ChannelCreationError::FileDescriptorsNotIdentical => "Input and output file descriptors are not identical.", ChannelCreationError::SetNonBlockingModeFailed(_) => "Could not get convert socket to async.", ChannelCreationError::TokioStreamCreation(_) => "Failed to create tokio unix socket.", @@ -121,8 +111,7 @@ impl From for Error { ErrorKind::Internal, match value { ChannelCreationError::FileDescriptorPropertyNotValid - | ChannelCreationError::InputFileDescriptorBytesWrongSize - | ChannelCreationError::OutputFileDescriptorBytesWrongSize + | ChannelCreationError::FileDescriptorBytesWrongSize | ChannelCreationError::FileDescriptorsNotIdentical => None, ChannelCreationError::SetNonBlockingModeFailed(src) | ChannelCreationError::TokioStreamCreation(src) => Some(Box::new(src)),