2 releases

0.1.3 Dec 11, 2024
0.1.2 Mar 9, 2021
0.1.1 Mar 9, 2021
0.1.0 Mar 9, 2021

#246 in Rust patterns

Download history 3684/week @ 2025-01-03 4532/week @ 2025-01-10 4116/week @ 2025-01-17 4146/week @ 2025-01-24 4915/week @ 2025-01-31 5381/week @ 2025-02-07 5300/week @ 2025-02-14 5541/week @ 2025-02-21 7991/week @ 2025-02-28 10174/week @ 2025-03-07 9313/week @ 2025-03-14 8650/week @ 2025-03-21 7851/week @ 2025-03-28 8488/week @ 2025-04-04 8076/week @ 2025-04-11 7755/week @ 2025-04-18

33,925 downloads per month
Used in 9 crates (5 directly)

MIT/Apache

31KB
674 lines

LICENSE LICENSE Documentation Crates.io Version

This crates brings likely and unlikely branch prediction hints to stable rust

use likely_stable::{likely,unlikely};
use rand::random;

if likely(random::<i32>() > 10) {
    println!("likely!")
} else {
    println!("unlikely!")
}

It also provides if_likely and if_unlikely for branch prediction for if let statements.

use likely_stable::if_likely;
use rand::random;

let v = Some(random()).filter(|v:&i32| *v > 10);

if_likely!{let Some(v) = v => {
    println!("likely!")
} else {
    println!("unlikely!")
}};

Moreover traits LikelyBool, LikelyOption and LikelyResult provides likely and unlikely versions of the methods commonly used for types bool, Option and Result

use likely_stable::LikelyOption;
use rand::random;

let v = Some(random()).filter(|v:&i32| *v > 10);

v.map_or_else_likely(
    || println!("unlikely"),
    |v| println!("likely {}",v));

Usage

Add this to your Cargo.toml:

[dependencies]
likely_stable = "0.1"

No runtime deps

~9KB