Expand description
WritableKeyPath built at run time, without a compiler.
\EnvironmentValues.contentTransition is not a symbol. It is a pattern —
a compact, compiler-emitted description that swift_getKeyPath turns into a
key path object — and several of SwiftUI’s modifiers are
@_alwaysEmitIntoClient wrappers whose inlined body is
environment(\.someKey, value). Reaching those from Rust means emitting the
pattern.
§The pattern
Forty-eight bytes, every internal reference a 32-bit offset from the field holding it:
| offset | contents |
|---|---|
+0 | relative pointer to an 8-byte instantiation cache, which must be writable |
+4 | zero |
+8 | relative pointer to the root type’s mangled name, bit 0 set |
+12 | relative pointer to the value type’s mangled name, bit 0 set |
+16 | zero |
+20 | KeyPathBuffer::Header — 0x80000018: trivial, 24 bytes of components |
+24 | component header 0 — kind External, no substitutions |
+28 | relative pointer to the property descriptor, with bit 0 set for indirect |
+32 | component header 0x02c00002 — kind Computed, mutating, settable, id resolved by indirect pointer |
+36 | relative pointer to a word holding the property’s getter — the component’s identity |
+40 | relative pointer to the getter thunk |
+44 | relative pointer to the setter thunk |
The external component names the resilient property so the runtime can use the framework’s own key path if it has one; the computed component after it is the local fallback, and the one that actually runs.
§Why a static
Those offsets are i32, and the thunks below are code — they live in this
binary’s text segment, which is further from the heap than two gigabytes
spans. So the pattern cannot be a Box: it is a static mut, which puts it
in the same image as the thunks and makes every offset small. The same
constraint bit crate::conformance, where the fix was one allocation; here
it has to be static storage.
Structs§
- Computed
Slot - Storage for one get-only computed key path pattern.
- Identity
Slot - Storage for one identity key path pattern.
- Property
- The accessors one key path needs, resolved at run time.
- Slot
- Storage for one key path pattern, laid out so every offset is small.
- Writable
Computed Slot - Storage for one settable computed key path pattern.
Constants§
- THUNK_
COUNT - How many key paths this crate can build.
- VALUE_
SLOTS - How many key paths this crate can synthesise from scratch.
Functions§
- build⚠
- Fill
slotin and hand back aWritableKeyPathforproperty. - build_
computed ⚠ - A
KeyPath<Root, Value>whose getter is a Rust function. - build_
identity ⚠ \Root.self, built at run time.- build_
writable_ ⚠computed - A
WritableKeyPath<Root, Value>whose accessors are Rust functions. - kp_
nil_ ⚠children_ get - Getter for a
KeyPath<Int, [Int]?>that always answersnil. - next_
thunks - Take the next unused thunk pair.
- next_
value_ slot - Hand out the next unused slot index.
- thunks
- The thunk pair at
index. - value_
slot ⚠ - The pattern slot at
index. - value_
thunks - The thunk pair for slot
index, and the words they dispatch through.