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

Refactor split_at/split_to #663

Merged
merged 8 commits into from
Feb 23, 2024

Conversation

braddunbar
Copy link
Contributor

I did a little refactoring of split_at and split_to to remove some extraneous checks and add some clarity around safety guarantees.

Each commit is fairly self-contained, but here are the highlights with some rationale:

  1. I set len a little more concisely in ea9dd60.
  2. split_at and split_to are doing redundant bounds checking and comparison that I removed in 851b59b and 8381ab7.
  3. I renamed set_start to advance_unchecked to better reflect it's usage in c70d6d2. I also added some explicit comments about the safety guarantees of that method and an explanation of why each usage is sound.

I'm pretty new to this repo, so if I've missed some rules or norms please let me know. I'll be happy to change things up!

The shallow clone call just above always results in the kind being
shared so we don't need to assert it here.
we already assert this at the top of the method
We know several things here:

1. self.len <= self.cap, always
2. at <= self.len, asserted at the top of this method
3. after calling shallow_clone, other.cap == self.cap

Therefore, at <= self.len <= other.cap.
This method never moves the cursor backward, only advances it forwards.
I think reflecting that in the name makes things a bit more clear.

I also added explicit safety comments to make it clear why each usage is
sound.
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.

LGTM. I left a minor nit inline, but the change is an overall good improvement to clarity of the unsafe code. Thanks.

other.len = at;
// SAFETY: We've checked that `at` <= `self.len()` and we know that `self.len()` <=
// `self.capacity()`.
self.advance_unchecked(at);
Copy link
Member

Choose a reason for hiding this comment

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

Very minor nitpick, but this is unsafe code so I think it might be worth applying. In split_off, you call advance_unchecked first then update cap and len. You reverse it here. Would you mind keeping them the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, thanks for pointing it out! Made things a little more consistent in b971743. Let me know if that isn't what you meant.

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!

@carllerche carllerche merged commit 4628927 into tokio-rs:master Feb 23, 2024
15 checks passed
@braddunbar braddunbar mentioned this pull request Mar 22, 2024
@braddunbar braddunbar deleted the split-at-to-refactor branch March 24, 2024 13:01
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