hecs_schedule/borrow/
into_borrow.rs

1///! This module works around the lifetimes for borrow when GAT isn't available
2use crate::{Read, SubWorld, Write};
3
4use super::{ContextBorrow, MaybeRead, MaybeWrite};
5
6use hecs::Component;
7
8/// Lifetime erasure in waiting of GAT
9pub trait IntoBorrow {
10    /// The borrow type
11    type Borrow: for<'x> ContextBorrow<'x>;
12}
13
14/// Macro for implementing lifetime eliding IntoBorrow
15#[macro_export]
16macro_rules! impl_into_borrow {
17    ($generic: tt, $name: tt => $borrower: tt) => {
18        #[doc(hidden)]
19        pub struct $borrower<T: $generic>(std::marker::PhantomData<T>);
20
21        impl<T: $generic> $crate::borrow::IntoBorrow for $name<'_, T> {
22            type Borrow = $borrower<T>;
23        }
24
25        impl<'a, T: $generic> $crate::borrow::ContextBorrow<'a> for $borrower<T> {
26            type Target = $name<'a, T>;
27
28            fn borrow(context: &'a $crate::Context) -> $crate::error::Result<Self::Target> {
29                Self::Target::borrow(context)
30            }
31        }
32    };
33}
34
35impl_into_borrow!(Component, Read => Borrower);
36impl_into_borrow!(Component, Write => BorrowMut);
37impl_into_borrow!(Component, MaybeRead => MaybeBorrower);
38impl_into_borrow!(Component, MaybeWrite => MaybeBorrowerMut);
39impl_into_borrow!(Component, SubWorld => SubWorldBorrower);