resman 0.17.2

Runtime managed resource borrowing.
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
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
#[cfg(not(feature = "fn_res"))]
fn main() {}

#[cfg(feature = "fn_res")]
fn main() {
    use std::{env, io::Write, path::Path};

    use common::{generate_impls_for_n_args, ArgExprs};

    let out_dir = env::var_os("OUT_DIR").expect("Failed to read `OUT_DIR` environment variable.");
    let out_dir = Path::new(&out_dir);

    #[cfg(feature = "fn_res")]
    let mut fn_resource_impl = common::open_impl_file(out_dir, "fn_resource_impl.rs");
    #[cfg(feature = "fn_res_once")]
    let mut fn_res_once_impl = common::open_impl_file(out_dir, "fn_res_once_impl.rs");
    #[cfg(feature = "fn_res_mut")]
    let mut fn_res_mut_impl = common::open_impl_file(out_dir, "fn_res_mut_impl.rs");
    #[cfg(feature = "fn_res")]
    let mut fn_res_impl = common::open_impl_file(out_dir, "fn_res_impl.rs");

    #[cfg(feature = "fn_meta")]
    let mut fn_resource_meta_impl = common::open_impl_file(out_dir, "fn_resource_meta_impl.rs");

    let mut write_fn = |arg_exprs: ArgExprs<'_>| {
        #[cfg(feature = "fn_res")]
        fn_resource_impl::write_fn_resource_impl(&mut fn_resource_impl, arg_exprs);

        #[cfg(feature = "fn_res_once")]
        fn_res_once_impl::write_fn_res_once_impl(&mut fn_res_once_impl, arg_exprs);
        #[cfg(feature = "fn_res_mut")]
        fn_res_mut_impl::write_fn_res_mut_impl(&mut fn_res_mut_impl, arg_exprs);
        #[cfg(feature = "fn_res")]
        fn_res_impl::write_fn_res_impl(&mut fn_res_impl, arg_exprs);

        #[cfg(feature = "fn_meta")]
        fn_resource_meta_impl::write_fn_resource_meta_impl(&mut fn_resource_meta_impl, arg_exprs);
    };

    generate_impls_for_n_args::<_, 1>(&mut write_fn);
    generate_impls_for_n_args::<_, 2>(&mut write_fn);
    generate_impls_for_n_args::<_, 3>(&mut write_fn);
    generate_impls_for_n_args::<_, 4>(&mut write_fn);
    generate_impls_for_n_args::<_, 5>(&mut write_fn);
    generate_impls_for_n_args::<_, 6>(&mut write_fn);

    #[cfg(feature = "high_arg_count")]
    generate_impls_for_n_args::<_, 7>(&mut write_fn);
    #[cfg(feature = "high_arg_count")]
    generate_impls_for_n_args::<_, 8>(&mut write_fn);

    #[cfg(feature = "fn_res")]
    fn_resource_impl
        .flush()
        .expect("Failed to flush writer for fn_resource_impl.rs");

    #[cfg(feature = "fn_meta")]
    fn_resource_meta_impl
        .flush()
        .expect("Failed to flush writer for fn_resource_meta_impl.rs");

    println!("cargo:rerun-if-changed=build.rs");
}

#[cfg(any(feature = "fn_meta", feature = "fn_res"))]
mod common {
    use std::{
        fmt::Write as _,
        fs::{File, OpenOptions},
        io::BufWriter,
        path::Path,
    };

    #[derive(Clone, Copy, Debug)]
    pub struct ArgExprs<'s> {
        pub args_csv: &'s str,
        pub arg_refs_csv: &'s str,
        pub arg_bounds_list: &'s str,
        pub resource_arg_borrows: &'s str,
        pub resource_arg_try_borrows: &'s str,
        pub resource_arg_vars: &'s str,
    }

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum Ref {
        Immutable,
        Mutable,
    }

    pub fn open_impl_file(out_dir: &Path, file_name: &str) -> BufWriter<File> {
        let fn_resource_impl_path = out_dir.join(file_name);
        let fn_resource_impl = OpenOptions::new()
            .create(true)
            .truncate(true)
            .write(true)
            .open(fn_resource_impl_path)
            .unwrap_or_else(|e| panic!("Failed to open `{file_name}`. Error: {e}"));
        BufWriter::new(fn_resource_impl)
    }

    pub fn generate_impls_for_n_args<FnWrite, const N: usize>(fn_write: &mut FnWrite)
    where
        FnWrite: FnMut(ArgExprs<'_>),
    {
        // "A0, A1"
        let args_csv = args_csv::<N>();

        // "    A0: 'static,\n    A1: 'static,"
        let arg_bounds_list = arg_bounds_list::<N>();

        arg_refs_combinations::<N>().for_each(|arg_refs| {
            let mut arg_refs_iter = arg_refs.iter().copied().enumerate();

            // &mut A0, &A1
            let arg_refs_csv = {
                let mut arg_refs_csv = String::with_capacity(N * 8);
                if let Some((_index, arg_ref_first)) = arg_refs_iter.next() {
                    match arg_ref_first {
                        Ref::Immutable => arg_refs_csv.push_str("&A0"),
                        Ref::Mutable => arg_refs_csv.push_str("&mut A0"),
                    }
                }

                if N == 1 {
                    arg_refs_csv.push(',');
                } else {
                    arg_refs_iter
                        .try_for_each(|(index, arg_ref)| match arg_ref {
                            Ref::Immutable => write!(&mut arg_refs_csv, ", &A{index}"),
                            Ref::Mutable => write!(&mut arg_refs_csv, ", &mut A{index}"),
                        })
                        .expect("Failed to append to `arg_refs_csv` string.");
                }

                arg_refs_csv
            };

            // let a0 = resources.borrow::<A0>();
            // let mut a1 = resources.borrow_mut::<A1>();
            // ..
            let resource_arg_borrows = resource_arg_borrows(arg_refs);
            let resource_arg_try_borrows = resource_arg_try_borrows(arg_refs);

            // &*a0, &mut *a1
            let resource_arg_vars = resource_arg_vars::<N>(arg_refs);

            let args_csv = args_csv.as_str();
            let arg_refs_csv = arg_refs_csv.as_str();
            let arg_bounds_list = arg_bounds_list.as_str();
            let resource_arg_borrows = resource_arg_borrows.as_str();
            let resource_arg_try_borrows = resource_arg_try_borrows.as_str();
            let resource_arg_vars = resource_arg_vars.as_str();

            let arg_exprs = ArgExprs {
                args_csv,
                arg_refs_csv,
                arg_bounds_list,
                resource_arg_borrows,
                resource_arg_try_borrows,
                resource_arg_vars,
            };

            fn_write(arg_exprs);
        })
    }

    fn resource_arg_vars<const N: usize>(arg_refs: [Ref; N]) -> String {
        let mut resource_arg_vars = String::with_capacity(N * 10);
        let mut arg_refs_iter = arg_refs.iter().copied().enumerate();
        if let Some((index, arg_ref)) = arg_refs_iter.next() {
            match arg_ref {
                Ref::Immutable => write!(&mut resource_arg_vars, "&*a{index}"),
                Ref::Mutable => write!(&mut resource_arg_vars, "&mut *a{index}"),
            }
            .expect("Failed to append to `resource_arg_vars` string.")
        }
        arg_refs_iter
            .try_for_each(|(index, arg_ref)| match arg_ref {
                Ref::Immutable => write!(&mut resource_arg_vars, ", &*a{index}"),
                Ref::Mutable => write!(&mut resource_arg_vars, ", &mut *a{index}"),
            })
            .expect("Failed to append to `resource_arg_vars` string.");
        resource_arg_vars
    }

    fn resource_arg_borrows<const N: usize>(arg_refs: [Ref; N]) -> String {
        let mut resource_arg_borrows = String::with_capacity(N * 44);
        let mut arg_refs_iter = arg_refs.iter().copied().enumerate();
        arg_refs_iter
            .try_for_each(|(index, arg_ref)| match arg_ref {
                Ref::Immutable => writeln!(
                    &mut resource_arg_borrows,
                    "let a{index} = resources.borrow::<A{index}>();",
                ),
                Ref::Mutable => writeln!(
                    &mut resource_arg_borrows,
                    "let mut a{index} = resources.borrow_mut::<A{index}>();",
                ),
            })
            .expect("Failed to append to `resource_arg_borrows` string.");
        resource_arg_borrows
    }

    fn resource_arg_try_borrows<const N: usize>(arg_refs: [Ref; N]) -> String {
        let mut resource_arg_try_borrows = String::with_capacity(N * 44);
        let mut arg_refs_iter = arg_refs.iter().copied().enumerate();
        arg_refs_iter
            .try_for_each(|(index, arg_ref)| match arg_ref {
                Ref::Immutable => writeln!(
                    &mut resource_arg_try_borrows,
                    "let a{index} = resources.try_borrow::<A{index}>()?;",
                ),
                Ref::Mutable => writeln!(
                    &mut resource_arg_try_borrows,
                    "let mut a{index} = resources.try_borrow_mut::<A{index}>()?;",
                ),
            })
            .expect("Failed to append to `resource_arg_try_borrows` string.");
        resource_arg_try_borrows
    }

    fn arg_refs_combinations<const N: usize>() -> impl Iterator<Item = [Ref; N]> {
        (0..(2 << (N - 1))).map(|m| {
            // `m` is the combination variation count.
            // Whether an argument is immutable or mutable is bed on its corresponding bit
            // value of `m`.

            let mut arg_refs: [Ref; N] = [Ref::Immutable; N];
            arg_refs
                .iter_mut()
                .enumerate()
                .for_each(move |(n, arg_ref)| {
                    // for N = 5
                    // m can be 0..32
                    // if 31 >> 5 is 0

                    if m >> n & 1 == 0 {
                        *arg_ref = Ref::Immutable;
                    } else {
                        *arg_ref = Ref::Mutable;
                    }
                });

            arg_refs
        })
    }

    fn arg_bounds_list<const N: usize>() -> String {
        let mut arg_bounds_list = String::with_capacity(N * 50);
        #[cfg(feature = "debug")]
        arg_bounds_list.push_str("    A0: std::fmt::Debug + Send + Sync + 'static,");

        #[cfg(not(feature = "debug"))]
        arg_bounds_list.push_str("    A0: Send + Sync + 'static,");
        (1..N).fold(arg_bounds_list, |mut arg_bounds_list, n| {
            #[cfg(feature = "debug")]
            write!(
                &mut arg_bounds_list,
                "\n    A{n}: std::fmt::Debug + Send + Sync + 'static,"
            )
            .expect("Failed to append to args_csv string.");

            #[cfg(not(feature = "debug"))]
            write!(&mut arg_bounds_list, "\n    A{n}: Send + Sync + 'static,")
                .expect("Failed to append to args_csv string.");
            arg_bounds_list
        })
    }

    fn args_csv<const N: usize>() -> String {
        let mut args_csv = String::with_capacity(N * 4);
        args_csv.push_str("A0");
        (1..N).fold(args_csv, |mut args_csv, n| {
            write!(&mut args_csv, ", A{n}").expect("Failed to append to args_csv string.");
            args_csv
        })
    }
}

#[cfg(feature = "fn_res")]
mod fn_resource_impl {
    use std::{
        fs::File,
        io::{BufWriter, Write},
    };

    use super::common::ArgExprs;

    pub fn write_fn_resource_impl(fn_resource_impl: &mut BufWriter<File>, arg_exprs: ArgExprs<'_>) {
        let ArgExprs {
            args_csv,
            arg_refs_csv,
            arg_bounds_list,
            resource_arg_borrows,
            resource_arg_try_borrows,
            resource_arg_vars,
        } = arg_exprs;

        #[cfg(feature = "fn_res_once")]
        write!(
            fn_resource_impl,
            r#"
impl<Fun, Ret, {args_csv}> FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: FnOnce({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
    pub fn call_once(self, resources: &Resources) -> Ret {{
        {resource_arg_borrows}

        (self.func)({resource_arg_vars})
    }}

    pub fn try_call_once(self, resources: &Resources) -> Result<Ret, BorrowFail> {{
        {resource_arg_try_borrows}

        let ret_value = (self.func)({resource_arg_vars});
        Ok(ret_value)
    }}
}}
"#,
        )
        .expect("Failed to write to fn_resource_impl.rs");

        #[cfg(feature = "fn_res_mut")]
        write!(
            fn_resource_impl,
            r#"
impl<Fun, Ret, {args_csv}> FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: FnMut({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
    pub fn call_mut(&mut self, resources: &Resources) -> Ret {{
        {resource_arg_borrows}

        (self.func)({resource_arg_vars})
    }}

    pub fn try_call_mut(&mut self, resources: &Resources) -> Result<Ret, BorrowFail> {{
        {resource_arg_try_borrows}

        let ret_value = (self.func)({resource_arg_vars});
        Ok(ret_value)
    }}
}}
"#,
        )
        .expect("Failed to write to fn_resource_impl.rs");

        write!(
            fn_resource_impl,
            r#"
impl<Fun, Ret, {args_csv}> FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: Fn({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
    pub fn call(&self, resources: &Resources) -> Ret {{
        {resource_arg_borrows}

        (self.func)({resource_arg_vars})
    }}

    pub fn try_call(&self, resources: &Resources) -> Result<Ret, BorrowFail> {{
        {resource_arg_try_borrows}

        let ret_value = (self.func)({resource_arg_vars});
        Ok(ret_value)
    }}
}}
"#,
        )
        .expect("Failed to write to fn_resource_impl.rs");
    }
}

#[cfg(feature = "fn_res_once")]
mod fn_res_once_impl {
    use std::{
        fs::File,
        io::{BufWriter, Write},
    };

    use super::common::ArgExprs;

    pub fn write_fn_res_once_impl(fn_resource_impl: &mut BufWriter<File>, arg_exprs: ArgExprs<'_>) {
        let ArgExprs {
            args_csv,
            arg_refs_csv,
            arg_bounds_list,
            ..
        } = arg_exprs;

        write!(
            fn_resource_impl,
            r#"
impl<Fun, Ret, {args_csv}> FnResOnce for FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: FnOnce({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
    type Ret = Ret;

    fn call_once(self, resources: &Resources) -> Ret {{
        Self::call_once(self, resources)
    }}

    fn try_call_once(self, resources: &Resources) -> Result<Ret, BorrowFail> {{
        Self::try_call_once(self, resources)
    }}
}}
"#,
        )
        .expect("Failed to write to fn_res_once_impl.rs");
    }
}

#[cfg(feature = "fn_res_mut")]
mod fn_res_mut_impl {
    use std::{
        fs::File,
        io::{BufWriter, Write},
    };

    use super::common::ArgExprs;

    pub fn write_fn_res_mut_impl(fn_resource_impl: &mut BufWriter<File>, arg_exprs: ArgExprs<'_>) {
        let ArgExprs {
            args_csv,
            arg_refs_csv,
            arg_bounds_list,
            ..
        } = arg_exprs;

        #[cfg(not(feature = "fn_res_once"))]
        let ret_type_str = "    type Ret = Ret;\n";
        #[cfg(feature = "fn_res_once")]
        let ret_type_str = "";

        write!(
            fn_resource_impl,
            r#"
impl<Fun, Ret, {args_csv}> FnResMut for FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: FnMut({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
{ret_type_str}
    fn call_mut(&mut self, resources: &Resources) -> Ret {{
        Self::call_mut(self, resources)
    }}

    fn try_call_mut(&mut self, resources: &Resources) -> Result<Ret, BorrowFail> {{
        Self::try_call_mut(self, resources)
    }}
}}
"#,
        )
        .expect("Failed to write to fn_res_mut_impl.rs");
    }
}

#[cfg(feature = "fn_res")]
mod fn_res_impl {
    use std::{
        fs::File,
        io::{BufWriter, Write},
    };

    use super::common::ArgExprs;

    pub fn write_fn_res_impl(fn_resource_impl: &mut BufWriter<File>, arg_exprs: ArgExprs<'_>) {
        let ArgExprs {
            args_csv,
            arg_refs_csv,
            arg_bounds_list,
            ..
        } = arg_exprs;

        #[cfg(not(feature = "fn_res_mut"))]
        let ret_type_str = "    type Ret = Ret;\n";
        #[cfg(feature = "fn_res_mut")]
        let ret_type_str = "";

        write!(
            fn_resource_impl,
            r#"
impl<Fun, Ret, {args_csv}> FnRes for FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: Fn({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
{ret_type_str}
    fn call(&self, resources: &Resources) -> Ret {{
        Self::call(self, resources)
    }}

    fn try_call(&self, resources: &Resources) -> Result<Ret, BorrowFail> {{
        Self::try_call(self, resources)
    }}
}}
"#,
        )
        .expect("Failed to write to fn_res_impl.rs");
    }
}

#[cfg(all(feature = "fn_res", feature = "fn_meta"))]
mod fn_resource_meta_impl {
    use std::{
        fs::File,
        io::{BufWriter, Write},
    };

    use super::common::ArgExprs;

    pub fn write_fn_resource_meta_impl(
        fn_resource_meta_impl: &mut BufWriter<File>,
        arg_exprs: ArgExprs<'_>,
    ) {
        let ArgExprs {
            args_csv,
            arg_refs_csv,
            arg_bounds_list,
            resource_arg_borrows: _,
            resource_arg_try_borrows: _,
            resource_arg_vars: _,
        } = arg_exprs;

        write!(
            fn_resource_meta_impl,
            r#"
impl<Fun, Ret, {args_csv}> fn_meta::FnMeta for FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: FnOnce({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
    fn borrows() -> fn_meta::TypeIds {{
        <fn_meta::FnMetadata<Fun, Ret, ({arg_refs_csv})> as fn_meta::FnMeta>::borrows()
    }}

    fn borrow_muts() -> fn_meta::TypeIds {{
        <fn_meta::FnMetadata<Fun, Ret, ({arg_refs_csv})> as fn_meta::FnMeta>::borrow_muts()
    }}
}}

impl<Fun, Ret, {args_csv}> fn_meta::FnMetaDyn for FnResource<Fun, Ret, ({arg_refs_csv})>
where
    Fun: FnOnce({arg_refs_csv}) -> Ret + 'static,
    Ret: 'static,
    {arg_bounds_list}
{{
    fn borrows(&self) -> fn_meta::TypeIds {{
        fn_meta::FnMetaExt::meta(&self.func).borrows()
    }}

    fn borrow_muts(&self) -> fn_meta::TypeIds {{
        fn_meta::FnMetaExt::meta(&self.func).borrow_muts()
    }}
}}
"#,
        )
        .expect("Failed to write to fn_resource_meta_impl.rs");
    }
}