#macro #api #arceos

nightly macro no-std crate_interface

Provides a way to define an interface (trait) in a crate, but can implement or use it in any crate

3 releases

0.1.2 Jul 11, 2024
0.1.1 May 3, 2023
0.1.0 May 3, 2023

#1751 in Procedural macros

Download history 54/week @ 2024-04-04 76/week @ 2024-04-11 49/week @ 2024-04-18 353/week @ 2024-04-25 243/week @ 2024-05-02 165/week @ 2024-05-09 160/week @ 2024-05-16 222/week @ 2024-05-23 306/week @ 2024-05-30 182/week @ 2024-06-06 121/week @ 2024-06-13 516/week @ 2024-06-20 104/week @ 2024-06-27 176/week @ 2024-07-04 285/week @ 2024-07-11 474/week @ 2024-07-18

1,238 downloads per month
Used in 3 crates (via kernel_guard)

GPL-3.0-or-later OR Apache-2…

10KB
141 lines

crate_interface

Crates.io

Provides a way to define an interface (trait) in a crate, but can implement or use it in any crate. It 's usually used to solve the problem of circular dependencies between crates.

Example

// Define the interface
#[crate_interface::def_interface]
pub trait HelloIf {
    fn hello(&self, name: &str, id: usize) -> String;
}

// Implement the interface in any crate
struct HelloIfImpl;

#[crate_interface::impl_interface]
impl HelloIf for HelloIfImpl {
    fn hello(&self, name: &str, id: usize) -> String {
        format!("Hello, {} {}!", name, id)
    }
}

// Call `HelloIfImpl::hello` in any crate
use crate_interface::call_interface;
assert_eq!(
    call_interface!(HelloIf::hello("world", 123)),
    "Hello, world 123!"
);
assert_eq!(
    call_interface!(HelloIf::hello, "rust", 456), // another calling style
    "Hello, rust 456!"
);

Dependencies

~285–740KB
~18K SLoC