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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Impls "using" a field

use super::{Error, ImplArgs, ImplTrait, Result};
use crate::SimplePath;
use proc_macro2::TokenStream as Toks;
use quote::quote;
use syn::{ItemStruct, PathArguments};

/// Implement [`core::borrow::Borrow`]
pub struct ImplBorrow;
impl ImplTrait for ImplBorrow {
    fn path(&self) -> SimplePath {
        SimplePath::new(&["", "core", "borrow", "Borrow"])
    }

    fn support_using(&self) -> bool {
        true
    }

    fn struct_items(&self, item: &ItemStruct, args: &ImplArgs) -> Result<(Toks, Toks)> {
        if let Some(field) = args.using_field(&item.fields) {
            let ty = field.ty.clone();
            let member = args.using_member().unwrap();
            let method = quote! {
                fn borrow(&self) -> & #ty {
                    &self.#member
                }
            };
            Ok((quote! { ::core::borrow::Borrow<#ty> }, method))
        } else {
            Err(Error::RequireUsing)
        }
    }
}

/// Implement [`core::borrow::BorrowMut`]
pub struct ImplBorrowMut;
impl ImplTrait for ImplBorrowMut {
    fn path(&self) -> SimplePath {
        SimplePath::new(&["", "core", "borrow", "BorrowMut"])
    }

    fn support_using(&self) -> bool {
        true
    }

    fn struct_items(&self, item: &ItemStruct, args: &ImplArgs) -> Result<(Toks, Toks)> {
        if let Some(field) = args.using_field(&item.fields) {
            let ty = field.ty.clone();
            let member = args.using_member().unwrap();
            let method = quote! {
                fn borrow_mut(&mut self) -> &mut #ty {
                    &mut self.#member
                }
            };
            Ok((quote! { ::core::borrow::BorrowMut<#ty> }, method))
        } else {
            Err(Error::RequireUsing)
        }
    }
}

/// Implement [`core::convert::AsRef`]
pub struct ImplAsRef;
impl ImplTrait for ImplAsRef {
    fn path(&self) -> SimplePath {
        SimplePath::new(&["", "core", "convert", "AsRef"])
    }

    fn support_using(&self) -> bool {
        true
    }

    fn struct_items(&self, item: &ItemStruct, args: &ImplArgs) -> Result<(Toks, Toks)> {
        if let Some(field) = args.using_field(&item.fields) {
            let ty = field.ty.clone();
            let member = args.using_member().unwrap();
            let method = quote! {
                fn as_ref(&self) -> & #ty {
                    &self.#member
                }
            };
            Ok((quote! { ::core::convert::AsRef<#ty> }, method))
        } else {
            Err(Error::RequireUsing)
        }
    }
}

/// Implement [`core::convert::AsMut`]
pub struct ImplAsMut;
impl ImplTrait for ImplAsMut {
    fn path(&self) -> SimplePath {
        SimplePath::new(&["", "core", "convert", "AsMut"])
    }

    fn support_using(&self) -> bool {
        true
    }

    fn struct_items(&self, item: &ItemStruct, args: &ImplArgs) -> Result<(Toks, Toks)> {
        if let Some(field) = args.using_field(&item.fields) {
            let ty = field.ty.clone();
            let member = args.using_member().unwrap();
            let method = quote! {
                fn as_mut(&mut self) -> &mut #ty {
                    &mut self.#member
                }
            };
            Ok((quote! { ::core::convert::AsMut<#ty> }, method))
        } else {
            Err(Error::RequireUsing)
        }
    }
}

/// Implement [`core::ops::Deref`]
pub struct ImplDeref;
impl ImplTrait for ImplDeref {
    fn path(&self) -> SimplePath {
        SimplePath::new(&["", "core", "ops", "Deref"])
    }

    fn support_path_arguments(&self) -> bool {
        true
    }

    fn support_using(&self) -> bool {
        true
    }

    fn struct_items(&self, item: &ItemStruct, args: &ImplArgs) -> Result<(Toks, Toks)> {
        if let Some(field) = args.using_field(&item.fields) {
            let target = match args.path_arguments {
                PathArguments::None => field.ty.clone(),
                PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
                    ref args,
                    ..
                }) => {
                    let mut result = None;
                    for arg in args {
                        if let syn::GenericArgument::Binding(b) = arg {
                            if b.ident == "Target" && result.is_none() {
                                result = Some(b.ty.clone());
                                continue;
                            }
                        }
                        return Err(Error::PathArguments("expected `<Target = ..>`"));
                    }
                    match result {
                        Some(r) => r,
                        None => return Err(Error::PathArguments("expected `<Target = ..>`")),
                    }
                }
                PathArguments::Parenthesized(_) => return Err(Error::PathArguments("unexpected")),
            };

            let member = args.using_member().unwrap();
            let method = quote! {
                type Target = #target;
                fn deref(&self) -> &Self::Target {
                    &self.#member
                }
            };
            Ok((quote! { ::core::ops::Deref }, method))
        } else {
            Err(Error::RequireUsing)
        }
    }
}

/// Implement [`core::ops::DerefMut`]
pub struct ImplDerefMut;
impl ImplTrait for ImplDerefMut {
    fn path(&self) -> SimplePath {
        SimplePath::new(&["", "core", "ops", "DerefMut"])
    }

    fn support_using(&self) -> bool {
        true
    }

    fn struct_items(&self, _: &ItemStruct, args: &ImplArgs) -> Result<(Toks, Toks)> {
        if let Some(member) = args.using_member() {
            let method = quote! {
                fn deref_mut(&mut self) -> &mut Self::Target {
                    &mut self.#member
                }
            };
            Ok((quote! { ::core::ops::DerefMut }, method))
        } else {
            Err(Error::RequireUsing)
        }
    }
}