-
Notifications
You must be signed in to change notification settings - Fork 53
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
Document how to add Swift files to a generated Swift Package #129
Comments
Is this related #138 ? |
I don't think so. But I might be wrong. Since To solve that, I use inversion-of-control - I declare a public protocol Proxy {
// ...
}
var proxy Proxy?
public func registerProxy(_ newProxy: Proxy) {
proxy = newProxy
} So basically this requires custom code inside the Swift package generated by Some options:
Either way, the most preferred way should be documented, maybe? |
I see, thanks for the example code. I'd like to better understand your use case so that we can figure out the best solution. Can you explain what you're trying to do, exactly? |
Sure — Here's the actual public protocol Proxy {
func getAuthToken() -> String
func getApiUrl() -> String
func log(line: String)
} The main Swift application supplies the Rust library with (amongst other things) an authentication token (on demand with short lifetime), the URL to the API server (can be different depending on dev/prod/region) and a logging facility so that I can easily mix Swift and Rust logs. |
To spell it out even more, the sequence of events would be: Rust needs auth token -> Calls getAuthToken through swift-bridge -> ends up in Swift package -> Swift package uses Proxy protocol to get actual token from main app |
Ok thanks I understand the flow now. So, would something like this work for your use case? swift-bridge-cli create-package --sources /path/to/RustApp.swift --sources /path/to/AnotherFile.swift The We'd also add support for protocols in Here's a rough sketch of what I mean: // lib.rs
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
type RustApp;
#[swift_bridge(init)]
fn new(api_provider: ApiProvider);
}
extern "Swift" {
// This would tell `swift-bridge` that `ApiProvider` is a protocol.
#[swift_bridge(protocol)]
type ApiProvider;
fn get_api_url(&self) -> String;
fn get_api_token(&self) -> String;
}
extern "Swift" {
// Corresponds to `class Logger`
type Logger;
#[swift_bridge(init)]
fn new();
fn log(&self, line: &str);
}
} // RustApp.swift
public protocol ApiProvider {
func get_api_url() -> String;
func get_token() -> String;
}
public final class Logger {
func log(line: RustStr) {
print(line.toString())
}
} Would this approach work for what you're trying to do? Another thing we can consider is letting users generated a protocol from #[swift_bridge::bridge]
mod ffi {
extern "Swift" {
// This would automatically generate a `protocol ApiProvider` with the given methods.
// note the `declare_protocol` instead of `protocol`.
#[swift_bridge(declare_protocol)]
type ApiProvider;
fn get_api_url(&self) -> String;
fn get_api_token(&self) -> String;
}
} So, we may want to let you both import as well as declare protocols. |
Technically I only need to add the Swift files to the project, so just having that would be a nice addition. But all the other suggestions are nice as well, but not strictly necessary to solve my requirements 👍. Those things would definitely make |
I would like to read some documentation on what you think the best place for auxillary Swift files for the generated Swift Package is.
Might it be
swift-bridge
?Is there some automatic facility for copying Swift files already in place?
The text was updated successfully, but these errors were encountered: