try-specialize 0.1.2

Zero-cost specialization in generic context on stable Rust
Documentation
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
#![allow(dead_code)]

#[cfg(feature = "alloc")]
use std::borrow::Cow;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(feature = "alloc")]
use std::collections::{BTreeMap, BTreeSet};
#[cfg(feature = "std")]
use std::path::Path;
#[cfg(feature = "std")]
use std::sync::Arc;

/// Allows to use the results of one macro as input for another macro in easy
/// way. Requires support for special `pipe`-like syntax from any macro in the
/// chain except the last one.
///
/// An example of the recursive macro expansion steps:
/// - `chain! { generator! | mapper! | filter! | flattener! | sink! }`,
/// - `generator! { chain! { | mapper! | filter! | flattener! | sink! }}`,
/// - `chain! { GENERATED_DATA | mapper! | filter! | flattener! | sink! }`,
/// - `mapper! { GENERATED_DATA | chain! { | filter! | flattener! | sink! }}`,
/// - `chain! { MAPPED_DATA | filter! | flattener! | sink! }`,
/// - `filter! { MAPPED_DATA | chain! { | flattener! | sink! }}`,
/// - `chain! { FILTERED_DATA | flattener! | sink! }`,
/// - `flattener! { FILTERED_DATA | chain! { | sink! }}`,
/// - `chain! { FLATTENED_DATA | sink! }`,
/// - `sink! FLATTENED_DATA`.
#[macro_export]
macro_rules! chain {
    { $in:tt { $( $tail:tt )* } } => {
        chain! { $in $( $tail )* }
    };
    { $in:tt | $func:ident! | $( $tail:tt )* } => {
        $func! { $in | chain! { | $( $tail )* } }
    };
    { $in:tt | $func:ident! } => {
        $func! $in
    };
    { $func:ident! | $( $tail:tt )* } => {
        $func! { chain! { | $( $tail )* } }
    };
}

/// Calls the callback macro for each item in a list.
#[macro_export]
macro_rules! for_each {
    { [ $( $tt:tt )* ] | $func:ident! $args:tt } => {
        $( $func! { $tt $args } )*
    };
}

/// Calls the callback macro for each pair of list cartesian square set.
#[macro_export]
macro_rules! for_each_cartesian_square_pair {
    { [ $( $tt:tt )* ] | $func:ident! $args:tt } => {
        for_each_cartesian_product_pair! {
            ( [ $( $tt )* ] x [ $( $tt )* ] ) | $func! $args
        }
    };
}

/// Calls the callback macro for each pair of two lists cartesian product set.
#[macro_export]
macro_rules! for_each_cartesian_product_pair {
    { ( [] x [ $( $right:tt )* ] ) | $func:ident! $args:tt } => {};
    {
        ( [ $left_next:tt $( $left_rest:tt )* ] x [ $( $right_all:tt )* ] )
        | $func:ident! $args:tt
    } => {
        $( $func! { { $left_next $right_all } $args } )*
        for_each_cartesian_product_pair! {
            ( [ $( $left_rest )* ] x [ $( $right_all )* ] ) | $func! $args
        }
    };
}

/// Computes if-then-else-like expressions.
///
/// Keep in mind that in Rust the order of macro expansion is undefined,
/// which forces to use recursion more often in the auxiliary macros
/// implementations.
#[macro_export]
macro_rules! mif {
    { if true { $( $then:tt )* } $( else { $( $else:tt )* } )? } => {
        $( $then )*
    };
    { if false { $( $then:tt )* } $( else { $( $else:tt )* } )? } => {
        $( $( $else )* )?
    };

    { if core $( $tt:tt )* } => { mif! { if true $( $tt )* } };
    { if alloc $( $tt:tt )* } => { mif_alloc! { $( $tt )* } };
    { if std $( $tt:tt )* } => { mif_std! { $( $tt )* } };

    { if ( eq $left:tt $right:tt ) $( $tt:tt )* } => {
        mif_eq! { ($left, $right) $( $tt )* }
    };
    { if ( not $cond:tt ) $then:tt } => {
        mif! { if $cond {} else $then }
    };
    { if ( not $cond:tt ) $then:tt else $else:tt } => {
        mif! { if $cond $else else $then }
    };
    { if ( all ) $( $tt:tt )* } => {
        mif! { if true $( $tt )* }
    };
    { if ( all $first:tt $( $rest:tt )* ) $( $tt:tt )* } => {
        mif! {
            if $first {
                mif! { if ( all $( $rest )* ) $( $tt )* }
            } else {
                mif! { if false $( $tt )* }
            }
        }
    };
}

/// An auxiliary feature-dependent macro for a `mif` macro.
#[macro_export]
#[cfg(feature = "alloc")]
macro_rules! mif_alloc {
    { $( $tt:tt )* } => { mif! { if true $( $tt )* } }
}

/// An auxiliary feature-dependent macro for a `mif` macro.
#[macro_export]
#[cfg(not(feature = "alloc"))]
macro_rules! mif_alloc {
    { $( $tt:tt )* } => { mif! { if false $( $tt )* } }
}

/// An auxiliary feature-dependent macro for a `mif` macro.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! mif_std {
    { $( $tt:tt )* } => { mif! { if true $( $tt )* } }
}

/// An auxiliary feature-dependent macro for a `mif` macro.
#[macro_export]
#[cfg(not(feature = "std"))]
macro_rules! mif_std {
    { $( $tt:tt )* } => { mif! { if false $( $tt )* } }
}

/// An auxiliary equality check macro for a `mif` macro.
#[macro_export]
macro_rules! mif_eq {
    { ( core, core ) $( $tt:tt )* } => { mif! { if true $( $tt )* } };
    { ( $_:tt, core ) $( $tt:tt )* } => { mif! { if false $( $tt )* } };
    { ( ltfree, ltfree ) $( $tt:tt )* } => { mif! { if true $( $tt )* } };
    { ( $_:tt, ltfree ) $( $tt:tt )* } => { mif! { if false $( $tt )* } };
    { ( sized, sized ) $( $tt:tt )* } => { mif! { if true $( $tt )* } };
    { ( $_:tt, sized ) $( $tt:tt )* } => { mif! { if false $( $tt )* } };
}

/// For a given checked type, determines the set of required reference
/// functions, and calls the callback macro for each such function.
#[macro_export]
macro_rules! for_each_reference_fn {
    {
        { $lib:tt $lt:tt $size:tt $name:ident: $ty:ty = $example:expr }
        | $func:ident! $args:tt
    } => {
        mif! { if $lib {
            mif! { if (eq $size sized) {
                $func! { { { $lib $name: $ty } { value, false } } $args }
                $func! { { { $lib $name: $ty } { value, true } } $args }
            }}
            $func! { { { $lib $name: &$ty } { ref, false } } $args }
            $func! { { { $lib $name: &$ty } { ref, true } } $args }
            $func! { { { $lib $name: &mut $ty } { mut, false } } $args }
            $func! { { { $lib $name: &mut $ty } { mut, true } } $args }
        }}
    };
}

/// For a given pair of checked types, determines the set of required checked
/// functions, and calls the callback macro for each such function.
#[macro_export]
macro_rules! for_each_cartesian_pair_tested_fn {
    {
        {
            { $lib1:tt $lt1:tt $size1:tt $name1:ident: $ty1:ty = $example1:expr }
            { $lib2:tt $lt2:tt $size2:tt $name2:ident: $ty2:ty = $example2:expr }
        } | $func:ident! $args:tt
    } => {
        mif! { if (all $lib1 $lib2) {
            mif! { if (eq $lt2 ltfree) {
                mif! { if (all (eq $size1 sized) (eq $size2 sized)) {
                    $func! {
                        {
                            { $lib1 $lt1 $size1 $name1: $ty1 = $example1 }
                            { $lib2 $lt2 $size2 $name2: $ty2 = $example2 }
                            { to_ltfree, arg => arg.try_specialize::<$ty2>() }
                            { Ok(_ok), Err(_err), Ok($example2), Err($example1) }
                        }
                        $args
                    }
                }}
                $func! {
                    {
                        { $lib1 $lt1 $size1 $name1: &$ty1 = &$example1 }
                        { $lib2 $lt2 $size2 $name2: &$ty2 = &$example2 }
                        { to_ltfree_ref, arg => arg.try_specialize_ref::<$ty2>() }
                        { Some(_some), None, Some(&$example2), None }
                    }
                    $args
                }
                $func! {
                    {
                        { $lib1 $lt1 $size1 $name1: &mut $ty1 = &mut $example1 }
                        { $lib2 $lt2 $size2 $name2: &mut $ty2 = &mut $example2 }
                        { to_ltfree_mut, arg => arg.try_specialize_mut::<$ty2>() }
                        { Some(_some), None, Some(&mut $example2), None }
                    }
                    $args
                }
            }}
            mif! { if (eq $lt1 ltfree) {
                mif! { if (all (eq $size1 sized) (eq $size2 sized)) {
                    $func! {
                        {
                            { $lib1 $lt1 $size1 $name1: $ty1 = $example1 }
                            { $lib2 $lt2 $size2 $name2: $ty2 = $example2 }
                            { from_ltfree, arg => <$ty2>::try_specialize_from(arg) }
                            { Ok(_ok), Err(_err), Ok($example2), Err($example1) }
                        }
                        $args
                    }
                }}
                $func! {
                    {
                        { $lib1 $lt1 $size1 $name1: &$ty1 = &$example1 }
                        { $lib2 $lt2 $size2 $name2: &$ty2 = &$example2 }
                        { from_ltfree_ref, arg => <$ty2>::try_specialize_from_ref(arg) }
                        { Some(_some), None, Some(&$example2), None }
                    }
                    $args
                }
                $func! {
                    {
                        { $lib1 $lt1 $size1 $name1: &mut $ty1 = &mut $example1 }
                        { $lib2 $lt2 $size2 $name2: &mut $ty2 = &mut $example2 }
                        { from_ltfree_mut, arg => <$ty2>::try_specialize_from_mut(arg) }
                        { Some(_some), None, Some(&mut $example2), None }
                    }
                    $args
                }
            }}
            mif! { if (all (eq $size1 sized) (eq $size2 sized)) {
                $func! {
                    {
                        { $lib1 $lt1 $size1 $name1: $ty1 = $example1 }
                        { $lib2 $lt2 $size2 $name2: $ty2 = $example2 }
                        { static, arg => arg.try_specialize_static::<$ty2>() }
                        { Ok(_ok), Err(_err), Ok($example2), Err($example1) }
                    }
                    $args
                }
            }}
            $func! {
                {
                    { $lib1 $lt1 $size1 $name1: &$ty1 = &$example1 }
                    { $lib2 $lt2 $size2 $name2: &$ty2 = &$example2 }
                    { static_ref, arg => arg.try_specialize_ref_static::<$ty2>() }
                    { Some(_some), None, Some(&$example2), None }
                }
                $args
            }
            $func! {
                {
                    { $lib1 $lt1 $size1 $name1: &mut $ty1 = &mut $example1 }
                    { $lib2 $lt2 $size2 $name2: &mut $ty2 = &mut $example2 }
                    { static_mut, arg => arg.try_specialize_mut_static::<$ty2>() }
                    { Some(_some), None, Some(&mut $example2), None }
                }
                $args
            }
        }}
    }
}

