| Crates.io | crate_interface_lite |
| lib.rs | crate_interface_lite |
| version | 0.1.0 |
| created_at | 2025-12-20 07:42:49.259156+00 |
| updated_at | 2025-12-20 07:42:49.259156+00 |
| description | Provides a way to define an interface (trait) in a crate, but can implement or use it in any crate. |
| homepage | https://github.com/arceos-org/arceos |
| repository | https://github.com/arceos-org/crate_interface |
| max_upload_size | |
| id | 1996142 |
| size | 10,839 |
A lightweight version of crate_interface written with declarative macros.
// Define the interface
crate_interface_lite::def_interface!(
pub trait HelloIf {
fn hello(name: &str, id: usize) -> String;
}
);
// Implement the interface in any crate
struct HelloIfImpl;
crate_interface_lite::impl_interface!(
impl HelloIf for HelloIfImpl {
fn hello(name: &str, id: usize) -> String {
format!("Hello, {} {}!", name, id)
}
}
);
// Call `HelloIfImpl::hello` in any crate
use crate_interface_lite::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!"
);
The public APIs are almost the same as crate_interface. One major difference is that you cannot use the exported macros as attributes.
// With crate_interface...
#[crate_interface::def_interface]
pub trait HelloIf {
fn hello(name: &str, id: usize) -> String;
}
// With crate_interface_lite...
crate_interface_lite::def_interface!(
pub trait HelloIf {
fn hello(name: &str, id: usize) -> String;
}
);
This is the major reason to use this crate, as it would result in a tidier dependency tree of your project and slightly speed up the compilation. However, if you already have proc-macro related dependencies in your crate’s dependency graph, there is almost no benefit from using this crate.
Unlike crate_interface::def_interface, the macro in this crate does not support
method receivers, namely self, &self, &mut self, etc. But in most cases, you
don't need them, since the impl_interface is often applied to an unit struct.
crate_interface_lite::def_interface!(
pub trait HelloIf {
fn hello(self, name: &str, id: usize) -> String;
// ^^^^ Not supported!
}
);
The def_interface in this crate does not support default implementations of
trait functions. In the future, we may support using default implementations as
fallbacks when no other implementations are provided.
crate_interface_lite::def_interface!(
pub trait HelloIf {
fn hello(name: &str, id: usize) -> String { todo!() }
// ^^^^^^^^^^^ Not supported!
}
);