5 releases

0.3.0 Dec 30, 2024
0.2.3 Dec 13, 2024
0.1.0 Dec 1, 2024

#211 in Procedural macros

Download history 121/week @ 2024-11-26 26/week @ 2024-12-03 452/week @ 2024-12-10 18/week @ 2024-12-17 83/week @ 2024-12-24 44/week @ 2024-12-31 8/week @ 2025-01-07 61/week @ 2025-01-14 40/week @ 2025-01-21 25/week @ 2025-01-28 66/week @ 2025-02-04 38/week @ 2025-02-11

189 downloads per month
Used in 4 crates (via sparreal-macros)

MIT license

7KB
100 lines

ABI SINGLETION

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.

Example

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);
}

Usage

See test_project in this repo for examples.

Limitations

Only C FFI func support, self fields are not supported.

Dependencies

~195–630KB
~15K SLoC