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

Don't set len in BytesMut::reserve #682

Merged
merged 2 commits into from
Apr 9, 2024

Conversation

braddunbar
Copy link
Contributor

A fundamental invariant of reserve is that it can extend capacity while the stored data remains the same, even if it's moved to a new allocation. As a result, len can never change during a call to reserve.

A fundamental invariant of `reserve` is that it can extend capacity
while the stored data remains the same, even if it's moved to a new
allocation. As a result, `len` can never change during a call to
`reserve`.
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Sorry for the delay.

@@ -639,7 +639,6 @@ impl BytesMut {

// Update the info
self.ptr = vptr(v.as_mut_ptr().add(off));
self.len = v.len() - off;
Copy link
Contributor

Choose a reason for hiding this comment

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

How about asserting that they are equal?

debug_assert_eq!(self.len, v.len() - off);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works for me! Added in f92c9f6.

Copy link
Contributor

@Darksonn Darksonn 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 e4af486 into tokio-rs:master Apr 9, 2024
15 checks passed
@braddunbar braddunbar deleted the dont-set-len-in-reserve branch April 9, 2024 14:03
@mox692 mox692 mentioned this pull request Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants