@@ -5,7 +5,7 @@ use std::{
5
5
task:: { Context , Poll } ,
6
6
} ;
7
7
8
- use objc_foundation:: INSData ;
8
+ use objc_foundation:: { INSData , NSData } ;
9
9
use objc_id:: { Id , Shared } ;
10
10
use tokio:: {
11
11
io:: { AsyncRead , AsyncWrite , ReadBuf } ,
@@ -33,8 +33,7 @@ pub struct Channel {
33
33
34
34
enum ChannelCreationError {
35
35
FileDescriptorPropertyNotValid ,
36
- InputFileDescriptorBytesWrongSize ,
37
- OutputFileDescriptorBytesWrongSize ,
36
+ FileDescriptorBytesWrongSize ,
38
37
FileDescriptorsNotIdentical ,
39
38
SetNonBlockingModeFailed ( std:: io:: Error ) ,
40
39
TokioStreamCreation ( std:: io:: Error ) ,
@@ -45,27 +44,21 @@ impl Channel {
45
44
let input_stream = channel. input_stream ( ) ;
46
45
let output_stream = channel. output_stream ( ) ;
47
46
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 ) ;
50
49
51
50
let ( Some ( in_data) , Some ( out_data) ) = ( in_stream_prop, out_stream_prop) else {
52
51
return Err ( ChannelCreationError :: FileDescriptorPropertyNotValid . into ( ) ) ;
53
52
} ;
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 {
67
55
return Err ( ChannelCreationError :: FileDescriptorsNotIdentical . into ( ) ) ;
68
56
} ;
57
+
58
+ let in_fd = RawFd :: from_ne_bytes (
59
+ in_data. try_into ( )
60
+ . map_err ( |_| ChannelCreationError :: FileDescriptorBytesWrongSize ) ?
61
+ ) ;
69
62
70
63
let stream = unsafe { std:: os:: unix:: net:: UnixStream :: from_raw_fd ( in_fd) } ;
71
64
stream
@@ -106,12 +99,9 @@ impl From<ChannelCreationError> for Error {
106
99
fn from ( value : ChannelCreationError ) -> Self {
107
100
let message = match & value {
108
101
ChannelCreationError :: FileDescriptorPropertyNotValid => "File descriptor property not valid." ,
109
- ChannelCreationError :: InputFileDescriptorBytesWrongSize => {
102
+ ChannelCreationError :: FileDescriptorBytesWrongSize => {
110
103
"Input file descriptor bytes are an invalid size."
111
104
}
112
- ChannelCreationError :: OutputFileDescriptorBytesWrongSize => {
113
- "Output file descriptor bytes are an invalid size."
114
- }
115
105
ChannelCreationError :: FileDescriptorsNotIdentical => "Input and output file descriptors are not identical." ,
116
106
ChannelCreationError :: SetNonBlockingModeFailed ( _) => "Could not get convert socket to async." ,
117
107
ChannelCreationError :: TokioStreamCreation ( _) => "Failed to create tokio unix socket." ,
@@ -121,8 +111,7 @@ impl From<ChannelCreationError> for Error {
121
111
ErrorKind :: Internal ,
122
112
match value {
123
113
ChannelCreationError :: FileDescriptorPropertyNotValid
124
- | ChannelCreationError :: InputFileDescriptorBytesWrongSize
125
- | ChannelCreationError :: OutputFileDescriptorBytesWrongSize
114
+ | ChannelCreationError :: FileDescriptorBytesWrongSize
126
115
| ChannelCreationError :: FileDescriptorsNotIdentical => None ,
127
116
ChannelCreationError :: SetNonBlockingModeFailed ( src)
128
117
| ChannelCreationError :: TokioStreamCreation ( src) => Some ( Box :: new ( src) ) ,
0 commit comments