-
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
The string API should be more generic. #10827
Comments
It's not immediately clear to me how one can merge e.g. |
@huonw Both fn split<M: StrMatch>(m: M) -> SplitIterator { ... } And the Provided the interface is defined in the right way, it should inline to efficient code. There might be a bit more repetition and ridiculous type signatures involved on the library side, but the user facing API would become smaller. |
This may be useful in Servo. We're going to need functions to work with UTF16 strings (required for JavaScript) and it would be nice if these were the same string API functions we were used to. |
@metajack: AFAIK JavaScript really treats them as UCS2 strings though, and a UTF-16 string type would be too strict. Of course, UCS2 strings could just be a type in Servo. |
@metajack Hm, this is actually not the kind of genericy I though of here - I'm thinking of making a few functions that work with But defining the traits in a way that would be reusable for custom string types makes sense too, it just would be a different issue. |
@thestinger Yes, UCS2 is what I meant. @Kimundi Ah, I see. |
Closing in favor of your RFC: rust-lang/rfcs#528 |
new lint: `large_stack_frames` This implements a lint that looks for functions that use a lot of stack space. It uses the MIR because conveniently every temporary gets its own local and I think it maps best to stack space used in a function. It's probably going to be quite inaccurate in release builds, but at least for debug builds where opts are less aggressive on LLVM's side I think this is accurate "enough". (This does not work for generic functions yet. Not sure if I should try to get it working in this PR or if it could land without it for now and be added in a followup PR.) I also put it under the nursery category because this probably needs more work... changelog: new lint: [`large_stack_frames`]
Current situation
Right now, many operation you can do with a string are hard coded to one specific type, like
&str
orchar
. For example:Improving the API
Using traits and generics, these two examples could get boiled down into one
contains
and onepush
, which would accept either type while still being just as efficient as before.There are quite a few methods that would benefit from such genericy, and so far I've identified two different kinds of traits:
StrPushable
- Anything that can be pushed to a string. String slices andchar
would implement this, but also things likeAscii
could.Functions that would benefit from this include:
This might be implementable with
fmt::Default
instead, but I'm not sure if it's a good idea to do that, as it would mean you could push anything to a string that implements that trait.StrMatcher
- Anything that could be used to find a string pattern in a string.Again, string slices and
char
would implement it, as well as predicate functions like|char| -> bool
, theAscii
type, or things like a regex type.A trait like this already exists in a limited form with the
CharEq
trait, and would if properly extend be useful for a number of functions:How it could look like
Status
I'm currently working on trying out this approach, to see if there are any rough edges or issues I haven't though of, but I think this would bring the string API a great step forward.
The text was updated successfully, but these errors were encountered: