Skip to content
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

Are Vec<Struct> return types supported? #305

Open
ryankaplan opened this issue Dec 3, 2024 · 0 comments
Open

Are Vec<Struct> return types supported? #305

ryankaplan opened this issue Dec 3, 2024 · 0 comments

Comments

@ryankaplan
Copy link

ryankaplan commented Dec 3, 2024

Hi again :)

I'm running into an issue and wonder if I'm missing something or if this is by design. I have an array of transparent structs that I'd like to return to Swift. I'd be fine with returning a RustVec, but eventually I want a Swift array of FfiThings structs.

Right now my setup looks like this...

// Thing is a Rust struct that I also expose to wasm, so I'd prefer
// not to move it into `mod ffi`.
impl From<Thing> for ffi::FfiThing {
  ...
}

fn clone_and_into(layers: Vec<Layer>) -> Vec<ffi::FfiLayer> {
    layers.into_iter().map(|l| l.into()).collect()
}

#[swift_bridge::bridge]
mod ffi {
    #[swift_bridge(swift_repr = "struct")]
    struct FfiThing {
       ... simple properties ...
    }

    extern "Rust" {
        // `clone_and_into` is necessary because without it,
        // swift-bridge says that Vec isn't clone-able.
        #[swift_bridge(return_with = clone_and_into)]
        fn get_things() -> Vec<FfiThing>;
    }
}

While the above works fine from a Rust perspective... when I compile Swift the return type is RustVec which doesn't compile because FfiThing isn't vectorizeable. This makes sense to me, since RustVecs are shallow views into Rust, and I want to copy FfiThing over to Swift.

Do you have a recommended path here? Is there a way, for example, to tell the swift-bridge that I want an Array in Swift and not a RustVec? The array and structs in question will be read frequently from Swift, so I really do just want to copy all the data over to Swift rather than repeatedly call into Rust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant