-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce ownership using Swift's consume operator #155
Comments
Swift 5.9 introduces some new ownership features. I haven't thought through what we can do with all of them yet. Linking to some resources here:
Some Early Thoughts
|
I think a good approach would be to introduce Swift compile-time enforced ownership support behind an attribute in the So, something like this would lead our code generators to emit Swift code that enforces ownership at compile time: #[swift_bridge::bridge]
mod ffi {
extern "Rust" {
#[swift_bridge(__unstable__swift_compile_time_ownership)]
type Computer
fn eat(&self, meal: Food);
fn shutdown(self);
}
} Then after things stabilize we'd remove the One approach would be to start by just diving in and writing some tests and implementations for some basic cases such as bridging a Rust struct that has a single primitive field, then using what we learn to get a better sense of the extent of the work that is needed and how to best approach it. |
Swift recently accepted a proposal for a
consume
operator.The
consume
operator will let us declare that a function takes unique ownership of a type https://github.com/apple/swift-evolution/blob/main/proposals/0366-move-function.md?plain=1#L175-L184 .Any attempts to use the type after it is consumed lead to a compile time error.
This new
consume
operator will make it possible for us to solve one of the memory safety issues described in the bookswift-bridge/book/src/safety/README.md
Lines 48 to 73 in d4f28b3
After the
consume
operator lands in stable Swift we can use it in our generated code to enforce ownership.So, the following bridge module:
would generate Swift code along the lines of:
I'm not sure when the
consume
operator will land in stable Swift, or how we can track its progress.The text was updated successfully, but these errors were encountered: