forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintrowospection.rs
1875 lines (1729 loc) · 71.8 KB
/
introwospection.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Traits and data types for language-level compile-time reflection facilities
//! (also sometimes referred to as "introspection".) Compile-time reflection is
//! built on the idea that the language itself is providing every single bit of
//! information it possibly can (without directly leaking tokens or AST
//! information) in a highly structured manner that resembles language
//! constructs (but may not map cleanly to compiler internals, requiring just
//! a tiny bit of compile-time data massaging to produce). The goal of all
//! introspection facilities is first and foremost the ability to act on the
//! information of the code itself. Most of the things presented here will be
//! primitive as compared to counterparts in languages such as C#, Java,
//! Haskell, JS, Ruby, or similar dynamic languages.
//!
//! In particular, the two core tenets that are non-negotiable for this:
//! - it must not perform runtime allocation, ever.
//! - it must not require runtime dispatch to use, ever.
//!
//! Users of this API may lower the concepts found here to dynamic alternatives,
//! and as part of this API there are fundamental elements describing the full
//! functionality but without all of the compile-time information (see the
//! `any_introwospection` module as part of the `std` crate).
//!
//! The explicit goal of this module is to provide the library components that
//! work with the language to give the full features of compile-time,
//! non-allocating reflection in Rust. Using the `introwospect` and
//! `introwospect_over` keywords, one is meant to do compile-time inspection
//! in conjunction with generic programming. This also allows users to do
//! things that `#[derive(...)]` macros would normally need to tag and
//! annotate their code with without needing to invade that crate or its
//! namespaces to provide similar or identical functionality.
//!
//! In this direction, the following Rust code would allow someone --
//! without allocating or using dynamic dispatch -- to walk over the
//! fields of a `struct` they do **not** own:
//!
//! ```
//! use std::introwospection::*;
//! use other_crate::Meow; // a struct of the form:
//! /*
//! pub struct Meow {
//! pub purr_level: i32,
//! hairballs: i32,
//! pub scratch_couch: SystemTime,
//! }
//! */
//!
//! fn main () {
//! type MeowInfo = introwospect_type<other_crate::Meow>;
//! println!("struct {}, with {} fields:\n\t{} ({}, {})\n\t{} ({}, {})",
//! <MeowInfo as StructDescriptor>::NAME,
//! <MeowInfo as StructDescriptor>::FIELD_COUNT,
//! <MeowInfo as FieldDescriptor<0>>::FIELD_COUNT,
//! std::any::type_name::<<MeowInfo as FieldDescriptor<0>>::Type>(),
//! <MeowInfo as FieldDescriptor<0>>::BYTE_OFFSET,
//! <MeowInfo as FieldDescriptor<1>>::NAME,
//! std::any::type_name::<<MeowInfo as FieldDescriptor<1>>::Type>(),
//! <MeowInfo as FieldDescriptor<1>>::BYTE_OFFSET);
//! // Should display:
//! /* struct other_create::Meow with 2 fields:
//! * purr_level (i32, 0)
//! * scratch_couch (std::time::SystemTime, 8)
//! */
//! }
//! ```
//!
//! As you can see, we can use the `StructDescriptor` trait to access the
//! associated, compile-time constants `NAME` and `FIELD_COUNT` for a
//! data type in an outside crate. It also provides compile-time access to
//! each and every field on the type that is **visible** to the current
//! scope. This means that private fields stay inaccessible, such as
//! `hairballs` on the `Meow` struct from the `other_crate`. Of course, it
//! is tedious to program in this fashion: this is effectively hard-coding
//! the number of fields you can access by way of accessing each field
//! directly through a Fully Qualified path. To reduce the boilerplate,
//! this can be simplified through the use of visitors:
//!
//! ```
//! use std::introwospection::*;
//! use other_crate::Meow; // a struct of the form:
//! /*
//! pub struct Meow {
//! pub purr_level: i32,
//! hairballs: i32,
//! pub scratch_couch: SystemTime
//! }
//! */
//!
//! struct DescriptorPrinter;
//! impl FieldDescriptorVisitor for DescriptorPrinter {
//! type Output = ()
//! fn visit_field<Type, const INDEX: usize>(&self)
//! -> Self::Output
//! where Type : FieldDescriptor<INDEX>
//! {
//! let type_name = std::any::type_name::<Type::Type>();
//! println!("\t{} ({}, {})",
//! Type::NAME,
//! type_name,
//! Type::BYTE_OFFSET);
//! }
//! }
//! impl StructDescriptorVisitor for DescriptorPrinter {
//! type Output = ()
//! fn visit_struct<Type>(&self)
//! -> Self::Output
//! where Type : StructDescriptor
//! {
//! println!("struct {}, with {} fields:",
//! Type::NAME,
//! Type::FIELD_COUNT);
//! // now, introspect over the fields of this type.
//! ( introwospect_over(Type::Type, Type::Fields, self) );
//! }
//! }
//!
//! fn main () {
//! type MeowInfo = introwospect<other_crate::Meow>;
//! let printer = DescriptorPrinter;
//! printer.visit_struct::<MeowInfo>();
//! // Should display:
//! /* struct other_create::Meow with 2 fields:
//! * purr_level (i32, 0)
//! * scratch_couch (std::time::SystemTime, 8)
//! */
//! }
//! ```
//!
//! Because calling the right visitor method is often annoying to
//! do with various different types or in generic contexts, a
//! different form of the `introwospect` keyword can be used to
//! do the exact same thing as above:
//!
//! ```
//! use std::introwospection::*;
//! use other_crate::Meow; // a struct of the form:
//! /*
//! pub struct Meow {
//! pub purr_level: i32,
//! hairballs: i32,
//! pub scratch_couch: SystemTime
//! }
//! */
//!
//! struct DescriptorPrinter;
//! impl FieldDescriptorVisitor for DescriptorPrinter {
//! type Output = ()
//! fn visit_field<Type, const INDEX: usize>(&self)
//! -> Self::Output
//! where Type : FieldDescriptor<INDEX>
//! {
//! let type_name = std::any::type_name::<Type::Type>();
//! println!("\t{} ({}, {})\n"
//! Type::NAME,
//! type_name,
//! Type::BYTE_OFFSET);
//! }
//! }
//! impl StructDescriptorVisitor for DescriptorPrinter {
//! type Output = ()
//! fn visit_struct<Type>(&self)
//! -> Self::Output
//! where Type : StructDescriptor
//! {
//! println!("struct {}, with {} fields:\n",
//! Type::NAME,
//! Type::FIELD_COUNT);
//! // now, introspect over the fields of this type.
//! ( introwospect_over(Type::Type, Type::Fields, self) );
//! }
//! }
//!
//! fn main () {
//! let printer = DescriptorPrinter;
//! printer.visit_struct(introwospect(other_crate::Meow))
//! // // Equivalent to this:
//! //
//! // introwospect(other_crate::Meow, printer);
//! //
//! // // or, equivalent to this:
//! //
//! // type MeowInfo = introwospect_type<other_crate::Meow>;
//! // printer.visit_struct::<MeowInfo>();
//! // and it should display:
//! /* struct other_create::Meow with 2 fields:
//! * purr_level (i32, 0)
//! * scratch_couch (std::time::SystemTime, 8)
//! */
//! }
//! ```
//!
//! Not all the time, however, is it beneficial to keep all of
//! this information at compile-time. So, this library offers a
//! visitor to convert everything to its more generic and
//! readily-usable form of descriptors that are type-erased,
//! employing several different kinds of type erasure and
//! possibly allocation:
//!
//! ```
//! use std::introwospection::*;
//! use other_crate::Meow; // a struct of the form:
//! /*
//! pub struct Meow {
//! pub purr_level: i32,
//! hairballs: i32,
//! pub scratch_couch: SystemTime
//! }
//! */
//!
//! fn main () {
//! let printer = DescriptorPrinter;
//! let any_struct: AnyStructDescriptor
//! = introwospect(other_crate::Meow, ToAnyDescriptorVisitor);
//! /* use type-erased information here. */
//! }
//! ```
//!
//! All of these data types follow the naming convention `Any{}Descriptor`,
//! where `{}` is filled in with `Struct`, `Field`, `Enum`, `Array`, `Tuple`,
//! and so-on, and so-forth.
#![unstable(feature = "introwospection", issue = "none")]
use crate::any::type_name;
use crate::any::{Any, TypeId};
use crate::fmt::{Debug, Display, Formatter, Result};
#[cfg(not(bootstrap))]
use crate::mem::offset_of;
use crate::mem::Discriminant;
use crate::option::Option;
/// The Abstract Data Type (ADT) identifier for determining the kind of ADT being
/// reflected over. Included as part of anything that implements ghe `AdtDescriptor`
/// trait.
#[non_exhaustive]
pub enum AdtId {
/// An abstract data type made using the `struct` keyword. Offsets are from the beginning
/// of the object, and may not correspond to the source code order of the field.
Struct,
/// An abstract data type made using the `union` keyword. Most
/// of the offsets for a `union` (or for a `#[repr(C)]` enumeration)
/// should be at or close to 0, and the discriminant is generally
/// managed by the user rather than the compiler, which makes it
/// an unsafe construct.
Union,
/// An abstract data type made using the `enum` keyword. It contains 0 or
/// more variants which are fields directly related to the enumeration itself,
/// and whose offsets are calculated as from the base of the enumeration,
/// and NOT as an independent data type.
Enum,
/// A tuple, created by a list of initializers or types enclosed by parentheses
/// (e.g., `( ... )`). Note that the `unit` type is just the empty tuple, and so
/// is not listed as a separate type in this list.
Tuple,
/// An array type, usually formed through initializer with square brackets. The type is
/// a type parameter `T` and a const generic parameter `const N: usize`, e.g. `[None; N]`.
Array,
/// A slice type, usually not directly instantiated but instead borrowed as a view
/// over another type like `[T; N]` or `Vec<T>`, typically via slice indexing,
/// e.g. `[0; 100][13..=37]`.
Slice,
/// A function definition, such as those defined with the `fn` keyword.
Function,
}
/// An empty `struct` whose sole job is to indicate when a variant is unspecified.
/// This is important in the case of two variants of e.g. an
/// enumeration which only differ by the use of nothing and the
/// use of () in the variants. That is:
/// ````
/// enum E0 { A }
/// enum E1 { A() }
/// ````
/// are two entirely separate constructs with different meanings. There
/// is no in-language type description for the `E0::A`, while `E1::A`'s
/// "field" is just the unit type `()`.
///
/// `struct`s such as
/// ```
/// struct S0;
/// struct S1();
/// struct S1{};
/// ```
/// may also, apparently, have subtle differences, but this is indicated
/// elsewhere in the API by field counts and whether or not those fields
/// are anonymous (their `NAME` is empty) or have names.
pub struct NoType;
/// An instance of the `NoType` structure, for use in returning a consistent
/// "not available" representation for specific fields on the creation of the
/// `Any{…}Descriptor` types.
const NO_TYPE: NoType = NoType;
/// An enumeration for determining the different ways a struct or variant
/// is defined.
#[non_exhaustive]
pub enum FieldSyntax {
/// Nothing was used, as in e.g. E::A in `enum E { A }`
/// or `struct S;`.
Nothing,
/// Parentheses were used, as in e.g. E::A in `enum E { A() }`
/// or `struct S()`.
Parentheses,
/// Braces were used, as in e.g. E::A in `enum E { A{} }`
/// or `struct S{}`.
Braces,
}
/// A list of names and values of attributes contained within the
/// `#[introwospection(...)]` attribute.
#[non_exhaustive]
pub struct AttributeDescriptor {
/// The name of an attribute. Required, comes before the `=`
/// sign (if present).
pub name: &'static str,
/// The optional value of the attribute. Optional, comes
/// after the `=` sign (if present). Does not include the
/// first matching set of quotation marks that delimit it.
pub value: Option<&'static str>,
}
/// The basic Abstract Data Type (ADT) descriptor.
pub unsafe trait AdtDescriptor {
/// The identifying ID of an abstract data type, for matching on whether it's a function,
/// `struct`, `union`, and similar useful identifications.
const ID: AdtId;
/// The name of the abstract data type. This is meant to be the full path of the data type,
/// according to the version of Rust this was compiled against.
const NAME: &'static str;
/// The introwospection attributes (`#[introwospection(...)`]) attached to this entity.
/// These are attributes capable of being used for compile-time introspection, such as for
/// marking a field as non-serializable or noting specific intended behaviors for a function
/// definition and the processing of its arguments.
///
/// NOTE
/// Only `introwospection` attributes are collected here. Other attributes are not,
/// as it is important for the author of a specific data type, function, or field to have
/// penultimate control of such attributes. (Individuals writing `*Visitor` types may alter
/// or ignore behavior for attributes, which gives final control to the individual writing
/// such visitor methods.)
const ATTRIBUTES: &'static [AttributeDescriptor] = &[];
}
/// A description for an ADT which may be associated with an inherent implementation,
/// such as `struct`s, `union`s, and enumerations.
pub unsafe trait InherentFunctionsAdt {
/// A type describing all of the inherent functions associated with this enumeration.
///
/// NOTE
/// TODO(thephd) Enable a succint way to describe all of the constraints on this type:
///
/// ```
/// type InherentFunctions :
/// (for <const I: usize = 0..Self::INHERENT_FUNCTION_COUNT>
/// InherentFunctionDescriptor<I>
/// )
/// = NoType;
/// ```
/// doing this would allow it to be acted upon in a meaningful fashion by generic code,
/// but such bounds/constraint technology does not exist yet.
type InherentFunctions = NoType;
/// The number of inherent functions for this enumeration.
const INHERENT_FUNCTION_COUNT: usize = 0;
}
/// A description for an ADT which may have fields as part of its creation and description,
/// such as `struct`s, `union`s, tuples, and the variants of enumerations.
pub unsafe trait FieldsAdt {
/// A type describing all of the fields associated with this type.
///
/// NOTE
/// TODO(thephd) Enable a succint way to describe all of the constraints on this type:
///
/// ```
/// type Fields :
/// (for <const I: usize = 0..Self::FIELD_COUNT> FieldDescriptor<I>)
/// = NoType;
/// ```
/// doing this would allow it to be acted upon in a meaningful fashion by generic code,
/// but such bounds/constraint technology does not exist yet.
type Fields = NoType;
/// The number of fields on this type.
const FIELD_COUNT: usize = 0;
/// What kind of syntax was used to encapsulate the fields on this type.
const FIELD_SYNTAX: FieldSyntax = FieldSyntax::Nothing;
/// Whether or not there are any fields which are not visible for this type.
const NON_VISIBLE_FIELDS: bool = false;
}
/// A description of a `struct` type.
pub unsafe trait StructDescriptor: AdtDescriptor + FieldsAdt {
/// The type of the `struct` that was described.
type Type;
}
/// A description of a `union` type.
pub unsafe trait UnionDescriptor: AdtDescriptor + FieldsAdt {
/// The type of the `union` that was described.
type Type;
}
/// A description of an enumeration type.
pub unsafe trait EnumDescriptor: AdtDescriptor {
/// The type of the `enum` that was described.
type Type;
/// A type describing all of the variants of this enumeration.
///
/// NOTE
/// TODO(thephd) Enable a succint way to describe all of the constraints on this type:
///
/// ```
/// type Variants :
/// (for <const I: usize = 0..Self::VARIANT_COUNT> VariantDescriptor<I>)
/// = NoType;
/// ```
/// doing this would allow it to be acted upon in a meaningful fashion by generic code,
/// but such bounds/constraint technology does not exist yet.
type Variants = NoType;
/// The number of variants for this enumeration.
const VARIANT_COUNT: usize = 0;
/// Whether or not this enumeration is an integer-style
/// (`#[repr(IntType)]`) enumeration.
const IS_INTEGER_ENUMERATION: bool = false;
}
/// A description of a function definition or similar construct.
pub unsafe trait FunctionDescriptor: AdtDescriptor {
/// The type of the function that was described.
type Type;
/// A type describing all of the parameters of this function. If this is `NoType`, then
/// there were no parameters that were part of this function.
///
/// NOTE
/// TODO(thephd) Enable a succinct way to describe all of the constraints on this type:
/// ```
/// type Parameters :
/// (for <const I: usize = 0..Self::PARAMETER_COUNT> ParameterDescriptor<I>)
/// = NoType;
/// ```
/// to specify the proper boundaries to make this type usable in generic contexts. (This is
/// bikeshed syntax and subject to change, as there is already a `for <T>` trait bounds
/// feature in Rust.)
type Parameters = NoType;
/// The return type of of the function.
type ReturnType;
/// The number of parameters in the function. Note that a pattern constitutes a
/// single parameter.
const PARAMETER_COUNT: usize = 0;
}
/// A description of a function implementation on an existing type as part of its `impl`
/// definition. Note that this does not include implementations of a specific trait for
/// a specific type.
pub unsafe trait InherentFunctionDescriptor<const INDEX: usize>: FunctionDescriptor {
/// The type this inherent function belongs to. Note that this does not imply a
/// `self` parameter or `Self` return exists on the function.
type Owner;
}
/// A description of a built-in array type.
pub unsafe trait ArrayDescriptor: AdtDescriptor {
/// The full type of the array.
type Type;
/// The element type of the array.
type Element;
/// The number of elements of type `Element` in this array.
const ELEMENT_COUNT: usize = 0;
}
/// A description of a built-in slice type.
pub unsafe trait SliceDescriptor: AdtDescriptor {
/// The full type of the slice.
type Type;
/// The element type of the slice.
type Element;
}
/// A description of a built-in tuple type.
pub unsafe trait TupleDescriptor: AdtDescriptor + FieldsAdt {
/// The full type of the tuple.
type Type;
}
/// A parameter for a function definition, or similar construction.
pub unsafe trait ParameterDescriptor<const PARAMETER_INDEX: usize> {
/// The function type related to this parameter descriptor.
type Owner;
/// The type of the function parameter.
type Type;
/// The 0-based declaration (source code) index.
const PARAMETER_INDEX: usize = PARAMETER_INDEX;
/// The name of the parameter in the function. This may be empty, as would be the case for a
/// function declaration that contains a destructuring that breaks the parameter down into the
/// constituent parts with destructing to match a pattern.
const NAME: &'static str;
/// The introwospection attributes (`#[introwospection(...)`]) attached to this entity.
/// These are attributes capable of being used for compile-time introspection, such as for
/// marking a field as non-serializable or noting specific intended behaviors for a function
/// definition and the processing of its arguments.
///
/// NOTE
/// Only `introwospection` attributes are collected here. Other attributes are not,
/// as it is important for the author of a specific data type, function, or field to have
/// penultimate control of such attributes. (Individuals writing `*Visitor` types may alter
/// or ignore behavior for attributes, which gives final control to the individual writing
/// such visitor methods.)
const ATTRIBUTES: &'static [AttributeDescriptor] = &[];
}
/// A descriptor that describes all the necessary information of a field that exists on the variant
/// of an enumeration, a field on a `struct`, or a field on a `union`.
///
/// `DECLARATION_INDEX` is the 0-based index of the field in declaration (source code) order.
pub unsafe trait FieldDescriptor<const DECLARATION_INDEX: usize> {
/// The type that owns this field. It may be any abstract data type, such as a `union`, a variant on an `enum`,
/// a tuple, or a `struct` type. All (byte) offsets are from the base of an `Self::Owner` object.
type Owner;
/// The data type of the field itself.
type Type;
/// The 0-based declaration (source code) index.
const DECLARATION_INDEX: usize = DECLARATION_INDEX;
/// The name of the field within the `union`, variant, tuple, or `struct`. If this is empty, it
/// signifies a completely unnamed field. If this is part of a tuple-like field syntax,
/// then the name of the field will not be empty, but instead be `0` or similar.
const NAME: &'static str;
/// The byte offset from the base of an owner type object to the data type of this field.
const BYTE_OFFSET: usize;
/// The introwospection attributes (`#[introwospection(...)`]) attached to this entity.
/// These are attributes capable of being used for compile-time introspection, such as for
/// marking a field as non-serializable or noting specific intended behaviors for a function
/// definition and the processing of its arguments.
///
/// NOTE
/// Only `introwospection` attributes are collected here. Other attributes are not,
/// as it is important for the author of a specific data type, function, or field to have
/// penultimate control of such attributes. (Individuals writing `*Visitor` types may alter
/// or ignore behavior for attributes, which gives final control to the individual writing
/// such visitor methods.)
const ATTRIBUTES: &'static [AttributeDescriptor] = &[];
}
/// A descriptor that describes all the necessary components of a variant, from its
/// names to its fields, at compile-time.
///
/// `DECLARATION_INDEX` is the index of the variant in declaration (source code) order.
pub unsafe trait VariantDescriptor<const DECLARATION_INDEX: usize>: FieldsAdt {
/// The type which owns this variant.
type Owner: 'static + EnumDescriptor;
/// The integer type that is used for this declaration if it was declared with the representation
/// attribute, `#[repr(Int)]`. Used in conjunction with the `INTEGER_VALUE` associated
/// `const` item.
type Int: 'static = NoType;
/// The 0-based index of the variant in declaration (source code) order.
const DECLARATION_INDEX: usize = DECLARATION_INDEX;
/// The name of the variant within the enumeration.
const NAME: &'static str;
/// The discriminant that identifies this variant of the data type. The discriminant can
/// be used when looping over all fields to find which variant of an enumeration is the
/// currently active variant. Then, the `Fields` or `INTEGER_VALUE` -- if present --
/// can be used to deduce the fields at the specific offset from an object of the enumeration
/// type, or can be used to get the constant integer value of this variant in the enumeration,
/// respectively.
///
/// NOTE(
/// TODO(thephd)) Enable supporting the intrinsic:
/// ```
/// const DISCRIMINANT : &'static Discriminant<Self::Owner> =
/// &std::mem::discriminant_at<Self::Owner>(Self::DECLARATION_INDEX);
/// ```
/// to get a discriminant at compile-time without needing to generate a fake
/// enumeration object.
const DISCRIMINANT: &'static Discriminant<Self::Owner>;
/// The size of this variant within the enumeration type.
const BYTE_SIZE: usize = 0;
/// The value of an enumeration which opts into a `#[repr(Int)]` representation.
/// If the enumeration has not opted into such a representation, then this will be
/// `None`. Otherwise, `Self::Int` will be set to the integer type specified in the
/// representation attribute and the value of the enumeration will be stored here.
const INTEGER_VALUE: Option<&'static Self::Int> = None;
/// The introwospection attributes (`#[introwospection(...)`]) attached to this entity.
/// These are attributes capable of being used for compile-time introspection, such as for
/// marking a field as non-serializable or noting specific intended behaviors for a function
/// definition and the processing of its arguments.
///
/// NOTE
/// Only `introwospection` attributes are collected here. Other attributes are not,
/// as it is important for the author of a specific data type, function, or field to have
/// penultimate control of such attributes. (Individuals writing `*Visitor` types may alter
/// or ignore behavior for attributes, which gives final control to the individual writing
/// such visitor methods.)
const ATTRIBUTES: &'static [AttributeDescriptor] = &[];
}
/// A visitor on a `StructDescriptor` trait implementation, to handle the compile-time
/// data stored on such a trait implementation.
#[const_trait]
pub trait TupleDescriptorVisitor {
/// The return type of the `visit_tuple` and `visit_tuple_mut` implementations.
type Output;
/// A visitation function for a specific `TupleDescriptor` type. This form
/// is immutable, and so cannot modify its `self` argument.
///
/// Returns `Self::Output`
fn visit_tuple<Type: 'static>(&self) -> Self::Output
where
Type: TupleDescriptor;
/// A visitation function for a specific `TupleDescriptor` type. This form
/// is mutable, and by default calls `Self::visit_tuple::<Type>(&self)`.
///
/// Returns `Self::Output`
fn visit_tuple_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: TupleDescriptor,
{
return Self::visit_tuple::<Type>(&self);
}
}
/// A visitor on a `SliceDescriptor` trait implementation, to handle the compile-time
/// data stored on such a trait implementation.
#[const_trait]
pub trait SliceDescriptorVisitor {
/// The return type of the `visit_slice` and `visit_slice_mut` implementations.
type Output;
/// A visitation function for a specific `SliceDescriptor` type. This form
/// is immutable, and so cannot modify its `self` argument.
///
/// Returns `Self::Output`
fn visit_slice<Type: 'static>(&self) -> Self::Output
where
Type: SliceDescriptor;
/// A visitation function for a specific `SliceDescriptor` type. This form
/// is mutable, and by default calls `Self::visit_slice::<Type>(&self)`.
///
/// Returns `Self::Output`
fn visit_slice_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: SliceDescriptor,
{
return Self::visit_slice::<Type>(&self);
}
}
/// A visitor on a `ArrayDescriptor` trait implementation, to handle the compile-time
/// data stored on such a trait implementation.
#[const_trait]
pub trait ArrayDescriptorVisitor {
/// The return type of the `visit_array` and `visit_array_mut` implementations.
type Output;
/// A visitation function for a specific `ArrayDescriptor` type. This form
/// is immutable, and so cannot modify its `self` argument.
///
/// Returns `Self::Output`
fn visit_array<Type: 'static>(&self) -> Self::Output
where
Type: ArrayDescriptor;
/// A visitation function for a specific `ArrayDescriptor` type. This form
/// is mutable, and by default calls `Self::visit_array::<Type>(&self)`.
///
/// Returns `Self::Output`
fn visit_array_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: ArrayDescriptor,
{
return Self::visit_array::<Type>(&self);
}
}
/// A visitor on a `ArrayDescriptor` trait implementation, to handle the compile-time
/// data stored on such a trait implementation.
#[const_trait]
pub trait StructDescriptorVisitor {
/// The return type of the `visit_struct` and `visit_struct_mut` implementations.
type Output;
/// A visitation function for a specific `StructDescriptor` type. This form
/// is immutable, and so cannot modify its `self` argument.
///
/// Returns `Self::Output`
fn visit_struct<Type: 'static>(&self) -> Self::Output
where
Type: StructDescriptor;
/// A visitation function for a specific `StructDescriptor` type. This form
/// is mutable, and by default calls `Self::visit_struct::<Type>(&self)`.
///
/// Returns `Self::Output`
fn visit_struct_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: StructDescriptor,
{
return Self::visit_struct::<Type>(&self);
}
}
/// A visitor on a `UnionDescriptor` trait implementation, to handle the compile-time
/// data stored on such a trait implementation.
#[const_trait]
pub trait UnionDescriptorVisitor {
/// The return type of the `visit_union` and `visit_union_mut` implementations.
type Output;
/// A visitation function for a specific `UnionDescriptor` type. This form
/// is immutable, and so cannot modify its `self` argument.
///
/// Returns `Self::Output`
fn visit_union<Type: 'static>(&self) -> Self::Output
where
Type: UnionDescriptor;
/// A visitation function for a specific `UnionDescriptor` type. This form
/// is mutable, and by default calls `Self::visit_union::<Type>(&self)`.
///
/// Returns `Self::Output`
fn visit_union_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: UnionDescriptor,
{
return Self::visit_union::<Type>(&self);
}
}
/// A visitor on a `EnumDescriptor` trait implementation, to handle the compile-time
/// data stored on such a trait implementation.
#[const_trait]
pub trait EnumDescriptorVisitor {
/// The return type of the `visit_enum` and `visit_enum_mut` implementations.
type Output;
/// A visitation function for a specific `EnumDescriptor` type. This form
/// is immutable, and so cannot modify its `self` argument.
///
/// Returns `Self::Output`
fn visit_enum<Type: 'static>(&self) -> Self::Output
where
Type: EnumDescriptor;
/// A visitation function for a specific `EnumDescriptor` type. This form
/// is mutable, and by default calls `Self::visit_enum::<Type>(&self)`.
///
/// Returns `Self::Output`
fn visit_enum_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: EnumDescriptor,
{
return Self::visit_enum::<Type>(&self);
}
}
/// A visitor on a `FunctionDescriptor` trait implementation, to handle the compile-time
/// data stored on such a trait implementation.
#[const_trait]
pub trait FunctionDescriptorVisitor {
/// The return type of the `visit_function` and `visit_function_mut` implementations.
type Output;
/// A visitation function for a specific `FunctionDescriptor` type. This form
/// is immutable, and so cannot modify its `self` argument.
///
/// Returns `Self::Output`
fn visit_function<Type: 'static>(&self) -> Self::Output
where
Type: FunctionDescriptor;
/// A visitation function for a specific `FunctionDescriptor` type. This form
/// is mutable, and by default calls `Self::visit_function::<Type>(&self)`.
///
/// Returns `Self::Output`
fn visit_function_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: FunctionDescriptor,
{
return Self::visit_function::<Type>(&self);
}
}
/// A visitor for a collection of parameter descriptors that handles all incoming compile-time
/// data stored on an implementation of a typical `ParameterDescriptor<I>`, where `I` is from `0`
/// to the maximum parameter count of a given type. This is used for function types.
#[const_trait]
pub trait ParameterDescriptorVisitor {
/// The return type of the `visit_parameter` and `visit_parameter_mut` implementations.
type Output;
/// A visitation function for a specific `ParameterDescriptor` type.
///
/// Returns `Self::Output`.
fn visit_parameter<Type: 'static, const DECLARATION_INDEX: usize>(&self) -> Self::Output
where
Type: ParameterDescriptor<DECLARATION_INDEX>;
/// A visitation function for a specific `ParameterDescriptor` type. This form is mutable,
/// and by default calls `Self::visit_parameter::<Type, DECLARATION_INDEX>`.
///
/// Returns `Self::Output`
fn visit_parameter_mut<Type: 'static, const DECLARATION_INDEX: usize>(&mut self) -> Self::Output
where
Type: ParameterDescriptor<DECLARATION_INDEX>,
{
return Self::visit_parameter::<Type, DECLARATION_INDEX>(&self);
}
}
/// A visitor for a collection of parameter descriptors that handles all incoming compile-time
/// data stored on an implementation of a typical `ParameterDescriptor<I>`, where `I` is from `0`
/// to the maximum parameter count of a given type.
///
/// In this version, the `DECLARATION_INDEX` is provided on the visitor itself, which allows for
/// each different parameter handled by this visitor to have a different `type Output` on its
/// implementation.
#[const_trait]
pub trait ParameterDescriptorVisitorAt<const DECLARATION_INDEX: usize> {
/// The return type of the `visit_parameter` and `visit_parameter_mut` implementations.
type Output;
/// A visitation function for a specific `ParameterDescriptor` type.
///
/// Returns `Self::Output`.
fn visit_parameter<Type: 'static>(&self) -> Self::Output
where
Type: ParameterDescriptor<DECLARATION_INDEX>;
/// A visitation function for a specific `ParameterDescriptor` type. This form is mutable,
/// and by default calls `Self::visit_parameter::<Type, DECLARATION_INDEX>`.
///
/// Returns `Self::Output`
fn visit_parameter_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: ParameterDescriptor<DECLARATION_INDEX>,
{
return Self::visit_parameter::<Type>(&self);
}
}
/// A visitor for a collection of field descriptors that handles all incoming compile-time
/// data stored on an implementation of a typical `FieldDescriptor<I>`, where `I` is from `0`
/// to the maximum field count of a given type. This is used for `struct` types, `union` types,
/// `enum` variants, and tuple types.
#[const_trait]
pub trait FieldDescriptorVisitor {
/// The return type of the `visit_field` and `visit_field_mut` implementations.
type Output;
/// A visitation function for a specific `FieldDescriptor<DECLARATION_INDEX>`
/// implementation.
///
/// Returns `Self::Output`.
fn visit_field<Type: 'static, const DECLARATION_INDEX: usize>(&self) -> Self::Output
where
Type: FieldDescriptor<DECLARATION_INDEX>;
/// A visitation function for a specific `FieldDescriptor<DECLARATION_INDEX>` type. This
/// form is mutable, and by default calls `Self::visit_field::<Type, DECLARATION_INDEX>`.
///
/// Returns `Self::Output`.
fn visit_field_mut<Type: 'static, const DECLARATION_INDEX: usize>(&mut self) -> Self::Output
where
Type: FieldDescriptor<DECLARATION_INDEX>,
{
return Self::visit_field::<Type, DECLARATION_INDEX>(&self);
}
}
/// A visitor for a collection of field descriptors that handles all incoming compile-time
/// data stored on an implementation of a typical `FieldDescriptor<I>`, where `I` is from `0`
/// to the maximum field count of a given type. This is used for `struct` and `union` types.
///
/// Providing the `DECLARATION_INDEX` as part of the visitor allows for each different parameter
/// handled by this visitor to have a different `type Output` on its implementation. This version
/// of the trait is preferred over a `FieldDescriptorVisitor` if it exists for this particular
/// `DECLARATION_INDEX` on the visitor.
#[const_trait]
pub trait FieldDescriptorVisitorAt<const DECLARATION_INDEX: usize> {
/// The return type of the `visit_field` and `visit_field_mut` implementations.
type Output;
/// A visitation function for a specific `FieldDescriptor<DECLARATION_INDEX>`
/// implementation.
///
/// Returns `Self::Output`.
fn visit_field<Type: 'static>(&self) -> Self::Output
where
Type: FieldDescriptor<DECLARATION_INDEX>;
/// A visitation function for a specific `FieldDescriptor<DECLARATION_INDEX>` type. This
/// form is mutable, and by default calls `Self::visit_field::<Type>`.
///
/// Returns `Self::Output`.
fn visit_field_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: FieldDescriptor<DECLARATION_INDEX>,
{
return Self::visit_field::<Type>(&self);
}
}
/// A visitor for a collection of variant descriptors that handles all incoming compile-time
/// data stored on an implementation of a typical `VariantDescriptor<I>`, where `I` is from `0`
/// to the maximum variant count of a given type. This is used for enumeration types.
#[const_trait]
pub trait VariantDescriptorVisitor {
/// The return type of the `visit_function` and `visit_function_mut` implementations.
type Output;
/// A visitation function for a specific `VariantDescriptor<DECLARATION_INDEX>`
/// implementation.
///
/// Returns `Self::Output`.
fn visit_variant<Type: 'static, const DECLARATION_INDEX: usize>(&self) -> Self::Output
where
Type: VariantDescriptor<DECLARATION_INDEX>;
/// A visitation function for a specific `VariantDescriptor<DECLARATION_INDEX>` type.
/// This form is mutable, and by default calls
/// `Self::visit_variant::<Type, DECLARATION_INDEX>`.
///
/// Returns `Self::Output`.
fn visit_variant_mut<Type: 'static, const DECLARATION_INDEX: usize>(&mut self) -> Self::Output
where
Type: VariantDescriptor<DECLARATION_INDEX>,
{
return Self::visit_variant::<Type, DECLARATION_INDEX>(&self);
}
}
/// A visitor for a collection of variant descriptors that handles all incoming compile-time
/// data stored on an implementation of a typical `VariantDescriptor<I>`, where `I` is from `0`
/// to the maximum variant count of a given type. This is used for enumeration types, and is
/// preferred over the `VariantDescriptorVisitor` if one exists for this for particular
/// `DECLARATION_INDEX`.
#[const_trait]
pub trait VariantDescriptorVisitorAt<const DECLARATION_INDEX: usize> {
/// The return type of the `visit_function` and `visit_function_mut` implementations.
type Output;
/// A visitation function for a specific `VariantDescriptor<DECLARATION_INDEX>`
/// implementation.
///
/// Returns `Self::Output`.
fn visit_variant<Type: 'static>(&self) -> Self::Output
where
Type: VariantDescriptor<DECLARATION_INDEX>;
/// A visitation function for a specific `VariantDescriptor<DECLARATION_INDEX>` type.
/// This form is mutable, and by default calls
/// `Self::visit_variant::<Type, DECLARATION_INDEX>`.
///
/// Returns `Self::Output`.
fn visit_variant_mut<Type: 'static>(&mut self) -> Self::Output
where
Type: VariantDescriptor<DECLARATION_INDEX>,
{
return Self::visit_variant::<Type>(&self);
}
}
/// A run-time description of a `struct` type.
#[derive(Debug)]
#[non_exhaustive]
pub struct AnyStructDescriptor {
/// The type of this `struct`.
pub adt_id: AdtId,
/// The type of the `struct` that was described.
pub type_id: TypeId,
/// The name of the `struct`.
pub name: &'static str,
/// Whether or not this `struct` is a tuple `struct` (it has the form
/// `struct SomeStruct(T0, ..., TN)`).
pub field_syntax: FieldSyntax,
/// A slice describing each field of this `struct` type.
pub fields: &'static [AnyFieldDescriptor],
/// The introwospection attributes (`#[introwospection(...)`]) attached to this entity.
/// These are attributes capable of being used for compile-time introspection, such as for
/// marking a field as non-serializable or noting specific intended behaviors for a function
/// definition and the processing of its arguments.
///
/// NOTE: only `introwospection` attributes are collected here. Other attributes are not,
/// as it is important for the author of a specific data type, function, or field to have
/// penultimate control of such attributes. (Individuals writing `*Visitor` types may alter
/// or ignore behavior for attributes, which gives final control to the individual writing
/// such visitor methods.)
pub attributes: &'static [AttributeDescriptor],
}
/// A run-time description of a `union` type.
#[derive(Debug)]
#[non_exhaustive]
pub struct AnyUnionDescriptor {
/// The type of this `union`.
pub adt_id: AdtId,
/// The type of the `union` that was described.
pub type_id: TypeId,
/// The name of the `union`.
pub name: &'static str,
/// What kind of syntax was used to encapsulate the fields for this `union`'s fields.