esyn/auto/
wrap.rs

1use crate::*;
2use std::fmt::Debug;
3use syn::*;
4
5// REFS: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-types
6#[repr(transparent)]
7pub struct Wrap<T>(T);
8
9impl<T> Wrap<T> {
10    pub fn new(v: T) -> Self {
11        Self(v)
12    }
13
14    pub fn get(self) -> T {
15        self.0
16    }
17
18    pub fn get_ref(&self) -> &T {
19        &self.0
20    }
21
22    pub fn get_mut(&mut self) -> &mut T {
23        &mut self.0
24    }
25
26    pub fn into_expr(self) -> syn::Expr
27    where
28        T: EsynSer,
29    {
30        syn::parse_quote!( #self )
31    }
32}
33
34impl<T> ToTokens for Wrap<T>
35where
36    T: EsynSer,
37{
38    fn to_tokens(&self, tokens: &mut TokenStream) {
39        tokens.extend(<T as EsynSer>::ser(&self.0));
40    }
41}
42
43impl<T> parse::Parse for Wrap<T>
44where
45    T: DeRs<Expr>,
46{
47    fn parse(input: parse::ParseStream) -> syn::Result<Self> {
48        let expr = input.parse()?;
49
50        Ok(Self(T::de(&expr).unwrap()))
51    }
52}
53
54impl<T: Debug> Debug for Wrap<T> {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        Debug::fmt(&self.0, f)
57    }
58}