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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion quest/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ generic std gap is planned here even when moq never calls it.
- [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies
- [A B-tree's drop order](/quest/m3/ts-btree-drop.md) - dropping a whole `BTreeMap` or `BTreeSet` drops its entries node by node, as std's does
- [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
- [References to byte-array keys a collection yields](/quest/m3/ts-byte-key-refs.md) - `keys`, `iter`, `get_key_value`, and `HashSet::get` of a `[u8; N]` key translate
- [An array as a slice](/quest/m3/ts-array-as-slice.md) - `<[T; N]>::as_slice` and `as_mut_slice` translate
- [reduce, max_by, min_by, and Peekable's next_if_eq and peek_mut](/quest/m3/ts-iter-reducers.md) - the iterator consumers std returns an item from that have no mapping translate
- [Closures that return an Option whose None is rt.None](/quest/m3/ts-option-closures.md) - `find_map`, `filter_map`, and `map_while` to `Option<Option<u8>>` keep `None` apart from `Some(None)`
- [Byte arrays compared for equality and hashed](/quest/m3/ts-byte-keys.md) - `==` of byte arrays, and byte arrays, slices, and vectors as hash keys, translate
- [A slice's edits in place](/quest/m3/ts-slice-edits.md) - `fill`, `rotate_left`, and `copy_from_slice` of other values translate
- [A slice's parts by inclusive range, checked range, and fixed size](/quest/m3/ts-slice-ranges.md) - `&v[a..=b]`, `get_mut(a..b)`, and `first_chunk_mut` translate, writing through
- [Waiting: a slice object for a mutable part of other values](/quest/m3/ts-slice-object.md) - waits on a moq profile showing the `Proxy` view's traps matter, then decides on an `rt.ByteSlice`-like form
Expand Down
14 changes: 14 additions & 0 deletions quest/m3/ts-array-as-slice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# [XS] An array as a slice

## Goal

rust2-ts translates `<[T; N]>::as_slice` and `as_mut_slice`, for bytes and other values.
Proven by differential tests against native Rust.

## Plan

`as_slice` refuses with a span today, as "unsupported call to `std::array::<impl [u8; 2]>::as_slice`".
A `&[u8; N]` is already an `rt.ByteSlice` and a reference to another array its JS array,
so each is the reference it is given.

Public API: the translator's output.
17 changes: 17 additions & 0 deletions quest/m3/ts-byte-key-refs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# [S] References to byte-array keys a collection yields

## Goal

rust2-ts translates what lends out a collection's `[u8; N]` key, such as `keys`, `iter`,
`get_key_value`, `Entry::key`, and `HashSet::get` and `iter`, each reference an
`rt.ByteSlice` of the key's bytes, as a `&[u8; N]` is elsewhere. Proven by differential
tests against native Rust.

## Plan

Each refuses with a span today, as "`HashMap::keys` of a reference to an array of bytes it
is generic over" (`bytes_across` in `src/lower/mir/call.rs`): the collection holds the key
as a typed array, which its mapping would hand back where the caller reads a slice.
Lookups by such a reference already translate, since they only read the key.

Public API: the translator's output.
18 changes: 0 additions & 18 deletions quest/m3/ts-byte-keys.md

This file was deleted.

4 changes: 3 additions & 1 deletion ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ finds any other key, a 64-bit one past 2^53 included, through its translated `Ha
`Eq`; a B-tree orders every key by its translated `Ord`. std's impls are the runtime's: an
`Option` orders `None` first, an `Arc` compares, orders, and hashes as what it holds,
so a map of `Arc<str>` finds a `&str`, and byte arrays, slices, and vectors order by their
bytes and then their length. A `&mut` to a value a map holds that JS copies,
bytes and then their length, and compare and hash alike whatever their form, so a map of
`Vec<u8>` finds a `&[u8]`. A collection's reference to a byte array it holds as a key, as
`keys` and `HashSet::get` return, is refused. A `&mut` to a value a map holds that JS copies,
such as a number, from `get_mut` or the entry API, is an `rt.Place` whose `v` reads and
writes the map's slot. A hash map iterates in an order
of the runtime's, mostly insertion order, which std leaves unspecified, and hashes a key
Expand Down
7 changes: 7 additions & 0 deletions ts/fixtures/lang/expected/core/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export function thenSome$0d5am30(self: boolean, t: rt.u8): rt.u8 | undefined {
return t;
}

export function thenSome$0rsp0eu(self: boolean, t: Uint8Array): Uint8Array | undefined {
if (!self) {
return undefined;
}
return t;
}

export function thenSome$1j8foe9(self: boolean, _: undefined): undefined | rt.None {
if (!self) {
return rt.None;
Expand Down
10 changes: 6 additions & 4 deletions ts/fixtures/lang/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import { edits, bytes } from "./edits.ts";
import { floats, formats, parses } from "./floats.ts";
import { ints, strings, derived, written, counts, rounding, panics, wrapped, writes } from "./fmt.ts";
import { methods } from "./generics.ts";
import { values, random } from "./hashes.ts";
import { values, random, bytes as hashes_bytes } from "./hashes.ts";
import { holds } from "./holds.ts";
import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast as ints_cast, convert, parses as ints_parses } from "./ints.ts";
import { adaptors, consumers, skips, ranked, ranges, options, own, values as iters_values, returned } from "./iters.ts";
import { keys, overrides } from "./keys.ts";
import { keys, overrides, bytes as keys_bytes } from "./keys.ts";
import { locals } from "./locals.ts";
import { locks, relock, rwlocks } from "./locks.ts";
import { nested } from "./nested.ts";
Expand Down Expand Up @@ -90,7 +90,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec)
refs(data21, out21);
}] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2121: 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", (_2122: rt.u8, _351: rt.ByteSlice, out23: rt.ByteVec): void => {
}] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _357: 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", (_2123: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => {
dyns(data23, out24);
Expand Down Expand Up @@ -168,7 +168,9 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec)
writes(data59, out60);
}] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["bounds::bounds", bounds] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::cells", (_2160: rt.u8, data60: rt.ByteSlice, out61: rt.ByteVec): void => {
pointers_cells(data60, out61);
}] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::parses", ints_parses] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::edits", strs_edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::subslices", subslices] 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], ["ints::parses", ints_parses] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::edits", strs_edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::subslices", subslices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["hashes::bytes", (_2161: rt.u8, data61: rt.ByteSlice, out62: rt.ByteVec): void => {
hashes_bytes(data61, out62);
}] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::bytes", keys_bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8);
}

/** Run one case. */
Expand Down
148 changes: 148 additions & 0 deletions ts/fixtures/lang/expected/hashes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { copy$u8s } from "./lib.ts";
import { thenSome$0n2gkz4 } from "./core/bool.ts";
import { unwrapOr$0k15iai, unwrapOr$0r2bmpr } from "./core/option.ts";
import { unwrapOr$01jdcm7 } from "./core/result.ts";
Expand All @@ -12,13 +13,27 @@ import * as Priorities from "./hashes.Priorities.ts";
import * as Shape from "./hashes.Shape.ts";
import * as Small from "./hashes.Small.ts";

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Arc_Vec_u8(value: rt.Arc<rt.ByteVec>, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashArc(rt.hashBytes)(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Arc_str(value: rt.Arc<string>, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashArc(rt.hashStr)(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Arc_u8s(value: rt.Arc<Uint8Array>, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashArc(rt.hashBytes)(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Dirs(value: Dir.Dir[], out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
Expand Down Expand Up @@ -54,13 +69,27 @@ export function digest$Option_Option_u8(value: rt.u8 | undefined | rt.None, out:
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Option_Vec_u8(value: rt.ByteVec | undefined, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashOption(rt.hashBytes)(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Option_u32(value: rt.u32 | undefined, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashOption(rt.hash32)(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Option_u8s(value: Uint8Array | undefined, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashOption(rt.hashBytes)(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Ordering(value: rt.Ordering, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
Expand Down Expand Up @@ -110,6 +139,13 @@ export function digest$Vec_u16(value: rt.u16[], out: rt.ByteVec): void {
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$Vec_u8(value: rt.ByteVec, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashBytes(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$bool(value: boolean, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
Expand Down Expand Up @@ -236,6 +272,55 @@ export function digest$u8_str_u64(value: [rt.u8, string, rt.u64], out: rt.ByteVe
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$u8s(value: rt.ByteSlice, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashBytes(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$u8s$2(value: rt.ByteSlice, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashBytes(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$u8s$3(value: rt.ByteSlice, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashBytes(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$u8s$4(value: rt.ByteSlice, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashBytes(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$u8s_Option_u8s(value: [Uint8Array, Uint8Array | undefined], out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashTuple([rt.hashBytes, rt.hashOption(rt.hashBytes)])(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$u8s_u8(value: [rt.ByteSlice, rt.u8], out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashTuple([rt.hashBytes, rt.hash8])(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$u8s_u8s(value: [Uint8Array, Uint8Array], out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
rt.hashTuple([rt.hashBytes, rt.hashBytes])(value, h);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u64ToBeBytes(rt.hashFinish(h)).reverse()));
}

/** `DefaultHasher::new`'s hash of `value`, little-endian. */
export function digest$usize(value: rt.usize, out: rt.ByteVec): void {
const h: rt.Hasher = rt.hasher();
Expand Down Expand Up @@ -423,4 +508,67 @@ export function random(data: rt.ByteSlice, out: rt.ByteVec): void {
}
}

/**
* What `Hash` writes of bytes, which is alike in each form, a `[u8; N]`, a `&[u8]`, or a
* `Vec<u8>`: its length as a `usize`, then the bytes. Held in a tuple, an `Option`, and an
* `Arc` too, and through `RandomState`'s `hash_one`, which hashes each form alike.
*/
export function bytes(data: rt.ByteSlice, out: rt.ByteVec): void {
const iter: rt.RangeInclusive<rt.usize> = new rt.RangeInclusive(0, rt.min(data.length, 5), rt.usizeStep);
while (true) {
const _8: rt.usize | rt.Done = iter.next();
if (_8 === rt.DONE) {
break;
}
const i: rt.usize = _8;
const s: rt.ByteSlice = rt.byteSliceRange(data, 0, i);
digest$u8s$4(s, out);
digest$Vec_u8(rt.byteSliceToByteVec(s), out);
digest$u8s_u8([s, i & 0xff] as [rt.ByteSlice, rt.u8], out);
digest$Option_Vec_u8(rt.byteSliceToByteVec(s), out);
const _28: rt.Arc<rt.ByteVec> = new rt.Arc(rt.byteSliceToByteVec(s));
digest$Arc_Vec_u8(_28, out);
_28[Symbol.dispose]();
}
digest$u8s(rt.byteSliceFrom(Uint8Array.of()), out);
digest$u8s_Option_u8s([Uint8Array.of(), Uint8Array.of()] as [Uint8Array, Uint8Array | undefined], out);
const state: rt.RandomState = rt.randomState();
const iter2: rt.Iter<rt.ByteSlice> = rt.byteSliceWindows(data, 3);
while (true) {
const _41: rt.ByteSlice | rt.Done = iter2.next();
if (_41 === rt.DONE) {
break;
}
rt.idx(_41, 0);
const _46: rt.u8 = _41.data[_41.start];
rt.idx(_41, 1);
const _50: rt.u8 = _41.data[_41.start + 1];
rt.idx(_41, 2);
const a: Uint8Array = Uint8Array.of(_46, _50, _41.data[_41.start + 2]);
rt.idx(_41, 0);
const _59: rt.u8 = rt.u8Rem(_41.data[_41.start], 2);
rt.idx(_41, 1);
const pair: Uint8Array = Uint8Array.of(_59, rt.u8Rem(_41.data[_41.start + 1], 2));
digest$u8s$3(rt.byteSliceFrom(a), out);
digest$u8s$2(rt.byteSliceFrom(pair), out);
digest$u8s_u8s([pair, a] as [Uint8Array, Uint8Array], out);
digest$Option_u8s(pair, out);
const _83: rt.Arc<Uint8Array> = new rt.Arc(copy$u8s(a));
digest$Arc_u8s(_83, out);
_83[Symbol.dispose]();
const _87: rt.u64 = rt.hashOne(state, a, rt.hashBytes);
const _91: rt.ByteSlice = rt.byteSliceRange(_41, 0, 3);
rt.byteVecPush(out, rt.u64Eq(_87, rt.hashOne(state, _91, rt.hashBytes)) ? 1 : 0);
const _96: rt.u64 = rt.hashOne(state, rt.byteSliceFrom(a), rt.hashBytes);
const _101: rt.ByteVec = rt.byteSliceToByteVec(_41);
rt.byteVecPush(out, rt.u64Eq(_96, rt.hashOne(state, _101, rt.hashBytes)) ? 1 : 0);
const _105: rt.u64 = rt.hashOne(state, [pair, 1] as [Uint8Array, rt.u8], rt.hashTuple([rt.hashBytes, rt.hash8]));
const _110: [rt.ByteSlice, rt.u8] = [rt.byteSliceRange(_41, 0, 2), 1];
rt.byteVecPush(out, rt.u64Eq(_105, rt.hashOne(state, _110, rt.hashTuple([rt.hashBytes, rt.hash8]))) ? 1 : 0);
}
const _116: rt.u64 = rt.hashOne(state, Uint8Array.of(), rt.hashBytes);
const _120: rt.ByteVec = new rt.ByteVec();
rt.byteVecPush(out, rt.u64Eq(_116, rt.hashOne(state, _120, rt.hashBytes)) ? 1 : 0);
}

//# sourceMappingURL=hashes.ts.map
Loading