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
//! Strongly-typed, zero-cost indexes wrapping integers.
//!
//! This crate is just one macro: [`new`]. It creates a wrapper around `usize` to make
//! type-safe indexes. That is, the indexes for your clients that you use to retrieve information
//! efficiently from the vector of client information do not have the same type as the indexes for
//! the files you have about your clients. [The example below](#example) illustrates this crate in
//! that context.
//!
//! The index type created implements
//!
//! - `Deref` and `From` for `usize`,
//! - `Debug`, `Default`, `Clone`, `Copy`, `PartialOrd`, `Ord`, `PartialEq`, `Eq`, `Hash` and `Display`.
//!
//! If you are experiencing problems upgrading from a version `< 0.9.17`, make sure you read the
//! [changelog][changelog 0.9.17].
//!
//! # Usage
//!
//! The most basic use of `new` is just to wrap something:
//!
//! ```
//! safe_index::new!{
//!     /// Arity.
//!     Arity
//! }
//! assert_eq! { core::mem::size_of::<Arity>(), core::mem::size_of::<usize>() }
//! ```
//!
//! This is not very useful however, there's nothing for our index to index. Thankfully `new`
//! can provide more types. After the mandatory identifier `Idx` for the type of indexes, you can
//! add these:
//!
//! - `map <Map>`: creates a wrapper named `<Map>` around a vector, indexed by `Idx`.
//! - `btree set <Set>`: alias type for a binary tree set of `Idx`s.
//! - `btree map <Map>`: alias type for a binary tree map from `Idx` to something.
//!
//!
//! See the [`examples` module] and the example below for illustrations of the `new` macro.
//!
//! # Example
//!
//! All the code for this example is in [`examples::clients`]. Say we have a `Data` structure that
//! stores some clients in a vector. It also stores files about these clients. A client can be
//! associated to several files, and a file can be about several clients. Let's handle everything
//! by indexes:
//!
//! ```rust
//! # use std::collections::BTreeSet;
//! /// Client information.
//! pub struct ClientInfo {
//!     /// Name of the client.
//!     pub name: String,
//!     /// Indices of files associated with the client.
//!     pub files: BTreeSet<usize>,
//! }
//! /// File information.
//! pub struct FileInfo {
//!     /// Name of the file.
//!     pub name: String,
//!     /// Indices of clients concerned by the file.
//!     pub clients: BTreeSet<usize>,
//! }
//!
//! /// Aggregates clients and files info.
//! pub struct Data {
//!     /// Map from client indexes to client information.
//!     pub clients: Vec<ClientInfo>,
//!     /// Map from file indexes to file information.
//!     pub files: Vec<FileInfo>,
//! }
//! ```
//!
//! Now, implementing `Data`'s functionalities is going to be painful. Client and file indexes are
//! both `usize`, terrible things are bound to happen.
//!
//! So let's instead create an index type for each.
//!
//! ```rust
//! /// Indices.
//! pub mod idx {
//!     safe_index::new! {
//!         /// Indices of clients.
//!         Client,
//!         /// Map from clients to something (really a vector).
//!         map: Clients,
//!         /// Set of clients.
//!         btree set: ClientSet,
//!     }
//!
//!     safe_index::new! {
//!         /// Indices of files.
//!         File,
//!         /// Map from files to something (really a vector).
//!         map: Files,
//!         /// Set of files.
//!         btree set: FileSet,
//!     }
//! }
//!
//! use idx::*;
//!
//! # use std::collections::BTreeSet;
//! /// Client information.
//! pub struct ClientInfo {
//!     /// Name of the client.
//!     pub name: String,
//!     /// Indices of files associated with the client.
//!     pub files: ClientSet,
//! }
//! /// File information.
//! pub struct FileInfo {
//!     /// Name of the file.
//!     pub name: String,
//!     /// Indices of clients concerned by the file.
//!     pub clients: FileSet,
//! }
//!
//! /// Aggregates clients and files info.
//! pub struct Data {
//!     /// Map from client indexes to client information.
//!     pub clients: Clients<ClientInfo>,
//!     /// Map from file indexes to file information.
//!     pub files: Files<FileInfo>,
//! }
//! ```
//!
//! The full code is available [here][clients src], and you can see it used in the documentation of
//! [`examples::clients`]. Here are a few functions on `Data` to (hopefully) show that `Client` and
//! `File` behave as (and in fact are) `usize` indexes.
//!
//! ```rust
//! # use safe_index::examples::clients::{idx::*, ClientInfo, FileInfo};
//! /// Aggregates clients and files info.
//! pub struct Data {
//!     /// Map from client indexes to client information.
//!     pub clients: Clients<ClientInfo>,
//!     /// Map from file indexes to file information.
//!     pub files: Files<FileInfo>,
//! }
//! impl Data {
//!     /// Adds a file, updates the clients concerned.
//!     pub fn add_file(&mut self, file: FileInfo) -> File {
//!         let idx = self.files.push(file);
//!         let file = &self.files[idx];
//!         for client in &file.clients {
//!             let is_new = self.clients[*client].files.insert(idx);
//!             debug_assert! { is_new }
//!         }
//!         idx
//!     }
//!
//!     /// Adds a client to a file.
//!     pub fn add_client_to_file(&mut self, client: Client, file: File) {
//!         let is_new = self.files[file].clients.insert(client);
//!         debug_assert! { is_new }
//!         let is_new = self.clients[client].files.insert(file);
//!         debug_assert! { is_new }
//!     }
//! }
//! ```
//!
//! [`new`]: ../../macro.new.html (new macro)
//! [`examples` module]: examples/index.html (safe_index examples)
//! [`examples::clients`]: examples/clients/index.html (clients example)
//! [clients src]: examples/clients.rs.html (Code of the clients example)
//! [changelog 0.9.17]: https://github.com/AdrienChampion/safe_index/blob/master/changelog.md#v0917

#![no_std]

pub extern crate alloc;

mod map;

/// Discards its input if the `strict` feature is active.
#[macro_export]
#[doc(hidden)]
#[cfg(feature = "strict")]
macro_rules! non_strict {
    ( $($stuff:tt)* ) => {};
}
/// Discards its input if the `strict` feature is active.
#[macro_export]
#[doc(hidden)]
#[cfg(not(feature = "strict"))]
macro_rules! non_strict {
    (
        #[doc = $doc:literal]
        $($tail:tt)*
    ) => {
        #[doc = concat!("[`non_strict`] ", $doc)]
        $($tail)*
    };
    (
        $($item:item)*
    ) => {
        $(
            #[doc = "[`non_strict`] "]
            $item
        )*
    };
}

/// Generates an alias type for [`alloc::collections::BTreeSet`] of indices.
#[macro_export]
#[doc(hidden)]
macro_rules! btree_set_codegen {
    { $t:ident,
        $(#[$meta:meta])*
        $set:ident $($tail:tt)*
    } => {
        $(#[$meta])*
        pub type $set = $crate::alloc::collections::BTreeSet<$t> ;
        $crate::handle!{ $t $($tail)* }
    };
}

/// Generates an alias type for [`alloc::collections::BTreeMap`] of indices.
#[macro_export]
#[doc(hidden)]
macro_rules! btree_map_codegen {
    { $t:ident,
        $(#[$meta:meta])*
        $map:ident $($tail:tt)*
    } => {
        $(#[$meta])*
        pub type $map<T> = $crate::alloc::collections::BTreeMap<$t, T> ;
        $crate::handle!{ $t $($tail)* }
    };
}

/// Handles some user input and decides what to do.
#[macro_export]
#[doc(hidden)]
macro_rules! handle {
    { $t:ident, $(#[$meta:meta])* btree set: $($tail:tt)* } => {
        $crate::btree_set_codegen! { $t, $(#[$meta])* $($tail)* }
    };
    { $t:ident, $(#[$meta:meta])* btree map: $($tail:tt)* } => {
        $crate::btree_map_codegen! { $t, $(#[$meta])* $($tail)* }
    };
    { $t:ident, $(#[$meta:meta])* map: $($tail:tt)* } => {
        $crate::map_codegen! { $t, $(#[$meta])* $($tail)* }
    };
    { $t:ident $(,)? } => {};

    { $t:ident with iter: $iter:ident $($tail:tt)* } => {
        compile_error!(concat!(
            "maps do not have dedicated iterator structures anymore, remove `with iter: ",
            stringify!($iter),
            "` from your input",
        ));
    };
    { $t:ident, range: $range:ident $($tail:tt)* } => {
        compile_error!(concat!(
            "`range` does not exist anymore, use `..` and `..=` operators instead and remove `range: ",
            stringify!($range),
            "` from your input",
        ));
    };
    { $t:ident, $token:tt $($tail:tt)* } => {
        compile_error!(concat!(
            "expected `btree set`, `btree map` or `map` but found unexpected token `",
            stringify!($token),
            "`",
        ));
    };
    { $t:ident $token:tt $($tail:tt)* } => {
        compile_error!(concat!(
            "expected comma, found unexpected token `",
            stringify!($token),
            "`",
        ));
    };
}

/// Wraps a `usize` into a struct (zero-cost). Also generates the relevant collections indexed by
/// the wrapper.
///
/// See the [module-level documentation](index.html) for more.
#[macro_export]
macro_rules! new {
    (
        $(#[$meta:meta])*
        $t:ident
        $($tail:tt)*
    ) => (
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
        pub struct $t {
            val: usize
        }

        impl $t {
            $crate::non_strict! {
                /// Wraps a [`usize`].
                #[inline]
                pub const fn new(val: usize) -> Self {
                    $t { val: val }
                }
            }
            $crate::non_strict! {
                /// Zero.
                #[inline]
                pub const fn zero() -> Self {
                    $t { val: 0 }
                }
            }
            $crate::non_strict! {
                /// One.
                #[inline]
                pub const fn one() -> Self {
                    $t { val: 1 }
                }
            }
            $crate::non_strict! {
                /// Increments the int.
                #[inline]
                pub fn inc(&mut self) {
                    self.val += 1
                }
            }
            $crate::non_strict! {
                /// Decrements the int.
                #[inline]
                pub fn dec(&mut self) {
                    self.val -= 1
                }
            }
            /// Underlying index accessor.
            #[inline]
            pub const fn get(& self) -> usize {
                self.val
            }
        }
        impl core::convert::Into<usize> for $t {
            #[inline]
            fn into(self) -> usize {
                self.val
            }
        }
        impl<'a> core::convert::Into<usize> for &'a $t {
            #[inline]
            fn into(self) -> usize {
                self.val
            }
        }
        impl core::ops::Deref for $t {
            type Target = usize ;
            #[inline]
            fn deref(& self) -> & usize {
                & self.val
            }
        }
        impl core::fmt::Display for $t {
            #[inline]
            fn fmt(& self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
                write!(fmt, "{}", self.val)
            }
        }
        impl core::cmp::PartialEq<usize> for $t {
            #[inline]
            fn eq(& self, int: & usize) -> bool {
                self.val.eq(int)
            }
        }
        impl core::cmp::PartialOrd<usize> for $t {
            #[inline]
            fn partial_cmp(& self, int: & usize) -> Option<
                core::cmp::Ordering
            > {
                self.val.partial_cmp(int)
            }
        }
        $crate::non_strict! {
            impl<T: core::convert::Into<usize>> core::ops::Add<T> for $t {
                type Output = $t ;
                #[inline]
                fn add(mut self, rhs: T) -> $t {
                    self.val += rhs.into() ;
                    self
                }
            }
            impl core::convert::From<usize> for $t {
                #[inline]
                fn from(val: usize) -> Self {
                    $t::new(val)
                }
            }
            impl<'a> core::convert::From<&'a usize> for $t {
                #[inline]
                fn from(val: &'a usize) -> Self {
                    $t::new(* val)
                }
            }
            impl<T: core::convert::Into<usize>> core::ops::AddAssign<T> for $t {
                #[inline]
                fn add_assign(&mut self, rhs: T) {
                    self.val += rhs.into()
                }
            }
            impl Default for $t {
                #[inline]
                fn default() -> Self {
                    Self::zero()
                }
            }
        }
        $crate::handle!{ $t $($tail)* }
    ) ;
}

pub mod examples;