diff --git a/quest/m3/README.md b/quest/m3/README.md index a808f543e..845f5374f 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -25,6 +25,6 @@ generic std gap is planned here even when moq never calls it. - [An inclusive range folded through an adaptor](/quest/m3/ts-range-fold.md) - a `RangeInclusive` folded through `map`, `filter`, or `rev` ends in core's state, and `by_ref` of one type-checks - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do -- [Ordering in std](/quest/m3/ts-ordering.md) - `cmp::max`/`min`, `is_sorted`, and `BinaryHeap`, and #173's cost on integers measured +- [Slice windows and chunks, Vec drain, and the order of byte arrays](/quest/m3/ts-slice-gaps.md) - `windows`, `chunks`, `Vec::drain`, and byte arrays' order translate - [Return a std iterator's next from a generic function](/quest/m3/ts-next-return.md) - `it.next()` returned from a generic function is an `Option`, as `tsc` expects - [Port the workflow guard to kixelated/quest](/quest/m3/quest-workflow-guard.md) - quest's environment guard parses YAML as this repository's does diff --git a/quest/m3/ts-ordering.md b/quest/m3/ts-ordering.md deleted file mode 100644 index 52609a6ea..000000000 --- a/quest/m3/ts-ordering.md +++ /dev/null @@ -1,22 +0,0 @@ -# [M] Ordering in std - -## Goal - -rust2-ts translates `cmp::max` and `cmp::min` (the free functions), the `is_sorted` -family, and `BinaryHeap`, proven by differential tests against native Rust, and the cost -of [#173](https://github.com/moq-dev/rust2/pull/173)'s comparison tests on integers is -measured and, if it shows, removed. - -## Plan - -Planned on 2026-10-28; rust2 aims to support all of std, so this has no moq consumer. -#173 found `cmp::max` and `cmp::min` refused. - -- These compare through `PartialOrd::lt` and the rest as #173 builds them, so a crate's own - `lt` is called where std calls it. `BinaryHeap`'s sift order decides which of equal - elements comes out first and which comparisons run, so port std's. -- #173 passes `rt.cmpLt(cmp)` where sorts and `max`/`min` took `cmp`. Measure sorts and - `max`/`min` of integers on the [TS benchmark](/ts/README.md#bench) against the - commit before #173. - -Public API: the translator's output. diff --git a/quest/m3/ts-slice-gaps.md b/quest/m3/ts-slice-gaps.md new file mode 100644 index 000000000..044ada20c --- /dev/null +++ b/quest/m3/ts-slice-gaps.md @@ -0,0 +1,17 @@ +# [S] Slice windows and chunks, Vec drain, and the order of byte arrays + +## Goal + +rust2-ts translates a slice's `windows` and `chunks`, `Vec::drain` of a range, and `<`, +`<=`, `>`, `>=`, and `cmp` of byte arrays such as `[u8; 2]`, matching std's iteration, +drop order of drained values, and comparison results. Proven by differential tests +against native Rust. + +## Plan + +Met by [#252](https://github.com/moq-dev/rust2/pull/252) on 2026-10-29: its ordering +fixtures and bench rewrote around each. rust2 aims to support all of std. Each refuses +with a span today; the byte-array comparison refuses because the mapping of a byte array +it is generic over would take the array, not a slice of it. + +Public API: the translator's output. diff --git a/quest/m3/ts-unsafe.md b/quest/m3/ts-unsafe.md index bd18c538c..76942a9d6 100644 --- a/quest/m3/ts-unsafe.md +++ b/quest/m3/ts-unsafe.md @@ -45,5 +45,11 @@ none of its own `unsafe`. range, and each integer's `from_le_bytes` and `from_ne_bytes`, and `chunks_vectored` needs `std::io::IoSlice`: 184 of moq-net's 196 refusals in `bytes`, the rest being 12 of [`f32`](/quest/m3/ts-f32.md). +- `BinaryHeap` and `PeekMut` translate from std's MIR, whose `Hole` moves elements + through raw pointers into the `Vec`'s storage, replacing `rt.BinaryHeap` and + `rt.PeekMut`, the hand-written port of std's sifts that + [#252](https://github.com/moq-dev/rust2/pull/252) mapped them to and that must track + std's by hand. The lang fixture's `orders` differential case proves the translation + still matches std's comparisons, pop order, and drops. Public API: the translator's output. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index f3665ef24..25b99d3cc 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -45,7 +45,7 @@ `true`. - The runtime's `iterMax`, `iterMin`, `iterMaxByKey`, and `iterMinByKey` take a `PartialOrd::lt` after the iterator and any key function, for items or keys of any type: - `rt.iterMax(it, rt.cmpLt(rt.cmp))`. + `rt.iterMax(it, rt.cmpLt(rt.cmpStr))`, or of integers, `rt.iterMax(it, rt.lt)`. - `rt.FromUtf8Error` holds its `rt.Utf8Error` as std's does: `e.validUpTo` and `e.errorLen` are `e.error.validUpTo` and `e.error.errorLen`. - `ts.toml` no longer takes `ignore-macros`: `tracing`'s events are left out without it, diff --git a/ts/README.md b/ts/README.md index e316fdf79..fe5cfb3b1 100644 --- a/ts/README.md +++ b/ts/README.md @@ -68,7 +68,7 @@ from. `rt.ts` beside them is the runtime: what std does that JS does not, with iterators in `rt/iter.ts`, `str`'s searches in `rt/str.ts`, collections in `rt/collections.ts`, ownership, atomics, and async in `rt/task.ts`, `f64`'s methods in `rt/float.ts`, and `Duration` in `rt/time.ts`. What translates from std's own MIR, -`Option`'s, `Result`'s, and `Poll`'s methods, goes in a module under its crate's name, as +`Option`'s, `Result`'s, and `Poll`'s methods and `cmp::max` and `min`, goes in a module under its crate's name, as `core/option.ts` holds `map$Option_u64_u64_rescale_closure0`. A dependency's modules go under its crate's name too, `kio.ts` and `kio/lock.ts`, with the instances of its generic functions that the package's types make, so a module at the package's root named for a @@ -95,7 +95,8 @@ crate translation reaches is refused, and so are two versions of one crate it re | an iterator | `rt.Iter`, whose adaptors are runtime functions: `rt.iterMap(rt.iterSlice(v), f)` | | `a..b`, `a..=b` | `rt.Range`, `rt.RangeInclusive`, which are iterators too | | `a..`, `..b`, `..=b`, `Bound` | `{ start }`, `{ end }`, `{ end, inclusive: true }`; `rt.RangeBound`, a union tagged by variant | -| `HashMap`, `HashSet`, `BTreeMap`, `BTreeSet`, `VecDeque` | `rt.HashMap` and so on, each a plain object whose functions are the runtime's: `rt.hashGet(map, key)` | +| `HashMap`, `HashSet`, `BTreeMap`, `BTreeSet`, `VecDeque`, `BinaryHeap` | `rt.HashMap` and so on, each a plain object whose functions are the runtime's: `rt.hashGet(map, key)` | +| `PeekMut` | `rt.PeekMut`, the `rt.Place` of a heap's greatest element, which sifts it down as it drops once `DerefMut` lent it | | `Ordering`, `DefaultHasher` | `rt.Ordering`, which is `-1`, `0`, or `1`; `rt.Hasher` | | `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 | @@ -254,7 +255,8 @@ impl, an `Arc`'s count, or a waker. Bodies lower from MIR, so each drop is where elaboration put it. A panic drops nothing. A value without `[Symbol.dispose]` of its own, such as an enum or a tuple, drops through glue lowered from rustc's `drop_in_place` shim, a function of its type's own (`drop$Held`). `bool`'s, `Option`'s, `Result`'s, and `Poll`'s -methods lower from core's MIR, so they drop what core's do, where core's do. The runtime +methods and `cmp::max`, `min`, and their `_by` and `_by_key` forms lower from core's MIR, +so they drop what core's do, where core's do. The runtime functions for std's collections, `Arc`, and the std functions that may discard an operand (`OnceCell::get_or_init`, a map's `insert`) take what drops each value they may drop, and drop it where std's do. So do iterators: one that owns its items, as a `Vec`'s `into_iter` or a @@ -527,6 +529,14 @@ they need. calling only the comparator, as std permits of an unstable sort: JS's `sort` moves `undefined`, a `None`, to the end without comparing it, and each engine orders its comparisons differently. +- A `BinaryHeap` is std's heap in an array, with std's sifts ported to the runtime: std's + move elements through raw pointers into its `Vec`, which no body of std's lowers + through, and the sifts decide which `PartialOrd` tests run and which of equal elements + leaves first. The sorts, `max` and `min`, the heap, and `is_sorted` take an integer's + tests as the runtime's `rt.lt`, `rt.le`, `rt.gt`, and `rt.ge` rather than through its + `partial_cmp`, `rt.cmpLt(rt.cmp)`: on `just ts bench`'s `order/` workloads that took + Firefox 0.8-0.86x the time on sorts and `max`/`min` and Chrome 0.66x on the heap, and + Chrome the same on the rest. - Iterators are the runtime's lazy classes rather than generators or ES iterator helpers: std's adaptors would lower from std's own bodies, whose slice iterators are raw pointers, ES helpers lack most of them @@ -581,7 +591,8 @@ what it captured, `mem::swap`, `replace`, or `take` of a reference's whole point assigning an `Arc`, a waker, a `MutexGuard`, a pinned reference, or a future through a reference, a switch on or a field of another crate's type without a mapping, and std functions without a mapping in `src/lower/library.rs`, but for `bool`'s, `Option`'s, -`Result`'s, and `Poll`'s methods that take no `&mut`, which lower from core's MIR. +`Result`'s, and `Poll`'s methods that take no `&mut`, and `cmp`'s `max` and `min` forms, +which lower from core's MIR. ## Tests @@ -676,11 +687,12 @@ frame headers decoded as they arrive a few bytes at a time, which prices a decod comes up `Short` and retries, iterator chains over bytes, a `BTreeMap` of 16, 256, and 4096 keys, and timestamps converted between timescales as moq-net's `Timestamp::convert` does, whose products pass 2^53, then added by its `checked_add` and ordered by its `Ord`, -whose cross products do too. Each runs four ways: natively, as wasm32 at moq-wasm's +whose cross products do too, and integers sorted, searched for their greatest and least, +and kept in a `BinaryHeap`. Each runs four ways: natively, as wasm32 at moq-wasm's size-optimized profile, hand-written (moq's `js/net` in `bench/moq.ts` and its `Timestamp` in `bench/timestamp.ts`, ES iterator helpers in `bench/iter.ts`, a sorted array in -`bench/sorted.ts`, and `bigint` in `bench/rescale.ts`, as moq's `js/hang` converts -timescales), and translated; every route's output must match +`bench/sorted.ts`, `bigint` in `bench/rescale.ts`, as moq's `js/hang` converts +timescales, and typed arrays and a heap of its own in `bench/order.ts`), and translated; every route's output must match native Rust's first. The JS routes run minified, the same bundles whose size it reports, in Node, Bun, Chrome, and Firefox, with the WASM route's copies across the boundary timed apart from its compute, and an A/A copy of the diff --git a/ts/bench/moq.ts b/ts/bench/moq.ts index 0e8d3caac..b677dc48b 100644 --- a/ts/bench/moq.ts +++ b/ts/bench/moq.ts @@ -13,6 +13,7 @@ import { Cursor, Reader, type StreamVersion, Writer } from "moq/js/net/src/strea import * as Varint from "moq/js/net/src/varint.ts"; import { iterate } from "./iter.ts"; +import { order } from "./order.ts"; import { rescale } from "./rescale.ts"; import { ordered } from "./sorted.ts"; import { interleave, stamp } from "./timestamp.ts"; @@ -59,6 +60,8 @@ export function run(input: Uint8Array): Uint8Array | Promise { return stamp(data); case 11: return interleave(data); + case 12: + return order(selector, data); default: throw new Error(`unknown workload ${op}`); } diff --git a/ts/bench/order.ts b/ts/bench/order.ts new file mode 100644 index 000000000..e5e5a397c --- /dev/null +++ b/ts/bench/order.ts @@ -0,0 +1,61 @@ +// The `order/` workloads' hand-written route: `bench.rs`'s op 12, as TypeScript orders +// numbers without a library: a typed array's `sort`, a stable `sort` by key, a loop for +// the greatest and least, and a binary heap of its own, as JS has none. + +/** Run op 12, as `bench::run` does, for the selector and the count `data` starts with. */ +export function order(selector: number, data: Uint8Array): Uint8Array { + const padded = new Uint8Array(8); + padded.set(data.subarray(0, 8)); + const count = Math.min(Math.max(Number(new DataView(padded.buffer).getBigUint64(0)), 1), 1 << 16); + let v = Uint32Array.from({ length: count }, (_, i) => (i * 40503) % (1 << 16)); + switch (selector) { + case 0: + v.sort(); + break; + case 1: + v = Uint32Array.from(Array.from(v).sort((a, b) => (a % 1024) - (b % 1024))); + break; + case 2: + case 3: { + let hi = 0; + let lo = 0xffffffff; + for (const x of v) { + if (x > hi) hi = x; + if (x < lo) lo = x; + } + v = Uint32Array.of(hi, lo); + break; + } + default: { + const heap: number[] = []; + for (const x of v) { + let i = heap.push(x) - 1; + while (i > 0 && heap[(i - 1) >> 1] < x) { + heap[i] = heap[(i - 1) >> 1]; + i = (i - 1) >> 1; + } + heap[i] = x; + } + for (let n = 0; n < count; n++) { + v[n] = heap[0]; + const x = heap.pop() as number; + const len = heap.length; + if (len === 0) break; + let i = 0; + for (;;) { + let child = 2 * i + 1; + if (child >= len) break; + if (child + 1 < len && heap[child + 1] > heap[child]) child++; + if (heap[child] <= x) break; + heap[i] = heap[child]; + i = child; + } + heap[i] = x; + } + } + } + const out = new Uint8Array(24); + const view = new DataView(out.buffer); + [v[0], v[v.length >> 1], v[v.length - 1]].forEach((x, i) => view.setBigUint64(8 * i, BigInt(x))); + return out; +} diff --git a/ts/bench/workloads.ts b/ts/bench/workloads.ts index e2e0cf574..267bfc46a 100644 --- a/ts/bench/workloads.ts +++ b/ts/bench/workloads.ts @@ -5,8 +5,8 @@ export interface Workload { /** What is coded, such as `varint/quic/2B` or `frames/video/lite-07`, iterated, or converted. */ name: string; - op: "encode" | "decode" | "iterate" | "map" | "convert" | "add" | "compare" | "handoff"; - /** Varints, frame headers, messages, items, keys, timestamps, or handoffs per call, which each time is divided by. */ + op: "encode" | "decode" | "iterate" | "map" | "convert" | "add" | "compare" | "order" | "handoff"; + /** Varints, frame headers, messages, items, keys, timestamps, integers, or handoffs per call, which each time is divided by. */ items: number; input: Uint8Array; } @@ -93,6 +93,12 @@ export function workloads(encode: (input: Uint8Array) => Uint8Array): Workload[] const time = (op: number, params: bigint[]) => Uint8Array.of(op, 0, ...[BigInt(count), ...params].flatMap(u64)); out.push({ name: "time/checked-add/micros", op: "add", items: count, input: time(10, video) }); out.push({ name: "time/ord/micros-48k", op: "compare", items: count, input: time(11, [...video, ...audio]) }); + + // `bench.rs`'s op 12: integers ordered by std's own `Ord`, which the translation tests. + const orders = ["sort", "sort-by-key", "iter-max-min", "ord-max-min", "binary-heap"]; + orders.forEach((name, selector) => { + out.push({ name: `order/${name}`, op: "order", items: count, input: Uint8Array.of(12, selector, ...u64(BigInt(count))) }); + }); return out; } diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index 5fedab81f..1c3a77c7c 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -43,6 +43,8 @@ export function run(input: rt.ByteSlice): rt.ByteVec { stamp(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); } else if (op === 11) { interleave(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); + } else if (op === 12) { + order(selector, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); } else { decoded(decodeGroup(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), liteVersion(selector), out), out); } @@ -302,6 +304,60 @@ export function ordered(data: rt.ByteSlice, out: rt.ByteVec): void { putU64(out, sum); } +/** + * `count` integers sorted, sorted by a key, searched for the greatest and least, or + * pushed onto a `BinaryHeap` and popped off, as `selector` picks, each by `u32`'s own + * `Ord`: the first, middle, and last of the order, or the greatest and least. + */ +export function order(selector: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let v: rt.u32[] = rt.iterToArray(rt.iterMap(new rt.Range(0, rt.u64Low(rt.u64Clamp(takeU64(data)[0], 1, 65536))), (i: rt.u32): rt.u32 => { + return rt.u32Rem(rt.u32Mul(i, 40503), 1 << 16); + })); + if (selector === 0) { + rt.sliceSort(v, rt.lt); + } else if (selector === 1) { + rt.sliceSortByKey(v, (_210: rt.u32): rt.u32 => { + return rt.u32Rem(_210, 1024); + }, rt.lt); + } else if (selector === 2) { + v = [rt.optionUnwrap(rt.iterMax(rt.iterSlice(v), rt.lt)), rt.optionUnwrap(rt.iterMin(rt.iterSlice(v), rt.lt))]; + } else if (selector === 3) { + const _35: [rt.u32, rt.u32] = rt.iterFold(rt.iterSlice(v), [0, 4294967295] as [rt.u32, rt.u32], (_2: [rt.u32, rt.u32], _33: rt.u32): [rt.u32, rt.u32] => { + const hi2: rt.u32 = _2[0]; + const lo2: rt.u32 = _2[1]; + const x4: rt.u32 = _33; + return [rt.max(hi2, x4), rt.min(lo2, x4)] as [rt.u32, rt.u32]; + }); + v = [_35[0], _35[1]]; + } else { + const heap: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; + for (const _47 of rt.replaceIn(v, [])) { + rt.heapPush(heap, _47, rt.le); + } + while (true) { + const _53: rt.u32 | undefined = rt.heapPop(heap, rt.le); + if (_53 !== undefined) { + v.push(_53); + } else { + break; + } + } + } + const _63: rt.u32[] = v; + const _61: rt.u32 = _63[rt.idx(_63, 0)]; + const _66: rt.u32[] = v; + const _64: rt.u32 = _66[rt.idx(_66, rt.usizeDiv(v.length, 2))]; + const _73: rt.u32[] = v; + const iter2: rt.Iter = rt.iterSlice([_61, _64, _73[rt.idx(_73, rt.usizeSub(v.length, 1))]]); + while (true) { + const _79: rt.u32 | rt.Done = iter2.next(); + if (_79 === rt.DONE) { + break; + } + putU64(out, _79 as rt.u64); + } +} + /** * One iterator chain over every byte, picked by `chain`, with closures that cost little * next to the iterator protocol they run through. diff --git a/ts/fixtures/codec/src/bench.rs b/ts/fixtures/codec/src/bench.rs index bedc2d972..ea6a65ca8 100644 --- a/ts/fixtures/codec/src/bench.rs +++ b/ts/fixtures/codec/src/bench.rs @@ -4,10 +4,11 @@ //! decode's input. Op 6 runs iterator chains over its bytes instead, as moq-net's model //! writes them, op 7 decodes frame headers as they arrive a few bytes at a time, op 8 //! keeps a `BTreeMap`, as moq-net's model keeps its state, op 9 converts timestamps -//! between timescales, and ops 10 and 11 add and order them as moq-net's `Timestamp` does. +//! between timescales, ops 10 and 11 add and order them as moq-net's `Timestamp` does, and +//! op 12 orders integers by std's own `Ord`. use std::cmp::Ordering; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BinaryHeap}; use crate::coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, Form, varint}; use crate::harness::{decode_error, encode_error, form, lite_version, put_u64, put_usize, take_u64}; @@ -35,6 +36,7 @@ pub fn run(input: &[u8]) -> Vec { 9 => rescale(selector, data, &mut out), 10 => stamp(data, &mut out), 11 => interleave(data, &mut out), + 12 => order(selector, data, &mut out), _ => decoded(decode_group(data, lite_version(selector), &mut out), &mut out), } out @@ -207,6 +209,36 @@ fn ordered(data: &[u8], out: &mut Vec) { put_u64(out, sum); } +/// `count` integers sorted, sorted by a key, searched for the greatest and least, or +/// pushed onto a `BinaryHeap` and popped off, as `selector` picks, each by `u32`'s own +/// `Ord`: the first, middle, and last of the order, or the greatest and least. +fn order(selector: u8, data: &[u8], out: &mut Vec) { + let (count, _) = take_u64(data); + let count = count.clamp(1, 1 << 16) as u32; + let mut v: Vec = (0..count).map(|i| (i * 40503) % (1 << 16)).collect(); + match selector { + 0 => v.sort(), + 1 => v.sort_by_key(|&x| x % 1024), + 2 => v = vec![*v.iter().max().unwrap(), *v.iter().min().unwrap()], + 3 => { + let (hi, lo) = v.iter().fold((0, u32::MAX), |(hi, lo), &x| (hi.max(x), lo.min(x))); + v = vec![hi, lo]; + } + _ => { + let mut heap = BinaryHeap::new(); + for x in std::mem::take(&mut v) { + heap.push(x); + } + while let Some(x) = heap.pop() { + v.push(x); + } + } + } + for x in [v[0], v[v.len() / 2], v[v.len() - 1]] { + put_u64(out, x as u64); + } +} + /// One iterator chain over every byte, picked by `chain`, with closures that cost little /// next to the iterator protocol they run through. fn iterate(chain: u8, data: &[u8], out: &mut Vec) { diff --git a/ts/fixtures/lang/expected/collections.ts b/ts/fixtures/lang/expected/collections.ts index eeb182fc1..8c53c49ce 100644 --- a/ts/fixtures/lang/expected/collections.ts +++ b/ts/fixtures/lang/expected/collections.ts @@ -653,7 +653,7 @@ export function btreeMaps(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi rt.byteVecPush(out, mapOr$Option_u8_u8_u8_btree_maps_closure0(rt.iterToOption(rt.btreeRange(map, { start: 4 }).nextBack()), 255, (_230: [rt.u8, rt.u8]): rt.u8 => { return _230[0] ^ _230[1]; })); - rt.byteVecPush(out, mapOr$Option_u8_u8_btree_maps_closure5(rt.iterMax(rt.btreeValues(map), rt.cmpLt(rt.cmp)), 255, (v6: rt.u8): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_u8_u8_btree_maps_closure5(rt.iterMax(rt.btreeValues(map), rt.lt), 255, (v6: rt.u8): rt.u8 => { return v6; })); rt.btreeRemove(map, 3); @@ -1231,7 +1231,7 @@ export function sorted$OptionMap_Option_String_HashMap_u8_Option_String(self: rt })); rt.sliceSortByKey(all, (_22: [rt.u8, string | undefined]): rt.u8 => { return _22[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); return all; } @@ -1241,7 +1241,7 @@ export function sorted$OptionMap_Option_u8_HashMap_u8_Option_u8(self: rt.HashMap })); rt.sliceSortByKey(all, (_22: [rt.u8, rt.u8 | undefined]): rt.u8 => { return _22[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); return all; } @@ -1253,7 +1253,7 @@ export function sorted$OptionMap_unit_HashMap_u8_unit(self: rt.HashMap { return _22[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); return all; } diff --git a/ts/fixtures/lang/expected/core/cmp.ts b/ts/fixtures/lang/expected/core/cmp.ts new file mode 100644 index 000000000..fdaca3a8e --- /dev/null +++ b/ts/fixtures/lang/expected/core/cmp.ts @@ -0,0 +1,142 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "../rt.ts"; +import * as Item from "../orders.Item.ts"; + +export function min$Item(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMin(v1, v2, Item.lt$PartialOrd); +} + +export function min$Item$2(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMin(v1, v2, Item.lt$PartialOrd, rt.drop); +} + +export function min$String(v1: string, v2: string): string { + return rt.ordMin(v1, v2, rt.cmpLt(rt.cmpStr)); +} + +export function min$u8(v1: rt.u8, v2: rt.u8): rt.u8 { + return rt.min(v1, v2); +} + +export function minBy$Item_cmps_closure2(v1: Item.Item, v2: Item.Item, compare: (a0: Item.Item, a1: Item.Item) => rt.Ordering): Item.Item { + let _0!: Item.Item; + let _10: boolean = false; + let _9: boolean = false; + _10 = true; + _9 = true; + if (!(compare(v1, v2) <= 0)) { + _9 = false; + _0 = v2; + } else { + _10 = false; + _0 = v1; + } + if (_9) { + v2[Symbol.dispose](); + } + if (_10) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +export function minByKey$Item_cmps_closure4_Reverse_u8(v1: Item.Item, v2: Item.Item, f: (a0: Item.Item) => rt.u8): Item.Item { + let _0!: Item.Item; + let _16: boolean = false; + let _15: boolean = false; + _16 = true; + _15 = true; + const _6: rt.u8 = f(v2); + const _11: rt.u8 = f(v1); + if (!(rt.cmpReverse(rt.cmp)(_6, _11) === -1)) { + _16 = false; + _0 = v1; + } else { + _15 = false; + _0 = v2; + } + if (_15) { + v2[Symbol.dispose](); + } + if (_16) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +export function max$Item(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMax(v1, v2, Item.lt$PartialOrd); +} + +export function max$Item$2(v1: Item.Item, v2: Item.Item): Item.Item { + return rt.ordMax(v1, v2, Item.lt$PartialOrd, rt.drop); +} + +export function max$String(v1: string, v2: string): string { + return rt.ordMax(v1, v2, rt.cmpLt(rt.cmpStr)); +} + +export function max$i8(v1: rt.i8, v2: rt.i8): rt.i8 { + return rt.max(v1, v2); +} + +export function max$u8(v1: rt.u8, v2: rt.u8): rt.u8 { + return rt.max(v1, v2); +} + +export function maxBy$Item_cmps_closure1(v1: Item.Item, v2: Item.Item, compare: (a0: Item.Item, a1: Item.Item) => rt.Ordering): Item.Item { + let _0!: Item.Item; + let _10: boolean = false; + let _9: boolean = false; + _10 = true; + _9 = true; + if (!(compare(v1, v2) > 0)) { + _9 = false; + _0 = v2; + } else { + _10 = false; + _0 = v1; + } + if (_9) { + v2[Symbol.dispose](); + } + if (_10) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +export function maxByKey$Item_cmps_closure3_Item(v1: Item.Item, v2: Item.Item, f: (a0: Item.Item) => Item.Item): Item.Item { + let _0!: Item.Item; + let _16: boolean = false; + let _15: boolean = false; + _16 = true; + _15 = true; + const _6: Item.Item = f(v2); + const _11: Item.Item = f(v1); + if (!Item.lt$PartialOrd(_6, _11)) { + _11[Symbol.dispose](); + _6[Symbol.dispose](); + _15 = false; + _0 = v2; + } else { + _11[Symbol.dispose](); + _6[Symbol.dispose](); + _16 = false; + _0 = v1; + } + if (_15) { + v2[Symbol.dispose](); + } + if (_16) { + v1[Symbol.dispose](); + return _0; + } + return _0; +} + +//# sourceMappingURL=cmp.ts.map diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 4087ec82a..c8f418edb 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -19,6 +19,7 @@ import type * as Hop from "../keys.Hop.ts"; import type * as Side from "../keys.Side.ts"; import type * as Way from "../keys.Way.ts"; import type * as locals_Noisy from "../locals.Noisy.ts"; +import type * as Item from "../orders.Item.ts"; import type * as Pair from "../pointers.Pair.ts"; import { from$From_Dir_u8 } from "../shapes.ts"; import type * as Dir from "../shapes.Dir.ts"; @@ -370,6 +371,13 @@ export function mapOr$Option_Hop_u64_options_closure2(self: Hop.Hop | undefined, return f(self); } +export function mapOr$Option_Item_u8_heaps_closure1(self: Item.Item | undefined, default_: rt.u8, f: (a0: Item.Item) => rt.u8): rt.u8 { + if (self === undefined) { + return default_; + } + return f(self); +} + export function mapOr$Option_Meters_u64_copies_closure0(self: Meters.Meters | undefined, default_: rt.u64, f: (a0: Meters.Meters) => rt.u64): rt.u64 { if (self === undefined) { return default_; @@ -650,7 +658,7 @@ export function mapOr$Option_usize_u8_consumers_closure8(self: rt.usize | undefi return f(self); } -export function mapOr$Option_usize_u8_u8_ranges_closure0(self: [rt.usize, rt.u8] | undefined, default_: rt.u8, f: (a0: [rt.usize, rt.u8]) => rt.u8): rt.u8 { +export function mapOr$Option_usize_u8_u8_heaps_closure0(self: [rt.usize, rt.u8] | undefined, default_: rt.u8, f: (a0: [rt.usize, rt.u8]) => rt.u8): rt.u8 { if (self === undefined) { return default_; } diff --git a/ts/fixtures/lang/expected/edits.ts b/ts/fixtures/lang/expected/edits.ts index 22af1c43a..f2dcb0fd9 100644 --- a/ts/fixtures/lang/expected/edits.ts +++ b/ts/fixtures/lang/expected/edits.ts @@ -120,7 +120,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, names2.length & 0xff); } else if (_8 === 2) { const v3: rt.u16[] = words.slice(); - rt.sliceSort(v3, rt.cmpLt(rt.cmp)); + rt.sliceSort(v3, rt.lt); putU16s(out, v3); const v4: rt.u16[] = words.slice(); rt.sliceSortBy(v4, (a: rt.u16, b6: rt.u16): rt.Ordering => { @@ -132,7 +132,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { })); rt.sliceSortByKey(pairs2, (p2: [rt.u16, rt.u16]): rt.u16 => { return p2[0]; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); for (const _159 of pairs2) { putU16s(out, [_159[0], _159[1]]); } @@ -493,12 +493,12 @@ export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const v11: rt.ByteVec = rt.byteSliceToByteVec(_101); rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(v11)), (b3: rt.u8): rt.u8 => { return rt.u8Rem(b3, 5); - }, rt.cmpLt(rt.cmp)); + }, rt.lt); rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v11)); const v12: rt.ByteVec = rt.byteSliceToByteVec(_101); rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(v12)), (b4: rt.u8): rt.u8 => { return rt.u8WrappingMul(b4, 13); - }, rt.cmpLt(rt.cmp)); + }, rt.lt); rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v12)); const v13: rt.ByteVec = rt.byteSliceToByteVec(_101); rt.sliceSortBy(rt.byteSliceView(rt.byteVecSlice(v13)), (a2: rt.u8, b5: rt.u8): rt.Ordering => { diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 4c0ec0dc5..87f6e9468 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -28,6 +28,7 @@ import { locals } from "./locals.ts"; import { locks, relock, rwlocks } from "./locks.ts"; import { nested } from "./nested.ts"; import { objects } from "./objects.ts"; +import { orders } from "./orders.ts"; import { lists, shared as pointers_shared, places, unchecked, nested as pointers_nested, copies, sentinels } from "./pointers.ts"; import { refs, toRefs } from "./refs.ts"; import { copies as shapes_copies, order, discriminants, loops, patterns, closures, library, flows } from "./shapes.ts"; @@ -42,109 +43,109 @@ import { wrappers } from "./wrappers.ts"; * case added at the end moves no other. */ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void][] { - return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_288: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { + return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_291: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { convert(data, out); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_289: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_292: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { shapes_copies(data2, out2); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_290: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_293: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { loops(data3, out3); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_291: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_294: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { patterns(data4, out4); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_292: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_295: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { closures(data5, out5); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_293: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_296: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { discriminants(data6, out6); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_294: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_297: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { library(data7, out7); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_295: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_298: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { strings(data8, out8); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_296: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_299: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { derived(data9, out9); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_297: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_2100: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { written(data10, out10); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_298: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_2101: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { methods(data11, out11); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_299: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_2102: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { ranges(data12, out12); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_2100: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_2103: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { options(data13, out13); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_2101: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_2104: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { own(data14, out14); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_2102: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_2105: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { values(data15, out15); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_2103: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_2106: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { order(data16, out16); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_2104: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_2107: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { deques(data17, out17); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_2105: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_2108: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { flows(data18, out18); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_2106: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_2109: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { vecs(data19, out19); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_2107: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_2110: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { stores(data20, out20); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_2108: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_2111: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { refs(data21, out21); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2109: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2112: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2110: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2113: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2111: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2114: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_2112: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_2115: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { shared(data24, out25); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_2113: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_2116: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { locks(data25, out26); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_2114: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_2117: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { relock(data26, out27); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2115: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2118: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { locals(data27, out28); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["statics::statics", (_2116: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["statics::statics", (_2119: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { statics(data28, out29); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_2117: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_2120: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { rwlocks(data29, out30); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_2118: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_2121: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { borrows(data30, out31); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2119: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2122: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { defaults(data31, out32); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2120: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2123: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { arcs(data32, out33); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2121: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2124: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { rcs(data33, out34); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2122: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2125: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { cyclicArcs(data34, out35); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2123: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2126: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { cyclicRcs(data35, out36); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::unique_arcs", (_2124: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::unique_arcs", (_2127: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { uniqueArcs(data36, out37); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::unique_rcs", (_2125: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::unique_rcs", (_2128: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { uniqueRcs(data37, out38); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2126: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2129: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { anys(data38, out39); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2127: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2130: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { fns(data39, out40); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2128: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2131: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { copies(data40, out41); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2129: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2132: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { lists(data41, out42); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2130: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2133: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { pointers_nested(data42, out43); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2131: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2134: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { places(data43, out44); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2132: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2135: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { sentinels(data44, out45); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2133: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2136: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { pointers_shared(data45, out46); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2134: rt.u8, data46: rt.ByteSlice, out47: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2137: rt.u8, data46: rt.ByteSlice, out47: rt.ByteVec): void => { unchecked(data46, out47); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2135: rt.u8, data47: rt.ByteSlice, out48: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2138: rt.u8, data47: rt.ByteSlice, out48: rt.ByteVec): void => { fromFns(data47, out48); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2136: rt.u8, data48: rt.ByteSlice, out49: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2139: rt.u8, data48: rt.ByteSlice, out49: rt.ByteVec): void => { objects(data48, out49); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["holds::holds", holds] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::wrapped", (_2137: rt.u8, data49: rt.ByteSlice, out50: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["holds::holds", holds] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::wrapped", (_2140: rt.u8, data49: rt.ByteSlice, out50: rt.ByteVec): void => { wrapped(data49, out50); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::to_refs", (_2138: rt.u8, data50: rt.ByteSlice, out51: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::to_refs", (_2141: rt.u8, data50: rt.ByteSlice, out51: rt.ByteVec): void => { toRefs(data50, out51); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["orders::orders", orders] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 05a9c2c5e..3ee3ec3d6 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -3,7 +3,7 @@ import * as rt from "./rt.ts"; import { copy$Bumped, copy$Option_u8_Reverse_Option_u8, copy$Option_usize_u8, copy$u8_u8 } from "./lib.ts"; import { thenSome$bool_u16, thenSome$bool_u8 } from "./core/bool.ts"; -import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_i64, unwrapOr$Option_u8, unwrapOr$Option_usize_u8, map$Option_u8_u8_elements_closure8, mapOr$Option_Tilt_u8_ranked_closure11_closure0, mapOr$Option_Vec_u16_u8_consumers_closure23, mapOr$Option_u8_u8_btree_maps_closure5, mapOr$Option_u8_u8_u8_btree_maps_closure0, mapOr$Option_u8_usize_iters_closure1, mapOr$Option_u8s_u8_skips_closure44, mapOr$Option_usize_u8_consumers_closure8, mapOr$Option_usize_u8_u8_ranges_closure0, filter$Option_u8_combinators_closure0 } from "./core/option.ts"; +import { unwrapOr$Option_NonZero_u64, unwrapOr$Option_i64, unwrapOr$Option_u8, unwrapOr$Option_usize_u8, map$Option_u8_u8_elements_closure8, mapOr$Option_Tilt_u8_ranked_closure11_closure0, mapOr$Option_Vec_u16_u8_consumers_closure23, mapOr$Option_u8_u8_btree_maps_closure5, mapOr$Option_u8_u8_u8_btree_maps_closure0, mapOr$Option_u8_usize_iters_closure1, mapOr$Option_u8s_u8_skips_closure44, mapOr$Option_usize_u8_consumers_closure8, mapOr$Option_usize_u8_u8_heaps_closure0, filter$Option_u8_combinators_closure0 } from "./core/option.ts"; import * as Bumped from "./iters.Bumped.ts"; import * as Countdown from "./iters.Countdown.ts"; import * as Doubled from "./iters.Doubled.ts"; @@ -249,14 +249,14 @@ export function consumers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): voi } rt.byteVecPush(out, back.len() & 0xff); } else if (_5 === 2) { - rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMax(rt.iterSlice(rt.byteSliceView(data)), rt.cmpLt(rt.cmp)), 0)); - rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMin(rt.iterSlice(rt.byteSliceView(data)), rt.cmpLt(rt.cmp)), 255)); + rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMax(rt.iterSlice(rt.byteSliceView(data)), rt.lt), 0)); + rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMin(rt.iterSlice(rt.byteSliceView(data)), rt.lt), 255)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMaxByKey(rt.iterSlice(rt.byteSliceView(data)), (_218: rt.u8): rt.u8 => { return rt.u8Rem(_218, 16); - }, rt.cmpLt(rt.cmp)), 238)); + }, rt.lt), 238)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterMinByKey(rt.iterSlice(rt.byteSliceView(data)), (_219: rt.u8): rt.u8 => { return rt.u8Div(_219, 16); - }, rt.cmpLt(rt.cmp)), 238)); + }, rt.lt), 238)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterLast(rt.iterSlice(rt.byteSliceView(data))), 221)); rt.byteVecPush(out, unwrapOr$Option_u8(rt.iterNth(rt.iterSlice(rt.byteSliceView(data)), 3), 204)); const it: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); @@ -623,7 +623,7 @@ export function skips(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } else { item21 = rt.iterNthBack(it23, n); } - rt.byteVecPush(out, mapOr$Option_usize_u8_u8_ranges_closure0(item21, 238, (_221: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_usize_u8_u8_heaps_closure0(item21, 238, (_221: [rt.usize, rt.u8]): rt.u8 => { return _221[1] ^ (_221[0] & 0xff); })); rt.byteVecPush(out, rest11.len() & 0xff); @@ -636,7 +636,7 @@ export function skips(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } else { item22 = rt.iterNthBack(it24, n); } - rt.byteVecPush(out, mapOr$Option_usize_u8_u8_ranges_closure0(item22, 238, (_223: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_usize_u8_u8_heaps_closure0(item22, 238, (_223: [rt.usize, rt.u8]): rt.u8 => { return _223[1] ^ (_223[0] & 0xff); })); rt.byteVecExtendFromSlice(out, rt.byteVecSlice(log11)); @@ -730,7 +730,7 @@ export function skips(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { */ export function ranked(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _4: (a0: rt.ByteVec, a1: [rt.usize, rt.u8] | undefined) => void = (out2: rt.ByteVec, found2: [rt.usize, rt.u8] | undefined): void => { - rt.byteVecPush(out2, mapOr$Option_usize_u8_u8_ranges_closure0(found2, 255, (_2: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out2, mapOr$Option_usize_u8_u8_heaps_closure0(found2, 255, (_2: [rt.usize, rt.u8]): rt.u8 => { return _2[0] & 0xff; })); }; @@ -910,7 +910,7 @@ export function ranges(data: rt.ByteSlice, out: rt.ByteVec): void { const n: rt.u8 = _149[1]; rt.byteVecPush(out, i3 & 0xff); rt.byteVecPush(out, n); - rt.byteVecPush(out, mapOr$Option_usize_u8_u8_ranges_closure0(rt.iterToOption(counted.next()), 204, (_2: [rt.usize, rt.u8]): rt.u8 => { + rt.byteVecPush(out, mapOr$Option_usize_u8_u8_heaps_closure0(rt.iterToOption(counted.next()), 204, (_2: [rt.usize, rt.u8]): rt.u8 => { return _2[0] & 0xff; })); const stepped: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8Step); @@ -937,8 +937,8 @@ export function ranges(data: rt.ByteSlice, out: rt.ByteVec): void { }), rt.u64Add, 0)))); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(unwrapOr$Option_NonZero_u64(rt.iterMaxByKey(rt.iterRev(new rt.RangeInclusive(edge, rt.u64Add(edge, 2), rt.u64Step)), (_24: rt.u64): rt.u64 => { return _24; - }, rt.cmpLt(rt.cmp)), 0)))); - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i64ToBeBytes(unwrapOr$Option_i64(rt.iterMin(new rt.Range(rt.i64Sub(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Add(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Step), rt.cmpLt(rt.cmp)), 0)))); + }, rt.lt), 0)))); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.i64ToBeBytes(unwrapOr$Option_i64(rt.iterMin(new rt.Range(rt.i64Sub(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Add(rt.i64Neg({ hi: 0x200000, lo: 0 } as rt.i64), 1), rt.i64Step), rt.lt), 0)))); for (const _295 of rt.iterToArray(rt.iterRev(rt.iterStepBy(new rt.Range(0, 48), 8)))) { rt.byteVecPush(out, _295 & 0xff); } diff --git a/ts/fixtures/lang/expected/orders.Item.ts b/ts/fixtures/lang/expected/orders.Item.ts new file mode 100644 index 000000000..b3a7386e8 --- /dev/null +++ b/ts/fixtures/lang/expected/orders.Item.ts @@ -0,0 +1,43 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { log } from "./orders.ts"; + +/** + * Ordered by `key` alone, so `tag` tells equal ones apart. Each comparison logs which it + * is and the two tags, and a drop logs the tag. + */ +export class Item { + key: rt.u8; + tag: rt.u8; + + constructor(key: rt.u8, tag: rt.u8) { + this.key = key; + this.tag = tag; + } + + [Symbol.dispose](): void { + drop$Drop(this); + } +} + +export function drop$Drop(self: Item): void { + log(Uint8Array.of(100, self.tag, 0)); +} + +export function lt$PartialOrd(self: Item, other: Item): boolean { + log(Uint8Array.of(60, self.tag, other.tag)); + return self.key < other.key; +} + +export function le$PartialOrd(self: Item, other: Item): boolean { + log(Uint8Array.of(108, self.tag, other.tag)); + return self.key <= other.key; +} + +export function ge$PartialOrd(self: Item, other: Item): boolean { + log(Uint8Array.of(103, self.tag, other.tag)); + return self.key >= other.key; +} + +//# sourceMappingURL=orders.Item.ts.map diff --git a/ts/fixtures/lang/expected/orders.ts b/ts/fixtures/lang/expected/orders.ts new file mode 100644 index 000000000..6ce39a64c --- /dev/null +++ b/ts/fixtures/lang/expected/orders.ts @@ -0,0 +1,442 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { copy$u8_u8 } from "./lib.ts"; +import { min$Item, min$Item$2, min$String, min$u8, minBy$Item_cmps_closure2, minByKey$Item_cmps_closure4_Reverse_u8, max$Item, max$Item$2, max$String, max$i8, max$u8, maxBy$Item_cmps_closure1, maxByKey$Item_cmps_closure3_Item } from "./core/cmp.ts"; +import { mapOr$Option_Item_u8_heaps_closure1, mapOr$Option_usize_u8_u8_heaps_closure0 } from "./core/option.ts"; +import * as Item from "./orders.Item.ts"; + +export const LOG: rt.LocalKey> = /* @__PURE__ */ new rt.LocalKey(LOG$init, false); + +export function LOG$init(): rt.RefCell { + return new rt.RefCell(new rt.ByteVec()); +} + +export function log(entry: Uint8Array): void { + const _5: rt.ByteSlice = rt.byteSliceFrom(entry); + rt.localKeyWithBorrowMut(LOG, (log_: rt.ByteVec): void => { + rt.iterExtendBytes(log_, rt.iterSlice(rt.byteSliceToArray(_5))); + }); +} + +/** Puts what was logged since the last flush. */ +export function flush(out: rt.ByteVec): void { + rt.localKeyWithBorrowMut(LOG, (log_: rt.ByteVec): void => { + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(log_)); + rt.byteVecClear(log_); + }); +} + +export function item(tag: rt.usize, key: rt.u8): Item.Item { + return new Item.Item(rt.u8Rem(key, 8), tag & 0xff); +} + +/** Run one case. */ +export function orders(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const _5: rt.u8 = rt.u8Rem(kind, 4); + if (_5 === 0) { + cmps(data, out); + } else if (_5 === 1) { + sorted(data, out); + } else if (_5 === 2) { + heaps(data, out); + } else { + values(data, out); + } + flush(out); +} + +/** + * `cmp::max` and `min` and their forms of each byte and the next, as integers, strings, + * and items, which the one not returned drops, and of references to items. + */ +export function cmps(data: rt.ByteSlice, out: rt.ByteVec): void { + let _7!: rt.usize; + let iterStart: rt.usize = 1; + const iterEnd: rt.usize = data.length; + while ((_7 = iterStart++) < iterEnd) { + const i: rt.usize = _7; + const _15: rt.usize = rt.usizeSub(i, 1); + rt.idx(data, _15); + const _14: rt.u8 = data.data[data.start + _15]; + rt.idx(data, i); + const _13: [rt.u8, rt.u8] = [_14, data.data[data.start + i]]; + const a: rt.u8 = _14; + const b: rt.u8 = _13[1]; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(max$u8(a, b), min$u8(a, b), max$i8((a << 24) >> 24, (b << 24) >> 24) & 0xff))); + const _37: rt.u8 = rt.u8Div(b, 2); + const _32: [string, string] = [rt.fmtToString((f: rt.Formatter) => rt.u8Display(a, f)), rt.fmtToString((f: rt.Formatter) => rt.u8Display(_37, f))]; + const s: string = _32[0]; + const t: string = _32[1]; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.stringLen(max$String(s, t)) & 0xff, rt.stringLen(min$String(s, t)) & 0xff))); + const _55: [Item.Item, Item.Item] = [item(rt.usizeMul(2, i), a), item(rt.usizeAdd(rt.usizeMul(2, i), 1), b)]; + const x: Item.Item = _55[0]; + const y: Item.Item = _55[1]; + rt.byteVecPush(out, max$Item(x, y).tag); + rt.byteVecPush(out, min$Item(x, y).tag); + const _76: Item.Item = max$Item$2(x, y); + rt.byteVecPush(out, _76.tag); + _76[Symbol.dispose](); + const pair: () => [Item.Item, Item.Item] = (): [Item.Item, Item.Item] => { + return [item(rt.usizeMul(2, i), a), item(rt.usizeAdd(rt.usizeMul(2, i), 1), b)] as [Item.Item, Item.Item]; + }; + const _83: [Item.Item, Item.Item] = pair(); + const _87: Item.Item = min$Item$2(_83[0], _83[1]); + rt.byteVecPush(out, _87.tag); + _87[Symbol.dispose](); + const _90: [Item.Item, Item.Item] = pair(); + const _94: Item.Item = maxBy$Item_cmps_closure1(_90[0], _90[1], (l: Item.Item, r: Item.Item): rt.Ordering => { + return rt.orderingReverse(rt.cmp(l.key, r.key)); + }); + rt.byteVecPush(out, _94.tag); + _94[Symbol.dispose](); + const _97: [Item.Item, Item.Item] = pair(); + const _101: Item.Item = minBy$Item_cmps_closure2(_97[0], _97[1], (l2: Item.Item, r2: Item.Item): rt.Ordering => { + return rt.cmp(rt.u8Div(l2.key, 2), rt.u8Div(r2.key, 2)); + }); + rt.byteVecPush(out, _101.tag); + _101[Symbol.dispose](); + const _104: [Item.Item, Item.Item] = pair(); + const _108: Item.Item = maxByKey$Item_cmps_closure3_Item(_104[0], _104[1], (v: Item.Item): Item.Item => { + return item(rt.usizeAdd(v.tag as rt.usize, 100), rt.u8Div(v.key, 2)); + }); + rt.byteVecPush(out, _108.tag); + _108[Symbol.dispose](); + const _111: [Item.Item, Item.Item] = pair(); + const _115: Item.Item = minByKey$Item_cmps_closure4_Reverse_u8(_111[0], _111[1], (v2: Item.Item): rt.u8 => { + return v2.key; + }); + rt.byteVecPush(out, _115.tag); + _115[Symbol.dispose](); + flush(out); + } +} + +/** + * The `is_sorted` family of slices and iterators of items, bytes, and strings, as given + * and sorted: each test std makes, and each item or key it drops. + */ +export function sorted(data: rt.ByteSlice, out: rt.ByteVec): void { + const _77: () => Item.Item = (): Item.Item => { + return item(200, 0); + }; + const ascending: rt.ByteVec = rt.byteSliceToByteVec(data); + rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(ascending)), (b: rt.u8): rt.u8 => { + return rt.u8Rem(b, 8); + }, rt.lt); + const iter: rt.Iter = rt.iterSlice([data, rt.byteVecSlice(ascending)]); + while (true) { + const _12: rt.ByteSlice | rt.Done = iter.next(); + if (_12 === rt.DONE) { + break; + } + const items: () => rt.Iter = (): rt.Iter => { + return rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(_12))), (_24: [rt.usize, rt.u8]): Item.Item => { + return item(_24[0], _24[1]); + }); + }; + const v: Item.Item[] = rt.iterToArray(items()); + rt.byteVecPush(out, rt.sliceIsSorted(v, Item.le$PartialOrd) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(v), (a: Item.Item, b3: Item.Item): boolean => { + return a.key >= b3.key; + }) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(v), (x: Item.Item): Item.Item => { + return item(rt.usizeAdd(x.tag as rt.usize, 100), rt.u8Div(x.key, 2)); + }), Item.le$PartialOrd, undefined, rt.drop) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(v), Item.le$PartialOrd) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.sliceRange(v, 0, rt.usizeDiv(v.length, 2))), (x2: Item.Item): rt.u8 => { + return x2.key; + }), rt.le) ? 1 : 0); + rt.dropAll(v); + flush(out); + rt.byteVecPush(out, rt.iterIsSortedBy(items(), Item.le$PartialOrd, undefined, rt.drop) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(items(), (a2: Item.Item, b4: Item.Item): boolean => { + if (!(a2.tag > b4.tag)) { + return a2.key <= b4.key; + } + return true; + }, undefined, rt.drop) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(items(), (x3: Item.Item): rt.u8 => { + const _09: rt.u8 = x3.key; + x3[Symbol.dispose](); + return _09; + }), rt.cmpLe(rt.cmpReverse(rt.cmp))) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterSlice(rt.iterToArray(items()), rt.drop), Item.le$PartialOrd, true, rt.drop) ? 1 : 0); + flush(out); + const _82: [Item.Item, Item.Item, Item.Item, Item.Item] = [_77(), _77(), _77(), _77()]; + const g: Item.Item = _82[0]; + const h: Item.Item = _82[1]; + const k: Item.Item = _82[2]; + const l: Item.Item = _82[3]; + const _94: rt.Iter = items(); + const captured = g; + rt.byteVecPush(out, rt.iterIsSortedBy(_94, rt.closure((a3: Item.Item, b5: Item.Item): boolean => { + if (!(captured.key <= a3.key)) { + return false; + } + return a3.key <= b5.key; + }, (): void => { + captured[Symbol.dispose](); + }), undefined, rt.drop, rt.drop) ? 1 : 0); + const _103: rt.Iter = rt.iterSlice(rt.iterToArray(items()), rt.drop); + const captured2 = h; + rt.byteVecPush(out, rt.iterIsSortedBy(_103, rt.closure((a4: Item.Item, b6: Item.Item): boolean => { + return a4.key <= rt.u8Add(b6.key, captured2.key); + }, (): void => { + captured2[Symbol.dispose](); + }), true, rt.drop, rt.drop) ? 1 : 0); + const _108: rt.Iter = items(); + const captured3 = k; + const _110: (a0: Item.Item) => rt.u8 = rt.closure((x4: Item.Item): rt.u8 => { + const _012: rt.u8 = rt.u8Add(x4.key, captured3.key); + x4[Symbol.dispose](); + return _012; + }, (): void => { + captured3[Symbol.dispose](); + }); + rt.byteVecPush(out, rt.dropAfter(_110, () => rt.iterIsSortedBy(rt.iterMap(_108, _110), rt.le)) ? 1 : 0); + const captured4 = l; + const key: (a0: Item.Item) => rt.u8 = rt.closure((x5: Item.Item): rt.u8 => { + const _013: rt.u8 = rt.u8Add(x5.key, captured4.key); + x5[Symbol.dispose](); + return _013; + }, (): void => { + captured4[Symbol.dispose](); + }); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.iterToArray(items()), rt.drop), rt.lent(key)), rt.le, true) ? 1 : 0); + rt.byteVecPush(out, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.iterToArray(items()), rt.drop), rt.lent(key)), rt.le, true) ? 1 : 0); + flush(out); + const strings: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (b7: rt.u8): string => { + const _415: rt.u8 = rt.u8Rem(b7, 16); + return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_415, f)); + })); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.sliceIsSorted(rt.byteSliceView(_12), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), (a5: rt.u8, b8: rt.u8): boolean => { + return rt.u8Div(a5, 4) <= rt.u8Div(b8, 4); + }) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (_25: rt.u8): rt.u8 => { + return rt.u8Rem(_25, 8); + }), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterSlice(rt.byteSliceView(_12)), rt.le) ? 1 : 0, rt.sliceIsSorted(strings, rt.cmpLe(rt.cmpStr)) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(strings), (a06: string): rt.usize => { + return rt.stringLen(a06); + }), rt.le) ? 1 : 0, rt.iterIsSortedBy(rt.iterMap(rt.iterSlice(rt.byteSliceView(_12)), (_29: rt.u8): rt.f64 => { + return (_29 as rt.f64) / 3.0; + }), rt.cmpLe(rt.f64PartialCmp)) ? 1 : 0))); + rt.drop(key); + } +} + +/** + * A heap of items changed by each pair of bytes, an operation and its operand: each + * comparison its sifts make, which of equal items it gives back, and when each drops. + */ +export function heaps(data: rt.ByteSlice, out: rt.ByteVec): void { + let _127: boolean = false; + let _128: boolean = false; + let heap: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; + const other: rt.BinaryHeap = rt.heapNew() as rt.BinaryHeap; + const ops: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))); + while (true) { + const _8: [rt.usize, rt.u8] | rt.Done = ops.next(); + if (_8 !== rt.DONE) { + const i: rt.usize = _8[0]; + const op: rt.u8 = _8[1]; + const v: rt.u8 = mapOr$Option_usize_u8_u8_heaps_closure0(rt.iterToOption(ops.next()), 0, (_2: [rt.usize, rt.u8]): rt.u8 => { + return _2[1]; + }); + b2: { + const _17: rt.u8 = rt.u8Rem(op, 12); + if (0 <= _17) { + if (_17 <= 2) { + rt.heapPush(heap, item(i, v), Item.le$PartialOrd); + break b2; + } + } + if (_17 === 3) { + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.heapPop(heap, Item.le$PartialOrd), 255, (x5: Item.Item): rt.u8 => { + const _06: rt.u8 = x5.tag; + x5[Symbol.dispose](); + return _06; + })); + } else if (_17 === 4) { + const _26: rt.PeekMut | undefined = rt.heapPeekMut(heap, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + if (_26 !== undefined) { + _127 = true; + const top: rt.PeekMut = _26; + rt.peekMutDerefMut(top); + top.v.key = rt.u8Rem(v, 8); + if ((v & 16) !== 0) { + _127 = false; + } + if (_127) { + rt.peekMutDrop(top); + } + _127 = false; + } + } else if (_17 === 5) { + const _37: rt.PeekMut | undefined = rt.heapPeekMut(heap, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + if (_37 !== undefined) { + _128 = true; + const top2: rt.PeekMut = _37; + const _41: rt.u8 = rt.u8Rem(v, 3); + if (_41 === 0) { + _128 = false; + const _44: Item.Item = rt.peekMutPop(top2); + rt.byteVecPush(out, _44.tag); + _44[Symbol.dispose](); + } else if (_41 === 1) { + rt.byteVecPush(out, top2.v.tag); + } else { + _128 = false; + const top3: rt.PeekMut = top2; + rt.peekMutDerefMut(top3); + const _50: Item.Item = top3.v; + _50.tag = _50.tag ^ 64; + const _54: Item.Item = rt.peekMutPop(top3); + rt.byteVecPush(out, _54.tag); + _54[Symbol.dispose](); + } + if (_128) { + rt.peekMutDrop(top2); + } + _128 = false; + } + } else if (_17 === 6) { + const _57: Item.Item = item(i, v); + rt.heapPush(other, _57, Item.le$PartialOrd); + } else if (_17 === 7) { + rt.heapAppend(heap, other, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + } else if (_17 === 8) { + rt.heapRetain(heap, (x4: Item.Item): boolean => { + return x4.key !== rt.u8Rem(v, 8); + }, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd, rt.drop); + } else if (_17 === 9) { + rt.heapExtend(heap, rt.iterMap(rt.iterTake(rt.iterSlice(rt.byteSliceView(data)), rt.usizeRem(v as rt.usize, 6)), (_28: rt.u8): Item.Item => { + return item(rt.usizeAdd(i, 50), _28); + }), Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + } else if (_17 === 10) { + const sorted_: Item.Item[] = rt.heapIntoSortedVec(rt.replaceIn(heap, rt.heapNew() as rt.BinaryHeap), Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(sorted_), (x3: Item.Item): rt.u8 => { + return x3.tag; + })); + flush(out); + const _81: rt.BinaryHeap = rt.heapFrom(sorted_, Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + rt.heapDrop(heap, rt.drop); + heap = _81; + } else { + rt.iterExtendBytes(out, rt.iterMap(rt.heapIter(heap), (x: Item.Item): rt.u8 => { + return x.tag; + })); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(mapOr$Option_Item_u8_heaps_closure1(heap.d[0], 255, (x2: Item.Item): rt.u8 => { + return x2.tag; + }), heap.d.length & 0xff, heap.d.length === 0 ? 1 : 0))); + } + } + flush(out); + } else { + break; + } + } + const drain: rt.Iter = rt.heapDrain(heap, rt.drop); + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.iterToOption(drain.next()), 255, (x6: Item.Item): rt.u8 => { + const _07: rt.u8 = x6.tag; + x6[Symbol.dispose](); + return _07; + })); + rt.drop(drain); + flush(out); + const collected: rt.BinaryHeap = rt.heapFrom(rt.iterToArray(rt.iterMap(rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))), (_29: [rt.usize, rt.u8]): Item.Item => { + return item(_29[0], _29[1]); + })), Item.le$PartialOrd, Item.ge$PartialOrd, Item.lt$PartialOrd); + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.heapPop(collected, Item.le$PartialOrd), 255, (x7: Item.Item): rt.u8 => { + const _09: rt.u8 = x7.tag; + x7[Symbol.dispose](); + return _09; + })); + const rest: rt.Iter = rt.heapIter(collected, rt.drop); + rt.byteVecPush(out, mapOr$Option_Item_u8_heaps_closure1(rt.iterToOption(rest.next()), 255, (x8: Item.Item): rt.u8 => { + const _010: rt.u8 = x8.tag; + x8[Symbol.dispose](); + return _010; + })); + rt.drop(rest); + flush(out); + rt.heapClear(other, rt.drop); + flush(out); + rt.heapDrop(other, rt.drop); + rt.heapDrop(heap, rt.drop); +} + +/** + * Heaps of values with no comparison of their own: bytes, a min-heap of them, strings, + * and tuples, built every way, and their vectors, slices, clones, and debug form. + */ +export function values(data: rt.ByteSlice, out: rt.ByteVec): void { + const bytes: rt.BinaryHeap = rt.heapFrom(Array.from(rt.byteVecView(rt.byteSliceToByteVec(data))), rt.le, rt.ge, rt.lt); + rt.heapPush(bytes, data.length & 0xff, rt.le); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceView(rt.byteSliceFrom(Uint8Array.from(bytes.d))))); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(rt.byteVecFrom(Uint8Array.from(rt.heapIntoSortedVec(rt.heapClone(bytes), rt.le, rt.ge, rt.lt)))))); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtList(bytes.d, f, rt.u8Debug)]; + rt.iterExtendBytes(out, rt.strBytes(rt.fmtFormat([args2[0]]))); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(rt.byteVecFrom(Uint8Array.from(bytes.d))))); + const least: rt.BinaryHeap = rt.heapFrom(rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): rt.u8 => { + return _2; + })), rt.cmpLe(rt.cmpReverse(rt.cmp)), rt.cmpGe(rt.cmpReverse(rt.cmp)), rt.cmpLt(rt.cmpReverse(rt.cmp))); + const _37: rt.Iter = rt.iterMap(rt.iterTake(rt.iterSlice(rt.byteSliceView(data)), 3), (_23: rt.u8): rt.u8 => { + return rt.u8Div(_23, 2); + }); + rt.heapExtend(least, _37, rt.cmpLe(rt.cmpReverse(rt.cmp)), rt.cmpGe(rt.cmpReverse(rt.cmp)), rt.cmpLt(rt.cmpReverse(rt.cmp))); + while (true) { + const _40: rt.u8 | undefined = rt.heapPop(least, rt.cmpLe(rt.cmpReverse(rt.cmp))); + if (_40 !== undefined) { + rt.byteVecPush(out, _40); + } else { + break; + } + } + const _51: rt.usize = data.length; + const strings: rt.BinaryHeap = rt.heapFrom(rt.iterToArray(rt.iterSlice(["pear", "apple", rt.fmtToString((f: rt.Formatter) => rt.usizeDisplay(_51, f))])), rt.cmpLe(rt.cmpStr), rt.cmpGe(rt.cmpStr), rt.cmpLt(rt.cmpStr)); + const copy: rt.BinaryHeap = rt.heapClone(strings); + const _56: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (b4: rt.u8): string => { + const _43: rt.u8 = rt.u8Rem(b4, 10); + return rt.fmtToString((f: rt.Formatter) => rt.u8Display(_43, f)); + }); + rt.heapExtend(copy, _56, rt.cmpLe(rt.cmpStr), rt.cmpGe(rt.cmpStr), rt.cmpLt(rt.cmpStr)); + rt.heapRetain(copy, (s: string): boolean => { + if (!(rt.stringLen(s) > 1)) { + return rt.cmpStr(s, "5") === -1; + } + return true; + }, rt.cmpLe(rt.cmpStr), rt.cmpGe(rt.cmpStr), rt.cmpLt(rt.cmpStr)); + rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(copy.d), (s2: string): rt.Iter => { + return rt.strBytes(s2); + })); + rt.iterExtendBytes(out, rt.iterMap(rt.heapIter(strings), (s3: string): rt.u8 => { + return rt.stringLen(s3) & 0xff; + })); + const pairs: rt.BinaryHeap<[rt.u8, rt.u8]> = rt.heapNew() as rt.BinaryHeap<[rt.u8, rt.u8]>; + const _73: rt.Iter<[rt.u8, rt.u8]> = rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_24: rt.u8): [rt.u8, rt.u8] => { + const b5: rt.u8 = _24; + return [rt.u8Rem(b5, 4), rt.u8Div(b5, 64)] as [rt.u8, rt.u8]; + }); + rt.heapExtend(pairs, _73, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + const _78: [rt.u8, rt.u8][] = [[rt.u8Rem(data.length & 0xff, 4), 9] as [rt.u8, rt.u8]]; + rt.heapExtend(pairs, rt.iterMap(rt.iterSlice(_78), copy$u8_u8), rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + const _84: rt.PeekMut<[rt.u8, rt.u8]> | undefined = rt.heapPeekMut(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + if (_84 !== undefined) { + const top: rt.PeekMut<[rt.u8, rt.u8]> = _84; + const _88: rt.u8 = rt.u8WrappingAdd(top.v[1], 1); + rt.peekMutDerefMut(top); + top.v[1] = _88; + rt.peekMutDrop(top); + } + const _94: rt.PeekMut<[rt.u8, rt.u8]> | undefined = rt.heapPeekMut(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); + if (_94 !== undefined) { + const top2: rt.PeekMut<[rt.u8, rt.u8]> = _94; + rt.peekMutDerefMut(top2); + Object.assign(top2.v, [0, 0] as [rt.u8, rt.u8]); + rt.peekMutDrop(top2); + } + rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(rt.heapIntoSortedVec(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp])))), (_25: [rt.u8, rt.u8]): Uint8Array => { + return Uint8Array.of(_25[0], _25[1]); + }, (value: Uint8Array) => rt.iterSlice(value))); +} + +//# sourceMappingURL=orders.ts.map diff --git a/ts/fixtures/lang/expected/refs.ts b/ts/fixtures/lang/expected/refs.ts index 0e3dffc7a..088225706 100644 --- a/ts/fixtures/lang/expected/refs.ts +++ b/ts/fixtures/lang/expected/refs.ts @@ -698,7 +698,7 @@ export function elements(data: rt.ByteSlice, out: rt.ByteVec): void { } const most: rt.Place | undefined = rt.iterMaxByKey(rt.iterSliceMut(v), (x7: rt.Place): rt.u32 => { return x7.v; - }, rt.cmpLt(rt.cmp)); + }, rt.lt); if (most !== undefined) { most.v = 0; } diff --git a/ts/fixtures/lang/expected/wrappers.ts b/ts/fixtures/lang/expected/wrappers.ts index 764999c84..81db8add3 100644 --- a/ts/fixtures/lang/expected/wrappers.ts +++ b/ts/fixtures/lang/expected/wrappers.ts @@ -324,7 +324,7 @@ export function wrappers(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void putU64(out, unwrapOr$Option_NonZero_u64(rt.iterLast(rt.iterBorrow(last)), 238)); bounds$RangeInclusive_u64(out, last); const most: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); - putU64(out, unwrapOr$Option_NonZero_u64(rt.iterMax(rt.iterBorrow(most), rt.cmpLt(rt.cmp)), 238)); + putU64(out, unwrapOr$Option_NonZero_u64(rt.iterMax(rt.iterBorrow(most), rt.lt), 238)); bounds$RangeInclusive_u64(out, most); const collected: rt.RangeInclusive = new rt.RangeInclusive(lo, hi, rt.u64Step); rt.byteVecPush(out, rt.iterToArray(rt.iterBorrow(collected)).length & 0xff); diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 07ab04fe3..61de0367b 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -3,8 +3,8 @@ use crate::{ arrays, atomics, borrows, captures, cells, chars, chunks, collections, combinators, counts, cows, defaults, - durations, dyns, edits, floats, fmt, generics, holds, ints, iters, keys, locals, locks, nested, objects, pointers, - refs, shapes, splits, statics, strs, utf8, wrappers, + durations, dyns, edits, floats, fmt, generics, holds, ints, iters, keys, locals, locks, nested, objects, orders, + pointers, refs, shapes, splits, statics, strs, utf8, wrappers, }; /// The native side's entry: [`run`], except that a case starting with 0xff prints a @@ -118,6 +118,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("holds::holds", holds::holds), ("fmt::wrapped", |_, data, out| fmt::wrapped(data, out)), ("refs::to_refs", |_, data, out| refs::to_refs(data, out)), + ("orders::orders", orders::orders), ] } diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index dbcb31708..a82b62cf8 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -40,6 +40,7 @@ mod locals; mod locks; mod nested; mod objects; +mod orders; mod pointers; mod refs; mod shapes; diff --git a/ts/fixtures/lang/src/orders.rs b/ts/fixtures/lang/src/orders.rs new file mode 100644 index 000000000..250b5e90f --- /dev/null +++ b/ts/fixtures/lang/src/orders.rs @@ -0,0 +1,273 @@ +//! Ordering in std: `cmp`'s `max` and `min` and their `_by` and `_by_key` forms, the +//! `is_sorted` family, and `BinaryHeap`, of a type that logs each comparison std makes of +//! it and each drop, so which tests run, in what order, which of equal values comes out, +//! and when each value drops all show. + +use std::cell::RefCell; +use std::cmp::{self, Ordering, Reverse}; +use std::collections::BinaryHeap; +use std::collections::binary_heap::PeekMut; + +thread_local! { + static LOG: RefCell> = const { RefCell::new(Vec::new()) }; +} + +fn log(entry: [u8; 3]) { + LOG.with_borrow_mut(|log| log.extend(entry)); +} + +/// Puts what was logged since the last flush. +fn flush(out: &mut Vec) { + LOG.with_borrow_mut(|log| { + out.extend_from_slice(log); + log.clear(); + }); +} + +/// Ordered by `key` alone, so `tag` tells equal ones apart. Each comparison logs which it +/// is and the two tags, and a drop logs the tag. +struct Item { + key: u8, + tag: u8, +} + +fn item(tag: usize, key: u8) -> Item { + Item { + key: key % 8, + tag: tag as u8, + } +} + +impl Drop for Item { + fn drop(&mut self) { + log([b'd', self.tag, 0]); + } +} + +impl PartialEq for Item { + fn eq(&self, other: &Self) -> bool { + self.key == other.key + } +} + +impl Eq for Item {} + +impl Ord for Item { + fn cmp(&self, other: &Self) -> Ordering { + log([b'c', self.tag, other.tag]); + self.key.cmp(&other.key) + } +} + +impl PartialOrd for Item { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } + + fn lt(&self, other: &Self) -> bool { + log([b'<', self.tag, other.tag]); + self.key < other.key + } + + fn le(&self, other: &Self) -> bool { + log([b'l', self.tag, other.tag]); + self.key <= other.key + } + + fn gt(&self, other: &Self) -> bool { + log([b'>', self.tag, other.tag]); + self.key > other.key + } + + fn ge(&self, other: &Self) -> bool { + log([b'g', self.tag, other.tag]); + self.key >= other.key + } +} + +/// Run one case. +pub fn orders(kind: u8, data: &[u8], out: &mut Vec) { + match kind % 4 { + 0 => cmps(data, out), + 1 => sorted(data, out), + 2 => heaps(data, out), + _ => values(data, out), + } + flush(out); +} + +/// `cmp::max` and `min` and their forms of each byte and the next, as integers, strings, +/// and items, which the one not returned drops, and of references to items. +fn cmps(data: &[u8], out: &mut Vec) { + for i in 1..data.len() { + let (a, b) = (data[i - 1], data[i]); + out.extend([cmp::max(a, b), cmp::min(a, b), cmp::max(a as i8, b as i8) as u8]); + let (s, t) = (a.to_string(), (b / 2).to_string()); + out.extend([cmp::max(s.clone(), t.clone()).len() as u8, cmp::min(s, t).len() as u8]); + let (x, y) = (item(2 * i, a), item(2 * i + 1, b)); + out.push(cmp::max(&x, &y).tag); + out.push(cmp::min(&x, &y).tag); + out.push(cmp::max(x, y).tag); + let pair = || (item(2 * i, a), item(2 * i + 1, b)); + let (x, y) = pair(); + out.push(cmp::min(x, y).tag); + let (x, y) = pair(); + out.push(cmp::max_by(x, y, |l, r| l.key.cmp(&r.key).reverse()).tag); + let (x, y) = pair(); + out.push(cmp::min_by(x, y, |l, r| (l.key / 2).cmp(&(r.key / 2))).tag); + let (x, y) = pair(); + out.push(cmp::max_by_key(x, y, |v| item(v.tag as usize + 100, v.key / 2)).tag); + let (x, y) = pair(); + out.push(cmp::min_by_key(x, y, |v| Reverse(v.key)).tag); + flush(out); + } +} + +/// The `is_sorted` family of slices and iterators of items, bytes, and strings, as given +/// and sorted: each test std makes, and each item or key it drops. +fn sorted(data: &[u8], out: &mut Vec) { + let mut ascending = data.to_vec(); + ascending.sort_by_key(|b| b % 8); + for bytes in [data, &ascending] { + let items = || bytes.iter().enumerate().map(|(i, &b)| item(i, b)); + let v: Vec = items().collect(); + out.push(v.is_sorted() as u8); + out.push(v.is_sorted_by(|a, b| a.key >= b.key) as u8); + out.push(v.is_sorted_by_key(|x| item(x.tag as usize + 100, x.key / 2)) as u8); + out.push(v.iter().is_sorted() as u8); + out.push(v[..v.len() / 2].iter().is_sorted_by_key(|x| x.key) as u8); + drop(v); + flush(out); + out.push(items().is_sorted() as u8); + out.push(items().is_sorted_by(|a, b| a.tag > b.tag || a.key <= b.key) as u8); + out.push(items().is_sorted_by_key(|x| Reverse(x.key)) as u8); + out.push(items().collect::>().into_iter().is_sorted() as u8); + flush(out); + // A function that owns an item drops as std's `all` drops it: before the last item + // and the iterator. + let owned = || item(200, 0); + let (g, h, k, l) = (owned(), owned(), owned(), owned()); + out.push(items().is_sorted_by(move |a, b| (&g).key <= a.key && a.key <= b.key) as u8); + let v = items().collect::>(); + out.push(v.into_iter().is_sorted_by(move |a, b| a.key <= b.key + (&h).key) as u8); + out.push(items().is_sorted_by_key(move |x| x.key + (&k).key) as u8); + let key = move |x: Item| x.key + (&l).key; + out.push(items().collect::>().into_iter().is_sorted_by_key(&key) as u8); + out.push(items().collect::>().into_iter().is_sorted_by_key(Box::new(&key)) as u8); + flush(out); + let strings: Vec = bytes.iter().map(|b| (b % 16).to_string()).collect(); + out.extend([ + bytes.is_sorted() as u8, + bytes.is_sorted_by(|a, b| a / 4 <= b / 4) as u8, + bytes.is_sorted_by_key(|&b| b % 8) as u8, + bytes.iter().is_sorted() as u8, + strings.is_sorted() as u8, + strings.iter().map(String::len).is_sorted() as u8, + bytes.iter().map(|&b| b as f64 / 3.0).is_sorted() as u8, + ]); + } +} + +/// A heap of items changed by each pair of bytes, an operation and its operand: each +/// comparison its sifts make, which of equal items it gives back, and when each drops. +fn heaps(data: &[u8], out: &mut Vec) { + let mut heap = BinaryHeap::new(); + let mut other = BinaryHeap::with_capacity(4); + let mut ops = data.iter().copied().enumerate(); + while let Some((i, op)) = ops.next() { + let v = ops.next().map_or(0, |(_, v)| v); + match op % 12 { + 0..=2 => heap.push(item(i, v)), + 3 => out.push(heap.pop().map_or(0xff, |x| x.tag)), + // `DerefMut` sifts the top once it drops, but of a heap of one, and forgotten, + // leaves the top alone in the heap. + 4 => { + if let Some(mut top) = heap.peek_mut() { + top.key = v % 8; + if v & 0x10 != 0 { + std::mem::forget(top); + } + } + } + // `Deref` alone sifts nothing, and `PeekMut::pop` pops. + 5 => { + if let Some(top) = heap.peek_mut() { + match v % 3 { + 0 => out.push(PeekMut::pop(top).tag), + 1 => out.push(top.tag), + _ => { + let mut top = top; + top.tag ^= 0x40; + out.push(PeekMut::pop(top).tag); + } + } + } + } + 6 => other.push(item(i, v)), + 7 => heap.append(&mut other), + 8 => heap.retain(|x| x.key != v % 8), + 9 => heap.extend(data.iter().take(v as usize % 6).map(|&b| item(i + 50, b))), + 10 => { + let sorted = std::mem::take(&mut heap).into_sorted_vec(); + out.extend(sorted.iter().map(|x| x.tag)); + flush(out); + heap = BinaryHeap::from(sorted); + } + _ => { + out.extend(heap.iter().map(|x| x.tag)); + out.extend([ + heap.peek().map_or(0xff, |x| x.tag), + heap.len() as u8, + heap.is_empty() as u8, + ]); + } + } + flush(out); + } + let mut drain = heap.drain(); + out.push(drain.next().map_or(0xff, |x| x.tag)); + drop(drain); + flush(out); + let mut collected: BinaryHeap = data.iter().enumerate().map(|(i, &b)| item(i, b)).collect(); + out.push(collected.pop().map_or(0xff, |x| x.tag)); + let mut rest = collected.into_iter(); + out.push(rest.next().map_or(0xff, |x| x.tag)); + drop(rest); + flush(out); + other.clear(); + flush(out); +} + +/// Heaps of values with no comparison of their own: bytes, a min-heap of them, strings, +/// and tuples, built every way, and their vectors, slices, clones, and debug form. +fn values(data: &[u8], out: &mut Vec) { + let mut bytes = BinaryHeap::from(data.to_vec()); + bytes.push(data.len() as u8); + out.extend(bytes.as_slice()); + out.extend(bytes.clone().into_sorted_vec()); + out.extend(format!("{bytes:?}").bytes()); + let v: Vec = bytes.into(); + out.extend(v); + let mut least: BinaryHeap> = data.iter().map(|&b| Reverse(b)).collect(); + least.extend(data.iter().take(3).map(|&b| Reverse(b / 2))); + while let Some(Reverse(b)) = least.pop() { + out.push(b); + } + let strings: BinaryHeap = + BinaryHeap::from([String::from("pear"), String::from("apple"), data.len().to_string()]); + let mut copy = strings.clone(); + copy.extend(data.iter().map(|b| (b % 10).to_string())); + copy.retain(|s| s.len() > 1 || s.as_str() < "5"); + out.extend(copy.into_vec().iter().flat_map(|s| s.bytes())); + out.extend(strings.into_iter().map(|s| s.len() as u8)); + let mut pairs: BinaryHeap<(u8, u8)> = BinaryHeap::new(); + pairs.extend(data.iter().map(|&b| (b % 4, b / 64))); + pairs.extend(&[(data.len() as u8 % 4, 9)]); + if let Some(mut top) = pairs.peek_mut() { + top.1 = top.1.wrapping_add(1); + } + if let Some(mut top) = pairs.peek_mut() { + *top = (0, 0); + } + out.extend(pairs.into_sorted_vec().into_iter().flat_map(|(a, b)| [a, b])); +} diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index eab761f45..a46d31dbc 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,21 +1,21 @@ # Census of `reject-fixture` -- Refusals: 97 -- Function instances: 113 +- Refusals: 99 +- Function instances: 115 - Generic functions no instance reached, lowered over their type parameters: 7 ## By construct | Count | Construct | First | | ---: | --- | --- | -| 16 | unsupported call to `_` | fixtures/reject/src/lib.rs:21:4 | +| 17 | unsupported call to `_` | fixtures/reject/src/lib.rs:21:4 | | 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:385:2 | +| 3 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:41:42 | | 3 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:260:14 | | 3 | an assignment through a reference to `_` | fixtures/reject/src/lib.rs:102:2 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:110:2 | | 3 | unsupported type `_` | fixtures/reject/src/lib.rs:7:14 | | 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | fixtures/reject/src/lib.rs:16:15 | -| 2 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:41:42 | | 2 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | fixtures/reject/src/lib.rs:250:16 | | 2 | `_`, a trait object of the generic trait `_`, which is not supported yet | fixtures/reject/src/lib.rs:518:23 | | 2 | a coercion to `_`, which is not supported yet | fixtures/reject/src/lib.rs:509:40 | @@ -77,8 +77,8 @@ The type or module each refused type or path belongs to. | Count | API | First | | ---: | --- | --- | +| 7 | `Option` | fixtures/reject/src/lib.rs:16:15 | | 7 | `std::borrow::Cow` | fixtures/reject/src/lib.rs:385:2 | -| 6 | `Option` | fixtures/reject/src/lib.rs:16:15 | | 6 | `f32` | fixtures/reject/src/lib.rs:7:14 | | 4 | `Iterator` | fixtures/reject/src/lib.rs:41:42 | | 4 | `std::pin::Pin` | fixtures/reject/src/lib.rs:110:2 | @@ -102,6 +102,7 @@ The type or module each refused type or path belongs to. | 2 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:102:2 | | 2 | `std::vec::IntoIter` | fixtures/reject/src/lib.rs:483:17 | | 1 | `Batched` | fixtures/reject/src/lib.rs:363:16 | +| 1 | `BinaryHeap` | fixtures/reject/src/lib.rs:730:7 | | 1 | `Bits` | fixtures/reject/src/lib.rs:643:1 | | 1 | `Blank` | fixtures/reject/src/lib.rs:725:2 | | 1 | `BytesMut` | fixtures/reject/src/lib.rs:437:6 | @@ -130,6 +131,7 @@ The type or module each refused type or path belongs to. | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:182:51 | | 1 | `std::iter::Rev` | fixtures/reject/src/lib.rs:176:14 | | 1 | `std::option::Option` | fixtures/reject/src/lib.rs:423:2 | +| 1 | `std::slice::IterMut` | fixtures/reject/src/lib.rs:735:15 | | 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:136:11 | | 1 | `std::std::Layout` | fixtures/reject/src/lib.rs:626:15 | | 1 | `std::string::String` | fixtures/reject/src/lib.rs:74:10 | @@ -162,6 +164,7 @@ The type or module each refused type or path belongs to. | 1 | `_` of `_` by `_`, which is not supported yet | `HashMap::contains_key`, `std::collections::HashMap`, `u8` | fixtures/reject/src/lib.rs:289:6 | | 1 | `_` of `_`, which drops `_`, which is not supported yet | `into_keys`, `std::collections::HashMap`, `Noisy` | fixtures/reject/src/lib.rs:53:13 | | 1 | `_` of `_`, which is not supported yet | `std::any::Any::type_id`, `dyn std::any::Any` | fixtures/reject/src/lib.rs:599:4 | +| 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `BinaryHeap::pop`, `Option` | fixtures/reject/src/lib.rs:730:7 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `HashSet::get`, `Option` | fixtures/reject/src/lib.rs:266:6 | | 1 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | `Iterator::find`, `Option` | fixtures/reject/src/lib.rs:41:42 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::count` | fixtures/reject/src/lib.rs:250:38 | @@ -214,6 +217,7 @@ The type or module each refused type or path belongs to. | 1 | unsupported call to `_` | `::deref` | fixtures/reject/src/lib.rs:397:2 | | 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:423:2 | | 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:136:11 | +| 1 | unsupported call to `_` | `::is_sorted` | fixtures/reject/src/lib.rs:735:15 | | 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:74:10 | | 1 | unsupported call to `_` | `::hash` | fixtures/reject/src/lib.rs:354:10 | | 1 | unsupported call to `_` | `std::any::TypeId::of` | fixtures/reject/src/lib.rs:599:17 | @@ -226,7 +230,7 @@ The type or module each refused type or path belongs to. ## Spans -
unsupported call to `_` (16) +
unsupported call to `_` (17) - fixtures/reject/src/lib.rs:21:4: unsupported call to `core::slice::::rotate_left` - fixtures/reject/src/lib.rs:74:10: unsupported call to `::write_fmt` @@ -244,6 +248,7 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:626:15: unsupported call to `std::alloc::Layout::new::` - fixtures/reject/src/lib.rs:628:11: unsupported call to `std::alloc::alloc` - fixtures/reject/src/lib.rs:631:3: unsupported call to `std::alloc::dealloc` +- fixtures/reject/src/lib.rs:735:15: unsupported call to ` as std::iter::Iterator>::is_sorted`
@@ -256,6 +261,14 @@ The type or module each refused type or path belongs to.
+
`_` of `_`, whose `None` is `rt.None`, which is not supported yet (3) + +- fixtures/reject/src/lib.rs:41:42: `Iterator::find` of `Option>`, whose `None` is `rt.None`, which is not supported yet +- fixtures/reject/src/lib.rs:266:6: `HashSet::get` of `Option<&std::option::Option>`, whose `None` is `rt.None`, which is not supported yet +- fixtures/reject/src/lib.rs:730:7: `BinaryHeap::pop` of `Option>`, whose `None` is `rt.None`, which is not supported yet + +
+
a copy of a closure that changes what it captured (3) - fixtures/reject/src/lib.rs:260:14: a copy of a closure that changes what it captured @@ -295,13 +308,6 @@ The type or module each refused type or path belongs to.
-
`_` of `_`, whose `None` is `rt.None`, which is not supported yet (2) - -- fixtures/reject/src/lib.rs:41:42: `Iterator::find` of `Option>`, whose `None` is `rt.None`, which is not supported yet -- fixtures/reject/src/lib.rs:266:6: `HashSet::get` of `Option<&std::option::Option>`, whose `None` is `rt.None`, which is not supported yet - -
-
`_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it (2) - fixtures/reject/src/lib.rs:250:16: `std::iter::Iterator::filter` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 51c39b9a3..6891e21fc 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -89,6 +89,8 @@ a raw pointer to `(u32, u32)` in an element, a payload, or a union field, which comparing pointers to `()`, which has no bytes and so no address of its own at fixtures/reject/src/lib.rs:692:2 a dangling pointer to `Only`, a value of no bytes made of more than structs and tuples at fixtures/reject/src/lib.rs:701:11 `Buf::copy_to_bytes` of `Blank`, a value JS copies, which is not supported at fixtures/reject/src/lib.rs:725:2 +`BinaryHeap::pop` of `Option>`, whose `None` is `rt.None`, which is not supported yet at fixtures/reject/src/lib.rs:730:7 +unsupported call to ` as std::iter::Iterator>::is_sorted` at fixtures/reject/src/lib.rs:735:15 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:149:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:169:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:203:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index 00b8b7524..823bbb1ec 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -724,3 +724,13 @@ impl bytes::Buf for Blank { pub fn blank_bytes(len: usize) -> usize { bytes::Buf::copy_to_bytes(&mut Blank::Empty, len).len() } + +/// A heap's `pop` of an item that can be `undefined`: its `None` and `Some(None)` would both be. +pub fn heap_optional(heap: &mut std::collections::BinaryHeap>) -> bool { + heap.pop().is_some() +} + +/// The places `iter_mut` lends, which the runtime would compare as the objects they are. +pub fn places_sorted(v: &mut [u8]) -> bool { + v.iter_mut().is_sorted() +} diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 1207a70d1..3d3709e11 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -77,5 +77,7 @@ roots = [ "units_compared", "dangling_enum", "blank_bytes", + "heap_optional", + "places_sorted", ] printable = ["Meters"] diff --git a/ts/fixtures/task/expected/drops.ts b/ts/fixtures/task/expected/drops.ts index bfd232f25..4f7884423 100644 --- a/ts/fixtures/task/expected/drops.ts +++ b/ts/fixtures/task/expected/drops.ts @@ -944,7 +944,7 @@ export function edited(log: Log.Log, id: rt.u32, arg: rt.u8): void { v11.push(_315); const part6 = mapOr$Option_Key_u32_drained_closure4(rt.iterMaxByKey(rt.iterSlice(v11, rt.drop), (k2: Key.Key): rt.u32 => { return rt.u32Rem(rt.u32Sub(k2.id, id), 3); - }, rt.cmpLt(rt.cmp), true, rt.drop), 0, (k3: Key.Key): rt.u32 => { + }, rt.lt, true, rt.drop), 0, (k3: Key.Key): rt.u32 => { const _05: rt.u32 = rt.u32Sub(k3.id, id); k3[Symbol.dispose](); return _05; diff --git a/ts/fixtures/task/expected/drops/closures.ts b/ts/fixtures/task/expected/drops/closures.ts index 647220553..b73494425 100644 --- a/ts/fixtures/task/expected/drops/closures.ts +++ b/ts/fixtures/task/expected/drops/closures.ts @@ -653,7 +653,7 @@ export function iterated(log: Log.Log, id: rt.u32): void { }, (): void => { captured21[Symbol.dispose](); }); - const part9 = map$Option_Guard_u32_folded_closure5_closure0(rt.dropAfter(_441, () => rt.iterMaxByKey(_435, _441, rt.cmpLt(rt.cmp), undefined, rt.drop)), (g18: Guard.Guard): rt.u32 => { + const part9 = map$Option_Guard_u32_folded_closure5_closure0(rt.dropAfter(_441, () => rt.iterMaxByKey(_435, _441, rt.lt, undefined, rt.drop)), (g18: Guard.Guard): rt.u32 => { const _028: rt.u32 = Guard.id(g18); g18[Symbol.dispose](); return _028; diff --git a/ts/fixtures/task/expected/drops/folds.ts b/ts/fixtures/task/expected/drops/folds.ts index 0b871c547..411d507ff 100644 --- a/ts/fixtures/task/expected/drops/folds.ts +++ b/ts/fixtures/task/expected/drops/folds.ts @@ -141,10 +141,10 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { captured61[Symbol.dispose](); })), (g73: Guard.Guard): rt.u32 => { return rt.u32Rem(Guard.id(g73), 3); - }, rt.cmpLt(rt.cmp), true, rt.drop); + }, rt.lt, true, rt.drop); const least: Guard.Guard | undefined = rt.iterMinByKey(rt.iterSlice(guards(6, 8), rt.drop), (g74: Guard.Guard): rt.u32 => { return rt.u32Add(Guard.id(g74), Guard.id(d)); - }, rt.cmpLt(rt.cmp), true, rt.drop); + }, rt.lt, true, rt.drop); const _131: rt.u32 | undefined = map$Option_Guard_u32_folded_closure5_closure0(best, (g75: Guard.Guard): rt.u32 => { const _082: rt.u32 = Guard.id(g75); g75[Symbol.dispose](); @@ -167,7 +167,7 @@ export function folded(log: Log.Log, id: rt.u32, arg: rt.u8): void { return _077; }, (): void => { captured58[Symbol.dispose](); - })), rt.cmpLt(rt.cmp), true); + })), rt.lt, true); const _148: rt.Iter = rt.iterSlice(guards(4, 7), rt.drop); _1085 = false; const captured59 = id; diff --git a/ts/fixtures/task/expected/drops/iters.ts b/ts/fixtures/task/expected/drops/iters.ts index 4b8c6fa5c..79b23577d 100644 --- a/ts/fixtures/task/expected/drops/iters.ts +++ b/ts/fixtures/task/expected/drops/iters.ts @@ -157,7 +157,7 @@ export function looped(log: Log.Log, id: rt.u32): void { v9.push(_303); const best: Guard.Guard | undefined = rt.iterMaxByKey(rt.iterSlice(v9, rt.drop), (g9: Guard.Guard): rt.u32 => { return rt.u32Rem(Guard.id(g9), 2); - }, rt.cmpLt(rt.cmp), true, rt.drop); + }, rt.lt, true, rt.drop); const v10: Array = []; const _314: Guard.Guard | undefined = guard(30); v10.push(_314); diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 610888975..bf4842088 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -391,6 +391,26 @@ export function sliceSort(s: { [i: number]: T; length: number }, less: (a: T, } } +/** + * `is_sorted`, by `le`, the elements' `PartialOrd::le`: as std's, which below 33 elements + * stops at the first pair out of order, and past that tests every pair of each chunk of 33 + * before it stops, the last element of one the first of the next. + */ +export function sliceIsSorted(s: { [i: number]: T; length: number }, le: (a: T, b: T) => boolean): boolean { + const CHUNK = 33; + const n = s.length; + let i = 0; + if (n >= CHUNK) { + for (; i < n - CHUNK; i += CHUNK - 1) { + let sorted = true; + for (let j = i + 1; j < i + CHUNK; j++) sorted = le(s[j - 1], s[j]) && sorted; + if (!sorted) return false; + } + } + for (i++; i < n; i++) if (!le(s[i - 1], s[i])) return false; + return true; +} + /** * Keeps each of the first `n` elements of `a` that `keep` of its index returns true for, * in order, and drops each other one once `keep` rejects it: how many it kept. diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index cc6295370..2ad6fc356 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -2015,3 +2015,257 @@ export function dequeClone(q: VecDeque, item: (value: T) => T = itself): V for (let i = 0; i < q.n; i++) dequePushBack(out, item(q.b[at(q, i)] as T)); return out; } + +// `BinaryHeap`: std's max-heap in an array, sifted as std's sifts it, so equal elements +// leave in std's order and each comparison std makes runs. Every function that sifts is +// given the elements' `PartialOrd` tests std's sifts make: `le` up, and `le`, `ge`, and +// `lt` down. + +/** A `PartialOrd` test. */ +type Test = (a: T, b: T) => boolean; + +/** `BinaryHeap`: its elements in `d`, in std's order. */ +export interface BinaryHeap { + d: T[]; +} + +/** `BinaryHeap::new`, `with_capacity`, and `default`. */ +export function heapNew(): BinaryHeap { + return { d: [] }; +} + +/** `From>`: the vector's elements, rebuilt into a heap in place, as std's are. */ +export function heapFrom(d: T[], le: Test, ge: Test, lt: Test): BinaryHeap { + rebuild(d, le, ge, lt); + return { d }; +} + +/** `push`: `item` sifted up from the end. */ +export function heapPush(h: BinaryHeap, item: T, le: Test): void { + h.d.push(item); + siftUp(h.d, 0, h.d.length - 1, le); +} + +/** `pop`: the greatest element, or `undefined` if empty, the last element sifted into its place to the bottom and back up. */ +export function heapPop(h: BinaryHeap, le: Test): T | undefined { + const d = h.d; + if (d.length === 0) return undefined; + let item = d.pop() as T; + if (d.length > 0) { + [item, d[0]] = [d[0], item]; + siftDownToBottom(d, 0, le); + } + return item; +} + +/** `into_sorted_vec`: the elements in ascending order, each greatest swapped to the end and the rest sifted. */ +export function heapIntoSortedVec(h: BinaryHeap, le: Test, ge: Test, lt: Test): T[] { + const d = h.d; + for (let end = d.length - 1; end > 0; end--) { + [d[0], d[end]] = [d[end], d[0]]; + siftDownRange(d, 0, end, le, ge, lt); + } + return d; +} + +/** `append`: `other`'s elements added to the larger of the two, which `h` then holds, and `other` left empty. */ +export function heapAppend(h: BinaryHeap, other: BinaryHeap, le: Test, ge: Test, lt: Test): void { + if (h.d.length < other.d.length) [h.d, other.d] = [other.d, h.d]; + const start = h.d.length; + for (const item of other.d) h.d.push(item); + other.d = []; + rebuildTail(h.d, start, le, ge, lt); +} + +/** `extend`, taking `items` as `take` says, and then the heap rebuilt past what it held. */ +export function heapExtend(h: BinaryHeap, items: Iter, le: Test, ge: Test, lt: Test, take?: Take): BinaryHeap { + const start = h.d.length; + extending(h, items, (item) => h.d.push(item), take); + rebuildTail(h.d, start, le, ge, lt); + return h; +} + +/** + * `retain`: the elements `f` keeps, in order, each other dropped by `drop` as `f` rejects + * it, and then the heap rebuilt from the first it rejected. + */ +export function heapRetain(h: BinaryHeap, f: (item: T) => boolean, le: Test, ge: Test, lt: Test, drop?: (item: T) => void): void { + const d = h.d; + const n = d.length; + let from = n; + let kept = 0; + for (let i = 0; i < n; i++) { + const item = d[i]; + if (f(item)) d[kept++] = item; + else { + if (from === n) from = i; + drop?.(item); + } + } + d.length = kept; + rebuildTail(d, from, le, ge, lt); +} + +/** `clear`, which leaves the heap empty and then drops each element in order. */ +export function heapClear(h: BinaryHeap, drop?: (item: T) => void): void { + const d = h.d; + h.d = []; + dropAll(d, drop); +} + +/** `drain`: an iterator of the elements in order, which leaves the heap empty and drops those it still holds by `drop`. */ +export function heapDrain(h: BinaryHeap, drop?: (item: T) => void): Iter { + const d = h.d; + h.d = []; + return iterSlice(d, drop); +} + +/** `iter` and `into_iter`: the elements in order, any not read dropped by `drop` as the iterator drops. */ +export function heapIter(h: BinaryHeap, drop?: (item: T) => void): Iter { + return iterSlice(h.d, drop); +} + +/** The heap's drop: each element in order. */ +export function heapDrop(h: BinaryHeap, drop: (item: T) => void): void { + dropAll(h.d, drop); +} + +/** `clone`, with each element through its `Clone`, or as itself without one. */ +export function heapClone(h: BinaryHeap, item: (value: T) => T = itself): BinaryHeap { + return { d: h.d.map((value) => item(value)) }; +} + +/** + * `PeekMut`, the greatest element lent: a place, which writes it, of a heap with at least + * one. Once `DerefMut` lends it of a heap of more, its drop sifts it down. + */ +export class PeekMut implements Place { + readonly h: BinaryHeap; + readonly le: Test; + readonly ge: Test; + readonly lt: Test; + /** The heap's elements `DerefMut` hid behind a heap of the top alone, as std's `set_len(1)` does, so a forgotten guard leaks them as std's does. */ + t: T[] | undefined; + + constructor(h: BinaryHeap, le: Test, ge: Test, lt: Test) { + this.h = h; + this.le = le; + this.ge = ge; + this.lt = lt; + } + + get v(): T { + return this.h.d[0]; + } + + set v(value: T) { + this.h.d[0] = value; + } +} + +/** `peek_mut`: the greatest element lent, or `undefined` if empty. */ +export function heapPeekMut(h: BinaryHeap, le: Test, ge: Test, lt: Test): PeekMut | undefined { + return h.d.length === 0 ? undefined : new PeekMut(h, le, ge, lt); +} + +/** `PeekMut`'s `DerefMut`, which hides the rest of a heap of more than the one element until the guard drops. */ +export function peekMutDerefMut(p: PeekMut): void { + const d = p.h.d; + if (d.length > 1) { + p.t = d; + p.h.d = [d[0]]; + } +} + +/** The elements `DerefMut` hid back in the heap, with the top as written; whether there were any. */ +function peekMutRestore(p: PeekMut): boolean { + const t = p.t; + if (t === undefined) return false; + t[0] = p.h.d[0]; + p.h.d = t; + p.t = undefined; + return true; +} + +/** `PeekMut`'s drop: the element sifted down from the top, if `DerefMut` lent it. */ +export function peekMutDrop(p: PeekMut): void { + if (peekMutRestore(p)) siftDownRange(p.h.d, 0, p.h.d.length, p.le, p.ge, p.lt); +} + +/** `PeekMut::pop`: the heap's `pop`, which leaves the drop nothing to sift. */ +export function peekMutPop(p: PeekMut): T { + peekMutRestore(p); + return heapPop(p.h, p.le) as T; +} + +/** std's `sift_up`: the element at `pos` moved up past each parent it is not `le`, but not above `start`. */ +function siftUp(d: T[], start: number, pos: number, le: Test): number { + const item = d[pos]; + while (pos > start) { + const parent = (pos - 1) >>> 1; + if (le(item, d[parent])) break; + d[pos] = d[parent]; + pos = parent; + } + d[pos] = item; + return pos; +} + +/** std's `sift_down_range`: the element at `pos` moved down past each greater child, within the first `end`. */ +function siftDownRange(d: T[], pos: number, end: number, le: Test, ge: Test, lt: Test): number { + const item = d[pos]; + let child = 2 * pos + 1; + while (child <= Math.max(end - 2, 0)) { + if (le(d[child], d[child + 1])) child++; + if (ge(item, d[child])) { + d[pos] = item; + return pos; + } + d[pos] = d[child]; + pos = child; + child = 2 * pos + 1; + } + if (child === end - 1 && lt(item, d[child])) { + d[pos] = d[child]; + pos = child; + } + d[pos] = item; + return pos; +} + +/** std's `sift_down_to_bottom`: the element at `pos` moved down to a leaf past the greater child at each level, then sifted up. */ +function siftDownToBottom(d: T[], pos: number, le: Test): void { + const end = d.length; + const start = pos; + const item = d[pos]; + let child = 2 * pos + 1; + while (child <= Math.max(end - 2, 0)) { + if (le(d[child], d[child + 1])) child++; + d[pos] = d[child]; + pos = child; + child = 2 * pos + 1; + } + if (child === end - 1) { + d[pos] = d[child]; + pos = child; + } + d[pos] = item; + siftUp(d, start, pos, le); +} + +/** std's `rebuild_tail`: the elements from `start` on added to the heap before them, by a rebuild or by sifting each up, as std's estimate picks. */ +function rebuildTail(d: T[], start: number, le: Test, ge: Test, lt: Test): void { + if (start === d.length) return; + const tail = d.length - start; + const better = start < tail || (d.length <= 2048 ? 2 * d.length < tail * (31 - Math.clz32(start)) : 2 * d.length < tail * 11); + if (better) rebuild(d, le, ge, lt); + else for (let i = start; i < d.length; i++) siftUp(d, 0, i, le); +} + +/** std's `rebuild`: each parent sifted down, the last first. */ +function rebuild(d: T[], le: Test, ge: Test, lt: Test): void { + for (let n = d.length >>> 1; n > 0; ) { + n--; + siftDownRange(d, n, d.length, le, ge, lt); + } +} diff --git a/ts/runtime/rt/int.ts b/ts/runtime/rt/int.ts index 825b17d2f..78df491fa 100644 --- a/ts/runtime/rt/int.ts +++ b/ts/runtime/rt/int.ts @@ -103,6 +103,21 @@ export function lt(a: number | Limbs, b: number | Limbs): boolean { return typeof b === "number" ? a.hi < 0 : compare(a, b) < 0; } +/** `PartialOrd::le` of integers, by `lt`. */ +export function le(a: number | Limbs, b: number | Limbs): boolean { + return !lt(b, a); +} + +/** `PartialOrd::gt` of integers, by `lt`. */ +export function gt(a: number | Limbs, b: number | Limbs): boolean { + return lt(b, a); +} + +/** `PartialOrd::ge` of integers, by `lt`. */ +export function ge(a: number | Limbs, b: number | Limbs): boolean { + return !lt(a, b); +} + /** The larger of two integers. */ export function max(a: number, b: number): number { return a >= b ? a : b; diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 097adba58..5301b1f13 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -606,6 +606,39 @@ function best(it: Iter, f: (item: T) => K, lt: Lt, least: boolean, d return item; } +/** + * `is_sorted_by`: whether `compare` holds of each item and the next, read by `next` and + * then as `all` folds, stopping at the first it fails. Each item drops by `drop` as the + * next replaces it, or after the one it failed against; then `compare` by `dropCompare`, + * as `all` drops it, the last item, and `it`, if `drops`. + */ +export function iterIsSortedBy( + it: Iter, + compare: (a: T, b: T) => boolean, + drops = false, + drop?: Drop, + dropCompare?: Drop<(a: T, b: T) => boolean>, +): boolean { + let last = it.next(); + let sorted = true; + if (last !== DONE) { + const folded = it.folded(); + for (let item = folded.next(); item !== DONE; item = folded.next()) { + if (!compare(last, item)) { + drop?.(item); + sorted = false; + break; + } + drop?.(last); + last = item; + } + } + dropCompare?.(compare); + if (last !== DONE) drop?.(last); + if (drops) dispose(it); + return sorted; +} + /** `collect` into a `Vec`, taking `it` as `take` says. */ export function iterToArray(it: Iter, take?: Take): T[] { const out: T[] = []; diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index d1ac6f868..96d9aec1c 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -167,6 +167,8 @@ pub fn call<'tcx>( Std::HashMap | Std::HashSet => hash(b, coll, call, owner, name, args, out), Std::BTreeMap | Std::BTreeSet => btree(b, coll, call, owner, name, args), Std::VecDeque => deque(b, coll, call, owner, name, args, out), + Std::BinaryHeap => heap(b, coll, call, owner, name, args, out), + Std::PeekMut if name == "pop" => Some(Expr::rt("peekMutPop", args)), Std::Entry => entry(b, coll, name, args, span), Std::Ordering => ordering(name, args), Std::Hasher if matches!(name, "new" | "default") => Some(Expr::rt("hasher", vec![])), @@ -216,6 +218,7 @@ fn discarded(std: Std, owner: &str, name: &str) -> Option<&'static [usize]> { (_, "clear" | "retain" | "retain_mut") if map => &[0, 1], (_, "clear" | "retain" | "retain_mut") if set => &[0], (_, "clear" | "retain" | "retain_mut" | "truncate") if std == Std::VecDeque => &[0], + (_, "clear" | "retain") if std == Std::BinaryHeap => &[0], _ => return None, }) } @@ -276,7 +279,7 @@ fn same_key<'tcx>(b: &Body<'_, '_, 'tcx>, key: Ty<'tcx>, by: Ty<'tcx>) -> bool { fn undefined_arg<'tcx>(b: &Body<'_, '_, 'tcx>, std: Std, coll: Ty<'tcx>) -> Option> { if !matches!( std, - Std::HashMap | Std::BTreeMap | Std::Entry | Std::HashSet | Std::BTreeSet | Std::VecDeque + Std::HashMap | Std::BTreeMap | Std::Entry | Std::HashSet | Std::BTreeSet | Std::VecDeque | Std::BinaryHeap ) { return None; } @@ -560,6 +563,10 @@ fn own_test<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str) -> bool { pub fn test_fn<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, test: &str, span: Span) -> Expr { let tcx = b.t.tcx; let ty = peel(b, ty); + // An integer's, which the runtime tests directly rather than through `partial_cmp`. + if matches!(ty.kind(), ty::Char | ty::Int(_) | ty::Uint(_)) { + return Expr::Rt(test.into()); + } if !own_test(b, ty, test) { let by = match test { "lt" => "cmpLt", @@ -868,6 +875,119 @@ fn deque<'tcx>( Some(e) } +/// `PartialOrd`'s tests of `item` that std's heap sifts make: `le` up, and `le`, `ge`, and +/// `lt` down. +fn heap_tests<'tcx>(b: &mut Body<'_, '_, 'tcx>, item: Ty<'tcx>, down: bool, span: Span) -> Vec { + let tests: &[&str] = if down { &["le", "ge", "lt"] } else { &["le"] }; + tests.iter().map(|test| test_fn(b, item, test, span)).collect() +} + +/// A method of `BinaryHeap`, or of a trait it implements, each that sifts given the tests +/// its sifts make. +fn heap<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + coll: Ty<'tcx>, + call: &Call<'tcx>, + owner: &str, + name: &str, + args: Vec, + out: &mut Vec, +) -> Option { + let span = call.span; + let item = arg(coll, 0); + let bytes = matches!(item.kind(), ty::Uint(ty::UintTy::U8)); + let mut values = args; + let e = match (owner, name) { + (_, "new" | "with_capacity") | ("Default", "default") => { + discard(values, out); + empty(b, Expr::rt("heapNew", vec![]), coll, span) + } + (_, "len") => values.pop()?.member("d").member("length"), + (_, "is_empty") => values.pop()?.member("d").member("length").binary("===", Expr::lit("0")), + (_, "peek") => values.pop()?.member("d").index(Expr::lit("0")), + (_, "push" | "pop" | "peek_mut" | "append" | "retain" | "into_sorted_vec") => { + values.extend(heap_tests(b, item, !matches!(name, "push" | "pop"), span)); + let f = match name { + "push" => "heapPush", + "pop" => "heapPop", + "peek_mut" => "heapPeekMut", + "append" => "heapAppend", + "retain" => "heapRetain", + _ => return Some(heap_vec(Expr::rt("heapIntoSortedVec", values), bytes)), + }; + Expr::rt(f, values) + } + (_, "into_vec") => heap_vec(values.pop()?.member("d"), bytes), + (_, "as_slice") if bytes => Expr::rt( + "byteSliceFrom", + vec![ + Expr::ident("Uint8Array") + .member("from") + .call(vec![values.pop()?.member("d")]), + ], + ), + (_, "as_slice") => values.pop()?.member("d"), + (_, "clear") => Expr::rt("heapClear", values), + (_, "drain") => { + let droppers = entry_droppers(b, coll, span); + Expr::rt("heapDrain", super::iter::tail(values, droppers)) + } + (_, "iter") => { + let by_ref = Ty::new_imm_ref(b.t.tcx, b.t.tcx.lifetimes.re_erased, coll); + into_iter(b, by_ref, values.pop()?, span)? + } + (_, "reserve" | "reserve_exact" | "shrink_to_fit" | "shrink_to") => { + discard(values, out); + Expr::undefined() + } + ("Clone", "clone") => { + let clone = clone_fn(b, item, span); + Expr::rt("heapClone", super::iter::tail(values, vec![clone])) + } + // `From>` rebuilds the vector in place, and `From<[T; N]>` collects the array + // into one first. + ("From", "from") if b.t.std_of(library::targ(call.args, 1)) == Some(Std::Vec) => { + let v = values.pop()?; + let d = match bytes { + true => Expr::ident("Array") + .member("from") + .call(vec![Expr::rt("byteVecView", vec![v])]), + false => v, + }; + Expr::rt("heapFrom", [vec![d], heap_tests(b, item, true, span)].concat()) + } + ("Extend", "extend") | ("From", "from") => { + let (source, iter) = extend_source(b, coll, call, owner, values.pop()?)?; + if owner == "From" { + let d = super::iter::to_array(b, source, iter, item, span); + return Some(Expr::rt( + "heapFrom", + [vec![d], heap_tests(b, item, true, span)].concat(), + )); + } + // Into its vector, of a `TrustedLen` iterator by `for_each`. + let folds = super::iter::implements(b.t, iter, "TrustedLen"); + let take = super::iter::iter_take(b, iter, folds, span); + let mut args = vec![values.pop()?, source]; + args.extend(heap_tests(b, item, true, span)); + Expr::rt("heapExtend", super::iter::tail(args, vec![take])) + } + _ => return None, + }; + Some(e) +} + +/// A `Vec` of a heap's elements `d`, an `rt.ByteVec` of bytes. +pub fn heap_vec(d: Expr, bytes: bool) -> Expr { + match bytes { + true => Expr::rt( + "byteVecFrom", + vec![Expr::ident("Uint8Array").member("from").call(vec![d])], + ), + false => d, + } +} + /// What `extend` or `from` adds to a collection: an `rt.Iter` of what `source` holds, and /// the type of the iterator std's makes of it. fn extend_source<'tcx>( @@ -1264,6 +1384,11 @@ pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, sp Std::HashMap if places(1) => "hashIterMut", Std::BTreeMap if places(1) => "btreeIterMut", Std::VecDeque if places(0) => "dequeIterMut", + // A heap's elements in order, as its `Vec`'s, which by reference are borrowed. + Std::BinaryHeap if ty.is_ref() => { + return Some(super::iter::by_ref(Expr::rt("heapIter", vec![value]), arg(coll, 0))); + } + Std::BinaryHeap => "heapIter", Std::HashMap => "hashIter", Std::HashSet => "hashKeys", Std::BTreeMap => "btreeIter", @@ -1293,6 +1418,12 @@ pub fn collect<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, iter: Expr, from: hash_extend(b, ty, target, iter, from, span) } Std::BTreeMap | Std::BTreeSet => btree_collect(b, ty, iter, from, span), + // std's collects into a `Vec`, and rebuilds it in place. + Std::BinaryHeap => { + let item = arg(ty, 0); + let d = super::iter::to_array(b, iter, from, item, span); + Expr::rt("heapFrom", [vec![d], heap_tests(b, item, true, span)].concat()) + } // std's collects into a `Vec` first. Std::VecDeque => { let target = deque_new(b, ty, span); diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index a97b10ae1..511e77889 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -31,8 +31,11 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let drops = match ty.kind() { ty::Adt(adt, _) if let Some(drops) = self.bound_drops(adt.did()) => drops, ty::Adt(adt, args) => match Std::of(tcx, adt.did()) { - // A guard unlocks its mutex or ends its borrow, and a `Weak` uncounts itself. - Some(Std::Arc | Std::Weak | Std::Waker | Std::MutexGuard | Std::Ref | Std::RefMut) => true, + // A guard unlocks its mutex or ends its borrow, a `Weak` uncounts itself, and a + // `PeekMut` sifts what it lent. + Some(Std::Arc | Std::Weak | Std::Waker | Std::MutexGuard | Std::Ref | Std::RefMut | Std::PeekMut) => { + true + } Some( Std::Option | Std::Result @@ -53,6 +56,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { | Std::BTreeMap | Std::BTreeSet | Std::VecDeque + | Std::BinaryHeap | Std::Bound | Std::TryLockError | Std::Reverse, @@ -298,6 +302,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { Std::HashSet => ("hashDrop", vec![value, each(self, 0)?]), Std::BTreeSet => ("btreeDrop", vec![value, each(self, 0)?]), Std::VecDeque => ("dequeDrop", vec![value, each(self, 0)?]), + Std::BinaryHeap => ("heapDrop", vec![value, each(self, 0)?]), + Std::PeekMut => ("peekMutDrop", vec![value]), // A vacant entry holds the key it was given; an occupied one dropped it as // `entry` found the map's. Std::Entry => match self.tcx.item_name(adt.did()).as_str() { diff --git a/ts/src/lower/fmt.rs b/ts/src/lower/fmt.rs index a6db2973c..71ff5f204 100644 --- a/ts/src/lower/fmt.rs +++ b/ts/src/lower/fmt.rs @@ -293,6 +293,10 @@ pub fn std_impl<'tcx>( let item = b.fmt_fn(tr, args.type_at(0), span); rt("list", vec![library::elements(value, ty), f, item]) } + (Std::BinaryHeap, Trait::Debug) => { + let item = b.fmt_fn(tr, args.type_at(0), span); + rt("list", vec![value.member("d"), f, item]) + } (Std::Option, Trait::Debug) => { let some = b.fmt_fn(tr, args.type_at(0), span); let none = b.t.none_arg(args.type_at(0)); diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index f48798e14..719e5d285 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -197,7 +197,7 @@ pub fn into_iter<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, value: Expr, sp /// `iter`, over the elements of a slice or `Vec` of `elem`s, yielding references to them: /// a slice of each array of bytes. -fn by_ref(iter: Expr, elem: Ty<'_>) -> Expr { +pub(super) fn by_ref(iter: Expr, elem: Ty<'_>) -> Expr { match super::byte_array(elem) { true => Expr::rt("iterMap", vec![iter, Expr::Rt("byteSliceFrom".into())]), false => iter, @@ -509,6 +509,33 @@ pub fn call<'tcx>( let droppers = vec![iter_drops(b, self_ty, span), b.t.dropper(item, span)]; method(&camel, tail([rest, vec![lt]].concat(), droppers)) } + // Whether each item and the next are in order: by `PartialOrd::le`, by a comparator, + // or by `le` of the keys a function makes, as std's maps to them. Not of places, as + // `max` is not. + "is_sorted" | "is_sorted_by" | "is_sorted_by_key" => { + let (items, compare, each) = match name { + "is_sorted_by" => (recv.clone(), rest.into_iter().next()?, item), + _ if b.t.has_place(item) => return None, + "is_sorted" => (recv.clone(), super::collections::test_fn(b, item, "le", span), item), + _ => { + let key = targ(2)?; + let le = super::collections::test_fn(b, key, "le", span); + // The map drops its function as the iterator drops, so one through a reference + // or box is lent, as one owned that drops is refused with such an iterator. + let (mut f, f_ty) = (rest.into_iter().next()?, targ(1)?); + let owned = matches!(f_ty.kind(), ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(..)); + if !owned && drops_iter(b, self_ty, span) { + f = Expr::rt("lent", vec![f]); + } + (Expr::rt("iterMap", vec![recv.clone(), f]), le, key) + } + }; + let mut droppers = vec![iter_drops(b, self_ty, span), b.t.dropper(each, span)]; + if name == "is_sorted_by" { + droppers.push(b.t.dropper(targ(1)?, span)); + } + Some(Expr::rt("iterIsSortedBy", tail(vec![items, compare], droppers))) + } "rposition" => method(&camel, rest), "rfind" => { let dropper = b.t.dropper(item, span); diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 4bfc1eede..4d84bde10 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -124,6 +124,9 @@ pub enum Std { BTreeSet, /// `rt.VecDeque`, a ring buffer. VecDeque, + /// `rt.BinaryHeap`, std's max-heap in an array, and `rt.PeekMut`, its greatest element lent. + BinaryHeap, + PeekMut, /// `rt.Entry`: a map's `Entry`, `OccupiedEntry`, or `VacantEntry`. Entry, /// `rt.Ordering`: its discriminant, -1, 0, or 1. @@ -187,6 +190,7 @@ impl Std { (rustc_span::Symbol::intern("BTreeMap"), Self::BTreeMap), (rustc_span::Symbol::intern("BTreeSet"), Self::BTreeSet), (rustc_span::Symbol::intern("VecDeque"), Self::VecDeque), + (rustc_span::Symbol::intern("BinaryHeap"), Self::BinaryHeap), (rustc_span::Symbol::intern("Duration"), Self::Duration), ] { if tcx.is_diagnostic_item(item, did) { @@ -246,6 +250,7 @@ impl Std { ["cell", "once", "OnceCell"] => Some(Self::OnceCell), ["sync", "once_lock", "OnceLock"] => Some(Self::OnceLock), ["cmp", "Reverse"] => Some(Self::Reverse), + ["collections", "binary_heap", "PeekMut"] => Some(Self::PeekMut), ["ops", "range", "Bound"] => Some(Self::Bound), ["num", "nonzero", "NonZero"] => Some(Self::NonZero), ["hash", "random", "DefaultHasher"] => Some(Self::Hasher), @@ -504,7 +509,7 @@ pub fn inherent_std(tcx: TyCtxt<'_>, def_id: DefId) -> Option { /// Whether std's `instance` lowers from its own MIR, as the crate's functions do, rather /// than mapping here: a method of `bool`, `Option`, `Result`, `Poll`, or `LocalKey`, or -/// their `Clone`. +/// their `Clone`, or `cmp`'s `max` and `min` and their `_by` and `_by_key` forms. /// Not one taking a `&mut` or a raw pointer, whose writes the crate's analysis of what it /// writes into would miss, nor an `unsafe` one, nor one mapped below as one expression or /// as the runtime's panic. @@ -513,7 +518,17 @@ pub fn from_mir<'tcx>(t: &Translator<'_, 'tcx>, instance: Instance<'tcx>) -> boo let ty::InstanceKind::Item(did) = instance.def else { return false; }; - if !is_std(tcx, did) || tcx.def_kind(did) != DefKind::AssocFn { + if !is_std(tcx, did) { + return false; + } + if tcx.def_kind(did) == DefKind::Fn { + let path = std_path(tcx, did); + return matches!( + path.iter().map(String::as_str).collect::>().as_slice(), + ["cmp", "max" | "min" | "max_by" | "min_by" | "max_by_key" | "min_by_key"] + ); + } + if tcx.def_kind(did) != DefKind::AssocFn { return false; } let Some(imp) = tcx.impl_of_assoc(did) else { @@ -617,6 +632,8 @@ fn discards(owner: &str, name: &str) -> Option<&'static [usize]> { // `get_or_init::` drops its function uncalled if the cell is set. ("OnceCell" | "OnceLock", "get_or_init") => &[1], ("Arc" | "Rc", "new_cyclic") => &[1], + // `is_sorted_by::` drops its comparator before the last item, as `all` takes it. + ("Iterator", "is_sorted_by") => &[1], ("LocalKey", "with" | "try_with" | "initialize_with" | "with_borrow_mut") => &[1], _ => return None, }) @@ -772,19 +789,20 @@ fn called_closures<'tcx>( if lent { continue; } + let index = call.args[..param.index as usize] + .iter() + .filter(|a| a.as_type().is_some()) + .count(); + let discards = discarded.is_some_and(|d| d.contains(&index)); let kept = inputs.iter().enumerate().any(|(j, &t)| j != i && mentions(t)); - if bound != ty::ClosureKind::FnOnce && !kept && i < args.len() { + if bound != ty::ClosureKind::FnOnce && !kept && !discards && i < args.len() { match dropping_operand(b, call) { None => closures.spent.push(args[i].clone()), Some(_) => closures.leaked = closures.leaked.or(Some(ty)), } continue; } - let index = call.args[..param.index as usize] - .iter() - .filter(|a| a.as_type().is_some()) - .count(); - if !discarded.is_some_and(|d| d.contains(&index)) { + if !discards { closures.leaked = closures.leaked.or(Some(ty)); continue; } @@ -993,6 +1011,12 @@ fn mapping<'tcx>( if to == from || some || (string(tcx, to) && string(tcx, from.peel_refs())) { return Some(value); } + // A heap's `Vec` is its elements. + if let ty::Adt(adt, args) = from.kind() + && Std::of(tcx, adt.did()) == Some(Std::BinaryHeap) + { + return Some(super::collections::heap_vec(value.member("d"), is_u8(args.type_at(0)))); + } // Each `From` between integers, `char`, and `f64`, or from `bool`, keeps the // value, as `as` then does. let scalar = |t: Ty<'tcx>| Int::of(t).is_some() || t.is_char() || super::float::is(t); @@ -1315,6 +1339,35 @@ fn mapping<'tcx>( } } } + // Whether each element and the next are in order, as `sort` would leave them: by + // `PartialOrd::le` in std's chunks, or as the slice's iterator's `is_sorted_by` and + // `is_sorted_by_key` test them, which std's are. + ("slice", "is_sorted") => { + let elem = targ(gargs, 0); + let mut values = args; + values[0] = elements(values[0].clone(), Ty::new_slice(tcx, elem)); + values.push(super::collections::test_fn(b, elem, "le", span)); + Expr::rt("sliceIsSorted", values) + } + ("slice", "is_sorted_by" | "is_sorted_by_key") => { + let elem = targ(gargs, 0); + let mut values = args.into_iter(); + let slice = elements(values.next()?, Ty::new_slice(tcx, elem)); + let items = super::iter::by_ref(Expr::rt("iterSlice", vec![slice]), elem); + let f = values.next()?; + let (items, compare, drop) = match name.as_str() { + "is_sorted_by" => (items, f, Expr::undefined()), + _ => { + let key = targ(gargs, 2); + let le = super::collections::test_fn(b, key, "le", span); + (Expr::rt("iterMap", vec![items, f]), le, b.t.dropper(key, span)) + } + }; + Expr::rt( + "iterIsSortedBy", + super::iter::tail(vec![items, compare], vec![Expr::undefined(), drop]), + ) + } // A boxed slice's elements, and `vec![a, b]`'s array once `mir::vec` took the box // away. A `Copy` aggregate's operand may be shared with where it came from, which the // vector must not change. @@ -1453,6 +1506,17 @@ fn mapping<'tcx>( Some(Std::Vec) => vec_view(v, vargs.type_at(0)), Some(Std::String | Std::Box) => v, Some(Std::Cow) if string(tcx, ty) => v, + // The greatest element of a heap, which `DerefMut` marks to sift once lent. + Some(Std::PeekMut) => { + let p = match v.is_path() { + true => v, + false => b.spill(v, "peek", out), + }; + if name == "deref_mut" { + out.push(StmtKind::Expr(Expr::rt("peekMutDerefMut", vec![p.clone()])).at(None)); + } + p.member("v") + } _ => return None, }, _ => return None, diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index 664e575ef..e0ae013af 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -111,9 +111,9 @@ fn instance_mir<'tcx>(t: &mut Translator<'_, 'tcx>, instance: Instance<'tcx>, sp /// Why a body of std's can't lower as the crate's do, if it can't. The crate's analysis of /// what it writes into reads only its own bodies, so one must change nothing in place: no -/// assignment through a projection or a reference, and no mutable borrow or pointer. And -/// core's plain integer arithmetic wraps, as `wrapping_add` lowers, where the crate's is -/// checked. +/// assignment through a projection or a reference, and no mutable borrow or pointer but of +/// a function to call it, as `FnMut` is, whose writes are its own body's. And core's plain +/// integer arithmetic wraps, as `wrapping_add` lowers, where the crate's is checked. fn std_refused<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) -> Option<&'static str> { const WRITE: &str = "a change in place, which a body of std's is not supported to make"; const WRAP: &str = "arithmetic that wraps, which a body of std's is not supported to do"; @@ -122,6 +122,12 @@ fn std_refused<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) -> Option<&'stat match &stmt.kind { StatementKind::Assign(assign) if !assign.0.projection.is_empty() => return Some(WRITE), StatementKind::Assign(assign) => match &assign.1 { + Rvalue::Ref(_, mir::BorrowKind::Mut { .. }, place) + if place.projection.is_empty() + && matches!( + body.local_decls[place.local].ty.kind(), + ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(..) + ) => {} Rvalue::Ref(_, mir::BorrowKind::Mut { .. }, _) | Rvalue::RawPtr(mir::RawPtrKind::Mut, _) => { return Some(WRITE); } diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 9e03fbda8..47356ad1c 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -1158,6 +1158,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { | library::Std::BTreeMap | library::Std::BTreeSet | library::Std::VecDeque + | library::Std::BinaryHeap + | library::Std::PeekMut | library::Std::Entry | library::Std::Hasher | library::Std::BytesMut @@ -1492,13 +1494,17 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { | library::Std::Entry | library::Std::HashSet | library::Std::BTreeSet - | library::Std::VecDeque => { + | library::Std::VecDeque + | library::Std::BinaryHeap + | library::Std::PeekMut => { let (name, n) = match std { library::Std::HashMap => ("HashMap", 2), library::Std::BTreeMap => ("BTreeMap", 2), library::Std::Entry => ("Entry", 2), library::Std::HashSet => ("HashSet", 1), library::Std::BTreeSet => ("BTreeSet", 1), + library::Std::BinaryHeap => ("BinaryHeap", 1), + library::Std::PeekMut => ("PeekMut", 1), _ => ("VecDeque", 1), }; let tys: Vec> = args.types().take(n).collect();