Skip to content

Commit 2bc72da

Browse files
committed
Move fileno logic to Connection impl
1 parent e7b059e commit 2bc72da

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

vm/src/stdlib/socket.rs

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,33 @@ impl Connection {
8686
_ => Err(io::Error::new(io::ErrorKind::Other, "oh no!")),
8787
}
8888
}
89+
90+
#[cfg(unix)]
91+
fn fileno(&self) -> i64 {
92+
use std::os::unix::io::AsRawFd;
93+
let raw_fd = match self {
94+
Connection::TcpListener(con) => con.as_raw_fd(),
95+
Connection::UdpSocket(con) => con.as_raw_fd(),
96+
Connection::TcpStream(con) => con.as_raw_fd(),
97+
};
98+
raw_fd as i64
99+
}
100+
101+
#[cfg(windows)]
102+
fn fileno(&self) -> i64 {
103+
use std::os::windows::io::AsRawSocket;
104+
let raw_fd = match self {
105+
Connection::TcpListener(con) => con.as_raw_socket(),
106+
Connection::UdpSocket(con) => con.as_raw_socket(),
107+
Connection::TcpStream(con) => con.as_raw_socket(),
108+
};
109+
raw_fd as i64
110+
}
111+
112+
#[cfg(all(not(unix), not(windows)))]
113+
fn fileno(&self) -> i64 {
114+
unimplemented!();
115+
}
89116
}
90117

91118
impl Read for Connection {
@@ -111,32 +138,6 @@ impl Write for Connection {
111138
}
112139
}
113140

114-
#[cfg(unix)]
115-
use std::os::unix::io::{AsRawFd, RawFd};
116-
#[cfg(unix)]
117-
impl AsRawFd for Connection {
118-
fn as_raw_fd(&self) -> RawFd {
119-
match self {
120-
Connection::TcpListener(con) => con.as_raw_fd(),
121-
Connection::UdpSocket(con) => con.as_raw_fd(),
122-
Connection::TcpStream(con) => con.as_raw_fd(),
123-
}
124-
}
125-
}
126-
127-
#[cfg(windows)]
128-
use std::os::windows::io::{AsRawSocket, RawSocket};
129-
#[cfg(windows)]
130-
impl AsRawSocket for Connection {
131-
fn as_raw_socket(&self) -> RawSocket {
132-
match self {
133-
Connection::TcpListener(con) => con.as_raw_socket(),
134-
Connection::UdpSocket(con) => con.as_raw_socket(),
135-
Connection::TcpStream(con) => con.as_raw_socket(),
136-
}
137-
}
138-
}
139-
140141
#[derive(Debug)]
141142
pub struct Socket {
142143
address_family: AddressFamily,
@@ -413,37 +414,16 @@ fn socket_close(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
413414
Ok(vm.get_none())
414415
}
415416

416-
#[cfg(unix)]
417-
fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
418-
use std::os::unix::io::AsRawFd;
419-
arg_check!(vm, args, required = [(zelf, None)]);
420-
421-
let socket = get_socket(zelf);
422-
423-
let fileno = match socket.con.borrow_mut().as_mut() {
424-
Some(v) => v.as_raw_fd(),
425-
None => return Err(vm.new_type_error("".to_string())),
426-
};
427-
Ok(vm.ctx.new_int(i64::from(fileno)))
428-
}
429-
430-
#[cfg(windows)]
431417
fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
432-
use std::os::windows::io::AsRawSocket;
433418
arg_check!(vm, args, required = [(zelf, None)]);
434419

435420
let socket = get_socket(zelf);
436421

437422
let fileno = match socket.con.borrow_mut().as_mut() {
438-
Some(v) => v.as_raw_socket(),
423+
Some(v) => v.fileno(),
439424
None => return Err(vm.new_type_error("".to_string())),
440425
};
441-
Ok(vm.ctx.new_int(i64::from(fileno)))
442-
}
443-
444-
#[cfg(all(not(unix), not(windows)))]
445-
fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
446-
unimplemented!();
426+
Ok(vm.ctx.new_int(fileno))
447427
}
448428

449429
fn socket_getsockname(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)