uninit-read 0.1.1

A marker trait and utilities for safe, high-performance reads into uninitialized buffers.
Documentation
pub(crate) mod is_uninit_read {
    use crate::UninitRead;
    use std::cell::Cell;
    use std::marker::PhantomData;

    /// Checks at runtime if a type implements the [`UninitRead`] trait.
    ///
    /// This function allows for dynamic dispatch based on whether a reader can be
    /// safely used with uninitialized buffers.
    ///
    /// The check is performed using a compile-time trick that is evaluated when the
    /// function is instantiated for a given type `T`, but it can be called like any
    /// normal function. It has a small runtime cost.
    pub fn is_uninit_read<T>() -> bool {
        struct IsUninitRead<'a, T> {
            is_uninit_read: &'a Cell<bool>,
            _marker: PhantomData<T>,
        }

        impl<T> Clone for IsUninitRead<'_, T> {
            fn clone(&self) -> Self {
                self.is_uninit_read.set(false);
                IsUninitRead {
                    is_uninit_read: self.is_uninit_read,
                    _marker: PhantomData,
                }
            }
        }
        impl<T: UninitRead> Copy for IsUninitRead<'_, T> {}

        let is_uninit_read = Cell::new(true);
        let _ = [IsUninitRead::<T> {
            is_uninit_read: &is_uninit_read,
            _marker: PhantomData,
        }]
        .clone();
        is_uninit_read.get()
    }
}