uord 0.1.0

An implementation of unordered pairs (or more generally, unordered sets)
Documentation
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(
  absolute_paths_not_starting_with_crate,
  redundant_imports,
  redundant_lifetimes,
  future_incompatible,
  deprecated_in_future,
  missing_copy_implementations,
  missing_debug_implementations,
  missing_docs,
  unnameable_types,
  unreachable_pub
)]

//! A library providing implementations of unordered pairs (or more generally, unordered sets).
//!
//! This is useful when, for example, you want to create a HashMap that associates data with pairs of things:
//! ```rust
//! # use uord::UOrd2;
//! # use std::collections::HashMap;
//! let mut map: HashMap<UOrd2<u16>, String> = HashMap::new();
//! map.insert(UOrd2::new([1, 6]), "1-6".to_owned());
//! map.insert(UOrd2::new([3, 5]), "3-5".to_owned());
//! map.insert(UOrd2::new([2, 4]), "2-4".to_owned());
//!
//! assert!(map.contains_key(&UOrd2::new([1, 6])));
//! ```
//!
//! When creating a [`UOrd`], the ordering of the items on creation does not matter,
//! and [`UOrd`]s created with different initial element orders will be equal to one another.

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
#[cfg(feature = "serde")]
extern crate serde;

#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
extern crate alloc;

mod iter;
pub mod proxy;
mod transmute;

pub use crate::iter::*;
use crate::proxy::{Proxy, ProxyWrapper};

use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};

#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use crate::tuple::*;

mod tuple {
  #[allow(unreachable_pub)]
  pub type Tuple<T, const N: usize> = <[T; N] as IntoTuple>::Tuple;

  #[allow(unnameable_types)]
  pub trait IntoTuple: Sized {
    type Tuple: From<Self>;
  }
}



/// An unordered tuple of items of type `T` and length 2.
pub type UOrd2<T> = UOrd<T, 2>;
/// An unordered tuple of items of type `T` and length 3.
pub type UOrd3<T> = UOrd<T, 3>;
/// An unordered tuple of items of type `T` and length 4.
pub type UOrd4<T> = UOrd<T, 4>;
/// An unordered tuple of items of type `T` and length 5.
pub type UOrd5<T> = UOrd<T, 5>;
/// An unordered tuple of items of type `T` and length 6.
pub type UOrd6<T> = UOrd<T, 6>;

/// An unordered tuple of items of type `T` and length `N`.
///
/// [`UOrd`] is implemented such that the order of elements on creation does not matter,
/// and the [`UOrd`]s created from different ordered lists will behave similarly in
/// [`PartialEq`], [`Ord`], or [`Hash`].
///
/// [`UOrd`]'s implementation maintains a sorted list of values that is not allowed
/// to be mutated. Using interior mutability to mutate elements of a [`UOrd`] is a logic error
/// and is not supported. Doing so is very likely to cause strange behavior.
#[repr(transparent)]
pub struct UOrd<T, const N: usize> {
  values: [T; N]
}

impl<T, const N: usize> UOrd<T, N> where T: Ord {
  /// Create a new [`UOrd`] from an array of values.
  pub fn new(mut values: [T; N]) -> Self {
    values.sort_unstable();
    UOrd { values }
  }

  /// Tests whether this [`UOrd`] contains the given value.
  pub fn contains<Q>(&self, x: &Q) -> bool
  where T: Borrow<Q>, Q: Eq + ?Sized {
    self.test_any(|value| value.borrow() == x)
  }

  /// Replaces any occurrence of `from` with `to`, creating a new [`UOrd`].
  pub fn replace<Q>(&self, from: &Q, to: &T) -> Self
  where T: Borrow<Q> + Clone, Q: Eq + ?Sized {
    self.map_each_ref(|value| {
      if value.borrow() == from {
        to.clone()
      } else {
        value.clone()
      }
    })
  }

  /// Applies the function `f` to each element of this [`UOrd`], returning a new [`UOrd`].
  pub fn map<U>(self, f: impl FnMut(T) -> U) -> UOrd<U, N> where U: Ord {
    UOrd::new(self.into_array().map(f))
  }

  /// Applies the function `f` to a reference to each element of this [`UOrd`], returning a new [`UOrd`].
  pub fn map_each_ref<U>(&self, f: impl FnMut(&T) -> U) -> UOrd<U, N> where U: Ord {
    UOrd::new(self.as_ref_array().map(f))
  }

