multi_readers/wrap.rs
1#[macro_export]
2/// A macro to create a `MultiReaders` instance from a list of values.
3///
4/// # Examples
5///
6/// ```rust,ignore
7/// // Using the macro with trait objects
8/// wrap!(dyn MyTrait, val1, val2, val3);
9///
10/// // Using the macro with regular values
11/// wrap!(val1, val2, val3);
12/// ```
13///
14/// # Parameters
15///
16/// - `dyn $trait`: The trait that the values implement (optional).
17/// - `$val`: The values to be wrapped in the `MultiReaders` instance.
18///
19/// # Usage
20///
21/// - When using the `dyn $trait` form, each value will be boxed and cast to a trait object.
22/// - When using the form without `dyn $trait`, the values will be used as they are.
23macro_rules! wrap {
24 (dyn $trait:path, $($val:expr), +) => {
25 $crate::MultiReaders::new( [ $(Box::new($val) as Box<dyn $trait>), + ].into_iter())
26 };
27 ($($val:expr), +) => {
28 $crate::MultiReaders::new( [ $($val), + ].into_iter())
29 };
30}
31
32#[macro_export]
33/// A macro to create a `MultiReaders` instance by opening multiple values asynchronously or synchronously.
34///
35/// # Examples
36///
37/// ```rust,ignore
38/// // Using the macro with asynchronous opening
39/// open!(async my_async_open, [val1, val2, val3]);
40///
41/// // Using the macro with synchronous opening
42/// open!(my_open, [val1, val2, val3]);
43/// ```
44///
45/// # Parameters
46///
47/// - `async $open`: The asynchronous open function (optional).
48/// - `$open`: The synchronous open function.
49/// - `$arg`: The arguments to be passed to the open function.
50///
51/// # Usage
52///
53/// - When using the `async $open` form, each value will be awaited and then wrapped in the `MultiReaders` instance.
54/// - When using the form without `async $open`, the values will be opened synchronously and wrapped in the `MultiReaders` instance.
55macro_rules! open {
56 (async $open:expr, [$($arg:expr), +]) => {
57 $crate::wrap!($($open($arg).await), +)
58 };
59 ($open:expr, [$($arg:expr), +]) => {
60 $crate::wrap!($($open($arg)), +)
61 };
62}