remove unsafe

This commit is contained in:
Σrebe - Romain GERARD 2024-07-31 21:42:05 +02:00
parent 89e71cc806
commit 05abcd441f
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4
2 changed files with 10 additions and 30 deletions

View file

@ -229,8 +229,8 @@ impl AsyncRead for Socks5ReadHalf {
buf: &mut ReadBuf<'_>, buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> { ) -> Poll<std::io::Result<()>> {
match self.get_mut() { match self.get_mut() {
Self::Tcp(s) => unsafe { Pin::new_unchecked(s) }.poll_read(cx, buf), Self::Tcp(s) => Pin::new(s).poll_read(cx, buf),
Self::Udp(s) => unsafe { Pin::new_unchecked(s) }.poll_read(cx, buf), Self::Udp(s) => Pin::new(s).poll_read(cx, buf),
} }
} }
} }
@ -238,22 +238,22 @@ impl AsyncRead for Socks5ReadHalf {
impl AsyncWrite for Socks5WriteHalf { impl AsyncWrite for Socks5WriteHalf {
fn poll_write(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>, buf: &[u8]) -> Poll<Result<usize, Error>> { fn poll_write(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>, buf: &[u8]) -> Poll<Result<usize, Error>> {
match self.get_mut() { match self.get_mut() {
Self::Tcp(s) => unsafe { Pin::new_unchecked(s) }.poll_write(cx, buf), Self::Tcp(s) => Pin::new(s).poll_write(cx, buf),
Self::Udp(s) => unsafe { Pin::new_unchecked(s) }.poll_write(cx, buf), Self::Udp(s) => Pin::new(s).poll_write(cx, buf),
} }
} }
fn poll_flush(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Error>> { fn poll_flush(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Error>> {
match self.get_mut() { match self.get_mut() {
Self::Tcp(s) => unsafe { Pin::new_unchecked(s) }.poll_flush(cx), Self::Tcp(s) => Pin::new(s).poll_flush(cx),
Self::Udp(s) => unsafe { Pin::new_unchecked(s) }.poll_flush(cx), Self::Udp(s) => Pin::new(s).poll_flush(cx),
} }
} }
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Error>> { fn poll_shutdown(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Error>> {
match self.get_mut() { match self.get_mut() {
Self::Tcp(s) => unsafe { Pin::new_unchecked(s) }.poll_shutdown(cx), Self::Tcp(s) => Pin::new(s).poll_shutdown(cx),
Self::Udp(s) => unsafe { Pin::new_unchecked(s) }.poll_shutdown(cx), Self::Udp(s) => Pin::new(s).poll_shutdown(cx),
} }
} }
@ -263,8 +263,8 @@ impl AsyncWrite for Socks5WriteHalf {
bufs: &[IoSlice<'_>], bufs: &[IoSlice<'_>],
) -> Poll<Result<usize, Error>> { ) -> Poll<Result<usize, Error>> {
match self.get_mut() { match self.get_mut() {
Self::Tcp(s) => unsafe { Pin::new_unchecked(s) }.poll_write_vectored(cx, bufs), Self::Tcp(s) => Pin::new(s).poll_write_vectored(cx, bufs),
Self::Udp(s) => unsafe { Pin::new_unchecked(s) }.poll_write_vectored(cx, bufs), Self::Udp(s) => Pin::new(s).poll_write_vectored(cx, bufs),
} }
} }
@ -275,22 +275,3 @@ impl AsyncWrite for Socks5WriteHalf {
} }
} }
} }
//#[cfg(test)]
//mod test {
// use super::*;
// use futures_util::StreamExt;
// use std::str::FromStr;
//
// #[tokio::test]
// async fn socks5_server() {
// let mut x = run_server(SocketAddr::from_str("[::]:4343").unwrap())
// .await
// .unwrap();
//
// loop {
// let cnx = x.next().await.unwrap().unwrap();
// eprintln!("{:?}", cnx);
// }
// }
//}

View file

@ -32,7 +32,6 @@ impl Stream for Socks5TunnelListener {
fn poll_next(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut(); let this = self.get_mut();
let ret = ready!(Pin::new(&mut this.listener).poll_next(cx)); let ret = ready!(Pin::new(&mut this.listener).poll_next(cx));
// TODO: Check if tokio::io::split can be avoided
let ret = match ret { let ret = match ret {
Some(Ok((stream, (host, port)))) => { Some(Ok((stream, (host, port)))) => {
let protocol = stream.local_protocol(); let protocol = stream.local_protocol();