Skip to content

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

@dtolnay

Description

@dtolnay

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 {}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions