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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! 类型转换类契定。
//!

/// 定义将只读引用转化至常量指针的契定。
pub trait AsPtr<T>: AsRef<T> {
    /// 将当前只读引用转化至常量指针。
    ///
    /// # Safety
    ///
    /// 强转指针属于危险操作,请务必确保其安全性。
    unsafe fn as_ptr(&self) -> *const T;
}

/// 定义将只读引用转化至可写指针的契定。
pub trait AsPtrMut<T>: AsPtr<T> {
    /// 将当前只读引用转化至可写指针。
    ///
    /// # Safety
    ///
    /// 强转指针属于危险操作,请务必确保其安全性。
    unsafe fn as_ptr_mut(&self) -> *mut T;
}

/// 用于帮助实现 [AsRef] 契定的宏。
///
/// [AsRef]: https://doc.rust-lang.org/std/convert/trait.AsRef.html
///
/// # Examples
///
/// ```
/// use pavo_traits::{impl_as_ref};
///
/// struct Bar {}
///
/// struct Foo {
///     bar: Bar,
/// }
///
/// impl_as_ref!(Foo);
/// // Comment/Uncomment to select one of the follow lines.
/// // impl_as_ref!(Foo, Bar); // Exclusive with the bellow.
/// impl_as_ref!(Foo, Bar, bar); // Exclusive with the above.
/// ```
#[macro_export]
macro_rules! impl_as_ref {
    ($Type:ty) => {
        impl AsRef<$Type> for $Type {
            fn as_ref(&self) -> &$Type {
                self
            }
        }
    };

    ($Type:ty, $Target:ty) => {
        impl AsRef<$Target> for $Type {
            fn as_ref(&self) -> &$Target {
                &self as &$Target
            }
        }
    };

    ($Type:ty, $Target:ty, $Expr:tt) => {
        impl AsRef<$Target> for $Type {
            fn as_ref(&self) -> &$Target {
                &self.$Expr
            }
        }
    };
}

/// 用于帮助实现 [AsMut] 契定的宏。
///
/// [AsMut]: https://doc.rust-lang.org/std/convert/trait.AsMut.html
///
/// # Examples
///
/// ```
/// use pavo_traits::{impl_as_mut};
///
/// struct Bar {}
///
/// struct Foo {
///     bar: Bar,
/// }
///
/// impl_as_mut!(Foo);
/// // Comment/Uncomment to select one of the follow lines.
/// // impl_as_mut!(Foo, Bar); // Exclusive with the bellow.
/// impl_as_mut!(Foo, Bar, bar); // Exclusive with the above.
/// ```
#[macro_export]
macro_rules! impl_as_mut {
    ($Type:ty) => {
        impl AsMut<$Type> for $Type {
            fn as_mut(&mut self) -> &mut $Type {
                self
            }
        }
    };

    ($Type:ty, $Target:ty) => {
        impl AsMut<$Target> for $Type {
            fn as_mut(&mut self) -> &mut $Target {
                unsafe { std::mem::transmute::<&mut $Type, &mut $Target>(self) }
            }
        }
    };

    ($Type:ty, $Target:ty, $Expr:tt) => {
        impl AsMut<$Target> for $Type {
            fn as_mut(&mut self) -> &mut $Target {
                &mut self.$Expr
            }
        }
    };
}

/// 用于帮助实现 `AsMut + AsRef` 等契定的宏。
#[macro_export(local_inner_macros)]
macro_rules! impl_as_mut_and_ref {
    ($($x:tt)*) => {
        impl_as_mut!($($x)*);
        impl_as_ref!($($x)*);
    }
}

/// 用于帮助实现 [AsPtr] 契定的宏。
///
/// [AsPtr]: trait.AsPtr.html
///
/// # Examples
///
/// ```
/// use pavo_traits::{impl_as_ref, impl_as_ptr, AsPtr};
///
/// struct Bar {}
/// struct Foo {
///     inner: Bar,
/// }
///
/// impl_as_ref!(Foo);
/// impl_as_ref!(Foo, Bar, inner);
/// impl_as_ptr!(Foo);
/// impl_as_ptr!(Foo, Bar);
/// ```
#[macro_export]
macro_rules! impl_as_ptr {
    ($Type:ty) => {
        impl AsPtr<$Type> for $Type {
            unsafe fn as_ptr(&self) -> *const $Type {
                self.as_ref() as *const $Type
            }
        }
    };

    ($Type:ty, $Target:ty) => {
        impl AsPtr<$Target> for $Type {
            unsafe fn as_ptr(&self) -> *const $Target {
                self.as_ref() as *const $Target
            }
        }
    };

    ($Type:ty, $Target:ty, $Expr:tt) => {
        impl_as_ptr!($Type, $Target);
    };
}

