use std::pin::Pin;
use std::marker::{Unpin, PhantomData};
use std::ops::Deref;
use std::fmt::{Debug, Display, Formatter};
pub struct Owner<'a, T: Unpin> {
data: Pin<Box<T>>,
_phantom: PhantomData<&'a ()>
}
impl<'a, T: Unpin> Owner<'a, T> {
pub unsafe fn new(data: T) -> Self {
Self {
data: Pin::new(Box::new(data)),
_phantom: PhantomData::default()
}
}
pub fn as_ref<'b>(&'b self) -> &'a T {
unsafe {
std::mem::transmute::<&'b T, &'a T>(self.data.deref())
}
}
}
impl<T: Unpin + Debug> Debug for Owner<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", *self.data)
}
}
impl<T: Unpin + Display> Display for Owner<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", *self.data)
}
}