Skip to content

Commit 5fee4e5

Browse files
committed
sqlx-core: Add poll methods to Socket, Add SocketExt
1 parent a0e4054 commit 5fee4e5

File tree

2 files changed

+33
-92
lines changed

2 files changed

+33
-92
lines changed

sqlx-core/src/net/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ mod socket;
22
pub mod tls;
33

44
pub use socket::{
5-
connect_tcp, connect_uds, BufferedSocket, Socket, SocketIntoBox, WithSocket, WriteBuffer,
5+
connect_tcp, connect_uds, BufferedSocket, Socket, SocketExt, SocketIntoBox, WithSocket,
6+
WriteBuffer,
67
};

sqlx-core/src/net/socket/mod.rs

Lines changed: 31 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::future::Future;
1+
use std::future::{poll_fn, Future};
22
use std::io;
33
use std::path::Path;
4-
use std::pin::Pin;
54
use std::task::{ready, Context, Poll};
65

76
use bytes::BufMut;
@@ -27,125 +26,66 @@ pub trait Socket: Send + Sync + Unpin + 'static {
2726
}
2827

2928
fn poll_shutdown(&mut self, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
30-
31-
fn read<'a, B: ReadBuf>(&'a mut self, buf: &'a mut B) -> Read<'a, Self, B>
32-
where
33-
Self: Sized,
34-
{
35-
Read { socket: self, buf }
36-
}
37-
38-
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>
39-
where
40-
Self: Sized,
41-
{
42-
Write { socket: self, buf }
43-
}
44-
45-
fn flush(&mut self) -> Flush<'_, Self>
46-
where
47-
Self: Sized,
48-
{
49-
Flush { socket: self }
50-
}
51-
52-
fn shutdown(&mut self) -> Shutdown<'_, Self>
53-
where
54-
Self: Sized,
55-
{
56-
Shutdown { socket: self }
57-
}
5829
}
5930

60-
pub struct Read<'a, S: ?Sized, B> {
61-
socket: &'a mut S,
62-
buf: &'a mut B,
63-
}
64-
65-
impl<S: ?Sized, B> Future for Read<'_, S, B>
66-
where
67-
S: Socket,
68-
B: ReadBuf,
69-
{
70-
type Output = io::Result<usize>;
71-
72-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
73-
let this = &mut *self;
74-
75-
while this.buf.has_remaining_mut() {
76-
match this.socket.try_read(&mut *this.buf) {
31+
pub trait SocketExt: Socket {
32+
fn poll_read(
33+
&mut self,
34+
cx: &mut Context<'_>,
35+
buf: &mut dyn ReadBuf,
36+
) -> Poll<Result<usize, io::Error>> {
37+
while buf.has_remaining_mut() {
38+
match self.try_read(buf) {
7739
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
78-
ready!(this.socket.poll_read_ready(cx))?;
40+
ready!(self.poll_read_ready(cx))?;
7941
}
8042
ready => return Poll::Ready(ready),
8143
}
8244
}
8345

8446
Poll::Ready(Ok(0))
8547
}
86-
}
87-
88-
pub struct Write<'a, S: ?Sized> {
89-
socket: &'a mut S,
90-
buf: &'a [u8],
91-
}
9248

93-
impl<S: ?Sized> Future for Write<'_, S>
94-
where
95-
S: Socket,
96-
{
97-
type Output = io::Result<usize>;
98-
99-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
100-
let this = &mut *self;
101-
102-
while !this.buf.is_empty() {
103-
match this.socket.try_write(this.buf) {
49+
fn poll_write(&mut self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, io::Error>> {
50+
while !buf.is_empty() {
51+
match self.try_write(buf) {
10452
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
105-
ready!(this.socket.poll_write_ready(cx))?;
53+
ready!(self.poll_write_ready(cx))?;
10654
}
10755
ready => return Poll::Ready(ready),
10856
}
10957
}
11058

11159
Poll::Ready(Ok(0))
11260
}
113-
}
11461

115-
pub struct Flush<'a, S: ?Sized> {
116-
socket: &'a mut S,
117-
}
118-
119-
impl<S: Socket + ?Sized> Future for Flush<'_, S> {
120-
type Output = io::Result<()>;
121-
122-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
123-
self.socket.poll_flush(cx)
62+
#[inline(always)]
63+
fn shutdown(&mut self) -> impl Future<Output = io::Result<()>> {
64+
poll_fn(|cx| self.poll_shutdown(cx))
12465
}
125-
}
12666

127-
pub struct Shutdown<'a, S: ?Sized> {
128-
socket: &'a mut S,
129-
}
67+
#[inline(always)]
68+
fn flush(&mut self) -> impl Future<Output = io::Result<()>> {
69+
poll_fn(|cx| self.poll_flush(cx))
70+
}
13071

131-
impl<S: ?Sized> Future for Shutdown<'_, S>
132-
where
133-
S: Socket,
134-
{
135-
type Output = io::Result<()>;
72+
#[inline(always)]
73+
fn write(&mut self, buf: &[u8]) -> impl Future<Output = io::Result<usize>> {
74+
poll_fn(|cx| self.poll_write(cx, buf))
75+
}
13676

137-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
138-
self.socket.poll_shutdown(cx)
77+
#[inline(always)]
78+
fn read(&mut self, buf: &mut impl ReadBuf) -> impl Future<Output = io::Result<usize>> {
79+
poll_fn(|cx| self.poll_read(cx, buf))
13980
}
14081
}
14182

83+
impl<S: Socket> SocketExt for S {}
84+
14285
pub trait WithSocket {
14386
type Output;
14487

145-
fn with_socket<S: Socket>(
146-
self,
147-
socket: S,
148-
) -> impl std::future::Future<Output = Self::Output> + Send;
88+
fn with_socket<S: Socket>(self, socket: S) -> impl Future<Output = Self::Output> + Send;
14989
}
15090

15191
pub struct SocketIntoBox;

0 commit comments

Comments
 (0)