#hint

no-std likely_stable

likely and unlikely compiler hints in stable rust

1 unstable release

0.1.2 Mar 9, 2021
0.1.1 Mar 9, 2021
0.1.0 Mar 9, 2021

#457 in No standard library

Download history 7209/week @ 2024-07-21 6988/week @ 2024-07-28 6071/week @ 2024-08-04 6213/week @ 2024-08-11 5165/week @ 2024-08-18 6185/week @ 2024-08-25 5414/week @ 2024-09-01 6435/week @ 2024-09-08 4961/week @ 2024-09-15 5305/week @ 2024-09-22 5290/week @ 2024-09-29 5626/week @ 2024-10-06 5660/week @ 2024-10-13 6920/week @ 2024-10-20 5488/week @ 2024-10-27 5410/week @ 2024-11-03

23,847 downloads per month
Used in 6 crates (4 directly)

MIT/Apache

29KB
675 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"

Dependencies

~195KB