New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tcp socket from std stream #3838
Changes from 3 commits
28eb241
bab7c2c
01e54bb
309036d
629876a
8057f89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -482,6 +482,48 @@ impl TcpSocket { | |||||
let mio = self.inner.listen(backlog)?; | ||||||
TcpListener::new(mio) | ||||||
} | ||||||
|
||||||
/// Converts a TcpStream into a TcpSocket | ||||||
/// This converts the TcpStream into | ||||||
/// the platform specific raw_fd(unix) / raw_socket(windows) | ||||||
/// and then converts them into the TcpSocket | ||||||
/// as TcpSocket implements the FromRawFd(unix) and | ||||||
/// FromRawSocket(windows) traits. | ||||||
|
||||||
/// # Examples | ||||||
/// ``` | ||||||
/// use tokio::net::TcpSocket; | ||||||
/// | ||||||
/// use std::net::TcpStream; | ||||||
/// | ||||||
/// #[tokio::main] | ||||||
/// async fn main() -> io::Result<()> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your example doesn't compile.
Suggested change
|
||||||
/// | ||||||
/// let stream = TcpStream::connect("127.0.0.1:8080")?; | ||||||
/// | ||||||
/// let socket = TcpSocket::from_std_stream(stream)?; | ||||||
/// # drop(socket); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't do anything.
Suggested change
|
||||||
/// | ||||||
/// Ok(()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to use |
||||||
/// } | ||||||
/// ``` | ||||||
pub fn from_std_stream(std_stream: std::net::TcpStream) -> io::Result<TcpSocket> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The conversion always succeeds, so no need for a
Suggested change
|
||||||
#[cfg(unix)] | ||||||
{ | ||||||
use std::os::unix::io::{FromRawFd, IntoRawFd}; | ||||||
|
||||||
let raw_fd = std_stream.into_raw_fd(); | ||||||
unsafe { Ok(TcpSocket::from_raw_fd(raw_fd)) } | ||||||
} | ||||||
|
||||||
#[cfg(windows)] | ||||||
{ | ||||||
use std::os::windows::io::{FromRawSocket, IntoRawSocket}; | ||||||
|
||||||
let raw_socket = std_stream.into_raw_socket(); | ||||||
unsafe { Ok(TcpSocket::from_raw_socket(raw_socket)) } | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl fmt::Debug for TcpSocket { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some improvements to the documentation:
TcpStream
to be a link, andTcpSocket
should be surrounded by backticks to display it as code.socket2
, and that the socket must not be in a connected state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your quick reply. I'll go over all the changes suggested and raise another request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not create a new PR. Just push more commits to your
tcp-socket-from-std-stream
branch.