oxi_types/
non_owning.rs

1use std::marker::PhantomData;
2use std::mem::ManuallyDrop;
3
4/// A non-owning value for lifetime `'a`.
5///
6/// Used for FFI functions that accept data by value, but don't destroy or move
7/// out of it. This is guaranteed to have the same layout as `T`.
8#[repr(transparent)]
9pub struct NonOwning<'a, T> {
10    inner: ManuallyDrop<T>,
11    lt: PhantomData<&'a ()>,
12}
13
14impl<'a, T> NonOwning<'a, T> {
15    pub const fn new(value: T) -> Self {
16        Self { inner: ManuallyDrop::new(value), lt: PhantomData }
17    }
18}
19
20impl<'a, T> core::fmt::Debug for NonOwning<'a, T>
21where
22    T: core::fmt::Debug,
23{
24    #[inline]
25    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
26        self.inner.fmt(f)
27    }
28}
29
30impl<'a, T> Default for NonOwning<'a, T>
31where
32    T: Default,
33{
34    #[inline]
35    fn default() -> Self {
36        Self { inner: ManuallyDrop::new(T::default()), lt: PhantomData }
37    }
38}