Skip to content

token.invoke_rite() — closure-based #[target_feature] entry without proc macros #7

Description

@lilith

Summary

Add a invoke_rite() method to all tokens that wraps a closure in the
appropriate #[target_feature] boundary, providing #[arcane]-like
functionality without proc macros.

Motivation

jxl-rs's SimdDescriptor::call() pattern demonstrates a clean way to
create a #[target_feature] boundary using only a method + closure:

if let Some(token) = Desktop64::summon() {
    token.invoke_rite(|t| process_simd(t, data))
}

This is useful when:

  • A proc-macro dependency is undesirable (lightweight crates)
  • Inline one-off dispatch is cleaner than a named #[arcane] function
  • Users want to experiment without pulling in archmage-macros

#[arcane] remains the recommended approach for named entry points.
invoke_rite() fills the gap for inline/closure use cases.

Design Sketch

impl Desktop64 {
    pub fn invoke_rite<R>(self, f: impl FnOnce(Self) -> R) -> R {
        #[target_feature(enable = "avx2,fma,bmi1,bmi2,f16c")]
        unsafe fn inner<R>(
            token: Desktop64,
            f: impl FnOnce(Desktop64) -> R,
        ) -> R {
            f(token)
        }
        // SAFETY: Token existence proves CPU features are available.
        unsafe { inner(self, f) }
    }
}

Generated per-token by xtask from token-registry.toml, like everything else.

Open Questions

  • Naming: invoke_rite(), call(), enter(), cast()?
    Thematic preference leans toward invoke_rite or cast.
  • Trait vs inherent: Should this be on the SimdToken trait or per-token inherent methods?
    Trait method would require GATs or boxing; inherent methods are simpler.
  • Inlining: Should the generated code use #[inline(never)] (like jxl-rs) or
    #[inline]? jxl-rs uses #[inline(never)] to prevent the
    feature-enabled code from inlining back into the non-feature caller.
    This matches #[arcane] semantics (optimization boundary at entry).

Prior Art

  • jxl-rs SimdDescriptor::call() — same pattern, #[inline(never)]
  • archmage #[arcane] — proc-macro version of the same idea

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions