Skip to main content

reifydb_value/value/number/
compare.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::cmp::Ordering;
5
6use crate::value::{is::IsNumber, number::promote::Promote};
7
8#[inline]
9pub fn partial_cmp<L, R>(l: &L, r: &R) -> Option<Ordering>
10where
11	L: Promote<R>,
12	R: IsNumber,
13	<L as Promote<R>>::Output: IsNumber,
14{
15	l.checked_promote(r).and_then(|(lp, rp)| lp.partial_cmp(&rp))
16}
17
18#[inline]
19pub fn is_equal<L, R>(l: &L, r: &R) -> bool
20where
21	L: Promote<R>,
22	R: IsNumber,
23	<L as Promote<R>>::Output: IsNumber,
24{
25	partial_cmp(l, r).is_some_and(|o| o == Ordering::Equal)
26}
27
28#[inline]
29pub fn is_not_equal<L, R>(l: &L, r: &R) -> bool
30where
31	L: Promote<R>,
32	R: IsNumber,
33	<L as Promote<R>>::Output: IsNumber,
34{
35	partial_cmp(l, r).is_none_or(|o| o != Ordering::Equal)
36}
37
38#[inline]
39pub fn is_greater_than<L, R>(l: &L, r: &R) -> bool
40where
41	L: Promote<R>,
42	R: IsNumber,
43	<L as Promote<R>>::Output: IsNumber,
44{
45	partial_cmp(l, r).is_some_and(|o| o == Ordering::Greater)
46}
47
48#[inline]
49pub fn is_greater_than_equal<L, R>(l: &L, r: &R) -> bool
50where
51	L: Promote<R>,
52	R: IsNumber,
53	<L as Promote<R>>::Output: IsNumber,
54{
55	partial_cmp(l, r).is_some_and(|o| o != Ordering::Less)
56}
57
58#[inline]
59pub fn is_less_than<L, R>(l: &L, r: &R) -> bool
60where
61	L: Promote<R>,
62	R: IsNumber,
63	<L as Promote<R>>::Output: IsNumber,
64{
65	partial_cmp(l, r).is_some_and(|o| o == Ordering::Less)
66}
67
68#[inline]
69pub fn is_less_than_equal<L, R>(l: &L, r: &R) -> bool
70where
71	L: Promote<R>,
72	R: IsNumber,
73	<L as Promote<R>>::Output: IsNumber,
74{
75	partial_cmp(l, r).is_some_and(|o| o != Ordering::Greater)
76}