Skip to main content

into_ref/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3
4use core::{
5    ops::Deref,
6    pin::Pin,
7};
8
9/// Consume self into reference
10pub trait IntoRef<'a>: 'a {
11    type Out: ?Sized;
12
13    /// Consume self into reference
14    fn into_ref(self) -> &'a Self::Out;
15}
16
17/// Consume self into mutable reference
18pub trait IntoRefMut<'a>: IntoRef<'a> {
19    /// Consume self into mutable reference
20    fn into_ref_mut(self) -> &'a mut Self::Out;
21}
22impl<'a, T: ?Sized> IntoRef<'a> for &'a T {
23    type Out = T;
24
25    fn into_ref(self) -> &'a Self::Out {
26        self
27    }
28}
29impl<'a, T: ?Sized> IntoRef<'a> for &'a mut T {
30    type Out = T;
31
32    fn into_ref(self) -> &'a Self::Out {
33        self
34    }
35}
36impl<'a, T: ?Sized> IntoRefMut<'a> for &'a mut T {
37    fn into_ref_mut(self) -> &'a mut Self::Out {
38        self
39    }
40}
41impl<'a, P> IntoRef<'a> for Pin<P>
42where P: Deref + IntoRef<'a>,
43      P::Target: Unpin,
44{
45    type Out = P::Out;
46
47    fn into_ref(self) -> &'a Self::Out {
48        Pin::into_inner(self).into_ref()
49    }
50}
51impl<'a, P> IntoRefMut<'a> for Pin<P>
52where P: Deref + IntoRefMut<'a>,
53      P::Target: Unpin,
54{
55    fn into_ref_mut(self) -> &'a mut Self::Out {
56        Pin::into_inner(self).into_ref_mut()
57    }
58}