|
| 1 | +use std::cmp::Ordering; |
| 2 | +use std::hash::{Hash, Hasher}; |
| 3 | +use std::ops::Deref; |
| 4 | +use std::ptr; |
| 5 | + |
| 6 | +mod private { |
| 7 | + #[derive(Clone, Copy, Debug)] |
| 8 | + pub struct PrivateZst; |
| 9 | +} |
| 10 | + |
| 11 | +/// A reference to a value that is interned, and is known to be unique. |
| 12 | +/// |
| 13 | +/// Note that it is possible to have a `T` and a `Interned<T>` that are (or |
| 14 | +/// refer to) equal but different values. But if you have two different |
| 15 | +/// `Interned<T>`s, they both refer to the same value, at a single location in |
| 16 | +/// memory. This means that equality and hashing can be done on the value's |
| 17 | +/// address rather than the value's contents, which can improve performance. |
| 18 | +/// |
| 19 | +/// The `PrivateZst` field means you can pattern match with `Interned(v, _)` |
| 20 | +/// but you can only construct a `Interned` with `new_unchecked`, and not |
| 21 | +/// directly. |
| 22 | +#[derive(Debug)] |
| 23 | +#[cfg_attr(not(bootstrap), rustc_pass_by_value)] |
| 24 | +pub struct Interned<'a, T>(pub &'a T, pub private::PrivateZst); |
| 25 | + |
| 26 | +impl<'a, T> Interned<'a, T> { |
| 27 | + /// Create a new `Interned` value. The value referred to *must* be interned |
| 28 | + /// and thus be unique, and it *must* remain unique in the future. This |
| 29 | + /// function has `_unchecked` in the name but is not `unsafe`, because if |
| 30 | + /// the uniqueness condition is violated condition it will cause incorrect |
| 31 | + /// behaviour but will not affect memory safety. |
| 32 | + #[inline] |
| 33 | + pub const fn new_unchecked(t: &'a T) -> Self { |
| 34 | + Interned(t, private::PrivateZst) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<'a, T> Clone for Interned<'a, T> { |
| 39 | + fn clone(&self) -> Self { |
| 40 | + *self |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<'a, T> Copy for Interned<'a, T> {} |
| 45 | + |
| 46 | +impl<'a, T> Deref for Interned<'a, T> { |
| 47 | + type Target = T; |
| 48 | + |
| 49 | + #[inline] |
| 50 | + fn deref(&self) -> &T { |
| 51 | + self.0 |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<'a, T> PartialEq for Interned<'a, T> { |
| 56 | + #[inline] |
| 57 | + fn eq(&self, other: &Self) -> bool { |
| 58 | + // Pointer equality implies equality, due to the uniqueness constraint. |
| 59 | + ptr::eq(self.0, other.0) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<'a, T> Eq for Interned<'a, T> {} |
| 64 | + |
| 65 | +impl<'a, T: PartialOrd> PartialOrd for Interned<'a, T> { |
| 66 | + fn partial_cmp(&self, other: &Interned<'a, T>) -> Option<Ordering> { |
| 67 | + // Pointer equality implies equality, due to the uniqueness constraint, |
| 68 | + // but the contents must be compared otherwise. |
| 69 | + if ptr::eq(self.0, other.0) { |
| 70 | + Some(Ordering::Equal) |
| 71 | + } else { |
| 72 | + let res = self.0.partial_cmp(&other.0); |
| 73 | + debug_assert!(res != Some(Ordering::Equal)); |
| 74 | + res |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<'a, T: Ord> Ord for Interned<'a, T> { |
| 80 | + fn cmp(&self, other: &Interned<'a, T>) -> Ordering { |
| 81 | + // Pointer equality implies equality, due to the uniqueness constraint, |
| 82 | + // but the contents must be compared otherwise. |
| 83 | + if ptr::eq(self.0, other.0) { |
| 84 | + Ordering::Equal |
| 85 | + } else { |
| 86 | + let res = self.0.cmp(&other.0); |
| 87 | + debug_assert!(res != Ordering::Equal); |
| 88 | + res |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<'a, T> Hash for Interned<'a, T> { |
| 94 | + #[inline] |
| 95 | + fn hash<H: Hasher>(&self, s: &mut H) { |
| 96 | + // Pointer hashing is sufficient, due to the uniqueness constraint. |
| 97 | + ptr::hash(self.0, s) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests; |
0 commit comments