-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Introduce String::into_chars #50845
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
Introduce String::into_chars #50845
Conversation
rust-highfive is sleeping. r? @SimonSapin |
/// ``` | ||
#[unstable(feature = "into_chars", reason = "new API", issue="0")] | ||
pub fn into_chars(self) -> IntoChars { | ||
let chars = unsafe { mem::transmute(self.chars()) }; |
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.
Lying about lifetimes is not nice and there were some soundness issues related to that in the past. For example this can very silently become a read of freed memory if _s
is freed and drop for chars
happens to do something with the data it references.
If we’re doing this, can we do this properly, for example, by adding a new CommonChars
into core
that’s generic over the iterator and making the original Chars
a type Chars = CommonChars<SliceIter>
and the string’s type Chars = core::CommonChars<vec::IntoIter<u8>>
or something?
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.
For example this can very silently become a read of freed memory if _s is freed and drop for chars happens to do something with the data it references.
Yeah, that's why I commented that Chars
doesn't implement Drop
😜
adding a new
CommonChars
into core that’s generic over the iterator
Certainly! I'm very much in favor of removing the unsafe
here.
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.
making the original
Chars
atype Chars =
I've done this, but it exposes the CommonChars
in the documentation; is that acceptable?
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.
I think CommonChars
should be private, or at least doc-hidden and perma-unstable. I think it follows that Chars
should remain a struct with private fields, even if that involves adding trival forwarding impls.
(But regardless I remain skeptical that String::into_chars
should exist in the first place, see other comment.)
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.
Yep, I figured we'd want to delegate all the parts, I just wanted to make sure others agreed before I put in that legwork.
It seems like this is a one-off workaround for the lack of self-referential structs and could just as well be implemented atop the owning_ref crate? If so, I'd prefer people do that rather than adding a type specifically for this use case. Adding new types for every kind of self-reference does not scale (and I see no good reason to support only |
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 |
I don't see it like that, but I can see how people might. Instead, I see it as the missing piece to this table:
|
That was the perspective I initially had when I saw the title and signature, but that confused the hell out of me because 1 In this perspective, i.e., if we ignore owning_ref style situations |
Ping from triage @SimonSapin! This PR needs your review. |
I understand the
(Re triage, I don’t know if there’s an appropriate label for "waiting on an alternative design".) |
I commented that I've already implemented that and was waiting on review feedback. |
I like to consider myself a reasonably capable Rust programmer, but I have never successfully used owning-ref to solve a problem, FWIW. On the other hand, Rental has solved problems for me. Because of this, I would discourage people from suggesting owning-ref as a "use this instead" solution. |
Anyway, I understand the team does not want this addition. I hope self-referential structs are ergonomic to use someday. |
This has come up a few times on Stack Overflow, so might as well see if it can be upstreamed. It's also nice when combined with
Iterator::flat_map
on an iterator ofString
s.