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 upConversions from `&mut T` to `&Cell<T>` #1789
Conversation
Kimundi
added
the
T-libs
label
Nov 13, 2016
This comment has been minimized.
This comment has been minimized.
edef1c
commented
Nov 13, 2016
•
|
Having this trait makes no real sense — I'd rather see |
Kimundi
force-pushed the
Kimundi:as-cell
branch
from
c50e3f6
to
05b7967
Nov 13, 2016
This comment has been minimized.
This comment has been minimized.
|
True, the trait only exists to enable method call syntax - maybe thats too extravagant... |
This comment has been minimized.
This comment has been minimized.
edef1c
commented
Nov 13, 2016
•
|
Making this usable with method call syntax only serves to further obscure what's going on, and this whole proposal is already designed to do non-obvious (but very useful) things by its very nature. |
This comment has been minimized.
This comment has been minimized.
camlorn
commented
Nov 13, 2016
|
How does this interact with the borrow checker? It seems to me that the point here is to get rid of the Can you add an example to the RFC that shows usage of the trait? You've only got one that shows how it's currently a problem and one that shows how I can make it work in current Rust. I would like to see this so I can conceptualize it, because I really feel like the current borrow checker brings this crashing down. |
This comment has been minimized.
This comment has been minimized.
edef1c
commented
Nov 13, 2016
•
|
@camlorn Casting |
This comment has been minimized.
This comment has been minimized.
|
Just for reference, there is a crate (by @huonw) providing this on For this kind of thing, where the interesting part is not the writing down of the |
This comment has been minimized.
This comment has been minimized.
|
Alright, I added an example of how it would be used, and switched over to using Also adding the lang team, since the main point here is whether the language can guarantee the cast to be correct. |
Kimundi
added
the
T-lang
label
Nov 13, 2016
This comment has been minimized.
This comment has been minimized.
camlorn
commented
Nov 13, 2016
|
I see it now. I focused in on the first conversion and missed the second. What is the use case of the first conversion? This RFC is all about iteration, but I'm not seeing how |
This comment has been minimized.
This comment has been minimized.
|
@camlorn Can you give an example of code you think won't work? If I transmute |
oli-obk
reviewed
Nov 13, 2016
| let v_slice: &[Cell<T>] = Cell::from_mut_slice(&mut v); | ||
| // example 1 | ||
| for i in v_slice.iter() { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I think the
So the API could look like this: impl<T: ?Sized> Cell<T> {
fn from_mut(val: &mut T) -> &Cell<T>;
}
impl<T> Index<usize> for Cell<[T]> {
type Output = Cell<T>;
fn index(&self, idx: usize) -> &Cell<T>;
}
impl<T> Index<Range<usize>> for Cell<[T]> {
type Output = [Cell<T>];
fn index(&self, idx: Range<usize>) -> &[Cell<T>];
}
impl<T> Index<RangeFrom<usize>> for Cell<[T]> {
type Output = [Cell<T>];
fn index(&self, idx: RangeFrom<usize>) -> &[Cell<T>];
}
impl<T> Index<RangeTo<usize>> for Cell<[T]> {
type Output = [Cell<T>];
fn index(&self, idx: RangeTo<usize>) -> &[Cell<T>];
}
impl<T> Index<RangeFull> for Cell<[T]> {
type Output = [Cell<T>];
fn index(&self, idx: RangeFull) -> &[Cell<T>];
}So given a cell of a slice ( As an aside, it'd also analogously make sense to support |
This comment has been minimized.
This comment has been minimized.
|
I don't think That said, I do quite like |
This comment has been minimized.
This comment has been minimized.
|
The idea is that internal references ( Furthermore, given two references to the same memory, one typed as |
This comment has been minimized.
This comment has been minimized.
camlorn
commented
Nov 13, 2016
|
@eddyb Let me put it this way instead: can someone show me why it's useful, and then I'll come back with an objection if I still have one? |
This comment has been minimized.
This comment has been minimized.
|
@camlorn I've used it in a constraint solver. A highly heterogenous UI tree contained I could've done all of that through a |
This comment has been minimized.
This comment has been minimized.
camlorn
commented
Nov 13, 2016
|
@eddyb |
This comment has been minimized.
This comment has been minimized.
|
@camlorn: let mut set: HashSet<i32> = ...;
let tmp: Vec<&Cell<i32>> = set.iter_mut().map(Cell::from_mut).collect();
for i in &tmp {
for j in &tmp {
i.set(j.get() + i.get());
}
}
|
This comment has been minimized.
This comment has been minimized.
camlorn
commented
Nov 14, 2016
|
@Kimundi |
This comment has been minimized.
This comment has been minimized.
|
Sure, but the need for the vector is kind of independent to this feature, and only caused by the need to have random access to all elements. Eg, the |
This comment has been minimized.
This comment has been minimized.
camlorn
commented
Nov 14, 2016
|
@Kimundi One of the problems of Rust (orthogonal to this RFC) is that some things like this suddenly require you to collect into a vec, which is fine until the container is large. Other such scenarios include deleting multiple keys from a map based on a predicate. The solution to this problem is for me to write up the extensions I think we should have and put them in an RFC, as I think most of them would be accepted without much argument. Nevertheless, every time I see something that ends in |
This comment has been minimized.
This comment has been minimized.
bluss
commented
Nov 25, 2016
|
Collecting into a vec is a workaround for when a more specific solution do not exist yet, for example an (random access?) iterator where each iterator element is a |
aturon
self-assigned this
Dec 22, 2016
This comment has been minimized.
This comment has been minimized.
|
No discussion has been going on in this RFC for almost a month. I'd nominate for FCP, but I don't know think I can. |
Kimundi
changed the title
`AsCell` conversion from `&mut T` to `&Cell<T>`
Conversion from `&mut T` to `&Cell<T>`
Jan 24, 2017
Kimundi
changed the title
Conversion from `&mut T` to `&Cell<T>`
Conversions from `&mut T` to `&Cell<T>`
Jan 24, 2017
This comment has been minimized.
This comment has been minimized.
|
Agreed - I updated the RFC text a bit for a post- @rfcbot fcp merge |
This comment has been minimized.
This comment has been minimized.
|
Glad to see this RFC received so much formal scrutiny. :) @nrc / @aturon: Sorry, I totally forgot to address your comments. I've pushed updated wordings. @pnkfelix: Yeah, the slice conversion is independent to the core proposal, and weird in its own way. If there where major objections I'd be happy to remove it from this RFC, but apparently no one sees an actual issue with it. (notwithstanding @RalfJung's confusion |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jun 11, 2017
|
The final comment period is now complete. |
aturon
referenced this pull request
Jul 3, 2017
Open
Tracking issue for RFC 1789: Conversions from `&mut T` to `&Cell<T>` #43038
aturon
merged commit 246ff86
into
rust-lang:master
Jul 3, 2017
This comment has been minimized.
This comment has been minimized.
|
Huzzah! This RFC has been merged. Tracking issue. |
RalfJung
referenced this pull request
Jul 3, 2017
Closed
Tracking issue for RFC 1758: Specify `repr(transparent)` #43036
This comment has been minimized.
This comment has been minimized.
|
Question, why is this merged but slice::ref_slice is deprecated? Aren't these fundamentally fairly similar? |
This comment has been minimized.
This comment has been minimized.
|
@whitequark There is no slice <-> non-slice conversion here. I don't know why |
This comment has been minimized.
This comment has been minimized.
|
@eddyb I mean this comment by @alexcrichton:
This seems to me to fall right into the "little nugget of functionality" area. I would rather we get ref_slice back, it's pretty useful in embedded contexts, if you have no Box! |
This comment has been minimized.
This comment has been minimized.
mzji
commented
Jul 4, 2017
•
|
@whitequark Then should it go to |
This comment has been minimized.
This comment has been minimized.
|
I don't think we have precedent for that and it seems like a really odd decision to me. |
This comment has been minimized.
This comment has been minimized.
edef1c
commented
Jul 7, 2017
|
Note that |
This comment has been minimized.
This comment has been minimized.
This proposal could have been done the same way and without even any new RFCs, just mark |
This comment has been minimized.
This comment has been minimized.
|
I think the libs team would be open to reconsidering |
This comment has been minimized.
This comment has been minimized.
|
For what it's worth, I still find uses for Most commonly, you have a function that takes an
Less commonly, you have an enum that sometimes has
I continue to think this function is a natural candidate for the standard library. It's a bit of unsafe code that is useful as a bit of glue in various scenarios and absolutely no dependencies or runtime implications. It is not entirely obvious that it is safe -- in particular, it requires understanding Rust's memory layout rules (which of course are not entirely defined). So putting it in the standard library is a way for us to advertise that indeed it is safe. =) It's also something that people might not realize is possible, and hence they wouldn't know to search for the crate. It's also ok for it to exist in a crate, of course, but I don't think there's any good reason to put it there. |
This comment has been minimized.
This comment has been minimized.
|
I'd like to get it back in std. What's the process here? Is it an RFC or just a PR? |
This comment has been minimized.
This comment has been minimized.
|
@whitequark I think it's a small enough addition that a PR suffices. Feel free to CC me. |
This comment has been minimized.
This comment has been minimized.
comex
commented
Aug 1, 2017
•
|
This is late, but I have a concern about this RFC's design. To go from However, a natural extension to the proposed use case would be a thread safe variant: in particular, a way to go from |
This comment has been minimized.
This comment has been minimized.
edef1c
commented
Aug 3, 2017
|
@comex You also can't swap out the contents of a |
This comment has been minimized.
This comment has been minimized.
|
I was going to reply with something similar but I think their point was that there is no |
This comment has been minimized.
This comment has been minimized.
comex
commented
Aug 3, 2017
|
@edef1c Ah... I kind of forgot about that. It might be possible to support the full But in any case, as @glaebhoerl said, there is no |
Kimundi commentedNov 13, 2016
•
edited by Centril
A proposal about legitimizing the cast of a
&mut Tto a&Cell<T>.Rendered