maybe_borrow/macros/
mod.rs

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
// MARK: Docs

#[cfg(doc)]
/// Runs the given block, either returning a value borrowing data from `$ptr`'s target, or
/// continuing by evaluating to a non-borrowing value.
///
#[doc = include_str!("./maybe_borrow.md")]
#[macro_export]
macro_rules! maybe_borrow {
    ( $(for<$lt:lifetime>)? |$($ptr:ident),*| -> $Ret:ty $block:block ) => {
        todo!()
    };
}

#[cfg(doc)]
#[doc = include_str!("./try_maybe_borrow.md")]
#[macro_export]
macro_rules! try_maybe_borrow {
    ( $(for<$lt:lifetime>)? |$($ptr:ident),*| -> $Ret:ty $block:block ) => {
        todo!()
    };
}

#[cfg(doc)]
#[macro_export]
/// Return from the containing function with potentially borrowed data from within a
/// [`maybe_borrow`] or [`try_maybe_borrow`] invocation.
///
/// This macro is only available within the aforementioned macros.
macro_rules! return_borrowed {
    ($return_value:expr) => {};
}

#[cfg(doc)]
#[macro_export]
/// Behaves like [`core::task::ready!`], but uses [`return_borrowed!`] rather than `return` so it can be used within [`maybe_borrow!`] or [`try_maybe_borrow!`].
///
/// This macro is only available within the aforementioned macros.
macro_rules! ready {
    ($return_value:expr) => {};
}

#[cfg(doc)]
pub use return_borrowed;

// MARK: Public

/// Runs the given block, either returning a value borrowing data from `$ptr`'s target, or
/// continuing by evaluating to a non-borrowing value.
///
#[cfg(not(doc))]
#[doc = include_str!("./maybe_borrow.md")]
#[macro_export]
macro_rules! maybe_borrow {
    ($(for<$lt:lifetime $(,)?>)? |$($ptr:ident),+ $(,)?| -> $Ret:ty $block:block $(,)?) => {{
        $crate::_m::__maybe_borrow! {
            $crate::_m::WithLt![$($lt ->)? $Ret],
            |[$($ptr)+]| { $crate::_m::ControlFlow::Continue({
                #[allow(unused)]
                use $crate::_m::__return_borrowed as return_borrowed;
                #[allow(unused)]
                use $crate::_m::__ready as ready;

                $block
            }) }
        }
    }};

    ($(for<$lt:lifetime $(,)?>)? |$ptr:ident $(,)?| $block:expr $(,)?) => {
        $crate::_m::compile_error!("Explicit return type required in maybe_borrow!");
    }

}

pub use maybe_borrow;

#[cfg(not(doc))]
#[doc = include_str!("./try_maybe_borrow.md")]
#[macro_export]
macro_rules! try_maybe_borrow {
    ($(for<$lt:lifetime $(,)?>)? |$($ptr:ident),+ $(,)?| -> $Ret:ty $block:block $(,)?) => {
        $crate::_m::__maybe_borrow! {
            $crate::_m::WithLt![$($lt ->)? $Ret],
            |[$($ptr)+]| { $crate::_m::try_maybe_borrow_helper(|w| w.wrap({
                #[allow(unused)]
                use $crate::_m::__return_borrowed_try as return_borrowed;
                #[allow(unused)]
                use $crate::_m::__ready as ready;
                $block
            })) }
        }
    };

    ($(for<$lt:lifetime $(,)?>)? |$ptr:ident $(,)?| $block:expr $(,)?) => {
        $crate::_m::compile_error!("Explicit return type required in try_maybe_borrow!");
    }
}

pub use try_maybe_borrow;

// MARK: Internal

#[doc(hidden)]
#[macro_export]
macro_rules! __maybe_borrow {
    ($Ret:ty, |$ptr:tt| $block:block) => {{
        let (_out, _ptr) = match $crate::_m::maybe_borrow::<_, $Ret, _>(
            $crate::_m::__nest_pattern!(@input <- $ptr),
            |$crate::_m::__nest_pattern!(@mut <- $ptr), _| {
                let _ = $crate::_m::__nest_pattern!(@noop_use_mut <- $ptr);
                $block
            },
        ) {
            $crate::_m::ControlFlow::Break(_ret) => return _ret,
            $crate::_m::ControlFlow::Continue(_pair) => _pair
        };

        #[allow(unused_assignments)]
        { $crate::_m::__pointer_assign! { $ptr <- _ptr } }
        _out
    }};
}

pub use __maybe_borrow;

#[doc(hidden)]
#[macro_export]
macro_rules! __nest_pattern {
    (@$type:tt <- [$arg0:tt $($arg:tt)+]) => {
        (
            $crate::_m::__nest_pattern!(@$type <- [$arg0]),
            $crate::_m::__nest_pattern!(@$type <- [$($arg)+]),
        )
    };
    (@input <- [$arg:tt]) => { $arg };
    (@mut <- [$arg:tt]) => { mut $arg };
    (@noop_use_mut <- [$arg:tt]) => { $crate::_m::noop_use_mut(&mut $arg) };
    (@type:tt <- []) => { () };
}

pub use __nest_pattern;

#[doc(hidden)]
#[macro_export]
macro_rules! __pointer_assign {
    ([$ptr:ident] <- $value:expr) => {
        $ptr = $value;
    };
    ([$ptr0:ident $($ptr:ident)+] <- $value:expr) => {
        $ptr0 = $value.0;
        $crate::_m::__pointer_assign! { [$($ptr)*] <- $value.1 }
    };
    ([] <- $value:expr) => {
        () = $value;
    }
}

pub use __pointer_assign;

#[doc(hidden)]
#[macro_export]
macro_rules! __return_borrowed {
    ($value:expr $(,)?) => {
        return $crate::_m::ControlFlow::Break($value)
    };
}

pub use __return_borrowed;

#[doc(hidden)]
#[macro_export]
macro_rules! __return_borrowed_try {
    ($value:expr $(,)?) => {
        return $crate::_m::CustomTry::map_continue($value, $crate::_m::Break)
    };
}

pub use __return_borrowed_try;

#[doc(hidden)]
#[macro_export]
macro_rules! __ready {
    ($value:expr $(,)?) => {
        match $value {
            $crate::_m::Poll::Ready(_value) => _value,
            $crate::_m::Poll::Pending => return_borrowed!($crate::_m::Poll::Pending),
        }
    };
}

pub use __ready;