fixed_map/
macro_support.rs

1//! ## PRIVATE API
2//!
3//! This API is private, for use only in the `derive(Key)` macro. Usage for
4//! other purposes is not supported, and this API will not abide by semver
5//! stability guarantees.
6
7#![allow(clippy::missing_inline_in_public_items)]
8
9use core::cmp::Ordering;
10
11#[inline]
12fn flatten<T>(value: (usize, &Option<T>)) -> Option<(usize, &T)> {
13    match value {
14        (index, Some(value)) => Some((index, value)),
15        _ => None,
16    }
17}
18
19/// `partial_cmp` implementation over iterators which ensures that storage
20/// ordering between `None` and `Some` is handled in a reasonable manner.
21pub fn __storage_iterator_partial_cmp<'a, A, B, T: 'a>(a: A, b: B) -> Option<Ordering>
22where
23    A: IntoIterator<Item = &'a Option<T>>,
24    B: IntoIterator<Item = &'a Option<T>>,
25    T: PartialOrd<T>,
26{
27    let a = a.into_iter().enumerate().filter_map(flatten);
28    let b = b.into_iter().enumerate().filter_map(flatten);
29    a.partial_cmp(b)
30}
31
32/// `cmp` implementation over iterators which ensures that storage ordering
33/// between `None` and `Some` is handled in a reasonable manner.
34pub fn __storage_iterator_cmp<'a, A, B, T: 'a>(a: A, b: B) -> Ordering
35where
36    A: IntoIterator<Item = &'a Option<T>>,
37    B: IntoIterator<Item = &'a Option<T>>,
38    T: Ord,
39{
40    let a = a.into_iter().enumerate().filter_map(flatten);
41    let b = b.into_iter().enumerate().filter_map(flatten);
42    a.cmp(b)
43}
44
45#[inline]
46fn filter_bool(&(_, value): &(usize, &bool)) -> bool {
47    *value
48}
49
50/// `partial_cmp` implementation over iterators which ensures that storage
51/// ordering between `false` and `true` is handled in a reasonable manner.
52pub fn __storage_iterator_partial_cmp_bool<'a, A, B>(a: A, b: B) -> Option<Ordering>
53where
54    A: IntoIterator<Item = &'a bool>,
55    B: IntoIterator<Item = &'a bool>,
56{
57    let a = a.into_iter().enumerate().filter(filter_bool);
58    let b = b.into_iter().enumerate().filter(filter_bool);
59    a.partial_cmp(b)
60}
61
62/// `cmp` implementation over iterators which ensures that storage ordering
63/// between `false` and `true` is handled in a reasonable manner.
64pub fn __storage_iterator_cmp_bool<'a, A, B>(a: A, b: B) -> Ordering
65where
66    A: IntoIterator<Item = &'a bool>,
67    B: IntoIterator<Item = &'a bool>,
68{
69    let a = a.into_iter().enumerate().filter(filter_bool);
70    let b = b.into_iter().enumerate().filter(filter_bool);
71    a.cmp(b)
72}