Skip to content
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

Keep track of initialized bytes in read_to_end #3426

Merged
merged 6 commits into from Jan 20, 2021
Merged

Conversation

Darksonn
Copy link
Contributor

This keeps track of the number of initialized bytes inside read_to_end.

Note that old implementation is actually unsound as replacing the buffer inside ReadBuf does not require unsafe, and it does not check whether the pointer has been changed.

// This should no longer even be possible in safe Rust. An implementor
// would need to have unsafely *replaced* the buffer inside `ReadBuf`,
// which... yolo?
assert!(new_len <= buf.capacity());

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-io Module: tokio/io labels Jan 14, 2021
@carllerche
Copy link
Member

We probably want to wait for #3428 to get merged into master before merging this.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@Darksonn Darksonn merged commit c1cf6b7 into master Jan 20, 2021
@Darksonn Darksonn deleted the read_to_end_readbuf branch January 20, 2021 08:49
aknuds1 pushed a commit to aknuds1/tokio that referenced this pull request Jan 20, 2021
@vitalyd
Copy link
Contributor

vitalyd commented Jan 20, 2021

@Darksonn a small suggestion for VecWithInitialized<V>: use a sealed trait to constrain V to be a Vec<u8> or &mut Vec<u8>. As it's written now, VecWithInitialized::new is unsafe because it requires V to be one of those 2 types, but the generic is constrained to just V: AsMut<Vec<u8>>.

Perhaps a formulation like the following would be better:

mod private {
   pub trait VecU8: AsMut<Vec<u8>> {} // better name welcome

   impl VecU8 for Vec<u8> {}
   impl VecU8 for &mut Vec<u8> {}
}

pub struct VecWithInitialized<V: VecU8> { ... }

Just an idea in passing ... 😄

@Darksonn
Copy link
Contributor Author

Feel free to submit a PR with that!

@vitalyd
Copy link
Contributor

vitalyd commented Jan 20, 2021

Ok, let me try to find a bit of time to do that 😁. Mostly wanted to run that by you in case you considered and rejected the idea for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-io Module: tokio/io
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants