loaned/loanable.rs
1use crate::*;
2
3/// The trait for types that can be used with [`Loaned::loan`] and
4/// [`LoanedMut::loan`].
5///
6/// # Safety
7///
8/// This type must ensure that the reference returned by [`Deref`] (and
9/// [`DerefMut`], if implemented) is valid for `'t`, as long as `self` is not
10/// used for the remainder of `'t` (though it may be moved).
11///
12/// In particular, this can't be implemented for types like `Cow`, as it may
13/// return a reference to data within `self` (which would be invalidated when
14/// `self` is moved).
15///
16/// This is closely related to whether the type can unconditionally implement
17/// [`Unpin`] (i.e. even when `Self::Target: !Unpin`).
18pub unsafe trait Loanable<'t>: Deref {}
19
20#[cfg(feature = "alloc")]
21mod _alloc {
22 use crate::*;
23 unsafe impl<'t, T: ?Sized> Loanable<'t> for alloc::boxed::Box<T> {}
24 unsafe impl<'t, T> Loanable<'t> for alloc::vec::Vec<T> {}
25 unsafe impl<'t> Loanable<'t> for alloc::string::String {}
26 unsafe impl<'t, T: ?Sized> Loanable<'t> for alloc::rc::Rc<T> {}
27 unsafe impl<'t, T: ?Sized> Loanable<'t> for alloc::sync::Arc<T> {}
28}
29
30// The usefulness of this implementation is dubious at best, but it's here for completeness.
31unsafe impl<'t, 'a: 't, T: ?Sized> Loanable<'t> for &'a T {}
32unsafe impl<'t, 'a: 't, T: ?Sized> Loanable<'t> for &'a mut T {}