use rkyv::util::AlignedVec;

pub type RkyvError = rkyv::rancor::Error;

pub type RkyvSer<'s> =
    rkyv::api::high::HighSerializer<AlignedVec, rkyv::ser::allocator::ArenaHandle<'s>, RkyvError>;

pub trait RkyvToBytes: rkyv::Archive + for<'s> rkyv::Serialize<RkyvSer<'s>> {}
impl<T> RkyvToBytes for T where T: rkyv::Archive + for<'s> rkyv::Serialize<RkyvSer<'s>> {}

pub type RkyvDe<E> = rkyv::api::high::HighDeserializer<E>;

pub trait RkyvFromBytes: rkyv::Archive + Sized {
    fn from_bytes_checked(bytes: &[u8]) -> Result<Self, RkyvError>;
}

impl<T> RkyvFromBytes for T
where
    T: rkyv::Archive,
    T::Archived: for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>
        + rkyv::Deserialize<T, RkyvDe<RkyvError>>,
{
    fn from_bytes_checked(bytes: &[u8]) -> Result<Self, RkyvError> {
        rkyv::api::high::from_bytes::<Self, RkyvError>(bytes)
    }
}
