mod as_usage;
pub use as_usage::*;
use std::{
borrow::{Borrow, BorrowMut},
marker::PhantomData,
ops::{Deref, DerefMut},
};
pub struct Usage<U, T> {
pub data: T,
_phantom: PhantomData<U>,
}
impl<U, T> std::fmt::Debug for Usage<U, T>
where
T: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Usage")
.field("data", &self.data)
.field(
"_phantom",
&format!("PhantomData<{}>", std::any::type_name::<U>()),
)
.finish()
}
}
impl<U, T> Default for Usage<U, T>
where
T: Default,
{
fn default() -> Self {
Usage {
data: Default::default(),
_phantom: Default::default(),
}
}
}
impl<U, T> Copy for Usage<U, T> where T: Copy {}
impl<U, T> Clone for Usage<U, T>
where
T: Clone,
{
fn clone(&self) -> Self {
Usage {
data: self.data.clone(),
_phantom: Default::default(),
}
}
}
impl<U, T> PartialEq for Usage<U, T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.data.eq(&other.data)
}
}
impl<U, T> Eq for Usage<U, T>
where
T: Eq,
{
fn assert_receiver_is_total_eq(&self) {
self.data.assert_receiver_is_total_eq()
}
}
impl<U, T> PartialOrd for Usage<U, T>
where
T: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.data.partial_cmp(&other.data)
}
}
impl<U, T> Ord for Usage<U, T>
where
T: Ord,
{
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.data.cmp(&other.data)
}
}
impl<U, T> std::hash::Hash for Usage<U, T>
where
T: std::hash::Hash,
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.data.hash(state)
}
}
impl<U, T> From<T> for Usage<U, T> {
fn from(t: T) -> Self {
U::as_usage(t)
}
}
impl<U, T, V> FromIterator<V> for Usage<U, T>
where
T: FromIterator<V>,
{
fn from_iter<I: IntoIterator<Item = V>>(iter: I) -> Self {
U::as_usage(iter.into_iter().collect())
}
}
#[cfg(feature = "rayon")]
mod rayon_impl {
use super::*;
use rayon::iter::{
FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator,
};
impl<U, T, V> FromParallelIterator<V> for Usage<U, T>
where
T: FromParallelIterator<V>,
V: Send,
{
fn from_par_iter<I: rayon::iter::IntoParallelIterator<Item = V>>(par_iter: I) -> Self {
U::as_usage(par_iter.into_par_iter().collect())
}
}
impl<U, T, V> ParallelExtend<V> for Usage<U, T>
where
T: ParallelExtend<V>,
V: Send,
{
fn par_extend<I: IntoParallelIterator<Item = V>>(&mut self, par_iter: I) {
self.data.par_extend(par_iter)
}
}
}
#[cfg(feature = "bytemuck")]
mod bytemuck_impl {
use super::*;
use bytemuck::{Pod, Zeroable};
unsafe impl<U, T> Zeroable for Usage<U, T> where T: Zeroable {}
unsafe impl<U, T> Pod for Usage<U, T>
where
U: 'static,
T: Pod,
{
}
}
impl<U, T> Borrow<T> for Usage<U, T> {
fn borrow(&self) -> &T {
&self.data
}
}
impl<U, T> BorrowMut<T> for Usage<U, T> {
fn borrow_mut(&mut self) -> &mut T {
&mut self.data
}
}
impl<U, T> Deref for Usage<U, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<U, T> DerefMut for Usage<U, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl<U, T> Usage<U, T> {
pub fn into_inner(self) -> T {
self.data
}
}