bon_macros/util/
fn_arg.rs

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
pub(crate) trait FnArgExt {
    fn attrs_mut(&mut self) -> &mut Vec<syn::Attribute>;
    fn ty_mut(&mut self) -> &mut syn::Type;
    fn as_receiver(&self) -> Option<&syn::Receiver>;
    fn as_typed(&self) -> Option<&syn::PatType>;
}

impl FnArgExt for syn::FnArg {
    fn attrs_mut(&mut self) -> &mut Vec<syn::Attribute> {
        match self {
            Self::Receiver(arg) => &mut arg.attrs,
            Self::Typed(arg) => &mut arg.attrs,
        }
    }

    fn ty_mut(&mut self) -> &mut syn::Type {
        match self {
            Self::Receiver(arg) => &mut arg.ty,
            Self::Typed(arg) => &mut arg.ty,
        }
    }

    fn as_receiver(&self) -> Option<&syn::Receiver> {
        match self {
            Self::Typed(_) => None,
            Self::Receiver(arg) => Some(arg),
        }
    }

    fn as_typed(&self) -> Option<&syn::PatType> {
        match self {
            Self::Typed(arg) => Some(arg),
            Self::Receiver(_) => None,
        }
    }
}