Skip to content

Generate implementations from written derive headers - #2052

Draft
micahscopes wants to merge 9 commits into
feat/typed-trait-familiesfrom
feat/written-typed-derive
Draft

micahscopes wants to merge 9 commits into
feat/typed-trait-familiesfrom
feat/written-typed-derive

Conversation

@micahscopes

Copy link
Copy Markdown
Collaborator
use core::derive::Derive
use core::meta::{Code, Item, Record, type_ref}

trait Wire { const CODE: u256 }
impl Wire for u8 { const CODE: u256 = 32 }
impl Wire for u16 { const CODE: u256 = 64 }

trait Decode<A> {
    const fn decode(_ value: A) -> A
    const fn wire_code() -> u256
}

struct Fields {}
impl<A: Wire> Derive<trait Decode<A>> for Fields {
    const fn derive(_ record: Record) -> Code<Item> {
        let wire = type_ref<A>()
        let code = A::CODE
        quote {
            const fn decode(_ value: ${wire}) -> ${wire} { value }
            const fn wire_code() -> u256 { ${code} }
        }
    }
}

struct Packet {}
impl Decode<u8> for Packet with (Fields)
impl Decode<u16> for Packet with (Fields)

#[test]
fn one_provider_supplies_both_implementations() {
    let byte: u8 = <Packet as Decode<u8>>::decode(7)
    let word: u16 = <Packet as Decode<u16>>::decode(300)
    assert!(byte == 7 && word == 300)
    assert!(<Packet as Decode<u8>>::wire_code() == 32)
    assert!(<Packet as Decode<u16>>::wire_code() == 64)
}

Both headers use one registration. The selected type determines the generated method signature and the constant it returns. The two implementations return 32 and 64 from wire_code.

The same preparation path supports impl Packet with (Provider) for generated inherent methods and constants. Its provider implements DeriveInherent.

  • Reflection. A provider receives the compiler-issued Record for the target it is allowed to inspect. Fe code can walk struct fields and enum payloads and use their types to build expressions or items.
  • Quasiquotation. The example splices a type and a constant into a Code<Item> value. Templates carry the scopes of the code that wrote them. The resulting signatures and bodies undergo ordinary implementation checks.
  • Preparation. The compiler checks all written requests, evaluates their providers against authored source and sealed dependency output, then checks what they generate together. A provider in the package sees the other written headers as declarations while their generated members are still unavailable.

Written provider selection builds on ordinary trait-family arguments. Writing or omitting a default that produces the same trait application yields the same candidates. A selected provider still has to satisfy its declared bounds.

The standalone driver checks covered 67 cases. The example passes both applied families through the same provider, and the preparation tests cover written prerequisites and generated members across packages.

@micahscopes
micahscopes added this pull request to stack #2051 November 20, 2026 21:59
@micahscopes
micahscopes force-pushed the feat/written-typed-derive branch from 93f6266 to ec7d5d6 Compare November 20, 2026 22:14

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant