Skip to content

Commit

Permalink
Optimized implementations of max, min, and clamp for bool
Browse files Browse the repository at this point in the history
  • Loading branch information
danflapjax committed Aug 11, 2023
1 parent e286f25 commit b75351e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,22 @@ mod impls {
_ => unsafe { unreachable_unchecked() },
}
}

#[inline]
fn min(self, other: bool) -> bool {
self & other
}

#[inline]
fn max(self, other: bool) -> bool {
self | other
}

#[inline]
fn clamp(self, min: bool, max: bool) -> bool {
assert!(min <= max);
self.max(min).min(max)
}
}

ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
Expand Down

0 comments on commit b75351e

Please sign in to comment.