maybe_borrow/
lib.rs

1//! [`maybe_borrow!`]: maybe_borrow
2//! [`try_maybe_borrow!`]: try_maybe_borrow
3#![doc = include_str!("../README.md")]
4#![no_std]
5
6extern crate should_it_compile;
7
8mod custom_try;
9mod macros;
10mod ptr_impls;
11mod utils;
12mod with_lt;
13
14should_it_compile::compile_test_mod!(compile_fail);
15
16pub mod prelude {
17    pub use crate::macros::{maybe_borrow, try_maybe_borrow};
18}
19
20#[doc(hidden)]
21pub mod _m {
22    use crate::custom_try::TryKind;
23    pub use crate::{
24        custom_try::{ContinueOf, CustomTry, WithContinue},
25        macros::*,
26        maybe_borrow_impl::maybe_borrow,
27        with_lt::*,
28    };
29    pub use core::{
30        self,
31        marker::PhantomData,
32        ops::ControlFlow::{self, Break, Continue},
33        prelude::rust_2021::*,
34        result::Result::{Err, Ok},
35        task::Poll,
36    };
37
38    /// Container that's not [`Copy`] so it automatically gets moved into a closure
39    /// rather than referenced.
40    pub struct ForceMove<T>(pub T);
41
42    /// Does nothing with a mutable borrow of `T`.
43    /// Call this from a macro to prevent `unused_mut` by proving a variable is mutably borrowed at
44    /// least once.
45    #[inline(always)]
46    pub fn noop_use_mut<T: ?Sized>(_: &mut T) {}
47
48    pub struct WrapTryMaybeBorrowExit<Out>(PhantomData<Out>);
49
50    impl<Out, Exit, T> WrapTryMaybeBorrowExit<Out>
51    where
52        Out: CustomTry<Continue = ControlFlow<T, Exit>>,
53    {
54        pub fn wrap(self, exit: Exit) -> Out {
55            Out::from_continue(Continue(exit))
56        }
57    }
58
59    pub fn try_maybe_borrow_helper<Tk, Ret, T, Exit>(
60        body: impl FnOnce(
61            WrapTryMaybeBorrowExit<Tk::WithContinue<ControlFlow<T, Exit>>>,
62        ) -> Tk::WithContinue<ControlFlow<T, Exit>>,
63    ) -> ControlFlow<Ret, Exit>
64    where
65        Tk: TryKind,
66        Ret: CustomTry<Kind = Tk, Continue = T>,
67    {
68        let body_out = body(WrapTryMaybeBorrowExit(PhantomData));
69        match body_out.into_ctrl() {
70            Break(r) => Break(Ret::from_residual(r)),
71            Continue(Ok(Break(b))) => Break(Ret::from_continue(b)),
72            Continue(Ok(Continue(exit))) => Continue(exit),
73            Continue(Err(e)) => Break(Ret::from_empty(e)),
74        }
75    }
76}
77
78mod traits;
79
80mod maybe_borrow_impl;