maybe_borrow/
lib.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
//! [`maybe_borrow!`]: maybe_borrow
//! [`try_maybe_borrow!`]: try_maybe_borrow
#![doc = include_str!("../README.md")]
#![no_std]

extern crate should_it_compile;

mod custom_try;
mod macros;
mod ptr_impls;
mod utils;
mod with_lt;

should_it_compile::compile_test_mod!(compile_fail);

pub mod prelude {
    pub use crate::macros::{maybe_borrow, try_maybe_borrow};
}

#[doc(hidden)]
pub mod _m {
    use crate::custom_try::TryKind;
    pub use crate::{
        custom_try::{ContinueOf, CustomTry, WithContinue},
        macros::*,
        maybe_borrow_impl::maybe_borrow,
        with_lt::*,
    };
    pub use core::{
        self,
        marker::PhantomData,
        ops::ControlFlow::{self, Break, Continue},
        prelude::rust_2021::*,
        result::Result::{Err, Ok},
        task::Poll,
    };

    /// Does nothing with a mutable borrow of `T`.
    /// Call this from a macro to prevent `unused_mut` by proving a variable is mutably borrowed at
    /// least once.
    #[inline(always)]
    pub fn noop_use_mut<T: ?Sized>(_: &mut T) {}

    pub struct WrapTryMaybeBorrowExit<Out>(PhantomData<Out>);

    impl<Out, Exit, T> WrapTryMaybeBorrowExit<Out>
    where
        Out: CustomTry<Continue = ControlFlow<T, Exit>>,
    {
        pub fn wrap(self, exit: Exit) -> Out {
            Out::from_continue(Continue(exit))
        }
    }

    pub fn try_maybe_borrow_helper<Tk, Ret, T, Exit>(
        body: impl FnOnce(
            WrapTryMaybeBorrowExit<Tk::WithContinue<ControlFlow<T, Exit>>>,
        ) -> Tk::WithContinue<ControlFlow<T, Exit>>,
    ) -> ControlFlow<Ret, Exit>
    where
        Tk: TryKind,
        Ret: CustomTry<Kind = Tk, Continue = T>,
    {
        let body_out = body(WrapTryMaybeBorrowExit(PhantomData));
        match body_out.into_ctrl() {
            Break(r) => Break(Ret::from_residual(r)),
            Continue(Ok(Break(b))) => Break(Ret::from_continue(b)),
            Continue(Ok(Continue(exit))) => Continue(exit),
            Continue(Err(e)) => Break(Ret::from_empty(e)),
        }
    }
}

mod traits;

mod maybe_borrow_impl;