#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

#2799 in #macro

Download history 14/week @ 2024-01-07 92/week @ 2024-01-14 86/week @ 2024-01-21 81/week @ 2024-01-28 27/week @ 2024-02-04 30/week @ 2024-02-18 55/week @ 2024-02-25 14/week @ 2024-03-03 40/week @ 2024-03-10 50/week @ 2024-03-17 89/week @ 2024-03-24 72/week @ 2024-03-31 79/week @ 2024-04-07 56/week @ 2024-04-14 57/week @ 2024-04-21

265 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

~340–790KB
~19K SLoC