Skip to content

Conversation

loichyan
Copy link
Contributor

This PR adds a lightweight version of crate_interface.

Motivation

The major reason for crate_interface_lite is the same as pin-project-lite -- using declarative macros does not require any external dependencies and could speed up the fresh builds (that is building from scratch with crate_interface requires compiling proc-macro related crates beforehand). Also, for potential library authors use this crate, crate_interface_lite would result in a tidier dependency tree of their projects.

Implementation

Due to the limitations of declarative macros, the lite one lacks supports for complex ASTs. And I have documented perceptible differences1.

Furthermore, crate_interface_lite::def_interface uses a different approach to work with call_interface. Instead of generating a private mod to include linked extern fns2, it implements the defined interface for a dummy struct, as illustrated below:

// With crate_interface_lite...
crate_interface_lite::def_interface!(
	trait SimpleIf {
	    fn foo() -> u32;
	}
);
impl SimpleIf for crate_interface_lite::DefaultImpl {
    fn foo() -> u32 {
        extern "Rust" {
            #[link_name = "__SimpleIf__foo"]
            fn foo() -> u32;
        }
        unsafe { foo() }
    }
}

// With crate_interface...
#[crate_interface::def_interface]
trait SimpleIf {
	fn foo() -> u32;
}
mod __SimpleIf_mod {
    use super::*;
    extern "Rust" {
        pub fn __SimpleIf_foo() -> u32;
    }
}

This should be transparent for users, since the implementation details are not exposed to them.

Future works

If you accept this PR, I would like to take the responsibility for the future maintenance of crate_interface_lite :)

Footnotes

  1. Though some of them could be resolved, but it would complicate the internal macro parsing procedure.

  2. This is actually impossible for declarative macros unless concat_idents is stable.

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

Successfully merging this pull request may close these issues.

1 participant