Expand description
Runtime synthesis of a Swift view type with real @State storage.
A Swift view type synthesised at runtime, with real @State storage.
Everything else in this crate hands SwiftUI an AnyView, which erases the
identity SwiftUI needs in order to see a change rather than a replacement.
That is why an external mutation never animated, why withAnimation had
nothing to interpolate, and why redisplay had to be driven by polling.
This module removes that constraint by building an actual Swift struct at runtime:
struct RustView: View {
@State var value: Int
var body: some View { <built by a Rust closure> }
}which means synthesising, by hand:
- a nominal type descriptor with one field;
- reflection field metadata — a field descriptor naming the field and its
type as
SwiftUI.State<Swift.Int>. This is the part that matters: SwiftUI discoversDynamicPropertystorage by walking a type’s reflection records, so without it@Stateis simply never initialised; - struct metadata carrying a field offset vector;
- a
Viewconformance with all seven witnesses, three of which are the underscored statics that point at SwiftUI’s own protocol-extension defaults; - a registered protocol conformance record, so the runtime can find it.
The struct has exactly one stored property, so its layout is identical to
State<Int> — which lets it borrow that type’s value witness table instead
of needing one synthesised too.
This works: SwiftUI installs the @State property, evaluates the body, and
re-renders when the value changes — dependency tracking, not polling.
§Two things that were not obvious
A value witness table cannot be borrowed. The first attempt reused
State<Int>’s, reasoning that a single-field struct is layout-identical to
its field — which the sizes confirm. But Swift shares one value witness
table between layout-compatible types, so State<Int>’s witnesses are
literally SwiftUI._AnimationView’s outlined functions. They are written for
that type and fault on any other. value_witness_table synthesises a
correct one instead.
Property accessors take the struct’s own metadata. State<Value>’s
wrappedValue getter wants State<Int>.Type, not Int.Type. Passing the
generic argument instead compiles, runs, and faults inside
State.getValue(forReading:).
Compare crate::views::timeline, which also lets Rust state drive the
interface but re-renders every frame regardless of whether anything changed.
Structs§
- Color
Scheme Key \EnvironmentValues.colorScheme— whether the interface is light or dark.- Composed
Builder - Collects the properties a composed view will have.
- Composed
View - A runtime-built Swift view type with any mix of property wrappers.
- Control
Size Key \EnvironmentValues.controlSize— how large controls in scope draw.- Dynamic
Type Size Key \EnvironmentValues.dynamicTypeSize— the reader’s chosen text size.- EnvProp
- An
@Environmentproperty of a composed view. - Environment
View - A runtime-built Swift view type with a real
@Environment. - Focus
Prop - A
@FocusStateproperty of a composed view. - IsEnabled
Key \EnvironmentValues.isEnabled— whether controls in scope accept input.- IsLuminance
Reduced Key \EnvironmentValues.isLuminanceReduced— whether the display is dimmed, as on an always-on screen.- Layout
Direction Key \EnvironmentValues.layoutDirection— which way text and layout run.- Props
- The properties of a composed view, read inside its body.
- State
Prop - A
@Stateproperty of a composed view. - Stateful
View - A runtime-built Swift view type with a real
@State. - Stored
Prop - An
@AppStorageproperty of a composed view. - Stored
View - A runtime-built Swift view type with a real
@AppStorage.
Traits§
- EnvKey
- One
\EnvironmentValuesproperty, as a type. - State
Value - A type
@Statecan hold. - Stored
Value - A type
@AppStoragecan hold.
Functions§
- ease_
in_ out Animation.easeInOut(duration:), as one reference.- refresh
- Re-run the root’s body, so the interface reflects current Rust state.
- refresh_
animated - As
refresh, insidewithAnimation— seeease_in_out. - root_
view - Build the application’s root view, or return the one already built.
- set_
state - Write a new value through the captured
Binding, which is what makes SwiftUI invalidate and re-run the body. - set_
state_ animated - set_
state_ animated_ with - As
set_state_animated, with the animation spelled out.