Code Monkey home page Code Monkey logo

Comments (4)

chinedufn avatar chinedufn commented on August 18, 2024 1

Hey very sorry for the delay, July 21-Aug 1 is super hectic for me and then I'll be back to normal once I settle back in.


The book seems to focus heavily on calling Rust FROM Swift but not the other way around.

Yup totally. The reason for this is that my current use case mainly involves calling Rust from Swift, so I've mainly focused on supporting and writing documentation for that use case.

That being said we also want swift-bridge to make it easy to call Swift from Rust so I'd love to help with anything that you need here.


So you're trying to compile a Swift native library and then link to it from Rust?

If so, there's a very tiny example of this in the book right now (we still need to write a deeper walkthrough) https://github.com/chinedufn/swift-bridge/blob/a78e617ffbb9471224c4ee79a6e42f0498ed4c43/book/src/building/swiftc-and-cargo/README.md#rust-links-to-a-swift-native-library


You don't need to annotate Swift code to call it from Rust. swift-bridge generates the FFI bridge for you.

For example, here we test the generated code for a Swift function that takes a Swift class instance as an argument:

/// Test code generation for freestanding Swift function that takes an opaque Swift type argument.
mod extern_swift_freestanding_fn_with_owned_opaque_swift_type_arg {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
mod foo {
extern "Swift" {
type MyType;
fn some_function (arg: MyType);
}
}
}
}

const EXPECTED_SWIFT_CODE: ExpectedSwiftCode = ExpectedSwiftCode::ContainsAfterTrim(
r#"
@_cdecl("__swift_bridge__$some_function")
func __swift_bridge__some_function (_ arg: __private__PointerToSwiftType) {
some_function(arg: Unmanaged<MyType>.fromOpaque(arg.ptr).takeRetainedValue())
}
"#,
);


I'm assuming there's some sort of build/compile step I'm missing with Swift to generate the needed header files that I may have just missed in the docs?

Yup it's that little blurb from the first link where you use the swift-bridge API or CLI to generate the header files. The CLI command doesn't exist yet, but I can add it for you if you need it (or give you step by step instructions on how to do it if you'd like to make a first contribution!)


Let me know if that helps.

I'd love to help make sure that all of your problems get solved here.

Our approach has been to add documentation as we go.. so once we get you sorted out I'll add some docs on this before closing out this issue.

Cheers!

from swift-bridge.

chinedufn avatar chinedufn commented on August 18, 2024

I'm working on a full example of a Rust binary that calls a Swift library

from swift-bridge.

chinedufn avatar chinedufn commented on August 18, 2024

Update: I have a working example. Now I need to clean it up.

from swift-bridge.

chinedufn avatar chinedufn commented on August 18, 2024

Whipped up an example of a Rust binary that calls a Swift package in #104

You can view the example here -> https://github.com/chinedufn/swift-bridge/tree/23d60feaa0f8e92f2c854d8665868aa5573ef505/examples/rust-binary-calls-swift-package

Please feel free to re-open this if this example isn't enough for your use case.

Cheers!

from swift-bridge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.