Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd Ref/RefMut map_split method #51466
Conversation
rust-highfive
assigned
shepmaster
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
|
r? @shepmaster (rust_highfive has picked a reviewer for you, use r? to override) |
rust-highfive
added
the
S-waiting-on-review
label
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
joshlf
force-pushed the
joshlf:ref-split
branch
2 times, most recently
from
123f1f1
to
6f065e6
Jun 10, 2018
kennytm
added
the
T-libs
label
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
kennytm
reviewed
Jun 10, 2018
| // | ||
| // `Ref` and `RefMut` are both two words in size, and so there can never be | ||
| // enough `Ref`s or `RefMut`s in existence to overflow half of the `usize` | ||
| // range. Thus, a `BorrowFlag` will never overflow. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
joshlf
Jun 10, 2018
Author
Contributor
Good call. I'll make sure that this is asserted everywhere it could overflow.
This comment has been minimized.
This comment has been minimized.
joshlf
force-pushed the
joshlf:ref-split
branch
2 times, most recently
from
3ab1411
to
80bcd16
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This comment has been minimized.
This comment has been minimized.
|
cc @RalfJung on the soundness of this (seems sound to me...) It would be good to list more use cases this supports. let mut borrow = c.borrow_mut();
let (begin, end) = borrow.split_at_mut(2);
assert_eq!(*begin, [1, 2]);
assert_eq!(*end, [3, 4]);
begin.copy_from_slice(&[4, 3]);
end.copy_from_slice(&[2, 1]);which seems more direct. |
Centril
reviewed
Jun 10, 2018
|
Some improvement suggestions for documentation. |
| /// ``` | ||
| /// use std::cell::{Ref, RefCell}; | ||
| /// | ||
| /// let c = RefCell::new([1, 2, 3, 4]); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| /// ``` | ||
| /// use std::cell::{RefCell, RefMut}; | ||
| /// | ||
| /// let c = RefCell::new([1, 2, 3, 4]); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
kennytm
added
the
T-lang
label
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
|
Too deep for me! r? @dtolnay |
rust-highfive
assigned
dtolnay
and unassigned
shepmaster
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
|
Why |
dtolnay
reviewed
Jun 10, 2018
| // be enough `Ref`s or `RefMut`s in existence to overflow half of the `usize` | ||
| // range. Thus, a `BorrowFlag` will probably never overflow. However, this is | ||
| // not a guarantee, as a pathological program could repeatedly create and then | ||
| // mem::drop `Ref`s or `RefMut`s. Thus, all code must explicitly check for |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The implementation looks good to me. I agree that after split they should be able to have different types |
joshlf
force-pushed the
joshlf:ref-split
branch
from
80bcd16
to
62da8d6
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
Done. Is there any canonical way to have the function return an arbitrary number of elements in the tuple (or at least, up to some reasonable bound, like 8)? I have a function called |
dtolnay
reviewed
Jun 10, 2018
| /// assert_eq!(*begin, [1, 2]); | ||
| /// assert_eq!(*end, [3, 4]); | ||
| /// ``` | ||
| #[unstable(feature = "refcell_map_split", issue = "51466")] |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
joshlf
referenced this pull request
Jun 10, 2018
Closed
Tracking issue for Ref/RefMut::map_split #51476
joshlf
force-pushed the
joshlf:ref-split
branch
from
62da8d6
to
e34f2e4
Jun 10, 2018
This comment has been minimized.
This comment has been minimized.
I was thinking of this myself. This is much like the issue faced by Libraries often solve this sort of problem by making a trait for the output type and implementing it for many tuple sizes (or implementing it on a small set of combinator types), but I seldom see this in the standard library... almost as though we're waiting for variadic generics (yet nobody wants to admit it!). |
This comment has been minimized.
This comment has been minimized.
I'm happy to merge this as is and just keep an eye out for being able to add something like |
This comment has been minimized.
This comment has been minimized.
|
It does make me wonder though if perhaps the standard library should simply expose a low-level API for this; one which might not be very ergonomic (it might even be (that said, doing so would ironically come at a much greater design cost, as care must be taken not to place unnecessary constraints on future evolution of the type) Edit: Of course, I guess there's no real reason |
bors
added
S-waiting-on-bors
and removed
S-waiting-on-review
labels
Jun 17, 2018
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Jun 17, 2018
This comment has been minimized.
This comment has been minimized.
|
|
bors
added
S-waiting-on-review
and removed
S-waiting-on-bors
labels
Jun 17, 2018
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This comment has been minimized.
This comment has been minimized.
bors
added
S-waiting-on-bors
and removed
S-waiting-on-review
labels
Jun 17, 2018
bors
added a commit
that referenced
this pull request
Jun 17, 2018
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
|
bors
merged commit 2a999b4
into
rust-lang:master
Jun 17, 2018
joshlf
deleted the
joshlf:ref-split
branch
Jun 17, 2018
This comment has been minimized.
This comment has been minimized.
|
This PR regressed compile time significantly, as shown by http://perf.rust-lang.org/compare.html?start=0f8f4903f73a21d7f408870551c08acd051abeb0&end=aec00f97e1cdcea2b079e209a7e759201ba6ca7c&stat=instructions:u Almost all benchmarks in rustc-perf were affected, the worst by 4%. @joshlf: Can you investigate? |
bors
added a commit
that referenced
this pull request
Jun 19, 2018
bors
added a commit
that referenced
this pull request
Jun 19, 2018
joshlf
added a commit
to joshlf/rust
that referenced
this pull request
Jun 19, 2018
bors
added a commit
that referenced
this pull request
Jun 20, 2018
bors
added a commit
that referenced
this pull request
Jun 26, 2018
bors
added a commit
that referenced
this pull request
Jun 27, 2018
Mark-Simulacrum
referenced this pull request
Jun 27, 2018
Closed
Crate stable-borrow-state regressed in 1.28 beta #51840
bors
added a commit
that referenced
this pull request
Jun 28, 2018
bors
added a commit
that referenced
this pull request
Jun 28, 2018
This comment has been minimized.
This comment has been minimized.
|
@nnethercote 's concern has been addressed in #51630. |
joshlf commentedJun 10, 2018
As proposed here.
TLDR: Add a
map_splitmethod that allows multipleRefMuts to exist simultaneously so long as they refer to non-overlapping regions of the originalRefCell. This is useful for things like the slicesplit_at_mutmethod.