8000 A #[host_module] type declares the ---@trait interfaces it implements, and a Rust trait is declared to Teal as one · Issue #570 · ynishi/htl · GitHub
[go: up one dir, main page]

Skip to content

A #[host_module] type declares the ---@trait interfaces it implements, and a Rust trait is declared to Teal as one #570

Open

Description

@ynishi

Problem

A Rust host type exposed through #[host_module] cannot say in Teal that it implements a ---@trait interface (#564). A Teal function written <T is Shape>, or one taking a Shape, therefore cannot accept a host value even when the host implements every method. Going the other way, a Rust trait that the host wants Teal code to implement has no Teal declaration.

Evidence

Read from the code, not reproduced:

  • #[host_module] writes its .d.tl "from a plain impl block" (crates/htl-macros/src/lib.rs:6). Its sibling #[c_export] refuses a trait impl outright: "c_export: put it on the inherent impl block, not on a trait impl" (crates/htl-macros/src/lib.rs:1957).
  • The .d.tl it writes declares a record per host type, with no is clause (crates/htl-core/src/dts.rs, "What each Rust shape becomes").

Proposal

Two halves, which can land separately:

  1. A host type implements a Teal trait. #[host_module(impls = [Shape])] (spelling to be decided) adds is Shape to the record the .d.tl declares for the type. The trait-impl lint then checks the host's methods against the trait as it checks a Teal record's.
  2. A Rust trait declared to Teal. An attribute on a Rust trait (#[host_trait], spelling to be decided) writes a ---@trait interface into the .d.tl, with the same type mapping #[host_module] uses for methods. Teal records then implement it with Trait-like polymorphism on plain Teal: a ---@trait interface, default methods through htl.trait.impl, and a trait-impl lint #564's markers.

Alternative: the host writes the is and the interface by hand in a separate .d.tl. That is the hand-written copy #[host_module] exists to avoid.

Acceptance

  1. A #[host_module] type marked as implementing Shape gets is Shape in the written .d.tl. A Teal function <T is Shape>(x: T) accepts the host value without a cast.
  2. If that host type lacks a method of Shape, htl check reports trait-impl (or the macro fails to compile), naming the type and the method.
  3. A Rust trait with the new attribute is written as a ---@trait interface in the .d.tl. A Teal record that is it and leaves out a method gets the trait-impl finding.
  4. htl dts writes the same declarations without a build, as it does for #[host_module] today.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0