/// 用于帮助实现 [AsPtrMut] 契定的宏。
///
/// [AsPtrMut]: trait.AsPtrMut.html
///
/// # Examples
///
/// ```
/// use pavo_traits::{impl_as_ref, impl_as_ptr, impl_as_ptr_mut, AsPtr, AsPtrMut};
///
/// struct Bar {}
/// struct Foo {
///     inner: Bar,
/// }
///
/// impl_as_ref!(Foo);
/// impl_as_ref!(Foo, Bar, inner);
/// impl_as_ptr!(Foo);
/// impl_as_ptr!(Foo, Bar);
/// impl_as_ptr_mut!(Foo);
/// impl_as_ptr_mut!(Foo, Bar);
/// ```
#[macro_export]
macro_rules! impl_as_ptr_mut {
    ($Type:ty) => {
        impl AsPtrMut<$Type> for $Type {
            unsafe fn as_ptr_mut(&self) -> *mut $Type {
                self.as_ptr() as *const $Type as *mut $Type
            }
        }
    };

    ($Type:ty, $Target:ty) => {
        impl AsPtrMut<$Target> for $Type {
            unsafe fn as_ptr_mut(&self) -> *mut $Target {
                self.as_ptr() as *const $Target as *mut $Target
            }
        }
    };

    ($Type:ty, $Target:ty, $Expr:tt) => {
        impl_as_mut!($Type, $Target);
    };
}

/// 用于帮助在单个类型上实现 `AsRef + AsMut + AsPtr + AsPtrMut` 等契定的宏。
#[macro_export(local_inner_macros)]
macro_rules! impl_as_bundle {
    () => {};
    ($($x:tt)*) => {
        impl_as_ref!($($x)*);
        impl_as_mut!($($x)*);
        impl_as_ptr!($($x)*);
        impl_as_ptr_mut!($($x)*);
    };
}

/// 用于帮助在多个类型上实现 `AsRef + AsMut + AsPtr + AsPtrMut` 等契定的宏。
#[macro_export(local_inner_macros)]
macro_rules! impl_as_bundle_many {
    ($($Type:ty),* $(,)?) => {
        $(
            impl_as_ref!($Type);
            impl_as_mut!($Type);
            impl_as_ptr!($Type);
            impl_as_ptr_mut!($Type);
        )*
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[repr(C)]
    #[derive(Debug)]
    struct Foo {
        v: usize,
    }

    impl_as_ref!(Foo);
    impl_as_ptr!(Foo);
    impl_as_ptr_mut!(Foo);

    fn read_ptr<T>(t: &T) -> usize
    where
        T: AsPtrMut<Foo>,
    {
        unsafe { std::ptr::read(t.as_ptr() as *const usize) }
    }

    fn write_ptr_mut<T>(t: &T)
    where
        T: AsPtrMut<Foo>,
    {
        unsafe {
            std::ptr::write(t.as_ptr_mut() as *mut usize, 456);
        }
    }

    #[test]
    fn test_as_ptr() {
        unsafe {
            let f = Foo { v: 123 };
            assert_eq!(f.as_ptr(), &f as *const Foo);
            let v = std::ptr::read(f.as_ptr() as *const usize);
            assert_eq!(v, f.v);
            let v2 = read_ptr(&f);
            assert_eq!(v, v2);
        }
    }

    #[test]
    fn test_as_ptr_mut() {
        unsafe {
            let f = Foo { v: 123 };
            assert_eq!(f.as_ptr_mut(), &f as *const Foo as *mut Foo);
            std::ptr::write(f.as_ptr_mut() as *mut usize, 321);
            assert_eq!(321, f.v);
            write_ptr_mut(&f);
            assert_eq!(456, f.v);
        }
    }
}