  /// Applies a fallible function `f` to each element of this [`UOrd`], returning a value of type `Option<UOrd<U, N>>`.
  /// The provided function takes a `T` and must return a value of type `Option<U>`.
  ///
  /// This function is temporary, and will be deprecated when `try_trait_v2` is stabilized.
  #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
  #[cfg(feature = "alloc")]
  pub fn try_map_opt<U>(self, f: impl FnMut(T) -> Option<U>) -> Option<UOrd<U, N>> where U: Ord {
    self.into_array().into_iter().map(f).collect::<Option<Vec<U>>>()
      .map(|list| <[U; N]>::try_from(list).unwrap_or_else(|_| unreachable!()))
      .map(UOrd::new)
  }

  /// Applies a fallible function `f` to each element of this [`UOrd`], returning a value of type `Result<UOrd<U, N>, E>`.
  /// The provided function takes a `T` and must return a value of type `Return<U, E>`.
  ///
  /// This function is temporary, and will be deprecated when `try_trait_v2` is stabilized.
  #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
  #[cfg(feature = "alloc")]
  pub fn try_map_res<U, E>(self, f: impl FnMut(T) -> Result<U, E>) -> Result<UOrd<U, N>, E> where U: Ord {
    self.into_array().into_iter().map(f).collect::<Result<Vec<U>, E>>()
      .map(|list| <[U; N]>::try_from(list).unwrap_or_else(|_| unreachable!()))
      .map(UOrd::new)
  }

  /// Converts each element of this [`UOrd`] into a given type `U`
  /// based on `T`'s `Into<U>` implementation.
  pub fn convert<U>(self) -> UOrd<U, N>
  where T: Into<U>, U: Ord {
    self.map(T::into)
  }

  /// Fallibly converts each element of this [`UOrd`] into a given type `U`
  /// based on `T`'s `TryInto<U>` implementation, alternatively returning error.
  #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
  #[cfg(feature = "alloc")]
  pub fn try_convert<U>(self) -> Result<UOrd<U, N>, T::Error>
  where T: TryInto<U>, U: Ord {
    self.try_map_res(T::try_into)
  }

  /// Tests if all elements pass the given predicate.
  ///
  /// Equivalent to `self.iter().all(f)`.
  pub fn test_all(&self, f: impl FnMut(&T) -> bool) -> bool {
    self.iter().all(f)
  }

  /// Tests if any element passes the given predicate.
  ///
  /// Equivalent to `self.iter().any(f)`.
  pub fn test_any(&self, f: impl FnMut(&T) -> bool) -> bool {
    self.iter().any(f)
  }

  /// Converts this [`UOrd`] into a [`UOrdProxied`] by wrapping its contained elements of type `T`
  /// in [`ProxyWrapper`]s, using the provided [`Proxy`] `P` for comparison and equality.
  pub fn make_proxied<P: Proxy<T>>(self) -> UOrdProxied<T, N, P> {
    UOrdProxied::new_proxied(self.into_array())
  }
}

impl<T, const N: usize> UOrd<T, N> {
  /// Gets the first (smallest) element in the internal list.
  ///
  /// # Panics
  /// This function will panic if `N == 0`.
  #[doc(alias = "first")]
  pub const fn min(&self) -> &T {
    self.values.first().expect("uord length must not be 0")
  }

  /// Gets the last (greatest) element in the internal list.
  ///
  /// # Panics
  /// This function will panic if `N == 0`.
  #[doc(alias = "last")]
  pub const fn max(&self) -> &T {
    self.values.last().expect("uord length must not be 0")
  }

  /// Borrows each element in this [`UOrd`], returning a [`UOrd`] of those references.
  pub fn each_ref(&self) -> UOrd<&T, N> {
    // It is fine to directly instantiate a `UOrd` here since `T` and `&T`'s Ord
    // implementation are identical, thus sorting the list would be redundant.
    UOrd { values: self.as_ref_array() }
  }

  /// Borrows each element in this [`UOrd`], returning an array of those references.
  pub fn as_ref_array(&self) -> [&T; N] {
    self.values.each_ref()
  }

  /// Returns an immutable reference to the array of elements, which is sorted.
  #[inline]
  pub const fn as_array(&self) -> &[T; N] {
    &self.values
  }

  /// Converts this [`UOrd`] into its array of elements, which is sorted.
  pub const fn into_array(self) -> [T; N] {
    unsafe { crate::transmute::transmute::<Self, [T; N]>(self) }
  }

