ifunky 0.1.1

Provides functions that choose a variant (and memoize that choice) on first call, like ifuncs without the need for loader support
Documentation
  • Coverage
  • 0%
    0 out of 2 items documented0 out of 0 items with examples
  • Size
  • Source code size: 5.74 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 129.88 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • eternaleye/ifunky
    0 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • eternaleye

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
}