Debian Codegraph @

src/ty_match.rs

raw symbols SCIP
1 use std::marker::PhantomData;
2
3 // These types allow the `query!()` and friends to compare a given parameter's type to
4 // an expected parameter type even if the former is behind a reference or in `Option`.
5
6 // For query parameters, Postgres gives us a single type ID which we convert to an "expected" or
7 // preferred Rust type, but there can actually be several types that are compatible for a given type
8 // in input position. E.g. for an expected parameter of `String`, we want to accept `String`,
9 // `Option<String>`, `&str` and `Option<&str>`. And for the best compiler errors we don't just
10 // want an `IsCompatible` trait (at least not without `#[on_unimplemented]` which is unstable
11 // for the foreseeable future).
12
13 // We can do this by using autoref (for method calls, the compiler adds reference ops until
14 // it finds a matching impl) with impls that technically don't overlap as a hacky form of
15 // specialization (but this works only if all types are statically known, i.e. we're not in a
16 // generic context; this should suit 99% of use cases for the macros).
17
18 #[allow(clippy::just_underscores_and_digits)]
19 pub fn same_type<T>(_1: &T, _2: &T) {}
20
21 pub struct WrapSame<T, U>(PhantomData<T>, PhantomData<U>);
22
23 impl<T, U> WrapSame<T, U> {
24 pub fn new(_arg: &U) -> Self {
25 WrapSame(PhantomData, PhantomData)
26 }
27 }
28
29 pub trait WrapSameExt: Sized {
30 type Wrapped;
31
32 fn wrap_same(self) -> Self::Wrapped {
33 panic!("only for type resolution")
34 }
35 }
36
37 impl<T, U> WrapSameExt for WrapSame<T, Option<U>> {
38 type Wrapped = Option<T>;
39 }
40
41 impl<T, U> WrapSameExt for &'_ WrapSame<T, U> {
42 type Wrapped = T;
43 }
44
45 pub struct MatchBorrow<T, U>(PhantomData<T>, PhantomData<U>);
46
47 impl<T, U> MatchBorrow<T, U> {
48 pub fn new(t: T, _u: &U) -> (T, Self) {
49 (t, MatchBorrow(PhantomData, PhantomData))
50 }
51 }
52
53 pub trait MatchBorrowExt: Sized {
54 type Matched;
55
56 fn match_borrow(self) -> Self::Matched {
57 panic!("only for type resolution")
58 }
59 }
60
61 impl<'a> MatchBorrowExt for MatchBorrow<Option<&'a str>, Option<String>> {
62 type Matched = Option<&'a str>;
63 }
64
65 impl<'a> MatchBorrowExt for MatchBorrow<Option<&'a [u8]>, Option<Vec<u8>>> {
66 type Matched = Option<&'a [u8]>;
67 }
68
69 impl<'a> MatchBorrowExt for MatchBorrow<Option<&'a str>, Option<&'a String>> {
70 type Matched = Option<&'a str>;
71 }
72
73 impl<'a> MatchBorrowExt for MatchBorrow<Option<&'a [u8]>, Option<&'a Vec<u8>>> {
74 type Matched = Option<&'a [u8]>;
75 }
76
77 impl<'a> MatchBorrowExt for MatchBorrow<&'a str, String> {
78 type Matched = &'a str;
79 }
80
81 impl<'a> MatchBorrowExt for MatchBorrow<&'a [u8], Vec<u8>> {
82 type Matched = &'a [u8];
83 }
84
85 impl<T> MatchBorrowExt for MatchBorrow<&'_ T, T> {
86 type Matched = T;
87 }
88
89 impl<T> MatchBorrowExt for MatchBorrow<&'_ &'_ T, T> {
90 type Matched = T;
91 }
92
93 impl<T> MatchBorrowExt for MatchBorrow<T, &'_ T> {
94 type Matched = T;
95 }
96
97 impl<T> MatchBorrowExt for MatchBorrow<T, &'_ &'_ T> {
98 type Matched = T;
99 }
100
101 impl<T> MatchBorrowExt for MatchBorrow<Option<&'_ T>, Option<T>> {
102 type Matched = Option<T>;
103 }
104
105 impl<T> MatchBorrowExt for MatchBorrow<Option<&'_ &'_ T>, Option<T>> {
106 type Matched = Option<T>;
107 }
108
109 impl<T> MatchBorrowExt for MatchBorrow<Option<T>, Option<&'_ T>> {
110 type Matched = Option<T>;
111 }
112
113 impl<T> MatchBorrowExt for MatchBorrow<Option<T>, Option<&'_ &'_ T>> {
114 type Matched = Option<T>;
115 }
116
117 impl<T, U> MatchBorrowExt for &'_ MatchBorrow<T, U> {
118 type Matched = U;
119 }
120
121 pub fn conjure_value<T>() -> T {
122 panic!()
123 }
124
125 pub fn dupe_value<T>(_t: &T) -> T {
126 panic!()
127 }
128
129 #[test]
130 fn test_dupe_value() {
131 let val = &(String::new(),);
132
133 if false {
134 let _: i32 = dupe_value(&0i32);
135 let _: String = dupe_value(&String::new());
136 let _: String = dupe_value(&val.0);
137 }
138 }
139
140 #[test]
141 fn test_wrap_same() {
142 if false {
143 let _: i32 = WrapSame::<i32, _>::new(&0i32).wrap_same();
144 let _: i32 = WrapSame::<i32, _>::new(&"hello, world!").wrap_same();
145 let _: Option<i32> = WrapSame::<i32, _>::new(&Some(String::new())).wrap_same();
146 }
147 }
148
149 #[test]
150 fn test_match_borrow() {
151 if false {
152 let (_, match_borrow) = MatchBorrow::new("", &String::new());
153 let _: &str = match_borrow.match_borrow();
154
155 let (_, match_borrow) = MatchBorrow::new(&&0i64, &0i64);
156 let _: i64 = match_borrow.match_borrow();
157
158 let (_, match_borrow) = MatchBorrow::new(&0i64, &0i64);
159 let _: i64 = match_borrow.match_borrow();
160
161 let (_, match_borrow) = MatchBorrow::new(0i64, &0i64);
162 let _: i64 = match_borrow.match_borrow();
163 }
164 }