← ClaudeAtlas

rust-type-erasurelisted

Use when you store values in a type-keyed map or erase a type at run time — Box<dyn Any>, TypeId, downcast_ref, a request-extension map, a resource registry, or an ECS-style world — and above all when the values are not 'static. Covers why Any is bound to 'static and how that bound surfaces as E0597 at the caller instead of at the map, why type_name is not a type key, a three-rung ladder from a lifetime-parameterized enum through a plain Box<dyn Any> type map to the GAT owner/element bijection that keys borrowed data on a 'static tag, the E0271 and lifetime-mismatch collisions the compiler rejects for you, the for<'x> bound a generic helper needs, the use-after-free an extractor layer reintroduces when it detaches the lifetime, and the E0117 orphan limit on shipping the pattern as a library. Triggers on "TypeId", "dyn Any", "downcast_ref", "type erasure", "anymap", "type map", "extensions map", "type_name", "non-static Any", "GAT bijection", "resource registry", "system param", or "E0117".
po4yka/rust-skills · ★ 2 · Data & Documents · score 76
Install: claude install-skill po4yka/rust-skills
# Rust type erasure ## Purpose Decide how to hold values whose types the code does not name, and stop before you erase more than you must. The one sentence not to get wrong: **`Any` keys nothing that borrows**, because `impl<T: 'static + ?Sized> Any for T` is the only impl, and the error for that bound lands on your caller, not on your map. This skill owns the design of a type-keyed store, not the taste question of whether to erase at all. `rust-discipline` owns that. Every error text and observed output below comes from rustc 1.97.0, edition 2024, on aarch64-apple-darwin, with Miri 0.1.0 on nightly. ## Route the symptom to a section | Symptom or task | Section | | --- | --- | | `error[E0597]: ... argument requires that s is borrowed for 'static`, or `error: lifetime may not live long enough ... requires that 'a must outlive 'static` | [`Any` is bound to `'static`](#any-is-bound-to-static) | | You reached for `type_name` because `TypeId::of` refused | [`type_name` is not a type key](#type_name-is-not-a-type-key) | | Choosing between an enum, a `dyn Any` map, and something exotic | [The ladder](#the-ladder) | | You must store a `Cow<'a, str>`, a `&'a [u8]`, or any borrow, keyed by type | [Rung 3](#rung-3-an-open-set-of-borrowed-values) | | `error[E0271]: type mismatch resolving <P<'a> as Element<'a>>::Owner == Tag` | [The bijection is the collision proof](#the-bijection-is-the-collision-proof) | | `error: incompatible lifetime on type` on an `Owner` or `Element` impl | [T