  /// Converts this [`UOrd`] into a tuple of length `N` similarly to [`into_array`][UOrd::into_array].
  pub fn into_tuple(self) -> Tuple<T, N> where [T; N]: IntoTuple {
    <Tuple<T, N>>::from(self.into_array())
  }

  /// Creates an iterator over references to each element of this [`UOrd`].
  #[inline]
  pub fn iter(&self) -> UOrdIter<'_, T, N> {
    self.into_iter()
  }
}

/// An unordered tuple of items of type `T` and length 2, additionally utilizing a [`Proxy`].
pub type UOrdProxied2<T, P> = UOrd2<ProxyWrapper<T, P>>;
/// An unordered tuple of items of type `T` and length 3, additionally utilizing a [`Proxy`].
pub type UOrdProxied3<T, P> = UOrd3<ProxyWrapper<T, P>>;
/// An unordered tuple of items of type `T` and length 4, additionally utilizing a [`Proxy`].
pub type UOrdProxied4<T, P> = UOrd4<ProxyWrapper<T, P>>;
/// An unordered tuple of items of type `T` and length 5, additionally utilizing a [`Proxy`].
pub type UOrdProxied5<T, P> = UOrd5<ProxyWrapper<T, P>>;
/// An unordered tuple of items of type `T` and length 6, additionally utilizing a [`Proxy`].
pub type UOrdProxied6<T, P> = UOrd6<ProxyWrapper<T, P>>;

/// An unordered tuple of items of type `T` and length `N`, additionally utilizing a [`Proxy`].
///
/// This is the same as `UOrd<ProxyWrapper<T, P>, N>` where `P` is a [`Proxy`], which customizes
/// the [`Ord`] and [`PartialEq`] implementations of [`ProxyWrapper`], modifying [`UOrd`]'s behavior.
pub type UOrdProxied<T, const N: usize, P> = UOrd<ProxyWrapper<T, P>, N>;

impl<T, const N: usize, P> UOrdProxied<T, N, P> where P: Proxy<T> {
  /// Create a new [`UOrdProxied`] from an array of values, utilizing a custom [`Proxy`] for comparison and equality.
  pub fn new_proxied(values: [T; N]) -> Self {
    UOrd::new(ProxyWrapper::wrap_array(values))
  }

  /// Tests whether this [`UOrdProxied`] contains the given value.
  pub fn contains_proxied<Q>(&self, x: &Q) -> bool
  where T: Borrow<Q>, Q: ?Sized, for<'q> P: Proxy<&'q Q> {
    self.test_any_proxied(|value| P::wrap(value.borrow()) == P::wrap(x))
  }

  /// Replaces any occurrence of `from` with `to`, creating a new [`UOrdProxied`].
  pub fn replace_proxied<Q>(&self, from: &Q, to: &T) -> Self
  where T: Borrow<Q> + Clone, Q: ?Sized, for<'q> P: Proxy<&'q Q> {
    self.map_each_ref_proxied(|value| {
      if P::wrap(value.borrow()) == P::wrap(from) {
        to.clone()
      } else {
        value.clone()
      }
    })
  }

  /// Gets the first (smallest) element in the internal list, stripped of its wrappers.
  ///
  /// # Panics
  /// This function will panic if `N == 0`.
  pub const fn min_proxied(&self) -> &T {
    ProxyWrapper::peel_ref(self.min())
  }

  /// Gets the last (greatest) element in the internal list, stripped of its wrappers.
  ///
  /// # Panics
  /// This function will panic if `N == 0`.
  pub const fn max_proxied(&self) -> &T {
    ProxyWrapper::peel_ref(self.max())
  }

  /// Borrows each element in this [`UOrdProxied`], returning a [`UOrd`] of those references, stripped of their wrappers.
  pub fn each_ref_proxied(&self) -> UOrd<&T, N> where T: Ord {
    UOrd::new(self.as_ref_array_proxied())
  }

  /// Borrows each element in this [`UOrdProxied`], returning an array of those references, stripped of their wrappers.
  pub fn as_ref_array_proxied(&self) -> [&T; N] {
    self.values.each_ref().map(ProxyWrapper::peel_ref)
  }

  /// Returns an immutable reference to the array of elements, which is sorted, and has each element stripped of their wrappers.
  pub const fn as_array_proxied(&self) -> &[T; N] {
    ProxyWrapper::peel_array_ref(self.as_array())
  }

