Closed cadnza closed 5 months ago
Property wrappers and macro annotations should also accept parameters such that it would be possible to generate this example of the @SmallNumber
property wrapper defined in tutorial:
struct NarrowRectangle {
@SmallNumber(wrappedValue: 2, maximum: 5) var height: Int
@SmallNumber(wrappedValue: 3, maximum: 4) var width: Int
}
Also, I don't see much of a need for UniFFI to define Swift property wrappers or macros—it should only need to apply them. So if you attach e.g. #[uniffi::swift_macro(tasty(tastiness = 3))]
to a struct, it should take it on faith that the @tasty
macro already has a definition somewhere on the Swift side and then proceed to blindly apply the @tasty(tastiness: 3)
annotation to the generated Swift type. Done that way, UniFFI can remain reasonably decoupled from the Swift release cycle in terms of property wrappers and macros, and UniFFI can apply any property wrapper or macro that exists anywhere on the Swift side.
That sounds great, but difficult to express in UniFFI's model. Happy to help if you want to have a start at this though.
@mhammond Thanks! Turns out there's already a convention for writing SwiftUI code with UniFFI, and it's a lot less complicated and more sustainable than this feature would be 😄 Thanks for humoring my whim. Closing as not planned.
@cadnza What is the convention for writing SwiftUI code with UniFFI with regard to having a view update when a field in a UniFFi Interface / rust struct changes?
@cadnza What is the convention for writing SwiftUI code with UniFFI with regard to having a view update when a field in a UniFFi Interface / rust struct changes?
@GraniteLake As far as I understand—first pointing out how wide and varied my lack of experience is with this—the way it's usually done is by writing an intermediary stuct/class in Swift that wraps UniFFI's class and adds SwiftUI's property wrappers and macro annotations. As far as how you get UniFFI to update the struct based on those wrappers and annotations, I'm not sure, but something along those lines is what I've been seeing going through other peoples' code.
As far as I can tell, UniFFI doesn't have a way to apply property wrappers or macro annotations to types and properties in Swift, so generating this example of the
@ObservedObject
property wrapper using UniFFI is impossible:Generating this example of the
@Observable
macro is also impossible:Property wrappers and macro annotations are used extensively in SwiftUI, so being able to specify them via proc macro or UDL would be wonderful.
To be clear: Property wrappers and macro annotations have uses outside SwiftUI, but SwiftUI is my primary interest.
Two considerations:
Anyway, I'm happy to write the feature if it'd be useful, but first it's worth asking: