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
use std::{marker::PhantomData, ops::Deref};

/// A non-ref counted wrapper that's valid for the lifetime of a parent struct
pub struct BorrowedFrom<'a, T: 'a> {
    inner: T,
    phantom: PhantomData<&'a T>,
}

impl<'a, T: 'a> BorrowedFrom<'a, T> {
    /// Creates a new borrowed-from wrapper
    ///
    /// # Safety
    ///
    /// The pointer must not be mutated whilst this wrapper exists.
    pub unsafe fn new(inner: T) -> Self {
        Self {
            inner,
            phantom: PhantomData,
        }
    }
}

impl<'a, T: 'a> Deref for BorrowedFrom<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}