|
1 | 1 | use platform_num::LinkType; |
2 | | -use std::borrow::Cow; |
| 2 | +use std::borrow::{Borrow, Cow}; |
3 | 3 | use std::ops::Index; |
4 | 4 | use std::slice::SliceIndex; |
5 | 5 |
|
6 | 6 | #[derive(Clone, Debug, PartialEq, Eq)] |
7 | | -pub struct Query<'a, T: LinkType> { |
8 | | - cow: Cow<'a, [T]>, |
9 | | -} |
| 7 | +pub struct Query<'a, T: Clone>(Cow<'a, [T]>); |
10 | 8 |
|
11 | | -impl<'a, T: LinkType> Query<'a, T> { |
| 9 | +impl<'a, T: Clone> Query<'a, T> { |
12 | 10 | pub fn new<C>(beef: C) -> Self |
13 | 11 | where |
14 | 12 | C: Into<Cow<'a, [T]>>, |
15 | 13 | { |
16 | | - Query { cow: beef.into() } |
| 14 | + Query(beef.into()) |
17 | 15 | } |
18 | 16 |
|
19 | 17 | pub fn len(&self) -> usize { |
20 | | - match self.cow { |
21 | | - Cow::Borrowed(ref beef) => beef.len(), |
| 18 | + match self.0 { |
| 19 | + Cow::Borrowed(beef) => beef.len(), |
22 | 20 | Cow::Owned(ref beef) => beef.len(), |
23 | 21 | } |
24 | 22 | } |
25 | 23 |
|
26 | 24 | pub fn into_inner(self) -> Cow<'a, [T]> { |
27 | | - self.cow |
| 25 | + self.0 |
28 | 26 | } |
29 | 27 |
|
30 | 28 | pub fn into_owned(self) -> Vec<T> { |
31 | | - match self.cow { |
32 | | - Cow::Borrowed(beef) => beef.to_vec(), |
| 29 | + match self.0 { |
| 30 | + Cow::Borrowed(beef) => beef.to_owned(), |
33 | 31 | Cow::Owned(beef) => beef, |
34 | 32 | } |
35 | 33 | } |
36 | 34 | } |
37 | 35 |
|
38 | | -impl<'a, I: SliceIndex<[T]>, T: LinkType> Index<I> for Query<'a, T> { |
| 36 | +impl<'a, I: SliceIndex<[T]>, T: Clone> Index<I> for Query<'a, T> { |
39 | 37 | type Output = I::Output; |
40 | 38 |
|
41 | 39 | fn index(&self, index: I) -> &Self::Output { |
42 | | - match self.cow { |
| 40 | + match self.0 { |
43 | 41 | Cow::Borrowed(ref s) => &s[index], |
44 | 42 | Cow::Owned(ref s) => &s[index], |
45 | 43 | } |
46 | 44 | } |
47 | 45 | } |
48 | 46 |
|
49 | | -pub trait ToQuery<T: LinkType> { |
| 47 | +pub trait ToQuery<T: Clone> { |
50 | 48 | fn to_query(&self) -> Query<'_, T>; |
51 | 49 | } |
52 | 50 |
|
53 | | -impl<T: LinkType> ToQuery<T> for Query<'_, T> { |
| 51 | +impl<T: Clone> ToQuery<T> for Query<'_, T> { |
54 | 52 | fn to_query(&self) -> Query<'_, T> { |
55 | 53 | Query::new(&self[..]) |
56 | 54 | } |
57 | 55 | } |
58 | 56 |
|
59 | | -impl<T: LinkType> ToQuery<T> for [T] { |
| 57 | +impl<T: Clone> ToQuery<T> for [T] { |
60 | 58 | fn to_query(&self) -> Query<'_, T> { |
61 | 59 | Query::new(self) |
62 | 60 | } |
63 | 61 | } |
64 | 62 |
|
65 | | -impl<'a, T: LinkType> ToQuery<T> for &'a [T] { |
| 63 | +impl<'a, T: Clone> ToQuery<T> for &'a [T] { |
66 | 64 | fn to_query(&self) -> Query<'a, T> { |
67 | 65 | Query::new(*self) |
68 | 66 | } |
69 | 67 | } |
70 | 68 |
|
71 | | -impl<T: LinkType> ToQuery<T> for Vec<T> { |
| 69 | +impl<T: Clone> ToQuery<T> for Vec<T> { |
72 | 70 | fn to_query(&self) -> Query<'_, T> { |
73 | 71 | Query::new(&self[..]) |
74 | 72 | } |
75 | 73 | } |
76 | 74 |
|
77 | | -impl<T: LinkType, const L: usize> ToQuery<T> for [T; L] { |
| 75 | +impl<T: Clone, const L: usize> ToQuery<T> for [T; L] { |
78 | 76 | fn to_query(&self) -> Query<'_, T> { |
79 | 77 | Query::new(&self[..]) |
80 | 78 | } |
|
0 commit comments