Skip to content

Commit 2e5ff38

Browse files
committed
Fix crash due to rust-lang/rust#124210 on rust 180
1 parent cf6ff29 commit 2e5ff38

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

src/corebluetooth/l2cap_channel.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
task::{Context, Poll},
66
};
77

8-
use objc_foundation::INSData;
8+
use objc_foundation::{INSData, NSData};
99
use objc_id::{Id, Shared};
1010
use tokio::{
1111
io::{AsyncRead, AsyncWrite, ReadBuf},
@@ -33,8 +33,7 @@ pub struct Channel {
3333

3434
enum ChannelCreationError {
3535
FileDescriptorPropertyNotValid,
36-
InputFileDescriptorBytesWrongSize,
37-
OutputFileDescriptorBytesWrongSize,
36+
FileDescriptorBytesWrongSize,
3837
FileDescriptorsNotIdentical,
3938
SetNonBlockingModeFailed(std::io::Error),
4039
TokioStreamCreation(std::io::Error),
@@ -45,27 +44,21 @@ impl Channel {
4544
let input_stream = channel.input_stream();
4645
let output_stream = channel.output_stream();
4746

48-
let in_stream_prop = input_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle });
49-
let out_stream_prop = output_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle });
47+
let in_stream_prop = input_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }).map(NSData::bytes);
48+
let out_stream_prop = output_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }).map(NSData::bytes);
5049

5150
let (Some(in_data), Some(out_data)) = (in_stream_prop, out_stream_prop) else {
5251
return Err(ChannelCreationError::FileDescriptorPropertyNotValid.into());
5352
};
54-
let in_bytes = in_data
55-
.bytes()
56-
.try_into()
57-
.map_err(|_| ChannelCreationError::InputFileDescriptorBytesWrongSize)?;
58-
let in_fd = RawFd::from_ne_bytes(in_bytes);
59-
60-
let out_bytes = out_data
61-
.bytes()
62-
.try_into()
63-
.map_err(|_| ChannelCreationError::OutputFileDescriptorBytesWrongSize)?;
64-
let out_fd = RawFd::from_ne_bytes(out_bytes);
65-
66-
if in_fd != out_fd {
53+
54+
if in_data != out_data {
6755
return Err(ChannelCreationError::FileDescriptorsNotIdentical.into());
6856
};
57+
58+
let in_fd = RawFd::from_ne_bytes(
59+
in_data.try_into()
60+
.map_err(|_| ChannelCreationError::FileDescriptorBytesWrongSize)?
61+
);
6962

7063
let stream = unsafe { std::os::unix::net::UnixStream::from_raw_fd(in_fd) };
7164
stream
@@ -106,12 +99,9 @@ impl From<ChannelCreationError> for Error {
10699
fn from(value: ChannelCreationError) -> Self {
107100
let message = match &value {
108101
ChannelCreationError::FileDescriptorPropertyNotValid => "File descriptor property not valid.",
109-
ChannelCreationError::InputFileDescriptorBytesWrongSize => {
102+
ChannelCreationError::FileDescriptorBytesWrongSize => {
110103
"Input file descriptor bytes are an invalid size."
111104
}
112-
ChannelCreationError::OutputFileDescriptorBytesWrongSize => {
113-
"Output file descriptor bytes are an invalid size."
114-
}
115105
ChannelCreationError::FileDescriptorsNotIdentical => "Input and output file descriptors are not identical.",
116106
ChannelCreationError::SetNonBlockingModeFailed(_) => "Could not get convert socket to async.",
117107
ChannelCreationError::TokioStreamCreation(_) => "Failed to create tokio unix socket.",
@@ -121,8 +111,7 @@ impl From<ChannelCreationError> for Error {
121111
ErrorKind::Internal,
122112
match value {
123113
ChannelCreationError::FileDescriptorPropertyNotValid
124-
| ChannelCreationError::InputFileDescriptorBytesWrongSize
125-
| ChannelCreationError::OutputFileDescriptorBytesWrongSize
114+
| ChannelCreationError::FileDescriptorBytesWrongSize
126115
| ChannelCreationError::FileDescriptorsNotIdentical => None,
127116
ChannelCreationError::SetNonBlockingModeFailed(src)
128117
| ChannelCreationError::TokioStreamCreation(src) => Some(Box::new(src)),

0 commit comments

Comments
 (0)