/// Calls the callback macro will all checked types.
///
/// Adding each additional type non-linearly increases compilation time.
#[macro_export]
macro_rules! for_all_types {
    ($func:ident ! $args:tt) => {
        $func! {
            [
                { core ltfree sized unit: () = () }
                { core ltfree sized bool: bool = true }
                { core ltfree sized u32: u32 = 123_456_789 }
                { core ltfree sized i32: i32 = -123_456_789 }
                { core ltfree sized f64: f64 = 123.456 }
                { core ltfree sized u128: u128 = u128::MAX - 123_456_789 }
                { core ltfree sized array_u8_8: [u8; 8] = [1, 2, 3, 4, 5, 6, 7, 8] }
                { core ltfree sized tuple_u32_f64_u8: (u32, f64, u8) = (987_654_321, 987.654, 123) }
                { core ltfree sized option_u32: Option<u32> = Some(123_123_123) }
                {
                    core ltfree sized result_unit_i8:
                        Result<(), ::core::num::NonZero<u8>> =
                        Err(::core::num::NonZero::new(1).unwrap())
                }
                {
                    core ltfree unsized str: str =
                        *unsafe { ::core::str::from_utf8_unchecked_mut(&mut { *b"bar" }) }
                    // expression above is used to make string literal mutable if needed
                }
                {
                    core ltfree unsized slice_option_u16: [Option<i16>] = [
                        Some(12_345), None, Some(-123), None, Some(1)
                    ]
                }
                { core static sized str_static_ref: &'static str = "foo" }
                {
                    alloc ltfree sized btreeset_string_vec_u8:
                        ::std::collections::BTreeSet<String> =
                        $crate::common::btreeset_string_vec_u8_example()
                }
                {
                    alloc static sized btreemap_cow_str_string:
                        ::std::collections::BTreeMap<
                            ::std::borrow::Cow<'static, str>,
                            String
                        > = $crate::common::btreemap_cow_str_string_example()
                }
                {
                    alloc static unsized slice_cow_static_u32:
                        [::std::borrow::Cow<'static, u8>] = [
                            ::std::borrow::Cow::Owned(12),
                            ::std::borrow::Cow::Borrowed(&23),
                            ::std::borrow::Cow::Owned(34),
                            ::std::borrow::Cow::Borrowed(&45)
                        ]
                }
                {
                    std ltfree sized hashmap_i32_box_str:
                        ::std::collections::HashMap<i32, Box<str>> =
                        $crate::common::hashmap_i32_box_str_example()
                }
                {
                    std static sized hashmap_arc_path_static_str:
                        ::std::collections::HashMap<
                            ::std::sync::Arc<::std::path::Path>,
                            &'static [u8]
                        > = $crate::common::hashmap_arc_path_static_str_example()
                }
            ]
            $args
        }
    };
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn btreeset_string_vec_u8_example() -> BTreeSet<String> {
    ["a", "b", "c"]
        .map(ToString::to_string)
        .into_iter()
        .collect()
}

#[cfg(feature = "alloc")]
#[must_use]
pub fn btreemap_cow_str_string_example() -> BTreeMap<Cow<'static, str>, String> {
    [
        (Cow::Owned("a".to_string() + "bc"), String::from("qwe")),
        (Cow::Borrowed("efg"), String::from("asd")),
        (Cow::Borrowed("ijk"), String::from("zxc")),
    ]
    .into_iter()
    .collect()
}

#[cfg(feature = "std")]
#[must_use]
pub fn hashmap_i32_box_str_example() -> HashMap<i32, Box<str>> {
    [
        (1, Box::from("foo")),
        (2, Box::from("bar")),
        (3, Box::from("qwe")),
    ]
    .into_iter()
    .collect()
}

#[cfg(feature = "std")]
#[must_use]
pub fn hashmap_arc_path_static_str_example() -> HashMap<Arc<Path>, &'static [u8]> {
    [
        (Arc::from(Path::new("here")), &b"first"[..]),
        (Arc::from(Path::new("there")), &[1, 2, 3]),
        (Arc::from(Path::new("nowhere")), &[123; 12]),
    ]
    .into_iter()
    .collect()
}

// When called, it implicitly coerce function to function pointer.
#[expect(clippy::as_conversions, reason = "okay in tests")]
pub const fn fn_raw_ptr<T, R>(func: extern "Rust" fn(T) -> R) -> *const u8 {
    (func as *const fn(T) -> R).cast::<u8>()
}