1use std::{
2 collections::hash_map,
3 fmt,
4 ops::{Deref, DerefMut},
5};
6
7use crate::Id;
8
9pub struct Row<'a, T> {
11 pub id: Id<T>,
13 pub data: &'a T,
15}
16
17impl<'a, T> Row<'a, T>
18where
19 T: Clone,
20{
21 pub fn cloned(self) -> OwnedRow<T> {
23 OwnedRow {
24 id: self.id,
25 data: self.data.clone(),
26 }
27 }
28}
29
30impl<'a, T> Clone for Row<'a, T> {
31 fn clone(&self) -> Self {
32 Row {
33 id: self.id,
34 data: self.data,
35 }
36 }
37}
38
39impl<'a, T> Copy for Row<'a, T> {}
40
41impl<'a, T, U> PartialEq<Row<'a, U>> for Row<'a, T>
42where
43 T: PartialEq<U>,
44{
45 fn eq(&self, other: &Row<U>) -> bool {
46 self.data.eq(&other.data)
47 }
48}
49
50impl<'a, T> Eq for Row<'a, T> where T: Eq {}
51
52impl<'a, T, U> PartialEq<RowMut<'a, U>> for Row<'a, T>
53where
54 T: PartialEq<U>,
55{
56 fn eq(&self, other: &RowMut<U>) -> bool {
57 self.data.eq(other.data)
58 }
59}
60
61impl<'a, T> Deref for Row<'a, T> {
62 type Target = T;
63 fn deref(&self) -> &Self::Target {
64 self.data
65 }
66}
67
68impl<'a, T> AsRef<T> for Row<'a, T> {
69 fn as_ref(&self) -> &T {
70 self.data
71 }
72}
73
74impl<'a, T> fmt::Debug for Row<'a, T>
75where
76 T: fmt::Debug,
77{
78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79 if f.alternate() {
80 write!(f, "{}: {:#?}", self.id, self.data)
81 } else {
82 write!(f, "{}: {:?}", self.id, self.data)
83 }
84 }
85}
86
87impl<'a, T> fmt::Display for Row<'a, T>
88where
89 T: fmt::Display,
90{
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92 <T as fmt::Display>::fmt(&self.data, f)
93 }
94}
95
96pub struct RowMut<'a, T> {
98 pub id: Id<T>,
100 pub data: &'a mut T,
102}
103
104impl<'a, T> RowMut<'a, T>
105where
106 T: Clone,
107{
108 pub fn cloned(self) -> OwnedRow<T> {
110 OwnedRow {
111 id: self.id,
112 data: self.data.clone(),
113 }
114 }
115}
116
117impl<'a, T, U> PartialEq<RowMut<'a, U>> for RowMut<'a, T>
118where
119 T: PartialEq<U>,
120{
121 fn eq(&self, other: &RowMut<U>) -> bool {
122 self.data.eq(&other.data)
123 }
124}
125
126impl<'a, T> Eq for RowMut<'a, T> where T: Eq {}
127
128impl<'a, T, U> PartialEq<Row<'a, U>> for RowMut<'a, T>
129where
130 T: PartialEq<U>,
131{
132 fn eq(&self, other: &Row<U>) -> bool {
133 (self.data as &T).eq(other.data)
134 }
135}
136
137impl<'a, T> Deref for RowMut<'a, T> {
138 type Target = T;
139 fn deref(&self) -> &Self::Target {
140 self.data
141 }
142}
143
144impl<'a, T> DerefMut for RowMut<'a, T> {
145 fn deref_mut(&mut self) -> &mut Self::Target {
146 self.data
147 }
148}
149
150impl<'a, T> AsRef<T> for RowMut<'a, T> {
151 fn as_ref(&self) -> &T {
152 self.data
153 }
154}
155
156impl<'a, T> AsMut<T> for RowMut<'a, T> {
157 fn as_mut(&mut self) -> &mut T {
158 self.data
159 }
160}
161
162impl<'a, T> fmt::Debug for RowMut<'a, T>
163where
164 T: fmt::Debug,
165{
166 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
167 if f.alternate() {
168 write!(f, "{}: {:#?}", self.id, self.data)
169 } else {
170 write!(f, "{}: {:?}", self.id, self.data)
171 }
172 }
173}
174
175impl<'a, T> fmt::Display for RowMut<'a, T>
176where
177 T: fmt::Display,
178{
179 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180 <T as fmt::Display>::fmt(&self.data, f)
181 }
182}
183
184pub struct OwnedRow<T> {
186 pub id: Id<T>,
188 pub data: T,
190}
191
192impl<T, U> PartialEq<OwnedRow<U>> for OwnedRow<T>
193where
194 T: PartialEq<U>,
195{
196 fn eq(&self, other: &OwnedRow<U>) -> bool {
197 self.data.eq(&other.data)
198 }
199}
200
201impl<T> Eq for OwnedRow<T> where T: Eq {}
202
203impl<'a, T, U> PartialEq<Row<'a, U>> for OwnedRow<T>
204where
205 T: PartialEq<U>,
206{
207 fn eq(&self, other: &Row<U>) -> bool {
208 self.data.eq(other.data)
209 }
210}
211
212impl<'a, T, U> PartialEq<RowMut<'a, U>> for OwnedRow<T>
213where
214 T: PartialEq<U>,
215{
216 fn eq(&self, other: &RowMut<U>) -> bool {
217 self.data.eq(other.data)
218 }
219}
220
221impl<T> Deref for OwnedRow<T> {
222 type Target = T;
223 fn deref(&self) -> &Self::Target {
224 &self.data
225 }
226}
227
228impl<T> DerefMut for OwnedRow<T> {
229 fn deref_mut(&mut self) -> &mut Self::Target {
230 &mut self.data
231 }
232}
233
234impl<T> AsRef<T> for OwnedRow<T> {
235 fn as_ref(&self) -> &T {
236 &self.data
237 }
238}
239
240impl<T> AsMut<T> for OwnedRow<T> {
241 fn as_mut(&mut self) -> &mut T {
242 &mut self.data
243 }
244}
245
246impl<T> fmt::Debug for OwnedRow<T>
247where
248 T: fmt::Debug,
249{
250 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
251 if f.alternate() {
252 write!(f, "{}: {:#?}", self.id, self.data)
253 } else {
254 write!(f, "{}: {:?}", self.id, self.data)
255 }
256 }
257}
258
259impl<T> fmt::Display for OwnedRow<T>
260where
261 T: fmt::Display,
262{
263 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
264 <T as fmt::Display>::fmt(&self.data, f)
265 }
266}
267
268#[derive(Debug)]
270pub struct RowIter<'a, T> {
271 pub(crate) inner: hash_map::Iter<'a, Id<T>, T>,
272}
273
274impl<'a, T> Iterator for RowIter<'a, T> {
275 type Item = Row<'a, T>;
276 fn next(&mut self) -> Option<Self::Item> {
277 self.inner.next().map(|(id, data)| Row { id: *id, data })
278 }
279}
280
281impl<'a, T> Clone for RowIter<'a, T> {
282 fn clone(&self) -> Self {
283 RowIter {
284 inner: self.inner.clone(),
285 }
286 }
287}
288
289#[derive(Debug)]
291pub struct RowIterMut<'a, T> {
292 pub(crate) inner: hash_map::IterMut<'a, Id<T>, T>,
293}
294
295impl<'a, T> Iterator for RowIterMut<'a, T> {
296 type Item = RowMut<'a, T>;
297 fn next(&mut self) -> Option<Self::Item> {
298 self.inner.next().map(|(id, data)| RowMut { id: *id, data })
299 }
300}
301
302pub trait Idd {
304 type RowType;
306 fn id(&self) -> Id<Self::RowType>;
308}
309
310impl<T> Idd for Id<T> {
311 type RowType = T;
312 fn id(&self) -> Id<Self::RowType> {
313 *self
314 }
315}
316
317impl<'a, T> Idd for Row<'a, T> {
318 type RowType = T;
319 fn id(&self) -> Id<Self::RowType> {
320 self.id
321 }
322}
323
324impl<'a, T> Idd for RowMut<'a, T> {
325 type RowType = T;
326 fn id(&self) -> Id<Self::RowType> {
327 self.id
328 }
329}