Skip to main content

scattered_collect/
lib.rs

1#![doc = include_str!("../README.md")]
2
3pub mod hash;
4pub mod iterable;
5pub mod map;
6pub mod referenced_slice;
7pub mod slice;
8pub mod sorted_referenced_slice;
9pub mod sorted_slice;
10
11pub use iterable::ScatteredIterable;
12pub use map::ScatteredMap;
13pub use referenced_slice::ScatteredReferencedSlice;
14pub use slice::ScatteredSlice;
15pub use sorted_referenced_slice::ScatteredSortedReferencedSlice;
16pub use sorted_slice::ScatteredSortedSlice;
17
18#[doc(hidden)]
19#[macro_export]
20macro_rules! __scatter_parse {
21    // Send the #[scatter]'d item into the collection's private macro.
22    (#[scatter ($($meta:tt)*)] $(#[$imeta:meta])* $($item:tt)*) => {
23        $($meta)* ! (
24            ([$($meta)*] =>
25            $(#[$imeta])*
26            $($item)*
27            )
28        );
29    };
30    (#[scatter] $($rest:tt)* ) => {
31        compile_error!("Unknown collection type");
32    };
33
34    (@reorder (#[scatter] $($item:tt)*) ($($rest:tt)*)) => {
35        $crate::__support::scatter_parse!(#[scatter] $($rest)* $($item)*);
36    };
37    (@reorder (#[$top:meta] $($item:tt)*) ($($rest:tt)*)) => {
38        $crate::__support::scatter_parse!(@reorder($($item)*) (#[$top] $($rest)*));
39    };
40    (@reorder ($item:item;) $($rest:tt)*) => {
41        compile_error!("Missing #[scatter] attribute.");
42    };
43    (@reorder $($rest:tt)*) => {
44        compile_error!("Missing #[scatter] attribute.");
45    };
46
47    ($($rest:tt)*) => {
48        $crate::__support::scatter_parse!(@reorder ($($rest)*) ());
49    };
50}
51
52#[doc(hidden)]
53#[macro_export]
54macro_rules! __gather_parse {
55    // Send the #[gather]'d item into the collection's private macro.
56    (@done ($collection:ident) #[gather] $(#[$imeta:meta])* $vis:vis static $name:ident: ScatteredSlice < $ty:ty >; ) => {
57        $crate::__slice ! (
58            @gather
59            $(#[$imeta])*
60            $vis static $name: $collection < $ty >;
61        );
62    };
63
64    (@done ($collection:ident) #[gather] $(#[$imeta:meta])* $vis:vis static $name:ident: ScatteredSortedSlice < $ty:ty >; ) => {
65        $crate::__sorted_slice ! (
66            @gather
67            $(#[$imeta])*
68            $vis static $name: $collection < $ty >;
69        );
70    };
71
72    (@done ($collection:ident) #[gather] $(#[$imeta:meta])* $vis:vis static $name:ident: ScatteredReferencedSlice < $ty:ty >; ) => {
73        $crate::__referenced_slice ! (
74            @gather
75            $(#[$imeta])*
76            $vis static $name: $collection < $ty >;
77        );
78    };
79
80    (@done ($collection:ident) #[gather] $(#[$imeta:meta])* $vis:vis static $name:ident: ScatteredSortedReferencedSlice < $ty:ty >; ) => {
81        $crate::__sorted_referenced_slice ! (
82            @gather
83            $(#[$imeta])*
84            $vis static $name: $collection < $ty >;
85        );
86    };
87
88    (@done ($map:ident) #[gather] $(#[$imeta:meta])* $vis:vis static $name:ident: ScatteredMap < $key:ty, $value:ty >; ) => {
89        $crate::__map ! (
90            @gather
91            $(#[$imeta])*
92            $vis static $name: $map < $key, $value >;
93        );
94    };
95
96    (@done ($collection:ident) #[gather] $(#[$imeta:meta])* $vis:vis static $name:ident: ScatteredIterable < $ty:ty >; ) => {
97        $crate::__iterable ! (
98            @gather
99            $(#[$imeta])*
100            $vis static $name: $collection < $ty >;
101        );
102    };
103
104    (@done #[gather] $($rest:tt)* ) => {
105        compile_error!("Unknown collection type");
106    };
107
108    (@reorder (#[gather] $($item:tt)*) ($($rest:tt)*) $collection:tt) => {
109        $crate::__support::gather_parse!(@done $collection #[gather] $($rest)* $($item)*);
110    };
111    (@reorder (#[$top:meta] $($item:tt)*) ($($rest:tt)*) $collection:tt) => {
112        $crate::__support::gather_parse!(@reorder ($($item)*) (#[$top] $($rest)*) $collection);
113    };
114    (@reorder ($item:item;) $($rest:tt)*) => {
115        compile_error!("Missing #[gather] attribute.");
116    };
117    (@reorder $($rest:tt)*) => {
118        compile_error!("Missing #[gather] attribute.");
119    };
120
121    ($(#$meta:tt)* $vis:vis static $name:ident: $collection:ident < $($rest:tt)* ) => {
122        $crate::__support::gather_parse!(@reorder ($(#$meta)* $vis static $name: $collection < $($rest)*) () ($collection));
123    };
124}
125
126#[doc(hidden)]
127#[allow(unused)]
128pub mod __support {
129    pub use crate::__gather_parse as gather_parse;
130    pub use crate::__scatter_parse as scatter_parse;
131
132    pub use ctor;
133    pub use link_section;
134    pub use scattered_collect_proc_macro::ident_concat;
135}
136
137/// Declarative `scatter!` / `gather!` entry points.
138pub mod declarative {
139    pub use crate::__gather_brace as gather;
140    pub use crate::__scatter_brace as scatter;
141}
142
143#[doc(inline)]
144pub use scattered_collect_proc_macro::{gather, scatter};
145
146#[doc(hidden)]
147#[macro_export]
148macro_rules! __gather_brace {
149    ($($item:tt)*) => {
150        $crate::__support::gather_parse!(#[gather] $($item)*);
151    };
152}
153
154#[doc(hidden)]
155#[macro_export]
156macro_rules! __scatter_brace {
157    ($($item:tt)*) => {
158        $crate::__support::scatter_parse!($($item)*);
159    };
160}