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
//! This crate provides a macro to generate aux macros for on-demand usage.
//!
//! # Examples
//!
//! ```rust
//! use on_demand::generate_on_demand_macro;
//!
//! fn foo() {
//!     generate_on_demand_macro!(a: usize = None, {
//!         println!("a");
//!         1
//!     });
//!     generate_on_demand_macro!(b: usize = None, {
//!         println!("b");
//!         let a_data = on_demand_get_a!();
//!         2 + *a_data
//!     });
//!     generate_on_demand_macro!(c: usize = None, {
//!         println!("c");
//!         let a_data = on_demand_get_a!();
//!         let b_data = on_demand_get_b!();
//!         3 + *a_data + *b_data
//!     });
//!
//!     let c_data = on_demand_get_c!();
//!     assert_eq!(*c_data, 6);
//! }
//! ```
//!
//! After calling `generate_on_demand_macro` to the variable (for example, `a`),
//! three new macros `on_demand_get_a`, `on_demand_get_a_mut` and `on_demand_into_a` are generated.
//! When `on_demand_get_a` is called, it determines whether `a` has been calculated. If it is,
//! then returns the reference to its data, otherwise calls the expression given as the second
//! parameter of `generate_on_demand_macro`, then assigns the returned value to `a`. The other two
//! generated macros do the similar job, but returns the mutable reference to, or takes ownership
//! of `a`.
//!
//! In all, this means calculating `a` lazily using the expression.
//!
//! # Notes
//!
//! The returned value of `on_demand_get_a` is essentially a [`Ref`][std::cell::Ref], and
//! `on_demand_get_a_mut` is [`RefMut`][std::cell::RefMut]. So remember to dereference it
//! when necessary.
//!
//! When calling this macro, we assign a default value by `generate_on_demand_macro!(a: MyType
//! = default_value_of_optional_my_type), { /* block */ });`. The `default_value_of_optional_my_type` should
//! be of type `Option<MyType>`. If it is not `None`, then `a` is considered calculated already, and the expression
//! given in block will never be called.
//!
//! There are some crates providing lazy feature, such as [spin](https://crates.io/crates/spin).
//! However, those methods are based on closure, which could not handle some situations such as below
//! (error handlings are omitted for simplicity):
//!
//! ```rust
//! # use on_demand::generate_on_demand_macro;
//! # use std::io::{Seek, Read, SeekFrom};
//! fn foo<T: Seek + Read>(binary: &mut T) {
//!     generate_on_demand_macro!(a: u32 = None, {
//!         let mut buf = [0; 4];
//!         binary.seek(SeekFrom::Start(0)).unwrap();
//!         binary.read(&mut buf).unwrap();
//!         u32::from_be_bytes(buf)
//!     });
//!     generate_on_demand_macro!(b: u32 = None, {
//!         let a_data = on_demand_get_a!();
//!         let mut buf = [0; 4];
//!         binary.seek(SeekFrom::Start(0)).unwrap();
//!         binary.read(&mut buf).unwrap();
//!         u32::from_be_bytes(buf) + *a_data
//!     });
//!     generate_on_demand_macro!(c: u32 = None, {
//!         let a_data = on_demand_get_a!();
//!         let b_data = on_demand_get_b!();
//!         let mut buf = [0; 4];
//!         binary.seek(SeekFrom::Start(0)).unwrap();
//!         binary.read(&mut buf).unwrap();
//!         u32::from_be_bytes(buf) + *a_data + *b_data
//!     });
//!     let a_data = on_demand_get_a!();
//!     let b_data = on_demand_get_b_mut!();
//!     drop(b_data); // drop here since c will take ownership
//!     let c_data = on_demand_into_c!();
//! }
//! ```
//!
//! `binary` is considered as uniquely borrowed if closure is used, then the borrow checker
//! won't allow us do above things. However, macros can do such things.

#[doc(hidden)]
pub use paste;

/// Macro to generate on-demand macro
#[macro_export]
macro_rules! generate_on_demand_macro {
    ($var: ident: $Inner: ty = $default_value: expr, $getter: expr) => {
        let $var: ::std::cell::RefCell<::std::option::Option<$Inner>> =
            ::std::cell::RefCell::new($default_value);
        $crate::paste::paste! {
            #[allow(unused_macros)]
            macro_rules! [<on_demand_get_ $var>] {
                () => {{
                    let is_some = $var.borrow().is_some();
                    if !is_some {
                        *($var.borrow_mut()) = Some({$getter});
                    }
                    ::std::cell::Ref::map($var.borrow(), |var| {
                        if let Some(data) = var.as_ref() {
                            data
                        } else {
                            unreachable!()
                        }
                    })
                }};
            }
            #[allow(unused_macros)]
            macro_rules! [<on_demand_get_ $var _mut>] {
                () => {{
                    let is_some = $var.borrow().is_some();
                    if !is_some {
                        *($var.borrow_mut()) = Some({$getter});
                    }
                    ::std::cell::RefMut::map($var.borrow_mut(), |var| {
                        if let Some(data) = var.as_mut() {
                            data
                        } else {
                            unreachable!()
                        }
                    })
                }};
            }
            #[allow(unused_macros)]
            macro_rules! [<on_demand_into_ $var>] {
                () => {{
                    if let Some(data) = $var.into_inner() {
                        data
                    } else {
                        {$getter}
                    }
                }};
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use super::generate_on_demand_macro;
    use std::io::{Cursor, Read, Seek, SeekFrom};

    #[test]
    fn test_reader() {
        let mut binary = Cursor::new([0x0u8, 0x1u8, 0x2u8, 0x3u8]);
        generate_on_demand_macro!(a: u32 = None, {
            let mut buf = [0; 4];
            binary.seek(SeekFrom::Start(0)).unwrap();
            binary.read(&mut buf).unwrap();
            u32::from_le_bytes(buf)
        });
        generate_on_demand_macro!(b: u32 = None, {
            let a_data = on_demand_get_a!();
            let mut buf = [0; 4];
            binary.seek(SeekFrom::Start(0)).unwrap();
            binary.read(&mut buf).unwrap();
            u32::from_le_bytes(buf) + *a_data
        });
        generate_on_demand_macro!(c: u32 = None, {
            let a_data = on_demand_get_a!();
            let b_data = on_demand_get_b!();
            let mut buf = [0; 4];
            binary.seek(SeekFrom::Start(0)).unwrap();
            binary.read(&mut buf).unwrap();
            u32::from_le_bytes(buf) + *a_data + *b_data
        });
        let a_data = on_demand_get_a!();
        assert_eq!(*a_data, 0x3020100);
        let b_data = on_demand_get_b_mut!();
        assert_eq!(*b_data, 0x6040200);
        drop(b_data); // drop here since c will take ownership
        let c_data = on_demand_into_c!();
        assert_eq!(c_data, 0xc080400);
    }
}