stack_graphs/utils.rs
1// -*- coding: utf-8 -*-
2// ------------------------------------------------------------------------------------------------
3// Copyright © 2021, stack-graphs authors.
4// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6// ------------------------------------------------------------------------------------------------
7
8pub(crate) fn equals_option<A, B, F>(a: Option<A>, b: Option<B>, mut eq: F) -> bool
9where
10 F: FnMut(A, B) -> bool,
11{
12 match a {
13 Some(a) => match b {
14 Some(b) => eq(a, b),
15 None => false,
16 },
17 None => match b {
18 Some(_) => false,
19 None => true,
20 },
21 }
22}
23
24pub(crate) fn cmp_option<T, F>(a: Option<T>, b: Option<T>, mut cmp: F) -> std::cmp::Ordering
25where
26 F: FnMut(T, T) -> std::cmp::Ordering,
27{
28 use std::cmp::Ordering;
29 match a {
30 Some(a) => match b {
31 Some(b) => cmp(a, b),
32 None => Ordering::Greater,
33 },
34 None => match b {
35 Some(_) => Ordering::Less,
36 None => Ordering::Equal,
37 },
38 }
39}