#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

2 releases

0.1.1 May 3, 2023
0.1.0 May 3, 2023

#2763 in #macro

Download history 43/week @ 2023-12-18 30/week @ 2024-01-08 76/week @ 2024-01-15 108/week @ 2024-01-22 59/week @ 2024-01-29 27/week @ 2024-02-05 33/week @ 2024-02-19 55/week @ 2024-02-26 15/week @ 2024-03-04 38/week @ 2024-03-11 50/week @ 2024-03-18 93/week @ 2024-03-25

199 downloads per month
Used in kernel_guard

GPL-3.0-or-later OR Apache-2.0

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

~320–770KB
~18K SLoC