-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
net: Add try_read_buf and try_recv_buf #3351
Conversation
Yeah, this is the right direction. As for vectored reads, I realized that this is mainly relevant for writes rather than reads, so you don't need to do that. (The way you do it for writes is call |
tokio/src/io/poll_evented.rs
Outdated
buf: &'a mut B, | ||
) -> io::Result<usize> | ||
where | ||
&'a E: io::Read + 'a, |
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 don't think this lifetime is necessary.
&'a E: io::Read + 'a, | |
&'a E: io::Read, |
tokio/src/io/poll_evented.rs
Outdated
let dst = buf.chunk_mut(); | ||
let dst = &mut *(dst as *mut _ as *mut [std::mem::MaybeUninit<u8>]); | ||
let mut buf = ReadBuf::uninit(dst); | ||
|
||
let b = &mut *(buf.unfilled_mut() as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]); | ||
let n = self.io.as_ref().unwrap().read(b)?; |
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 don't have to go through ReadBuf
here.
let dst = buf.chunk_mut(); | |
let dst = &mut *(dst as *mut _ as *mut [std::mem::MaybeUninit<u8>]); | |
let mut buf = ReadBuf::uninit(dst); | |
let b = &mut *(buf.unfilled_mut() as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]); | |
let n = self.io.as_ref().unwrap().read(b)?; | |
let dst = buf.chunk_mut(); | |
let dst = &mut *(dst as *mut _ as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]); | |
let n = self.io.as_ref().unwrap().read(dst)?; |
tokio/src/net/udp.rs
Outdated
pub fn try_recv_buf<B: BufMut>(&self, buf: &mut B) -> io::Result<usize> { | ||
self.io | ||
.registration() | ||
.try_io(Interest::READABLE, || unsafe { self.io.read_buf(buf) }) |
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 will need to do something different for the recv_buf
variants. I'm not sure there is an equivalent trait to Read
for datagram types.
tokio/src/net/tcp/stream.rs
Outdated
/// Try to read data from the stream into the provided buffer, advancing the | ||
/// buffer's internal cursor, returning how many bytes were read. |
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.
The documentation could use an example. You can take inspiration from the existing try_read
methods, but reading into a vector created using Vec::with_capacity(1024)
to show how you can read into the unused capacity of a vector.
not sure whether or not it's a good idea to put this in |
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.
The changes seem reasonable to me.
Co-authored-by: Alice Ryhl <alice@ryhl.io>
bd71dbe
to
2e80d5b
Compare
Thank you for the PR! |
Refs: #3313
two questions:
vectored read
function should look like?function signature like below?