battler/common/reference.rs
1use std::mem;
2
3/// Trait for unsafely detaching an immutable borrow, attaching a new lifetime to it.
4pub trait UnsafelyDetachBorrow<'a, 'b, T> {
5 /// Unsafely detaches an immutable borrow, attaching a new lifetime.
6 ///
7 /// This method primarily allows a borrow to be used alongside other mutable borrows of the
8 /// same lifetime. It should not be used lightly and it should likely only be used alongside an
9 /// explanation of the safety guarantee.
10 unsafe fn unsafely_detach_borrow(&'a self) -> &'b T;
11}
12
13impl<'a, 'b, T> UnsafelyDetachBorrow<'a, 'b, T> for T {
14 unsafe fn unsafely_detach_borrow(&'a self) -> &'b T {
15 mem::transmute(self)
16 }
17}
18
19/// Trait for unsafely detaching a mutable borrow, attaching a new lifetime to it.
20pub trait UnsafelyDetachBorrowMut<'a, 'b, T> {
21 /// Unsafely detaches a mutable borrow, attaching a new lifetime.
22 ///
23 /// This method primarily allows a borrow to be used alongside other mutable borrows of the
24 /// same lifetime. It should not be used lightly and it should likely only be used alongside an
25 /// explanation of the safety guarantee.
26 unsafe fn unsafely_detach_borrow_mut(&'a mut self) -> &'b mut T;
27}
28
29impl<'a, 'b, T> UnsafelyDetachBorrowMut<'a, 'b, T> for T {
30 unsafe fn unsafely_detach_borrow_mut(&'a mut self) -> &'b mut T {
31 mem::transmute(self)
32 }
33}