@@ -511,3 +511,65 @@ pub const fn black_box<T>(dummy: T) -> T {
511
511
pub const fn must_use < T > ( value : T ) -> T {
512
512
value
513
513
}
514
+
515
+ /// Hints to the compiler that branch condition is likely to be true.
516
+ /// Returns the value passed to it.
517
+ ///
518
+ /// It can be used with `if` or boolean `match` expressions.
519
+ ///
520
+ /// # Examples
521
+ ///
522
+ /// ```
523
+ /// #![feature(likely_unlikely)]
524
+ /// use core::hint::likely;
525
+ ///
526
+ /// fn foo(x: i32) {
527
+ /// if likely(x > 0) {
528
+ /// println!("this branch is likely to be taken");
529
+ /// } else {
530
+ /// println!("this branch is unlikely to be taken");
531
+ /// }
532
+ ///
533
+ /// match likely(x > 0) {
534
+ /// true => println!("this branch is likely to be taken"),
535
+ /// false => println!("this branch is unlikely to be taken"),
536
+ /// }
537
+ /// }
538
+ /// ```
539
+ #[ unstable( feature = "likely_unlikely" , issue = "26179" ) ]
540
+ #[ rustc_nounwind]
541
+ #[ inline( always) ]
542
+ pub const fn likely ( b : bool ) -> bool {
543
+ crate :: intrinsics:: likely ( b)
544
+ }
545
+
546
+ /// Hints to the compiler that the branch condition is unlikely to be true.
547
+ /// Returns the value passed to it.
548
+ ///
549
+ /// It can be used with `if` or boolean `match` expressions.
550
+ ///
551
+ /// # Examples
552
+ ///
553
+ /// ```
554
+ /// #![feature(likely_unlikely)]
555
+ /// use core::hint::unlikely;
556
+ ///
557
+ /// fn foo(x: i32) {
558
+ /// if unlikely(x > 0) {
559
+ /// println!("this branch is unlikely to be taken");
560
+ /// } else {
561
+ /// println!("this branch is likely to be taken");
562
+ /// }
563
+ ///
564
+ /// match unlikely(x > 0) {
565
+ /// true => println!("this branch is unlikely to be taken"),
566
+ /// false => println!("this branch is likely to be taken"),
567
+ /// }
568
+ /// }
569
+ /// ```
570
+ #[ unstable( feature = "likely_unlikely" , issue = "26179" ) ]
571
+ #[ rustc_nounwind]
572
+ #[ inline( always) ]
573
+ pub const fn unlikely ( b : bool ) -> bool {
574
+ crate :: intrinsics:: unlikely ( b)
575
+ }
0 commit comments