1use std::{
2 borrow::Borrow,
3 cmp::Ordering,
4 ffi::{OsStr, OsString},
5 hash::{Hash, Hasher},
6 ops::Deref,
7 path::{Path, PathBuf},
8 sync::LazyLock,
9};
10
11use crate::{borrow::BorrowedInterned, entry::Entry, pool::POOL};
12
13#[derive(Eq)]
14#[repr(transparent)]
15pub struct Interned(Entry);
19
20impl Interned {
21 pub fn new(value: &[u8]) -> Self {
34 Self(POOL.get_or_insert(value))
35 }
36
37 pub(crate) fn from_existing(entry: Entry) -> Self {
38 Self(entry)
39 }
40}
41
42impl Clone for Interned {
43 fn clone(&self) -> Self {
44 Self(unsafe { self.0.acquire() })
46 }
47}
48
49pub(crate) static DEFAULT: LazyLock<Interned> = LazyLock::new(|| Interned::new(Default::default()));
50
51impl Default for Interned {
52 fn default() -> Self {
53 DEFAULT.clone()
54 }
55}
56
57impl Drop for Interned {
58 fn drop(&mut self) {
59 unsafe { POOL.release(self.0) };
61 }
62}
63
64impl Deref for Interned {
65 type Target = BorrowedInterned;
66
67 fn deref(&self) -> &Self::Target {
68 BorrowedInterned::new(unsafe { self.0.data() })
70 }
71}
72
73impl PartialEq for Interned {
74 fn eq(&self, other: &Self) -> bool {
75 self.0 == other.0
77 }
78}
79
80impl Hash for Interned {
81 fn hash<H: Hasher>(&self, state: &mut H) {
82 self.0.data_ptr().hash(state)
84 }
85}
86
87impl PartialOrd for Interned {
88 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
89 Some(self.cmp(other))
90 }
91}
92
93impl Ord for Interned {
94 fn cmp(&self, other: &Self) -> Ordering {
95 self.deref().cmp(other)
96 }
97}
98
99impl From<&[u8]> for Interned {
100 fn from(value: &[u8]) -> Self {
101 Interned::new(value)
102 }
103}
104impl From<Vec<u8>> for Interned {
105 fn from(value: Vec<u8>) -> Self {
106 value.as_slice().into()
107 }
108}
109
110impl From<&str> for Interned {
111 fn from(value: &str) -> Self {
112 value.as_bytes().into()
113 }
114}
115
116impl From<String> for Interned {
117 fn from(value: String) -> Self {
118 value.as_bytes().into()
119 }
120}
121
122impl From<&String> for Interned {
123 fn from(value: &String) -> Self {
124 value.as_bytes().into()
125 }
126}
127
128impl From<&OsStr> for Interned {
129 fn from(value: &OsStr) -> Self {
130 value.as_encoded_bytes().into()
131 }
132}
133
134impl From<OsString> for Interned {
135 fn from(value: OsString) -> Self {
136 value.as_encoded_bytes().into()
137 }
138}
139
140impl From<&OsString> for Interned {
141 fn from(value: &OsString) -> Self {
142 value.as_encoded_bytes().into()
143 }
144}
145
146impl From<&Path> for Interned {
147 fn from(value: &Path) -> Self {
148 value.as_os_str().into()
149 }
150}
151
152impl From<PathBuf> for Interned {
153 fn from(value: PathBuf) -> Self {
154 value.as_os_str().into()
155 }
156}
157
158impl From<&PathBuf> for Interned {
159 fn from(value: &PathBuf) -> Self {
160 value.as_os_str().into()
161 }
162}
163
164impl Borrow<BorrowedInterned> for Interned {
165 fn borrow(&self) -> &BorrowedInterned {
166 self.deref()
167 }
168}
169
170impl AsRef<BorrowedInterned> for Interned {
171 fn as_ref(&self) -> &BorrowedInterned {
172 self.deref()
173 }
174}