Crates.io | argcall |
lib.rs | argcall |
version | 0.3.1 |
source | src |
created_at | 2024-11-10 08:12:20.860814 |
updated_at | 2024-11-25 13:06:11.063522 |
description | Enables enums and structs to be callable by associating functions with their variants or fields. |
homepage | |
repository | https://github.com/yamaura/argcall |
max_upload_size | |
id | 1442723 |
size | 5,433 |
argcall is a Rust crate that allows you to make enums and structs callable by associating custom functions with their variants or fields using attribute macros. This crate provides an elegant way to define callable data structures, enabling each variant or field to execute a unique function and return customizable outputs.
use argcall::Callable;
#[derive(Callable)]
#[argcall(output = i32)]
enum MyEnum {
#[argcall(fn = one())]
Unit,
#[argcall(fn_path = "one")]
UnitPath,
UnNamed(Two),
#[argcall(fn = add(x))]
Named {
x: i32
},
#[argcall(fn_path = "add")]
NamedPath{
x: i32
}
}
fn one() -> i32 {
1
}
fn add(x: &i32) -> i32 {
1 + x
}
struct Two;
impl Callable for Two {
type Output = i32;
fn call_fn(&self, _: ()) -> Self::Output {
2
}
}
fn main() {
assert_eq!(MyEnum::Unit.call_fn(()), 1);
assert_eq!(MyEnum::UnitPath.call_fn(()), 1);
assert_eq!(MyEnum::UnNamed(Two{}).call_fn(()), 2);
assert_eq!(MyEnum::Named{x: 2}.call_fn(()), 3);
assert_eq!(MyEnum::NamedPath{x: 1}.call_fn(()), 2)
}
#[derive(Callable)]
: Makes MyEnum callable, enabling each variant to be associated with a specific function.#[argcall(fn = <function>)]
and #[argcall(fn_path = "<function_path>")]
: Specify the function to be called for each variant.