  /// Converts this [`UOrdProxied`] into its array of elements, which is sorted, and has each element stripped of their wrappers.
  pub const fn into_array_proxied(self) -> [T; N] {
    ProxyWrapper::peel_array(self.into_array())
  }

  /// Converts this [`UOrd`] into a tuple of length `N` similarly to [`into_array`][UOrd::into_array], and has each element stripped of their wrappers..
  pub fn into_tuple_proxied(self) -> Tuple<T, N> where [T; N]: IntoTuple {
    <Tuple<T, N>>::from(self.into_array_proxied())
  }

  /// Applies the function `f` to each element of this [`UOrdProxied`], returning a new [`UOrdProxied`].
  /// This function also allows a proxy, `R`, for the new [`UOrdProxied`] to be specified.
  pub fn map_proxied<U, R>(self, f: impl FnMut(T) -> U) -> UOrdProxied<U, N, R> where R: Proxy<U> {
    UOrdProxied::new_proxied(self.into_array_proxied().map(f))
  }

  /// Applies the function `f` to a reference to each element of this [`UOrdProxied`], returning a new [`UOrdProxied`].
  /// This function also allows a proxy, `R`, for the new [`UOrdProxied`] to be specified.
  pub fn map_each_ref_proxied<U, R>(&self, f: impl FnMut(&T) -> U) -> UOrdProxied<U, N, R> where R: Proxy<U> {
    UOrdProxied::new_proxied(self.as_ref_array_proxied().map(f))
  }

  /// Applies a fallible function `f` to each element of this [`UOrdProxied`], returning a value of type `Option<UOrdProxied<U, N, R>>`.
  /// The provided function takes a `T` and must return a value of type `Option<U>`.
  /// This function also allows a proxy, `R`, for the new [`UOrdProxied`] to be specified.
  ///
  /// This function is temporary, and will be deprecated when `try_trait_v2` is stabilized.
  #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
  #[cfg(feature = "alloc")]
  pub fn try_map_proxied_opt<U, R>(self, mut f: impl FnMut(T) -> Option<U>) -> Option<UOrdProxied<U, N, R>> where R: Proxy<U> {
    self.try_map_opt(|value| f(ProxyWrapper::into_inner(value)).map(ProxyWrapper::new))
  }

  /// Applies a fallible function `f` to each element of this [`UOrdProxied`], returning a value of type `Result<UOrdProxied<U, N, R>, E>`.
  /// The provided function takes a `T` and must return a value of type `Return<U, E>`.
  /// This function also allows a proxy, `R`, for the new [`UOrdProxied`] to be specified.
  ///
  /// This function is temporary, and will be deprecated when `try_trait_v2` is stabilized.
  #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
  #[cfg(feature = "alloc")]
  pub fn try_map_proxied_res<U, R, E>(self, mut f: impl FnMut(T) -> Result<U, E>) -> Result<UOrdProxied<U, N, R>, E> where R: Proxy<U> {
    self.try_map_res(|value| f(ProxyWrapper::into_inner(value)).map(ProxyWrapper::new))
  }

  /// Converts each element of this [`UOrdProxied`] into a given type `U`
  /// based on `T`'s `Into<U>` implementation.
  pub fn convert_proxied<U, R>(self) -> UOrdProxied<U, N, R>
  where T: Into<U>, R: Proxy<U> {
    self.map_proxied(T::into)
  }

  /// Fallibly converts each element of this [`UOrdProxied`] into a given type `U`
  /// based on `T`'s `TryInto<U>` implementation, alternatively returning error.
  #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
  #[cfg(feature = "alloc")]
  pub fn try_convert_proxied<U, R>(self) -> Result<UOrdProxied<U, N, R>, T::Error>
  where T: TryInto<U>, R: Proxy<U> {
    self.try_map_proxied_res(T::try_into)
  }

  /// Tests if all elements pass the given predicate.
  ///
  /// Equivalent to `self.iter_proxied().all(f)`.
  pub fn test_all_proxied(&self, f: impl FnMut(&T) -> bool) -> bool {
    self.iter_proxied().all(f)
  }

  /// Tests if any element passes the given predicate.
  ///
  /// Equivalent to `self.iter_proxied().any(f)`.
  pub fn test_any_proxied(&self, f: impl FnMut(&T) -> bool) -> bool {
    self.iter_proxied().any(f)
  }

  /// Converts this [`UOrdProxied`] into a [`UOrd`], unwrapping the contained
  /// [`ProxyWrapper`]s, using the default implementation of [`Ord`] on `T` for comparison and equality.
  pub fn make_unproxied(self) -> UOrd<T, N> where T: Ord {
    UOrd::new(self.into_array_proxied())
  }

