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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use platform_num::LinkType;
use std::borrow::Cow;
use std::ops::Index;
use std::slice::SliceIndex;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Query<'a, T: LinkType> {
cow: Cow<'a, [T]>,
}
impl<'a, T: LinkType> Query<'a, T> {
pub fn new<C>(beef: C) -> Self
where
C: Into<Cow<'a, [T]>>,
{
Query { cow: beef.into() }
}
pub fn len(&self) -> usize {
match self.cow {
Cow::Borrowed(ref beef) => beef.len(),
Cow::Owned(ref beef) => beef.len(),
}
}
pub fn into_inner(self) -> Cow<'a, [T]> {
self.cow
}
pub fn into_owned(self) -> Vec<T> {
match self.cow {
Cow::Borrowed(beef) => beef.to_vec(),
Cow::Owned(beef) => beef,
}
}
}
impl<'a, I: SliceIndex<[T]>, T: LinkType> Index<I> for Query<'a, T> {
type Output = I::Output;
fn index(&self, index: I) -> &Self::Output {
match self.cow {
Cow::Borrowed(ref s) => &s[index],
Cow::Owned(ref s) => &s[index],
}
}
}
pub trait ToQuery<T: LinkType> {
fn to_query(&self) -> Query<'_, T>;
}
impl<T: LinkType> ToQuery<T> for Query<'_, T> {
fn to_query(&self) -> Query<'_, T> {
Query::new(&self[..])
}
}
impl<T: LinkType> ToQuery<T> for [T] {
fn to_query(&self) -> Query<'_, T> {
Query::new(self)
}
}
impl<'a, T: LinkType> ToQuery<T> for &'a [T] {
fn to_query(&self) -> Query<'a, T> {
Query::new(*self)
}
}
impl<T: LinkType> ToQuery<T> for Vec<T> {
fn to_query(&self) -> Query<'_, T> {
Query::new(&self[..])
}
}
impl<T: LinkType, const L: usize> ToQuery<T> for [T; L] {
fn to_query(&self) -> Query<'_, T> {
Query::new(&self[..])
}
}
#[macro_export]
macro_rules! query {
() => (
$crate::Query::new(&[][..])
);
($($x:expr),*) => (
$crate::Query::new(&[$($x),*][..])
);
}