Skip to content

Commit 8df5747

Browse files
authored
hybrid-array: use handwritten instead of derived impls (#940)
Uses handwritten impls for the following traits on the `Array` type: - Debug - Eq - Hash - PartialEq - PartialOrd - Ord The handwritten impls have looser bounds.
1 parent 0726442 commit 8df5747

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

hybrid-array/src/lib.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub use typenum::consts;
2929
use core::{
3030
array::{IntoIter, TryFromSliceError},
3131
borrow::{Borrow, BorrowMut},
32+
cmp::Ordering,
33+
fmt::{self, Debug},
34+
hash::{Hash, Hasher},
3235
ops::{Deref, DerefMut, Index, IndexMut, Range},
3336
slice::{Iter, IterMut},
3437
};
@@ -38,7 +41,6 @@ use typenum::Unsigned;
3841
///
3942
/// Provides the flexibility of typenum-based expressions while also
4043
/// allowing interoperability and a transition path to const generics.
41-
#[derive(Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
4244
#[repr(transparent)]
4345
pub struct Array<T, U: ArraySize>(pub U::ArrayType<T>);
4446

@@ -188,6 +190,17 @@ where
188190
{
189191
}
190192

193+
impl<T, U> Debug for Array<T, U>
194+
where
195+
T: Debug,
196+
U: ArraySize,
197+
U::ArrayType<T>: Debug,
198+
{
199+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200+
f.debug_tuple("Array").field(&self.0).finish()
201+
}
202+
}
203+
191204
impl<T, U> Default for Array<T, U>
192205
where
193206
T: Default,
@@ -220,6 +233,13 @@ where
220233
}
221234
}
222235

236+
impl<T, U> Eq for Array<T, U>
237+
where
238+
T: Eq,
239+
U: ArraySize,
240+
{
241+
}
242+
223243
impl<T, U, const N: usize> From<[T; N]> for Array<T, U>
224244
where
225245
Self: ArrayOps<T, N>,
@@ -253,6 +273,17 @@ where
253273
}
254274
}
255275

276+
impl<T, U> Hash for Array<T, U>
277+
where
278+
T: Hash,
279+
U: ArraySize,
280+
{
281+
#[inline]
282+
fn hash<H: Hasher>(&self, state: &mut H) {
283+
self.0.as_ref().hash(state);
284+
}
285+
}
286+
256287
impl<T, I, U> Index<I> for Array<T, U>
257288
where
258289
[T]: Index<I>,
@@ -277,6 +308,36 @@ where
277308
}
278309
}
279310

311+
impl<T, U> PartialEq for Array<T, U>
312+
where
313+
T: PartialEq,
314+
U: ArraySize,
315+
{
316+
fn eq(&self, other: &Self) -> bool {
317+
self.0.as_ref().eq(other.0.as_ref())
318+
}
319+
}
320+
321+
impl<T, U> PartialOrd for Array<T, U>
322+
where
323+
T: PartialOrd,
324+
U: ArraySize,
325+
{
326+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
327+
self.0.as_ref().partial_cmp(other.0.as_ref())
328+
}
329+
}
330+
331+
impl<T, U> Ord for Array<T, U>
332+
where
333+
T: Ord,
334+
U: ArraySize,
335+
{
336+
fn cmp(&self, other: &Self) -> Ordering {
337+
self.0.as_ref().cmp(other.0.as_ref())
338+
}
339+
}
340+
280341
impl<'a, T, U> TryFrom<&'a [T]> for Array<T, U>
281342
where
282343
T: Copy,

0 commit comments

Comments
 (0)