  /// Creates an iterator over references to each element of this [`UOrdProxied`], stripped of their wrappers.
  #[inline]
  pub fn iter_proxied(&self) -> UOrdProxiedIter<'_, T, N, P> {
    UOrdProxiedIter::new(self.as_array())
  }

  /// Creates an iterator over each element of this [`UOrdProxied`], stripped of their wrappers.
  #[inline]
  pub fn into_iter_proxied(self) -> UOrdProxiedIntoIter<T, N, P> {
    UOrdProxiedIntoIter::new(self.into_array())
  }
}

impl<T> UOrd2<T> where T: Ord {
  /// Returns `true` if this pair was distinct (the values in it were not equal to each other) otherwise returns `false`.
  pub fn is_distinct(&self) -> bool {
    let [min, max] = self.as_array();
    min != max
  }

  /// If this unordered pair contains the given value, this function will return the other value.
  /// If this unordered pair did not contain the given value, this will return `None`.
  ///
  /// Note that this function does not care about the distinctness of the pair, and will
  /// still return the other value, even if it was equal to the input value.
  /// If you need this behavior, see [`UOrd::other_distinct`].
  pub fn other<Q>(&self, x: &Q) -> Option<&T>
  where T: Borrow<Q>, Q: Eq + ?Sized {
    let [min, max] = self.as_array();
    Option::or(
      if max.borrow() == x { Some(min) } else { None },
      if min.borrow() == x { Some(max) } else { None }
    )
  }

  /// If this unordered pair contains the given value, this function will return the other value.
  /// If this unordered pair did not contain the given value, this will return `None`.
  ///
  /// Additionally, this function will return `None` if the two items in this pair were equal,
  /// guaranteeing that the output value is never equal to the input value in the case of an indistinct pair.
  pub fn other_distinct<Q>(&self, x: &Q) -> Option<&T>
  where T: Borrow<Q>, Q: Eq + ?Sized {
    let [min, max] = self.as_array();
    Option::xor(
      if max.borrow() == x { Some(min) } else { None },
      if min.borrow() == x { Some(max) } else { None }
    )
  }
}

impl<T, P> UOrdProxied2<T, P> where P: Proxy<T> {
  /// If this unordered pair contains the given value, this function will return the other value.
  /// If this unordered pair did not contain the given value, this will return `None`.
  ///
  /// Note that this function does not care about the distinctness of the pair, and will
  /// still return the other value, even if it was equal to the input value.
  /// If you need this behavior, see [`UOrdProxied::other_distinct_proxied`].
  pub fn other_proxied<Q>(&self, x: &Q) -> Option<&T>
  where T: Borrow<Q>, Q: ?Sized, for<'q> P: Proxy<&'q Q> {
    let [min, max] = self.as_array_proxied();
    Option::or(
      if P::wrap(max.borrow()) == P::wrap(x) { Some(min) } else { None },
      if P::wrap(min.borrow()) == P::wrap(x) { Some(max) } else { None }
    )
  }

  /// If this unordered pair contains the given value, this function will return the other value.
  /// If this unordered pair did not contain the given value, this will return `None`.
  ///
  /// Additionally, this function will return `None` if the two items in this pair were equal,
  /// guaranteeing that the output value is never equal to the input value in the case of an indistinct pair.
  pub fn other_distinct_proxied<Q>(&self, x: &Q) -> Option<&T>
  where T: Borrow<Q>, Q: ?Sized, for<'q> P: Proxy<&'q Q> {
    let [min, max] = self.as_array_proxied();
    Option::xor(
      if P::wrap(max.borrow()) == P::wrap(x) { Some(min) } else { None },
      if P::wrap(min.borrow()) == P::wrap(x) { Some(max) } else { None }
    )
  }
}

impl<T: fmt::Display, const N: usize> fmt::Display for UOrd<T, N> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    let mut d = f.debug_list();
    for value in self.values.iter() {
      d.entry(&format_args!("{value}"));
    };
    d.finish()
  }
}

impl<T: fmt::Debug, const N: usize> fmt::Debug for UOrd<T, N> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    let mut d = f.debug_tuple("UOrd");
    for value in self.values.iter() {
      d.field(&value);
    };
    d.finish()
  }
}

impl<T, const N: usize> Copy for UOrd<T, N> where T: Copy {}

