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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use proc_macro2::TokenStream;
use quote::quote;
use crate::input::Input;
use crate::names;
pub fn derive(input: &Input) -> TokenStream {
let name = &input.name;
let visibility = &input.visibility;
let attrs = &input.attrs.ptr;
let mut_attrs = &input.attrs.ptr_mut;
let vec_name = names::vec_name(&input.name);
let ptr_name = names::ptr_name(&input.name);
let ptr_mut_name = names::ptr_mut_name(&input.name);
let ref_name = names::ref_name(&input.name);
let ref_mut_name = names::ref_mut_name(&input.name);
let doc_url = format!("[`{0}`](struct.{0}.html)", name);
let vec_doc_url = format!("[`{0}`](struct.{0}.html)", vec_name);
let ptr_doc_url = format!("[`{0}`](struct.{0}.html)", ptr_name);
let ptr_mut_doc_url = format!("[`{0}`](struct.{0}.html)", ptr_mut_name);
let ref_doc_url = format!("[`{0}`](struct.{0}.html)", ref_name);
let ref_mut_doc_url = format!("[`{0}`](struct.{0}.html)", ref_mut_name);
let fields_names = &input.fields.iter()
.map(|field| field.ident.clone().unwrap())
.collect::<Vec<_>>();
let ptr_fields_types = input.map_fields_nested_or(
|_, field_type| {
let field_ptr_type = names::ptr_name(field_type);
quote! { #field_ptr_type }
},
|_, field_type| quote! { *const #field_type },
).collect::<Vec<_>>();
let ptr_mut_fields_types = input.map_fields_nested_or(
|_, field_type| {
let field_ptr_type = names::ptr_mut_name(field_type);
quote! { #field_ptr_type }
},
|_, field_type| quote! { *mut #field_type },
).collect::<Vec<_>>();
let as_ptr = input.map_fields_nested_or(
|ident, _| quote! { self.#ident.as_ptr() },
|ident, _| quote! { self.#ident as *const _ },
).collect::<Vec<_>>();
let as_mut_ptr = input.map_fields_nested_or(
|ident, _| quote! { self.#ident.as_mut_ptr() },
|ident, _| quote! { self.#ident as *mut _ },
).collect::<Vec<_>>();
quote! {
/// An analog of a pointer to
#[doc = #doc_url]
/// with struct of array layout.
#(#[#attrs])*
#[derive(Copy, Clone)]
#visibility struct #ptr_name {
#(
/// pointer to the `
#[doc = stringify!(#fields_names)]
///` field of a single
#[doc = #doc_url]
/// inside a
#[doc = #vec_doc_url]
pub #fields_names: #ptr_fields_types,
)*
}
/// An analog of a mutable pointer to
#[doc = #doc_url]
/// with struct of array layout.
#(#[#mut_attrs])*
#[derive(Copy, Clone)]
#visibility struct #ptr_mut_name {
#(
/// pointer to the `
#[doc = stringify!(#fields_names)]
///` field of a single
#[doc = #doc_url]
/// inside a
#[doc = #vec_doc_url]
pub #fields_names: #ptr_mut_fields_types,
)*
}
#[allow(dead_code)]
impl #ptr_name {
/// Convert a
#[doc = #ptr_doc_url]
/// to a
#[doc = #ptr_mut_doc_url]
/// ; *i.e.* do a `*const T as *mut T` transformation.
#visibility fn as_mut_ptr(&self) -> #ptr_mut_name {
#ptr_mut_name {
#( #fields_names: #as_mut_ptr, )*
}
}
/// Similar to [`*const T::is_null()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.is_null).
pub fn is_null(self) -> bool {
false #( || self.#fields_names.is_null())*
}
/// Similar to [`*const T::as_ref()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref),
/// with the same safety caveats.
pub unsafe fn as_ref<'a>(self) -> Option<#ref_name<'a>> {
if self.is_null() {
None
} else {
Some(#ref_name {
#(#fields_names: self.#fields_names.as_ref().expect("should not be null"), )*
})
}
}
/// Similar to [`*const T::offset()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.offset),
/// with the same safety caveats.
pub unsafe fn offset(self, count: isize) -> #ptr_name {
#ptr_name {
#(#fields_names: self.#fields_names.offset(count), )*
}
}
/// Similar to [`*const T::offset()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.offset).
pub fn wrapping_offset(self, count: isize) -> #ptr_name {
#ptr_name {
#(#fields_names: self.#fields_names.wrapping_offset(count), )*
}
}
/// Similar to [`*const T::add()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.add),
/// with the same safety caveats.
pub unsafe fn add(self, count: usize) -> #ptr_name {
#ptr_name {
#(#fields_names: self.#fields_names.add(count), )*
}
}
/// Similar to [`*const T::sub()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.sub),
/// with the same safety caveats.
pub unsafe fn sub(self, count: usize) -> #ptr_name {
#ptr_name {
#(#fields_names: self.#fields_names.sub(count), )*
}
}
/// Similar to [`*const T::wrapping_add()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_add).
pub fn wrapping_add(self, count: usize) -> #ptr_name {
#ptr_name {
#(#fields_names: self.#fields_names.wrapping_add(count), )*
}
}
/// Similar to [`*const T::wrapping_sub()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_sub).
pub fn wrapping_sub(self, count: usize) -> #ptr_name {
#ptr_name {
#(#fields_names: self.#fields_names.wrapping_sub(count), )*
}
}
/// Similar to [`*const T::read()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read),
/// with the same safety caveats.
pub unsafe fn read(self) -> #name {
#name {
#(#fields_names: self.#fields_names.read(), )*
}
}
/// Similar to [`*const T::read_volatile()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read_volatile),
/// with the same safety caveats.
pub unsafe fn read_volatile(self) -> #name {
#name {
#(#fields_names: self.#fields_names.read_volatile(), )*
}
}
/// Similar to [`*const T::read_unaligned()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read_unaligned),
/// with the same safety caveats.
pub unsafe fn read_unaligned(self) -> #name {
#name {
#(#fields_names: self.#fields_names.read_unaligned(), )*
}
}
}
impl ::soa_derive::SoAPointers for #name {
type Ptr = #ptr_name;
type MutPtr = #ptr_mut_name;
}
#[allow(dead_code)]
#[allow(clippy::forget_non_drop)]
impl #ptr_mut_name {
/// Convert a
#[doc = #ptr_mut_doc_url]
/// to a
#[doc = #ptr_doc_url]
/// ; *i.e.* do a `*mut T as *const T` transformation
#visibility fn as_ptr(&self) -> #ptr_name {
#ptr_name {
#( #fields_names: #as_ptr, )*
}
}
/// Similar to [`*mut T::is_null()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.is_null).
pub fn is_null(self) -> bool {
false #( || self.#fields_names.is_null())*
}
/// Similar to [`*mut T::as_ref()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref),
/// with the same safety caveats.
pub unsafe fn as_ref<'a>(self) -> Option<#ref_name<'a>> {
if self.is_null() {
None
} else {
Some(#ref_name {
#(#fields_names: self.#fields_names.as_ref().expect("should not be null"), )*
})
}
}
/// Similar to [`*mut T::as_mut()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_mut),
/// with the same safety caveats.
pub unsafe fn as_mut<'a>(self) -> Option<#ref_mut_name<'a>> {
if self.is_null() {
None
} else {
Some(#ref_mut_name {
#(#fields_names: self.#fields_names.as_mut().expect("should not be null"), )*
})
}
}
/// Similar to [`*mut T::offset()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.offset),
/// with the same safety caveats.
pub unsafe fn offset(self, count: isize) -> #ptr_mut_name {
#ptr_mut_name {
#(#fields_names: self.#fields_names.offset(count), )*
}
}
/// Similar to [`*mut T::wrapping_offset()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_offset)
pub fn wrapping_offset(self, count: isize) -> #ptr_mut_name {
#ptr_mut_name {
#(#fields_names: self.#fields_names.wrapping_offset(count), )*
}
}
/// Similar to [`*mut T::add()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.add),
/// with the same safety caveats.
pub unsafe fn add(self, count: usize) -> #ptr_mut_name {
#ptr_mut_name {
#(#fields_names: self.#fields_names.add(count), )*
}
}
/// Similar to [`*mut T::sub()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.sub),
/// with the same safety caveats.
pub unsafe fn sub(self, count: usize) -> #ptr_mut_name {
#ptr_mut_name {
#(#fields_names: self.#fields_names.sub(count), )*
}
}
/// Similar to [`*mut T::wrapping_add()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_add),
/// with the same safety caveats.
pub fn wrapping_add(self, count: usize) -> #ptr_mut_name {
#ptr_mut_name {
#(#fields_names: self.#fields_names.wrapping_add(count), )*
}
}
/// Similar to [`*mut T::wrapping_sub()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_sub),
/// with the same safety caveats.
pub fn wrapping_sub(self, count: usize) -> #ptr_mut_name {
#ptr_mut_name {
#(#fields_names: self.#fields_names.wrapping_sub(count), )*
}
}
/// Similar to [`*mut T::read()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read),
/// with the same safety caveats.
pub unsafe fn read(self) -> #name {
#name {
#(#fields_names: self.#fields_names.read(), )*
}
}
/// Similar to [`*mut T::read_volatile()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read_volatile),
/// with the same safety caveats.
pub unsafe fn read_volatile(self) -> #name {
#name {
#(#fields_names: self.#fields_names.read_volatile(), )*
}
}
/// Similar to [`*mut T::read_unaligned()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.read_unaligned),
/// with the same safety caveats.
pub unsafe fn read_unaligned(self) -> #name {
#name {
#(#fields_names: self.#fields_names.read_unaligned(), )*
}
}
/// Similar to [`*mut T::write()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.write),
/// with the same safety caveats.
#[allow(clippy::forget_non_drop)]
pub unsafe fn write(self, val: #name) {
unsafe {
#(self.#fields_names.write(::std::ptr::read(&val.#fields_names));)*
}
// if val implements Drop, we don't want to run it here, only
// when the vec itself will be dropped
::std::mem::forget(val);
}
/// Similar to [`*mut T::write_volatile()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.write_volatile),
/// with the same safety caveats.
#[allow(clippy::forget_non_drop)]
pub unsafe fn write_volatile(self, val: #name) {
unsafe {
#(self.#fields_names.write_volatile(::std::ptr::read(&val.#fields_names));)*
}
// if val implements Drop, we don't want to run it here, only
// when the vec itself will be dropped
::std::mem::forget(val);
}
/// Similar to [`*mut T::write_unaligned()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.write_unaligned),
/// with the same safety caveats.
#[allow(clippy::forget_non_drop)]
pub unsafe fn write_unaligned(self, val: #name) {
unsafe {
#(self.#fields_names.write_unaligned(::std::ptr::read(&val.#fields_names));)*
}
// if val implements Drop, we don't want to run it here, only
// when the vec itself will be dropped
::std::mem::forget(val);
}
}
#[allow(dead_code)]
impl<'a> #ref_name<'a> {
/// Convert a
#[doc = #ref_doc_url]
/// to a
#[doc = #ptr_doc_url]
/// ; *i.e.* do a `&T as *const T` transformation
#visibility fn as_ptr(&self) -> #ptr_name {
#ptr_name {
#( #fields_names: #as_ptr, )*
}
}
}
#[allow(dead_code)]
impl<'a> #ref_mut_name<'a> {
/// Convert a
#[doc = #ref_mut_doc_url]
/// to a
#[doc = #ptr_doc_url]
/// ; *i.e.* do a `&mut T as *const T` transformation
#visibility fn as_ptr(&self) -> #ptr_name {
#ptr_name {
#( #fields_names: #as_ptr, )*
}
}
/// Convert a
#[doc = #ref_mut_doc_url]
/// to a
#[doc = #ptr_mut_doc_url]
/// ; *i.e.* do a `&mut T as *mut T` transformation
#visibility fn as_mut_ptr(&mut self) -> #ptr_mut_name {
#ptr_mut_name {
#( #fields_names: #as_mut_ptr, )*
}
}
}
}
}