nanvm_lib/vm/impls/from.rs
1//! One `From<X> for Unpacked<A>` per payload type, written by hand on
2//! purpose; `try_from.rs` holds the matching `TryFrom<Any<A>> for X`.
3//!
4//! A sealed helper trait (`nanvm/lib/AGENTS.md` §2.1, rung 1) carrying each
5//! type's variant can derive only the `From` half: with a `Sealed`
6//! supertrait, `impl<A, T: Variant<A>> From<T> for Unpacked<A>` compiles,
7//! but `impl<A, T: Variant<A>> TryFrom<Any<A>> for T` breaks the orphan rule
8//! (E0210), so each type keeps a concrete `TryFrom`. Every type would then
9//! carry a `Variant` impl (its wrap and its unwrap) and a `TryFrom` that
10//! delegates to it, which is longer than these impls and removes no
11//! per-type code; a `build.rs` table (rung 2) is machinery for eight rows.
12//! So the duplication stays (rung 3). Each impl names its variant, and the
13//! payload type fixes which variant it can be, so a mismatch does not
14//! compile.
15//!
16//! `f64` is not a payload: it enters `Unpacked::Number` through `Number`'s
17//! canonicalizing conversion, and nothing comes back out as `f64`, so it has a
18//! `From` and no `TryFrom`.
19
20use crate::vm::{Array, BigInt, Function, IVm, Number, Object, String, Unpacked, nullish::Nullish};
21
22impl<A: IVm> From<Nullish> for Unpacked<A> {
23 fn from(value: Nullish) -> Self {
24 Unpacked::Nullish(value)
25 }
26}
27
28impl<A: IVm> From<bool> for Unpacked<A> {
29 fn from(value: bool) -> Self {
30 Unpacked::Boolean(value)
31 }
32}
33impl<A: IVm> From<Number> for Unpacked<A> {
34 fn from(value: Number) -> Self {
35 Unpacked::Number(value)
36 }
37}
38
39impl<A: IVm> From<f64> for Unpacked<A> {
40 fn from(value: f64) -> Self {
41 Unpacked::Number(value.into())
42 }
43}
44
45impl<A: IVm> From<String<A>> for Unpacked<A> {
46 fn from(value: String<A>) -> Self {
47 Unpacked::String(value)
48 }
49}
50
51impl<A: IVm> From<BigInt<A>> for Unpacked<A> {
52 fn from(value: BigInt<A>) -> Self {
53 Unpacked::BigInt(value)
54 }
55}
56
57impl<A: IVm> From<Object<A>> for Unpacked<A> {
58 fn from(value: Object<A>) -> Self {
59 Unpacked::Object(value)
60 }
61}
62
63impl<A: IVm> From<Array<A>> for Unpacked<A> {
64 fn from(value: Array<A>) -> Self {
65 Unpacked::Array(value)
66 }
67}
68
69impl<A: IVm> From<Function<A>> for Unpacked<A> {
70 fn from(value: Function<A>) -> Self {
71 Unpacked::Function(value)
72 }
73}