impl<T, const N: usize> Clone for UOrd<T, N> where T: Clone {
  fn clone(&self) -> Self {
    UOrd { values: self.values.clone() }
  }
}

impl<T, const N: usize> PartialEq for UOrd<T, N> where T: Ord {
  fn eq(&self, other: &Self) -> bool {
    self.as_array() == other.as_array()
  }
}

impl<T, const N: usize> Eq for UOrd<T, N> where T: Ord {}

impl<T, const N: usize> PartialOrd for UOrd<T, N> where T: Ord {
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(Self::cmp(self, other))
  }
}

impl<T, const N: usize> Ord for UOrd<T, N> where T: Ord {
  fn cmp(&self, other: &Self) -> Ordering {
    Ord::cmp(self.as_array(), other.as_array())
  }
}

impl<T, const N: usize> Hash for UOrd<T, N> where T: Hash {
  fn hash<H: Hasher>(&self, state: &mut H) {
    self.as_array().hash(state);
  }
}

impl<T, const N: usize> From<[T; N]> for UOrd<T, N> where T: Ord {
  fn from(value: [T; N]) -> Self {
    UOrd::new(value)
  }
}

impl<T, const N: usize> From<UOrd<T, N>> for [T; N] {
  fn from(value: UOrd<T, N>) -> Self {
    value.into_array()
  }
}

macro_rules! impl_uord_tuple_conversion {
  ($T:ident, $N:literal, $Tuple:ty) => {
    impl<$T> IntoTuple for [$T; $N] {
      type Tuple = $Tuple;
    }

    impl<$T> From<$Tuple> for UOrd<$T, $N> where T: Ord {
      #[inline]
      fn from(value: $Tuple) -> UOrd<$T, $N> {
        UOrd::new(<[$T; $N]>::from(value))
      }
    }

    impl<$T> From<UOrd<$T, $N>> for $Tuple {
      #[inline]
      fn from(value: UOrd<$T, $N>) -> $Tuple {
        <$Tuple>::from(value.into_array())
      }
    }
  };
}

impl_uord_tuple_conversion!(T, 1, (T,));
impl_uord_tuple_conversion!(T, 2, (T, T));
impl_uord_tuple_conversion!(T, 3, (T, T, T));
impl_uord_tuple_conversion!(T, 4, (T, T, T, T));
impl_uord_tuple_conversion!(T, 5, (T, T, T, T, T));
impl_uord_tuple_conversion!(T, 6, (T, T, T, T, T, T));
impl_uord_tuple_conversion!(T, 7, (T, T, T, T, T, T, T));
impl_uord_tuple_conversion!(T, 8, (T, T, T, T, T, T, T, T));
impl_uord_tuple_conversion!(T, 9, (T, T, T, T, T, T, T, T, T));
impl_uord_tuple_conversion!(T, 10, (T, T, T, T, T, T, T, T, T, T));
impl_uord_tuple_conversion!(T, 11, (T, T, T, T, T, T, T, T, T, T, T));
impl_uord_tuple_conversion!(T, 12, (T, T, T, T, T, T, T, T, T, T, T, T));

impl<'a, T, const N: usize> IntoIterator for &'a UOrd<T, N> {
  type Item = &'a T;
  type IntoIter = UOrdIter<'a, T, N>;

  /// Creates an iterator over references to each element of this [`UOrd`].
  #[inline]
  fn into_iter(self) -> Self::IntoIter {
    UOrdIter::new(self.as_array())
  }
}

impl<T, const N: usize> IntoIterator for UOrd<T, N> {
  type Item = T;
  type IntoIter = UOrdIntoIter<T, N>;

  /// Creates an iterator over each element of this [`UOrd`].
  #[inline]
  fn into_iter(self) -> Self::IntoIter {
    UOrdIntoIter::new(self.into_array())
  }
}

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
#[cfg(feature = "serde")]
impl<T, const N: usize> serde::Serialize for UOrd<T, N>
where T: serde::Serialize {
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where S: serde::Serializer {
    <[T; N] as serde_big_array::BigArray<T>>::serialize(self.as_array(), serializer)
  }
}

#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
#[cfg(feature = "serde")]
impl<'de, T, const N: usize> serde::Deserialize<'de> for UOrd<T, N>
where T: serde::Deserialize<'de> + Ord {
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where D: serde::Deserializer<'de> {
    <[T; N] as serde_big_array::BigArray<T>>::deserialize(deserializer).map(UOrd::new)
  }
}