/
germanubis
/
sqlx
Обзор
Документация
Войти
/
germanubis
/
sqlx
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
sqlx-core/src/net/tls/util.rs
63 строки
1 KB
Paolo Barbolini
Replace some more futures_util APIs with std variants (#3874)
16 июн 2025, 01:18
Не верифицирован
16 июн 2025, 01:18
df47ffe
Код
Авторство
О чём код?
use crate::net::Socket; use std::future; use std::io::{self, Read, Write}; use std::task::{ready, Context, Poll}; pub struct StdSocket<S> { pub socket: S, wants_read: bool, wants_write: bool, } impl<S: Socket> StdSocket<S> { pub fn new(socket: S) -> Self { Self { socket, wants_read: false, wants_write: false, } } pub fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<io::Result<()>> { if self.wants_write { ready!(self.socket.poll_write_ready(cx))?; self.wants_write = false; } if self.wants_read { ready!(self.socket.poll_read_ready(cx))?; self.wants_read = false; } Poll::Ready(Ok(())) } pub async fn ready(&mut self) -> io::Result<()> { future::poll_fn(|cx| self.poll_ready(cx)).await } } impl<S: Socket> Read for StdSocket<S> { fn read(&mut self, mut buf: &mut [u8]) -> io::Result<usize> { self.wants_read = true; let read = self.socket.try_read(&mut buf)?; self.wants_read = false; Ok(read) } } impl<S: Socket> Write for StdSocket<S> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.wants_write = true; let written = self.socket.try_write(buf)?; self.wants_write = false; Ok(written) } fn flush(&mut self) -> io::Result<()> { // NOTE: TCP sockets and unix sockets are both no-ops for flushes Ok(()) } }