Skip to content

eternaleye/ifunky

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Usage:

Cargo.toml:

[dependencies]
ifunky = "0.1"

Your code:

#[macro_use]
extern crate ifunky;

ifunky! {
    // Declare your function signature
    pub fn foo(x: u32) -> u32 {
        // And write a dispatcher that will be
        // invoked the first time the function is called
        if rand::random::<bool>() {
            foo_big as fn(u32) -> u32
        } else {
            foo_bigger as fn(u32) -> u32
        }
    }

    // That's it!

    pub fn bar(x: u32) -> u32 {
        if rand::random::<bool>() {
            bar_small as fn(u32) -> u32
        } else {
            bar_smaller as fn(u32) -> u32
        }
    }
}

fn main() {
    foo(3);
    bar(7);
}

fn foo_big(x: u32) -> u32 {
    x + 1
}

fn foo_bigger(x: u32) -> u32 {
    (x + 1) * 2
}

fn bar_small(x: u32) -> u32 {
    x - 1
}

fn bar_smaller(x: u32) -> u32 {
    (x - 1) / 2
}

About

ifunc-like behavior in Rust without a glibc dependency

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages