restless_core/
methods.rs

1//! Request method wrappers.
2//!
3//! For a given type T that implements [`GetRequest`], [`Get<T>`] implements [`Request`]. These are
4//! types that wrap request method traits and turn them into something that implements [`Request`].
5//!
6//! You do not need to use these directly, rather you can use the [`RequestMethod`] trait to make
7//! your type implement [`Request`] directing, using one of these wrapper types.
8use crate::*;
9
10macro_rules! wrapper {
11    ($wrapper:ident, $trait:ident) => {
12        wrapper!(@struct, $wrapper, $trait);
13        wrapper!(@from, $wrapper, $trait);
14    };
15
16    (@struct, $wrapper:ident, $trait:ident) => {
17        #[doc = "Turn a [`"]
18        #[doc = stringify!($trait)]
19        #[doc = "`] into a [`Request`]."]
20        #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
21        pub struct $wrapper<T: $trait> {
22            /// Inner request
23            pub inner: T
24        }
25    };
26
27    (@from, $wrapper:ident, $trait:ident) => {
28        impl<'a, T: $trait> From<&'a T> for &'a $wrapper<T> {
29            fn from(request: &T) -> &$wrapper<T> {
30                unsafe {
31                    &*(request as *const T as *const $wrapper<T>)
32                }
33            }
34        }
35
36        impl<T: $trait> From<T> for $wrapper<T> {
37            fn from(request: T) -> $wrapper<T> {
38                $wrapper {
39                    inner: request
40                }
41            }
42        }
43    };
44}
45
46wrapper!(Get, GetRequest);
47wrapper!(Head, HeadRequest);
48wrapper!(Post, PostRequest);
49wrapper!(Delete, DeleteRequest);
50wrapper!(Patch, PatchRequest);