Crates.io | abi-singleton |
lib.rs | abi-singleton |
version | |
source | src |
created_at | 2024-12-01 04:28:22.533847 |
updated_at | 2024-12-13 01:54:08.839195 |
description | Simple singleton trait |
homepage | |
repository | https://gitee.com/zr233/abi-singleton |
max_upload_size | |
id | 1467213 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This is a simple ABI singleton Trait helper crate. When you need a trait that only one implementation can exist, you can use this crate.
A lib crate define a trait that only one implementation exist, you can use it with out <T>
like code.
#[api_trait]
pub trait Cat {
fn eat(food: Food) -> usize;
}
/// Only one kind of cat, so no need to known type with `<T>`.
pub fn cat_eat(food: Food) -> usize {
//`CatImpl` is auto generated.
CatImpl::eat(food)
}
A crate implements the trait and use lib funcs.
struct BlackCat;
// There is only one black cat in the world.
#[api_impl]
impl Cat for BlackCat {
fn eat(food: Food) -> usize {
println!("black cat eat one");
food.count - 1
}
}
fn main() {
let food = Food { count: 3 };
println!("There are {} food", food.count);
let left = cat_eat(food);
println!("food left: {}", left);
}
See test_project
in this repo for examples.
Only C FFI func support, self
fields are not supported.