expiring_ref 0.7.3

A crate designed to implement a mechanism for owning values, via a destructively-moved equivalent to C++'s xvalues/`T&&`
Documentation
/// Experimental features for this crate.
#[cfg(feature = "unstable_out")]
pub mod out {
	use crate::Own;
	use core::mem::{MaybeUninit, transmute_prefix};
	use core::ptr::NonNull;

	/// For the duration of this value's lifetime, it points to at least partially uninitialized data. However, the location it points to can be considered safely initialized when it is dropped, as dropping without writing to it instantly aborts.
	#[repr(transparent)]
	pub struct Out<'a, T> {
		inner: &'a mut MaybeUninit<T>
	}

	const impl<'a, T> Out<'a, T> {
		fn write(self, val: Own<T>) -> &'a mut T {
			let src: NonNull<T> = unsafe { transmute_prefix(val) };
			let dst = unsafe { NonNull::new_unchecked(self.inner.as_mut_ptr()) };

			unsafe { src.copy_to_nonoverlapping(dst, 1); }
			unsafe { transmute_prefix(self) }
		}

		/// SAFETY: [`self`] must be initialized.
		unsafe fn assume_initialized(self) -> &'a mut T {
			// SAFETY: checks ensured by caller
			unsafe { transmute_prefix(self) }
		}
	}

	impl<'a, T> Drop for Out<'a, T> {
		#[rustc_nounwind]
		fn drop(&mut self) {
			panic!("Out reference was dropped without initialization!")
		}
	}

	#[macro_export]
	macro_rules! let_out {
		(let out $p:pat_param) => {{
			super let __OUT_UNINIT = ::core::mem::MaybeUninit::uninit();
			&mut __OUT_UNINIT
		}};
	}

	pub trait Relocate {
		fn relocate(self: Own<Self>, dst: Out<Self>)
			where
				Self: Sized;
	}

	impl<T> Relocate for T {
		#[inline(always)]
		default fn relocate(self: Own<Self>, dst: Out<Self>) {
			dst.write(self);
		}
	}
}

#[cfg(feature = "unstable_dyn_example")]
/// Proof of concept for an expansion of `dyn Trait` to sorta exist for types with a by-value `self` parameter
pub mod dyn_example {
	use crate::Own;
	use crate::traits::DerefMove;
	use core::marker::PhantomData;
	use core::mem::transmute_prefix;
	use core::ptr::NonNull;

	pub struct Output(usize);

	pub trait X {
		fn do_stuff(self) -> Output;
	}

	struct DynXMeta {
		size: usize,
		do_stuff: unsafe fn(NonNull<()>) -> Output,
	}

	const fn dyn_x_meta<'a, T: X + 'a>() -> &'a DynXMeta {
		&const {
			DynXMeta {
				size: size_of::<T>(),
				do_stuff: unsafe {
					transmute_prefix(do_stuff_through_own_ref::<T> as fn(Own<'a, T>) -> Output)
				}
			}
		}
	}

	fn do_stuff_through_own_ref<T: X>(this: Own<T>) -> Output {
		this.deref_move().do_stuff()
	}

	pub struct OwnDynXPointee<'a> {
		meta: &'a DynXMeta,
		ptr: NonNull<()>,
		phantom: PhantomData<Own<'a, ()>>
	}

	impl<'a> X for OwnDynXPointee<'a> {
		fn do_stuff(self) -> Output {
			unsafe { (self.meta.do_stuff)(self.ptr) }
		}
	}

	impl<'a> OwnDynXPointee<'a> {
		pub const fn of<T: X>(ptr: Own<'a, T>) -> Self {
			Self {
				meta: const { dyn_x_meta::<T>() },
				ptr: unsafe { transmute_prefix(ptr) },
				phantom: PhantomData
			}
		}
	}
}