forked from jneem/imbl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.rs
More file actions
73 lines (65 loc) · 1.88 KB
/
Copy pathutil.rs
File metadata and controls
73 lines (65 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Every codebase needs a `util` module.
use std::cmp::Ordering;
use std::ops::{Bound, Range, RangeBounds};
use archery::{SharedPointer, SharedPointerKind};
pub(crate) fn clone_ref<A, P>(r: SharedPointer<A, P>) -> A
where
A: Clone,
P: SharedPointerKind,
{
SharedPointer::try_unwrap(r).unwrap_or_else(|r| (*r).clone())
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Side {
Left,
Right,
}
#[allow(dead_code)]
pub(crate) fn linear_search_by<'a, A, I, F>(iterable: I, mut cmp: F) -> Result<usize, usize>
where
A: 'a,
I: IntoIterator<Item = &'a A>,
F: FnMut(&A) -> Ordering,
{
let mut pos = 0;
for value in iterable {
match cmp(value) {
Ordering::Equal => return Ok(pos),
Ordering::Greater => return Err(pos),
Ordering::Less => {}
}
pos += 1;
}
Err(pos)
}
pub(crate) fn to_range<R>(range: &R, right_unbounded: usize) -> Range<usize>
where
R: RangeBounds<usize>,
{
let start_index = match range.start_bound() {
Bound::Included(i) => *i,
Bound::Excluded(i) => *i + 1,
Bound::Unbounded => 0,
};
let end_index = match range.end_bound() {
Bound::Included(i) => *i + 1,
Bound::Excluded(i) => *i,
Bound::Unbounded => right_unbounded,
};
start_index..end_index
}
#[cfg(test)]
macro_rules! assert_covariant {
($name:ident<$($gen:tt),*> in $param:ident) => {
#[allow(unused_assignments, unused_variables)]
const _: () = {
type Tmp<$param> = $name<$($gen),*>;
fn assign<'a, 'b: 'a>(src: Tmp<&'b i32>, mut dst: Tmp<&'a i32>) {
dst = src;
}
};
}
}