Skip to main content

Module keypath

Module keypath 

Source
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:

offsetcontents
+0relative pointer to an 8-byte instantiation cache, which must be writable
+4zero
+8relative pointer to the root type’s mangled name, bit 0 set
+12relative pointer to the value type’s mangled name, bit 0 set
+16zero
+20KeyPathBuffer::Header — 0x80000018: trivial, 24 bytes of components
+24component header 0 — kind External, no substitutions
+28relative pointer to the property descriptor, with bit 0 set for indirect
+32component header 0x02c00002 — kind Computed, mutating, settable, id resolved by indirect pointer
+36relative pointer to a word holding the property’s getter — the component’s identity
+40relative pointer to the getter thunk
+44relative 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§

ComputedSlot
Storage for one get-only computed key path pattern.
IdentitySlot
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.
WritableComputedSlot
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 slot in and hand back a WritableKeyPath for property.
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 answers nil.
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.