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
use core::{
    fmt,
    fmt::{Binary, Debug, Display, Formatter, LowerExp, LowerHex, Octal, UpperExp, UpperHex},
    num::NonZeroUsize,
    ops::Deref,
};

use crate::{FromIndex, Idx, IntoIndex, NewIndex};

/// This is partly like [`Cow`], but the 'owned' type is the same as the 'borrowed'
/// type.
///
/// [`Cow`]: alloc::borrow::Cow
#[cfg_attr(feature = "gc", derive(gc::Trace))]
#[derive(Copy, Clone)]
pub enum Key<'a, T> {
    Owned(T),
    Borrowed(&'a T),
}

impl<'a, T> Deref for Key<'a, T> {
    type Target = T;
    fn deref(&self) -> &T {
        match *self {
            Key::Owned(ref t) => t,
            Key::Borrowed(t) => t,
        }
    }
}

impl<'a, T: IntoIndex> IntoIndex for Key<'a, T> {
    fn into_index(&self) -> Option<Idx<Self>> {
        Some(unsafe { (**self).into_index()?.cast() })
    }
}

impl<'a, T: FromIndex> FromIndex for Key<'a, T> {
    fn from_index(v: Idx<Self>) -> Self {
        Self::Owned(T::from_index(unsafe { v.cast() }))
    }
}

unsafe impl<'a, T: NewIndex> NewIndex for Key<'a, T> {
    fn new_index_allowed(idx: NonZeroUsize) -> bool {
        T::new_index_allowed(idx)
    }
}

// All the formatting traits

impl<T: Display> Display for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(&**self, f)
    }
}

impl<T: Debug> Debug for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Debug::fmt(&**self, f)
    }
}

impl<T: Binary> Binary for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Binary::fmt(&**self, f)
    }
}

impl<T: LowerExp> LowerExp for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        LowerExp::fmt(&**self, f)
    }
}

impl<T: LowerHex> LowerHex for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        LowerHex::fmt(&**self, f)
    }
}

impl<T: Octal> Octal for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Octal::fmt(&**self, f)
    }
}

impl<T: UpperExp> UpperExp for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        UpperExp::fmt(&**self, f)
    }
}

impl<T: UpperHex> UpperHex for Key<'_, T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        UpperHex::fmt(&**self, f)
    }
}