1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::{Soapy, WithRef};
use std::{
    fmt::Debug,
    hash::{Hash, Hasher},
    ops::{Deref, DerefMut},
};

/// An immutable reference to an element of a [`Soa`].
///
/// This is similar to `&T` for an element of a `&[T]`. However, since the
/// fields are stored separately for SoA, we need a different type that has
/// references to each of fields for the element. This is a convenience wrapper
/// over [`Soapy::Ref`], which is implemented for each type that derives
/// [`Soapy`]. It uses [`WithRef`] to provide implementations of common standard
/// library traits with less codegen and ceremony than doing the same in the
/// macro.
///
/// [`Soa`]: crate::Soa
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Ref<'a, T>(pub(crate) T::Ref<'a>)
where
    T: 'a + Soapy;

/// A mutable reference to an element of a [`Soa`].
///
/// This is similar to `&mut T` for an element of a `&mut [T]`. See [`Ref`] for
/// more details.
///
/// [`Soa`]: crate::Soa
#[repr(transparent)]
pub struct RefMut<'a, T>(pub(crate) T::RefMut<'a>)
where
    T: 'a + Soapy;

impl<'a, T> Deref for Ref<'a, T>
where
    T: Soapy,
{
    type Target = T::Ref<'a>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a, T> Deref for RefMut<'a, T>
where
    T: Soapy,
{
    type Target = T::RefMut<'a>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a, T> DerefMut for RefMut<'a, T>
where
    T: Soapy,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a, T> AsRef<T::Ref<'a>> for Ref<'a, T>
where
    T: Soapy,
{
    fn as_ref(&self) -> &T::Ref<'a> {
        &self.0
    }
}

impl<'a, T> AsRef<T::RefMut<'a>> for RefMut<'a, T>
where
    T: Soapy,
{
    fn as_ref(&self) -> &T::RefMut<'a> {
        &self.0
    }
}

impl<'a, T> AsMut<T::RefMut<'a>> for RefMut<'a, T>
where
    T: Soapy,
{
    fn as_mut(&mut self) -> &mut T::RefMut<'a> {
        &mut self.0
    }
}

macro_rules! ref_impls {
    ($t:ident) => {
        impl<'a, T> WithRef for $t<'a, T>
        where
            T: Soapy,
        {
            type Item = T;

            fn with_ref<F, U>(&self, f: F) -> U
            where
                F: FnOnce(&Self::Item) -> U,
            {
                self.0.with_ref(f)
            }
        }

        impl<'a, T> Debug for $t<'a, T>
        where
            T: Soapy + Debug,
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.with_ref(|me| me.fmt(f))
            }
        }

        impl<'a, T, U, R> PartialEq<R> for $t<'a, T>
        where
            T: Soapy + PartialEq<U>,
            R: WithRef<Item = U>,
        {
            fn eq(&self, other: &R) -> bool {
                self.with_ref(|me| other.with_ref(|them| me.eq(them)))
            }
        }

        impl<'a, T> Eq for $t<'a, T> where T: 'a + Soapy + Eq {}

        impl<'a, T, U, R> PartialOrd<R> for $t<'a, T>
        where
            T: Soapy + PartialOrd<U>,
            R: WithRef<Item = U>,
        {
            fn partial_cmp(&self, other: &R) -> Option<std::cmp::Ordering> {
                self.with_ref(|me| other.with_ref(|them| me.partial_cmp(them)))
            }
        }

        impl<'a, T> Ord for $t<'a, T>
        where
            T: Soapy + Ord,
        {
            fn cmp(&self, other: &Self) -> std::cmp::Ordering {
                self.with_ref(|me| other.with_ref(|them| me.cmp(them)))
            }
        }

        impl<'a, T> Hash for $t<'a, T>
        where
            T: Soapy + Hash,
        {
            fn hash<H: Hasher>(&self, state: &mut H) {
                self.with_ref(|me| me.hash(state))
            }
        }
    };
}

ref_impls!(Ref);
ref_impls!(RefMut);