Skip to main content

Module state

Module state 

Source
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 discovers DynamicProperty storage by walking a type’s reflection records, so without it @State is simply never initialised;
  • struct metadata carrying a field offset vector;
  • a View conformance 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§

ColorSchemeKey
\EnvironmentValues.colorScheme — whether the interface is light or dark.
ComposedBuilder
Collects the properties a composed view will have.
ComposedView
A runtime-built Swift view type with any mix of property wrappers.
ControlSizeKey
\EnvironmentValues.controlSize — how large controls in scope draw.
DynamicTypeSizeKey
\EnvironmentValues.dynamicTypeSize — the reader’s chosen text size.
EnvProp
An @Environment property of a composed view.
EnvironmentView
A runtime-built Swift view type with a real @Environment.
FocusProp
A @FocusState property of a composed view.
IsEnabledKey
\EnvironmentValues.isEnabled — whether controls in scope accept input.
IsLuminanceReducedKey
\EnvironmentValues.isLuminanceReduced — whether the display is dimmed, as on an always-on screen.
LayoutDirectionKey
\EnvironmentValues.layoutDirection — which way text and layout run.
Props
The properties of a composed view, read inside its body.
StateProp
A @State property of a composed view.
StatefulView
A runtime-built Swift view type with a real @State.
StoredProp
An @AppStorage property of a composed view.
StoredView
A runtime-built Swift view type with a real @AppStorage.

Traits§

EnvKey
One \EnvironmentValues property, as a type.
StateValue
A type @State can hold.
StoredValue
A type @AppStorage can 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, inside withAnimation — see ease_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.