Repository navigation
feat(ts): translate collect::<String>, String's Extend, and String::drain #284
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef66b4f
chore(quest): claim quest/m3/ts-string-iters.md
kixelated 4d22bce
feat(ts): translate collect::<String>, String's Extend, and String::d…
kixelated ce85d83
feat(ts): collect Cow<str> and Box<str> as String's from_iter does
kixelated 7847825
feat(ts): reserve the lower size hint as String's Extend<char> does
kixelated 75dd541
fix(ts): give each source its size hint's lower bound, and wide range…
kixelated 69e2286
Merge remote-tracking branch 'origin/main' into quest/m3/ts-string-iters
kixelated 811fcf2
Merge remote-tracking branch 'origin/main' into HEAD
62414db
test(ts): regenerate the lang harness after merging main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # [M] Size hints of std's iterators | ||
|
|
||
| ## Goal | ||
|
|
||
| [rust2-ts](/ts/README.md) translates `Iterator::size_hint` of std's iterators, and each | ||
| collect and extend of a collection that reserves by one, such as `Vec`'s `collect` and | ||
| `extend`, panics with std's `capacity overflow` where std's does, proven by differential | ||
| tests against native Rust. | ||
|
|
||
| ## Plan | ||
|
|
||
| Found by [#284](https://github.com/moq-dev/rust2/pull/284) on 2026-10-30, which gives | ||
| `rt.Iter` a `lower` bound, exact for ranges and what adapts them, and reserves it in | ||
| `String`'s `Extend<char>`. `Vec`'s `collect` of a `TrustedLen` iterator reserves its upper | ||
| bound and panics without one, and its `extend` reserves the lower bound; `rt.iterToArray` | ||
| and `rt.iterExtend` reserve neither, so `(0..u32::MAX).map(f).collect::<Vec<_>>()` reads | ||
| on until memory runs out where std's panics before calling `f`. A call of `size_hint` | ||
| itself needs the upper bound too, and the crate's own `size_hint` is refused. | ||
|
|
||
| Public API: the translator's output. |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Generated by rust2-ts. Do not edit. | ||
|
|
||
| import * as rt from "./rt.ts"; | ||
|
|
||
| /** A crate's own iterator over `items`, which counts each `next` it is asked. */ | ||
| export interface Counted<T> { | ||
| items: T[]; | ||
| nexts: rt.Cell<rt.u8>; | ||
| } | ||
|
|
||
| export function next$Iterator_String(self: Counted<string>): string | rt.Done { | ||
| rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); | ||
| const _8: [string, string[]] | undefined = rt.sliceSplitFirst(self.items); | ||
| if (_8 !== undefined) { | ||
| const val: [string, string[]] = _8; | ||
| self.items = val[1]; | ||
| return val[0]; | ||
| } | ||
| return rt.DONE; | ||
| } | ||
|
|
||
| export function next$Iterator_char(self: Counted<rt.char>): rt.char | rt.Done { | ||
| rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); | ||
| const _8: [rt.char, rt.char[]] | undefined = rt.sliceSplitFirst(self.items); | ||
| if (_8 !== undefined) { | ||
| const val: [rt.char, rt.char[]] = _8; | ||
| self.items = val[1]; | ||
| return val[0]; | ||
| } | ||
| return rt.DONE; | ||
| } | ||
|
|
||
| export function next$Iterator_str(self: Counted<string>): string | rt.Done { | ||
| rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); | ||
| const _8: [string, string[]] | undefined = rt.sliceSplitFirst(self.items); | ||
| if (_8 !== undefined) { | ||
| const val: [string, string[]] = _8; | ||
| self.items = val[1]; | ||
| return val[0]; | ||
| } | ||
| return rt.DONE; | ||
| } | ||
|
|
||
| //# sourceMappingURL=strs.Counted.ts.map |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Correct the README's
collectdescription for owned string items.The sentence says
collectreads its iterator only byfor_each. For ownedStringandCow<str>items, the runtime takes the first item bynextand reads the rest byfor_each. The README should state this exception.📝 Suggested README fix
Committable suggestion