Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quest/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ generic std gap is planned here even when moq never calls it.
- [Cow of a slice, and Wrapping](/quest/m3/ts-cow-slices.md) - `Cow<[T]>` and `Wrapping<T>` translate, `PartialEq` included
- [LinkedList](/quest/m3/ts-linked-list.md) - `LinkedList` and its methods translate
- [Zip's next_back in constant time](/quest/m3/ts-zip-back.md) - a reversed zip of random-access iterators skips the longer side's excess at once
- [Cloning std's iterators](/quest/m3/ts-iter-clone.md) - `clone` of std's iterators and adaptors translates, each clone reading on independently
- [Cycling std's iterators](/quest/m3/ts-iter-cycle.md) - `Iterator::cycle` translates, cloning its iterator each time it ends
- [Comparing iterators](/quest/m3/ts-iter-compare.md) - `eq`, `cmp`, `lt`, and the rest of `Iterator`'s comparisons translate against any `IntoIterator`
27 changes: 0 additions & 27 deletions quest/m3/ts-iter-clone.md

This file was deleted.

17 changes: 17 additions & 0 deletions quest/m3/ts-iter-cycle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# [S] Cycling std's iterators

## Goal

`Iterator::cycle` translates, proven by a differential test against native Rust, which
the reject fixture's `cycled` refuses today.

## Plan

Found by [#322](https://github.com/moq-dev/rust2/pull/322), which gave each `rt.Iter`
that std implements `Clone` for a `clone(h)` and the translation its `rt.Clones`. std's
`Cycle` holds its iterator as it was and a clone it reads, which it clones again each
time that ends, so an adaptor of the runtime's can hold the original and its `Clones`
and clone it the same way. Planned without a moq consumer, under the maintainer's
2026-10-27 decision that rust2 aims to support all of std.

Public API: the translator's output.
12 changes: 11 additions & 1 deletion ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ crate translation reaches is refused, and so are two versions of one crate it re
| `PeekMut<T>` | `rt.PeekMut<T>`, the `rt.Place` of a heap's greatest element, which sifts it down as it drops once `DerefMut` lent it |
| `Ordering`, `DefaultHasher`, `RandomState` | `rt.Ordering`, which is `-1`, `0`, or `1`; `rt.Hasher`; `rt.RandomState`, the keys of the `DefaultHasher` it builds, never changed once made |
| `Duration` | `rt.Duration`: `{ secs, nanos }`, std's whole seconds as an `rt.u64` and the nanoseconds past them, never changed once made |
| closure | an arrow function, with `[Symbol.dispose]` if it owns a value that drops |
| closure | an arrow function, with `[Symbol.dispose]` if it owns a value that drops, and what makes its clone if a clone can't be itself |
| `fn` pointer, function item as a value | the function, as a closure is typed: a `()` result is `undefined`, where a declared function returns `void` |
| `&dyn Trait`, `&mut dyn Trait`, `Box<dyn Trait>`, `Arc<dyn Trait>` | an object with the trait's methods, its supertraits' included: `shape.area$Shape()`; behind an `Arc`, an `rt.ArcDyn` sharing the count of the `Arc` it was made from |
| `dyn Any` | an instance of the class its value's type was wrapped in, which a downcast checks for: `a instanceof Any.Square ? a.v : undefined` |
Expand Down Expand Up @@ -238,6 +238,16 @@ bound `undefined` for `None`, and a `collect` or `extend` into a `Vec`, `VecDequ
std's capacity overflow past the most it holds. Capacity isn't modeled, so a collection
an `extend` starts from is taken as full.

`clone` of one of std's iterators or adaptors is its `rt.Iter`'s `clone`, which reads on
from where the original was, independently of it, as std's `Clone` does; one over a hash
collection steps a fresh iterator of it past what the original read, as JS can't copy
where a `Map`'s iterator is. What it holds that the runtime can't clone, an item one that
owns its items holds, an adaptor's closure, or the crate's own iterator, it clones by
what the translation gives it, as `it.clone({ v: Item.clone$Clone })`. A closure clones as Rust
clones its captures: one that writes none of them, each of which only copies or clones
as itself, is its own clone, and another whose clone translation reaches is made by a
function of its captures, which makes the clone again of theirs, in order.

A `str` is a JS string, which is UTF-16: `chars` steps by code point, and a search by a
`char`, a `&str`, a predicate, or an array or slice of chars, as in `rt.strSplit(s, "/")`, takes the text or the
closure, which it calls on each char it reads, in the order std reads them. An offset it
Expand Down
23 changes: 23 additions & 0 deletions ts/fixtures/lang/expected/clones.Countdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";

export function clone$Clone(self: Countdown): Countdown {
return { _0: self._0 };
}

/** The crate's own iterator, which std's adaptors hold by value. */
export interface Countdown {
_0: rt.u8;
}

export function next$Iterator(self: Countdown): rt.u8 | rt.Done {
const _2: rt.u8 | undefined = rt.u8CheckedSub(self._0, 1);
if (_2 !== undefined) {
self._0 = _2;
return self._0;
}
return rt.DONE;
}

//# sourceMappingURL=clones.Countdown.ts.map
41 changes: 41 additions & 0 deletions ts/fixtures/lang/expected/clones.Logged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";

/**
* Logs its id as it drops, and as it is cloned, the id with the high bit set: its clone's
* id is one more.
*/
export class Logged {
id: rt.u8;
log: rt.Arc<rt.RefCell<rt.ByteVec>>;

constructor(id: rt.u8, log: rt.Arc<rt.RefCell<rt.ByteVec>>) {
this.id = id;
this.log = log;
}

[Symbol.dispose](): void {
drop$Drop(this);
this.log[Symbol.dispose]();
}
}

export function id(self: Logged): rt.u8 {
return self.id;
}

export function clone$Clone(self: Logged): Logged {
const _5: rt.RefCell<rt.ByteVec> = rt.refCellBorrowMut(self.log.value);
rt.byteVecPush(_5.value, 128 | self.id);
rt.refMutRelease(_5);
return new Logged(rt.u8WrappingAdd(self.id, 1), rt.arcClone(self.log));
}

export function drop$Drop(self: Logged): void {
const _5: rt.RefCell<rt.ByteVec> = rt.refCellBorrowMut(self.log.value);
rt.byteVecPush(_5.value, self.id);
rt.refMutRelease(_5);
}

//# sourceMappingURL=clones.Logged.ts.map
18 changes: 18 additions & 0 deletions ts/fixtures/lang/expected/clones.Plan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { unwrapOr$0r2bmpr } from "./core/option.ts";

/** What `turns` reads by: the case's bytes, each step's taken in order. */
export interface Plan {
data: rt.ByteSlice;
at: rt.usize;
}

export function next(self: Plan): rt.u8 {
const b: rt.u8 = unwrapOr$0r2bmpr(rt.byteSliceGet(self.data, rt.usizeRem(self.at, rt.max(self.data.length, 1))), 0);
self.at = rt.usizeAdd(self.at, 1);
return b ^ rt.u8WrappingMul(self.at & 0xff, 29);
}

//# sourceMappingURL=clones.Plan.ts.map
Loading