-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yilin Chen <[email protected]>
- Loading branch information
Showing
11 changed files
with
111 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#[cfg(feature = "futures-io")] | ||
mod futures; | ||
|
||
/// A compatibility layer for using non-tokio types with this crate. | ||
/// | ||
/// Example: | ||
/// ```no_run | ||
/// use async_std::os::unix::net::UnixStream; | ||
/// use tokio_socks::{io::Compat, tcp::Socks5Stream}; | ||
/// let socket = Compat::new(UnixStream::connect(proxy_addr) | ||
/// .await | ||
/// .map_err(Error::Io)?); // Compat<UnixStream> | ||
/// let conn = | ||
/// Socks5Stream::connect_with_password_and_socket(socket, target, username, password).await?; | ||
/// // Socks5Stream has implemented futures-io AsyncRead + AsyncWrite. | ||
/// ``` | ||
pub struct Compat<S>(S); | ||
|
||
#[cfg(feature = "futures-io")] | ||
impl<S> Compat<S> { | ||
pub fn new(inner: S) -> Self { | ||
Compat(inner) | ||
} | ||
|
||
/// Consumes the `Compat``, returning the inner value. | ||
pub fn into_inner(self) -> S { | ||
self.0 | ||
} | ||
} | ||
|
||
#[cfg(feature = "futures-io")] | ||
impl<S> AsRef<S> for Compat<S> { | ||
fn as_ref(&self) -> &S { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[cfg(feature = "futures-io")] | ||
impl<S> AsMut<S> for Compat<S> { | ||
fn as_mut(&mut self) -> &mut S { | ||
&mut self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use super::Compat; | ||
use crate::io::AsyncSocket; | ||
use futures_io::{AsyncRead, AsyncWrite}; | ||
use std::{ | ||
io::Result as IoResult, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
impl<S> AsyncSocket for Compat<S> | ||
where S: AsyncRead + AsyncWrite + Unpin | ||
{ | ||
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<IoResult<usize>> { | ||
AsyncRead::poll_read(Pin::new(&mut self.0), cx, buf) | ||
} | ||
|
||
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IoResult<usize>> { | ||
AsyncWrite::poll_write(Pin::new(&mut self.0), cx, buf) | ||
} | ||
} | ||
|
||
impl<S> AsyncRead for Compat<S> | ||
where S: AsyncRead + Unpin | ||
{ | ||
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<IoResult<usize>> { | ||
AsyncRead::poll_read(Pin::new(&mut self.0), cx, buf) | ||
} | ||
} | ||
|
||
impl<S> AsyncWrite for Compat<S> | ||
where S: AsyncWrite + Unpin | ||
{ | ||
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IoResult<usize>> { | ||
AsyncWrite::poll_write(Pin::new(&mut self.0), cx, buf) | ||
} | ||
|
||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IoResult<()>> { | ||
AsyncWrite::poll_flush(Pin::new(&mut self.0), cx) | ||
} | ||
|
||
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IoResult<()>> { | ||
AsyncWrite::poll_close(Pin::new(&mut self.0), cx) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters