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 @@ -33,7 +33,8 @@ generic std gap is planned here even when moq never calls it.
- [A crate's own find and for_each](/quest/m3/ts-iter-own-calls.md) - a crate's iterator that overrides `find`, `rfind`, `find_map`, or `for_each` translates, each called where std's adaptors and consumers call it
- [A vector's exact capacity as it shrinks](/quest/m3/ts-vec-exact-capacity.md) - a deque made from a `Vec` splits its slices where std's does
- [A B-tree's searches call Ord as std's do](/quest/m3/ts-btree-search-order.md) - a B-tree's lookups and inserts call a key's own `Ord` in std's order and number
- [Rc and Arc of a slice](/quest/m3/ts-shared-slices.md) - `Rc<[T]>` and `Arc<[T]>` from a `Vec`, a slice, or an iterator translate
- [A Vec from a slice, and a slice's chunks as arrays](/quest/m3/ts-slice-conversions.md) - `Vec::from(&[T])` and `as_chunks` translate
- [An Arc's pointer](/quest/m3/ts-arc-pointers.md) - `Arc::as_ptr` and `Rc::as_ptr` translate, comparable by `ptr::addr_eq`
- [The rest of a vector's methods](/quest/m3/ts-vec-rest.md) - `append`, `resize`, `split_off`, `try_reserve`, and `Vec::from(VecDeque)` translate
- [A reversed zip's per-pair cost](/quest/m3/ts-zip-rev-overhead.md) - a reversed by-index zip costs per pair what a forward one does, or the gap is explained
- [Whole writes into a lazy cell or thread-local key](/quest/m3/ts-lazy-writes.md) - a whole write through a `&mut` to a `LazyCell`, `LazyLock`, or `LocalKey` keeps its state
Expand Down
16 changes: 16 additions & 0 deletions quest/m3/ts-arc-pointers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# [S] An Arc's pointer

## Goal

[rust2-ts](/ts/README.md) translates `Arc::as_ptr` and `Rc::as_ptr`, of a sized value and
of a slice, and `ptr::addr_eq` of the pointers they give, proven by a differential test
against native Rust that `make_mut` keeps the allocation only when no other owner shares it.

## Plan

Found writing the `lang/shares` fixture for `Rc<[T]>`: `Rc::as_ptr` and `ptr::addr_eq` are
refused as unsupported calls. An `rt.Arc` is its allocation, so its pointer may be the
`rt.Arc` itself or a place of its value; the choice must keep what `ptr::eq` and
`addr_eq` already compare. No consumer calls them yet; they are planned here as part of std.

Public API: the translator's output.
17 changes: 0 additions & 17 deletions quest/m3/ts-shared-slices.md

This file was deleted.

15 changes: 15 additions & 0 deletions quest/m3/ts-slice-conversions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# [S] A Vec from a slice, and a slice's chunks as arrays

## Goal

[rust2-ts](/ts/README.md) translates `Vec::from` of a `&[T]` and a `&mut [T]`, and
`slice::as_chunks`, `as_rchunks`, and `as_chunks_mut`, proven by differential tests
against native Rust at `u8`, at another element, and at one that drops.

## Plan

Found writing the `lang/shares` fixture for `Rc<[T]>`: `Vec::<u8>::from(&[u8])` and
`<[u8]>::as_chunks::<2>` are refused as unsupported calls, while `to_vec` and
`chunks_exact` translate. No consumer calls them yet; they are planned here as part of std.

Public API: the translator's output.
4 changes: 4 additions & 0 deletions ts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ crate translation reaches is refused, and so are two versions of one crate it re
| `static` | a module's value, named for the static, that every use shares, read from the memory rustc evaluated; one that reads another static or a class is an `rt.LazyCell` of it, made on first use, since that may not be made yet when its module is. A `static mut`, and a pointer in one into memory the translation can't name, fail translation |
| a type that drops | a class with `[Symbol.dispose]`: its `Drop` impl, then its fields |
| a type's function | a function of the type's module, taking `self` first: `Decoder.slice(dec, 4)` after `import * as Decoder` |
| `Arc<T>`, `Rc<T>`, `Weak<T>`, `Mutex<T>` | `rt.Arc` for either, with its strong and weak counts, and `new_cyclic`'s `Weak` upgrading only once its value is made, `get_mut` lending its value only while no other `Arc` or `Weak` shares it, `make_mut` cloning it into a new `rt.Arc` while another `Arc` shares it, or moving it there while only `Weak`s do, which then dangle, and `into_inner` giving it only from the last strong `Arc`, after which its `Weak`s no longer upgrade; `rt.Weak`, counted in its `rt.Arc` until it drops, and of no `Arc` once made by `Weak::new`; `rt.Mutex`, its own guard, which a lock gives as `Ok` and the guard's drop unlocks: locking it again while held panics and `try_lock` gives `WouldBlock`, as wasm32's do, and a guard's `map` and `filter_map` make `rt.MutexMap`s, each where the part it lends is, which hold the mutex locked until they drop, and a panic aborts, so nothing poisons it and `thread::panicking` is `false`; a `Mutex`'s, a `RefCell`'s, and an `RwLock`'s `into_inner` gives its value and `get_mut` lends it, or an `rt.Place` of it where JS copies it |
| `Arc<T>`, `Rc<T>`, `Weak<T>`, `Mutex<T>` | `rt.Arc` for either, of what a `Box<T>` is for a slice or `str`, with its strong and weak counts, an `Arc<[u8]>` of an `Arc<str>`'s bytes sharing its count, and `new_cyclic`'s `Weak` upgrading only once its value is made, `get_mut` lending its value only while no other `Arc` or `Weak` shares it, `make_mut` cloning it into a new `rt.Arc` while another `Arc` shares it, or moving it there while only `Weak`s do, which then dangle, and `into_inner` giving it only from the last strong `Arc`, after which its `Weak`s no longer upgrade; `rt.Weak`, counted in its `rt.Arc` until it drops, and of no `Arc` once made by `Weak::new`; `rt.Mutex`, its own guard, which a lock gives as `Ok` and the guard's drop unlocks: locking it again while held panics and `try_lock` gives `WouldBlock`, as wasm32's do, and a guard's `map` and `filter_map` make `rt.MutexMap`s, each where the part it lends is, which hold the mutex locked until they drop, and a panic aborts, so nothing poisons it and `thread::panicking` is `false`; a `Mutex`'s, a `RefCell`'s, and an `RwLock`'s `into_inner` gives its value and `get_mut` lends it, or an `rt.Place` of it where JS copies it |
| `PoisonError<T>`, `TryLockError<T>` | the `T` it holds; `rt.TryLockError<T>`, tagged as `Bound` is |
| `Cell<T>`, `UnsafeCell<T>`, `RefCell<T>`, `RwLock<T>` | `rt.Cell`, the place holding its value in `v`, for either of the first two: `UnsafeCell::get`'s pointer is the cell where it is an `rt.Place`, and otherwise the object in `v`, refused where the crate may put another cell or object in its place; `rt.RefCell` for either of the others, its own guard, counting its borrows: borrowing it while that conflicts panics as `RefCell`'s does, and locking it so aborts as wasm32's `RwLock` does; `try_borrow` and `try_borrow_mut` fail with `rt.BorrowError` and `rt.BorrowMutError` instead, a clone borrows the cell, `Ref::clone` counts another borrow of it, and a guard's `map`, `filter_map`, `try_map`, and `map_split`, an `RwLock` guard's included, make `rt.RefMap`s, each where a part it lends is, which a write through a mapped `RefMut` reaches in place, sharing the cell's borrow until the last drops; `RwLockWriteGuard::downgrade` keeps the lock as one read, and `Ref::leak` and `RefMut::leak` keep the borrow until `RefCell::undo_leak` ends it; refused for a part of a sized type JS can't write in place, such as a binding's |
| `thread_local!`, `LocalKey<T>` | an `rt.LocalKey`, a module's value, which makes its value on first use from the `const` or the expression and never drops it, as wasm32's do; `with` calls its function on that value, `try_with` never fails, the `Cell` and `RefCell` helpers lower from std, and a `RefCell` key's `set` and `with_borrow_mut` borrow it as `borrow_mut` does |
Expand Down
14 changes: 14 additions & 0 deletions ts/fixtures/lang/shares/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "lang-shares-fixture"
description = "A part of the language rust2-ts translates, exercised against native Rust"
version = "0.0.0"
edition.workspace = true
license.workspace = true
publish = false

[lib]
# The cdylib is the native side of the differential test, built for wasm32.
crate-type = ["lib", "cdylib"]

[lints]
workspace = true
41 changes: 41 additions & 0 deletions ts/fixtures/lang/shares/expected/core/option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "../rt.ts";
import type * as Logged from "../shares.Logged.ts";

export function isSomeAnd$0vr0nda(self: rt.Arc<rt.ByteSlice> | undefined, f: (a0: rt.Arc<rt.ByteSlice>) => boolean): boolean {
if (self === undefined) {
return false;
}
return f(self);
}

export function unwrapOr$0r2bmpr(self: rt.u8 | undefined, default_: rt.u8): rt.u8 {
if (self === undefined) {
return default_;
}
return self;
}

export function mapOr$1e4a1e0(self: rt.Ordering | undefined, default_: rt.u8, f: (a0: rt.Ordering) => rt.u8): rt.u8 {
if (self === undefined) {
return default_;
}
return f(self);
}

export function mapOr$03siheh(self: rt.Arc<string> | undefined, default_: rt.u8, f: (a0: rt.Arc<string>) => rt.u8): rt.u8 {
if (self === undefined) {
return default_;
}
return f(self);
}

export function mapOr$148vfk5(self: Logged.Logged | undefined, default_: rt.u8, f: (a0: Logged.Logged) => rt.u8): rt.u8 {
if (self === undefined) {
return default_;
}
return f(self);
}

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

import * as rt from "./rt.ts";
import { cases } from "./lib.ts";

/** Run one case. */
export function run(input: rt.ByteSlice): rt.ByteVec {
const out: rt.ByteVec = new rt.ByteVec();
if (!(input.length >= 3)) {
return out;
}
const cases_: [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined][] = cases();
cases_[rt.idx(cases_, rt.usizeRem(((input.data[input.start] << 8) | input.data[input.start + 1]) as rt.usize, cases_.length))][1](input.data[input.start + 2], new rt.ByteSlice(input.data, input.start + 3, input.length - 3), out);
return out;
}

/**
* The first two bytes, as one big-endian number, that pick the case named `name`, which
* `lang.ts` targets by.
*/
export function op(name: string): rt.u16 {
const _11: rt.usize | undefined = rt.iterPosition(rt.iterSlice(cases()), (_2: [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]): boolean => {
return _2[0] === name;
});
if (_11 !== undefined) {
const _20: rt.Result<rt.u16, rt.TryFromIntError> = rt.u16TryFrom(_11);
if (_20.ok) {
if ((_20.value >> 8) !== 255) {
return _20.value;
}
}
const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, name)];
rt.panicFmt(["case ", args4[0], " is past what two bytes pick"]);
} else {
const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, name)];
rt.panicFmt(["no case ", args2[0]]);
}
}

/** Write `s` and the byte no UTF-8 holds, which ends it. */
export function put(out: rt.ByteVec, s: string): void {
rt.byteVecExtendFromSlice(out, rt.stringAsBytes(s));
rt.byteVecPush(out, 255);
}

/** A char of the alphabet for each byte, by its low bits. */
export function text(data: rt.ByteSlice): string {
let s: string = "";
const iterS: Uint8Array = rt.byteSliceView(data);
let iterI: number = 0;
while (true) {
const _5: rt.u8 | rt.Done = iterI < iterS.length ? iterS[iterI++] : rt.DONE;
if (_5 === rt.DONE) {
return s;
}
const _13: rt.usize = rt.u8Rem(_5, 16) as rt.usize;
if (!(_13 < 16)) {
rt.panic("index out of bounds: the len is " + 16 + " but the index is " + _13);
}
s = s + String.fromCodePoint([47, 92, 97, 48, 57, 127, 128, 233, 2047, 2048, 55295, 57344, 65535, 65536, 128512, 1114111][_13]);
}
}

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

import * as rt from "./rt.ts";
import { bytes, wides, copies, drops, strs, generics, keys } from "./shares.ts";
import type * as Logged from "./shares.Logged.ts";

export function copy$array_u8(v: Uint8Array): Uint8Array {
return v.slice();
}

export function copy$tuple2_u8_u16(v: [rt.u8, rt.u16]): [rt.u8, rt.u16] {
return [v[0], v[1]];
}

export function drop$Option_Rc_slice_shares_Logged(_1: rt.Arc<Logged.Logged[]> | undefined): void {
if (_1 !== undefined) {
rt.arcDrop(_1, drop$slice_shares_Logged);
}
}

export function drop$Rc_slice_shares_Logged(value: rt.Arc<Logged.Logged[]>): void {
rt.arcDrop(value, drop$slice_shares_Logged);
}

export function drop$slice_shares_Logged(value: Logged.Logged[]): void {
rt.dropAll(value);
}

/**
* Each case's name and what it runs. The first two bytes, big-endian, pick one modulo
* their count, so a case added at the end moves no other.
*/
export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined][] {
return [["shares::bytes", bytes as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["shares::wides", wides as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["shares::copies", copies as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["shares::drops", drops as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["shares::strs", strs as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["shares::generics", generics as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["shares::keys", keys as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]];
}

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

import * as rt from "./rt.ts";
import { drop$slice_shares_Logged } from "./lib.ts";
import type * as Logged from "./shares.Logged.ts";

/** A shared slice in a field, beside one that drops after it. */
export class Held {
items: rt.Arc<Logged.Logged[]>;
last: Logged.Logged;

constructor(items: rt.Arc<Logged.Logged[]>, last: Logged.Logged) {
this.items = items;
this.last = last;
}

[Symbol.dispose](): void {
rt.arcDrop(this.items, drop$slice_shares_Logged);
this.last[Symbol.dispose]();
}
}

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

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

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

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

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

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

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

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

import * as rt from "./rt.ts";
import { drop$Rc_slice_shares_Logged } from "./lib.ts";
import * as Logged from "./shares.Logged.ts";

/**
* A shared slice of its parameter, made from a `Vec` and an iterator, cloned, and written,
* as each instance holds it.
*/
export class Shared<T> {
items: rt.Arc<rt.SliceOf<T>>;

constructor(items: rt.Arc<rt.SliceOf<T>>) {
this.items = items;
}

[Symbol.dispose](): void {
rt.arcDrop(this.items as rt.Arc<T[]>, (value: T[]) => rt.dropAll(value));
}
}

export function new$shares_Logged(items: Logged.Logged[]): Shared<Logged.Logged> {
return new Shared(new rt.Arc(items));
}

export function new$u8(items: rt.ByteVec): Shared<rt.u8> {
return new Shared(new rt.Arc(rt.byteVecSlice(items)));
}

export function collected$u16_iter_Map_slice_Iter_u8_shares_generics_closure0(items: rt.Iter<rt.u16>): Shared<rt.u16> {
return new Shared(new rt.Arc(rt.iterToArray(items, undefined, 2, true)));
}

export function first$shares_Logged(self: Shared<Logged.Logged>): Logged.Logged | undefined {
return rt.sliceFirst((self.items as rt.Arc<Logged.Logged[]>).value);
}

export function first$u8(self: Shared<rt.u8>): rt.u8 | undefined {
return rt.byteSliceFirst((self.items as rt.Arc<rt.ByteSlice>).value);
}

/** Its items, made its own, with `item` in place of its last. */
export function setLast$shares_Logged(self: Shared<Logged.Logged>, item: Logged.Logged): void {
let _9: boolean = false;
_9 = true;
const _3: Logged.Logged | undefined = rt.sliceLast(rt.arcMakeMut(rt.place(() => self.items as rt.Arc<Logged.Logged[]>, (value) => (self.items as rt.Arc<Logged.Logged[]>) = value), (a0: Logged.Logged[]): Logged.Logged[] => {
return a0.map(Logged.clone$Clone);
}, drop$Rc_slice_shares_Logged));
if (_3 !== undefined) {
_9 = false;
_3[Symbol.dispose]();
Object.assign(_3, item);
}
if (_9) {
item[Symbol.dispose]();
}
}

/** Its items, made its own, with `item` in place of its last. */
export function setLast$u16(self: Shared<rt.u16>, item: rt.u16): void {
const _3: rt.Place<rt.u16> | undefined = rt.sliceLastMut(rt.arcMakeMut(rt.place(() => self.items as rt.Arc<rt.u16[]>, (value) => (self.items as rt.Arc<rt.u16[]>) = value), (a0: rt.u16[]): rt.u16[] => {
return a0.slice();
}, rt.drop));
if (_3 !== undefined) {
_3.v = item;
}
}

/** Its items, made its own, with `item` in place of its last. */
export function setLast$u8(self: Shared<rt.u8>, item: rt.u8): void {
const _3: rt.Place<rt.u8> | undefined = rt.sliceLastMut(rt.byteSliceView(rt.arcMakeMut(rt.place(() => self.items as rt.Arc<rt.ByteSlice>, (value) => (self.items as rt.Arc<rt.ByteSlice>) = value), (a0: rt.ByteSlice): rt.ByteSlice => {
return rt.byteSliceFrom(rt.byteSliceToArray(a0));
}, rt.drop)));
if (_3 !== undefined) {
_3.v = item;
}
}

export function clone$Clone_shares_Logged(self: Shared<Logged.Logged>): Shared<Logged.Logged> {
return new Shared(rt.arcClone(self.items as rt.Arc<Logged.Logged[]>));
}

export function clone$Clone_u16(self: Shared<rt.u16>): Shared<rt.u16> {
return new Shared(rt.arcClone(self.items as rt.Arc<rt.u16[]>));
}

export function clone$Clone_u8(self: Shared<rt.u8>): Shared<rt.u8> {
return new Shared(rt.arcClone(self.items as rt.Arc<rt.ByteSlice>));
}

//# sourceMappingURL=shares.Shared.ts.map
Loading