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

Nonlocal extern "Rust" types trick in #942 doesn't work with Boxed types #1323

Open
djmitche opened this issue Mar 10, 2024 · 0 comments
Open

Comments

@djmitche
Copy link

I tried the trick in #942 to use an opaque type in multiple Rust modules no longer works.

// task.rs
#[cxx::bridge(namespace = "tc")]
mod ffi {
    extern "Rust" {
        type Task;
    }
}

pub struct Task(tc::Task);
unsafe impl cxx::ExternType for Task {
    type Id = cxx::type_id!("tc::Task");
    type Kind = cxx::kind::Opaque;
}
// replica.rs
#[cxx::bridge(namespace = "tc")]
mod ffi {
    extern "C++" {
        include!("taskchampion-cpp/task.h");
        type Task = crate::task::Task;
    }
    extern "Rust" {
        fn foo(task: Box<Task>);
    }
}
use crate::task::Task;
fn foo(task: Box<Task>) { todo!() }

This results in

error[E0271]: type mismatch resolving `<Task as ExternType>::Kind == Trivial`
   --> src/tc/src/replica.rs:11:14
    |
11  |         type Task = crate::task::Task;
    |              ^^^^ type mismatch resolving `<Task as ExternType>::Kind == Trivial`
    |
note: expected this to be `Trivial`
   --> src/tc/src/task.rs:14:17
    |
14  |     type Kind = cxx::kind::Opaque;
    |                 ^^^^^^^^^^^^^^^^^
note: required by a bound in `verify_extern_kind`
   --> /home/dustin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cxx-1.0.119/src/extern_type.rs:187:41
    |
187 | pub fn verify_extern_kind<T: ExternType<Kind = Kind>, Kind: self::Kind>() {}
    |                                         ^^^^^^^^^^^ required by this bound in `verify_extern_kind`

Changing the type to Trivial in the ExternType impl results in

/home/dustin/p/taskwarrior/build/src/tc/corrosion_generated/cxxbridge/taskchampion-cpp/src/replica.cpp:457:40: error: static assertion failed: type tc::Task should be trivially move constructible and trivially destructible in C++ to be used as type Box<Task> in Rust
  457 |     ::rust::IsRelocatable<::tc::Task>::value,

Everything works with Opaque if I take &Task instead of Box<Task>, which would be fine except I need to return an (owned) Task.

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