Socket
Book a DemoInstallSign in
Socket

abi-singleton

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abi-singleton

0.3.0
Cargo
Version published
Maintainers
1
Created
Source

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.

FAQs

Package last updated on 30 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.