Skip to content

Commit 155fc12

Browse files
committed
async read write impl
1 parent 9ecb819 commit 155fc12

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

library/std/src/os/windows/net/listener.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use core::mem;
33
use super::sockaddr_un;
44
use crate::io;
55
use crate::os::raw::c_int;
6+
use crate::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
67
use crate::os::windows::net::{SocketAddr, UnixStream, from_sockaddr_un};
78
use crate::path::Path;
89
use crate::sys::c::{self, bind, getsockname, listen};
@@ -57,3 +58,47 @@ impl UnixListener {
5758
pub struct Incoming<'a> {
5859
listener: &'a UnixListener,
5960
}
61+
#[stable(feature = "rust1", since = "1.0.0")]
62+
impl<'a> Iterator for Incoming<'a> {
63+
type Item = io::Result<UnixStream>;
64+
65+
fn next(&mut self) -> Option<io::Result<UnixStream>> {
66+
Some(self.listener.accept().map(|s| s.0))
67+
}
68+
69+
fn size_hint(&self) -> (usize, Option<usize>) {
70+
(usize::max_value(), None)
71+
}
72+
}
73+
#[stable(feature = "rust1", since = "1.0.0")]
74+
impl AsRawSocket for UnixListener {
75+
fn as_raw_socket(&self) -> RawSocket {
76+
self.0.as_raw_socket()
77+
}
78+
}
79+
80+
#[stable(feature = "rust1", since = "1.0.0")]
81+
impl FromRawSocket for UnixListener {
82+
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
83+
UnixListener(unsafe { Socket::from_raw_socket(sock) })
84+
}
85+
}
86+
87+
#[stable(feature = "rust1", since = "1.0.0")]
88+
impl IntoRawSocket for UnixListener {
89+
fn into_raw_socket(self) -> RawSocket {
90+
let ret = self.0.as_raw_socket();
91+
mem::forget(self);
92+
ret
93+
}
94+
}
95+
96+
#[stable(feature = "rust1", since = "1.0.0")]
97+
impl<'a> IntoIterator for &'a UnixListener {
98+
type Item = io::Result<UnixStream>;
99+
type IntoIter = Incoming<'a>;
100+
101+
fn into_iter(self) -> Incoming<'a> {
102+
self.incoming()
103+
}
104+
}

library/std/src/os/windows/net/stream.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#![stable(feature = "rust1", since = "1.0.0")]
22

3+
use core::mem;
34
use core::time::Duration;
45

5-
use crate::io;
6+
use crate::io::{self, IoSlice};
67
use crate::net::Shutdown;
8+
use crate::os::windows::io::{
9+
AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, IntoRawSocket, RawSocket,
10+
};
711
use crate::os::windows::net::{SocketAddr, sockaddr_un};
812
use crate::path::Path;
913
use crate::sys::c::{SO_RCVTIMEO, SO_SNDTIMEO, connect, getpeername, getsockname};
@@ -68,3 +72,68 @@ impl UnixStream {
6872
self.0.timeout(SO_SNDTIMEO)
6973
}
7074
}
75+
76+
#[stable(feature = "rust1", since = "1.0.0")]
77+
impl io::Read for UnixStream {
78+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
79+
io::Read::read(&mut &*self, buf)
80+
}
81+
}
82+
83+
#[stable(feature = "rust1", since = "1.0.0")]
84+
impl<'a> io::Read for &'a UnixStream {
85+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
86+
self.0.read(buf)
87+
}
88+
}
89+
90+
#[stable(feature = "rust1", since = "1.0.0")]
91+
impl io::Write for UnixStream {
92+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
93+
io::Write::write(&mut &*self, buf)
94+
}
95+
96+
fn flush(&mut self) -> io::Result<()> {
97+
io::Write::flush(&mut &*self)
98+
}
99+
}
100+
#[stable(feature = "rust1", since = "1.0.0")]
101+
impl<'a> io::Write for &'a UnixStream {
102+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
103+
self.0.write_vectored(&[IoSlice::new(buf)])
104+
}
105+
106+
fn flush(&mut self) -> io::Result<()> {
107+
Ok(())
108+
}
109+
}
110+
111+
#[stable(feature = "rust1", since = "1.0.0")]
112+
impl AsSocket for UnixStream {
113+
fn as_socket(&self) -> BorrowedSocket {
114+
self.0.as_socket()
115+
}
116+
}
117+
118+
#[stable(feature = "rust1", since = "1.0.0")]
119+
impl AsRawSocket for UnixStream {
120+
fn as_raw_socket(&self) -> RawSocket {
121+
self.0.as_raw_socket()
122+
}
123+
}
124+
125+
#[stable(feature = "rust1", since = "1.0.0")]
126+
impl FromRawSocket for UnixStream {
127+
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
128+
unsafe { UnixStream(Socket::from_raw_socket(sock)) }
129+
}
130+
}
131+
132+
#[stable(feature = "rust1", since = "1.0.0")]
133+
impl IntoRawSocket for UnixStream {
134+
fn into_raw_socket(self) -> RawSocket {
135+
let ret = self.0.as_raw_socket();
136+
mem::forget(self);
137+
ret
138+
}
139+
}

0 commit comments

Comments
 (0)