-
Notifications
You must be signed in to change notification settings - Fork 286
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
Conversation
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.
c70d6d2
to
442f85d
Compare
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.
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); |
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.
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?
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.
Yeah, thanks for pointing it out! Made things a little more consistent in b971743. Let me know if that isn't what you meant.
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!
I did a little refactoring of
split_at
andsplit_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:
len
a little more concisely in ea9dd60.split_at
andsplit_to
are doing redundant bounds checking and comparison that I removed in 851b59b and 8381ab7.set_start
toadvance_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!