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

ExternType impl for types that have nonunique C++ path #529

Open
dtolnay opened this issue Nov 30, 2020 · 0 comments
Open

ExternType impl for types that have nonunique C++ path #529

dtolnay opened this issue Nov 30, 2020 · 0 comments

Comments

@dtolnay
Copy link
Owner

dtolnay commented Nov 30, 2020

Currently our ExternType trait has the C++ type_id as an associated type, which means a single Rust type can have at most one ExternType impl and therefore at most one type_id associated to them.

We should consider adding something like:

pub unsafe trait ExternTypeAlias<Id> {}

unsafe impl<T: ExternType> ExternTypeAlias<T::Id> for T {}

and then substituting the following change to how we do type_id verification:

- pub fn verify_extern_type<T: ExternType<Id = Id>, Id>() {}
+ pub fn verify_extern_type<T: ExternTypeAlias<Id>, Id>() {}

This makes it possible to work with types that might be exposed at multiple different locations on the C++ side.

unsafe impl ExternType for i16 {
    type Id = cxx::type_id!("std::int16_t");
}

unsafe impl ExternType for i32 {
    type Id = cxx::type_id!("std::int32_t");
}

// this works regardless of whether pid_t is typedef'd to
// i16 or i32 on the platform
unsafe impl ExternTypeAlias<cxx::type_id!("pid_t")> for libc::pid_t {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant