-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Merge the Str and StrSlice? #14614
Comments
This sounds pretty reasonable to me. Vectors and slices suffer a similar problem, and we should discuss how to solve them in the same way as well. Additionally, this should have a little discussion before moving forward, I think, to ensure that it's the right direction that we want to go in. |
cc me |
cc #11876 |
@alexcrichton I wonder if you can spell out the rationale for the current design? One of the things I was most confused about when first learning Rust was the various traits around strings, vectors, and slices. In particular, do you know why the I am very much in favor of cutting down the number of traits for these core types. We should also think carefully about the naming here; we already have Does anyone see any downsides to doing the proposed merger? |
I believe I actually tried this a while ago, but could not get the lifetimes to work correctly (I may be misremembering). |
Hmm, maybe one of us should take a crack at doing this and see what goes wrong? I'd be happy to do it, unless @huonw wants to dig up an old checkout/branch? |
I can't find the branch, sorry. |
These traits originally existed, to the best of my knowledge, to allow for I don't really see a reason for the duplication any more (as opposed to leveraging default methods). I also agree that the names should be chosen wisely. |
Thanks @alexcrichton. I'll work on a patch soon and see if I run into lifetime troubles. My worry about Some of these worries would be mitigated by having rustdoc pages for the actual slice and str types, though. (Didn't you recently land a patch for that? I don't see those in the docs, but maybe I'm missing something?) |
Yes, it landed recently: e.g. http://doc.rust-lang.org/master/std/primitive.slice.html |
Oh, I see, it's linked from the main rustdoc page, rather than from within the modules defining these types. @alexcrichton I still find it confusing that if I head to the rustdoc for the relevant module, I don't see the docs for the key type it defines. Is there any way that e.g. the slice module could actually list the slice type as something it defines, with a link to the primitive page? |
@alexcrichton wow these pages are great! |
Ah yes, of course! I meant to do that but I forgot. The Also, you can actually click on the slice type to go to the slice page, it's just that most of the time the only clickable thing is the left-bracket because the actual type inside the slice is itself clickable. |
tl;dr: the proposed implementation strategy is probably not viable. @huonw You're right: there are lifetime problems. The current trait StrSlice<'a> {
fn slice(&self, uint, uint) -> &'a str;
...
} Note that the lifetime of Now suppose we want to add fn as_slice(&self) -> &'a str;
fn as_slice(&'a self) -> &'a str;
fn as_slice<'b>(&'b self) -> &'b str; Unfortunately, none of these options work:
Why is Concretely, it means that examples like let a = "test".to_str();
let mut b = a.as_slice();
b = b.slice(1, 2); work, which rely on Philosophically, I think the point is that a slice already has a determined lifetime, which we want to record in the trait's type precisely to play the games above. So saying On the other hand, a design like trait StrSlice {
fn slice<'a>(&'a self, uint, uint) -> &'a str;
...
} can be implemented for both In summary, I'm not sure that |
Hm, does |
@huonw Not really: it won't autoborrow, so the end result is that if |
On Tue, Jun 03, 2014 at 11:10:05PM -0700, Aaron Turon wrote:
Your analysis seems correct. However, you COULD implement the same |
@nikomatsakis Agreed -- I think this is what @huonw was suggesting above. But at least in today's Rust, as I mentioned above,
because autoborrowing only applies to arguments of type |
On Thu, Jun 05, 2014 at 09:23:10AM -0700, Aaron Turon wrote:
Right. But in the new algorithm I am working on, I actually know how |
|
Agreed. Closing. |
Motivation: The StrSlice trait currently has a bunch of string convenience methods, implemented for
&str
but not the ownedString
. To use them on an owned string, you need e.g.s.as_slice().contains(needle)
where it seems likes.contains(needle)
should be enough.It’d be nice if these methods were available on
String
. I’d told the answer is DST, and implementingDeref
onString
. But DST is not there yet. Instead…Proposal: Move all the
StrSlice
methods into theStr
trait and make them default methods based on the.as_slice()
method.CC @alexcrichton, @brson
The text was updated successfully, but these errors were encountered: