Skip to content

Commit ea84e08

Browse files
committed
make inode table's Pathish type a bit cleaner
add some comments and use #[repr(transparent)] to make it clearer what's going on
1 parent 936fe91 commit ea84e08

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

src/inode_table.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,18 @@ impl InodeTable {
251251
}
252252
}
253253

254-
/// Facilitates comparing Rc<PathBuf> and &Path
254+
/// Facilitates comparing Arc<PathBuf> and &Path.
255+
///
256+
/// We can't implement arbitrary traits like Borrow<Path> on Arc<PathBuf>, but we can invent our
257+
/// own type like this that's identical to Path, and use that instead.
255258
#[derive(Debug)]
256-
struct Pathish {
257-
inner: Path,
258-
}
259+
#[repr(transparent)]
260+
struct Pathish(Path);
259261

260262
impl Pathish {
261263
pub fn new(p: &Path) -> &Pathish {
262-
unsafe { &*(p as *const _ as *const Pathish) }
264+
// safe because Path and Pathish are identical, guarenteed by #[repr(transparent)]
265+
unsafe { std::mem::transmute(p) }
263266
}
264267
}
265268

@@ -277,16 +280,15 @@ impl PartialOrd<Self> for Pathish {
277280

278281
impl Ord for Pathish {
279282
fn cmp(&self, other: &Self) -> Ordering {
280-
self.inner.cmp(&other.inner)
283+
self.0.cmp(&other.0)
281284
}
282285
}
283286

284-
impl Eq for Pathish {
285-
}
287+
impl Eq for Pathish {}
286288

287289
impl PartialEq for Pathish {
288290
fn eq(&self, other: &Pathish) -> bool {
289-
self.inner.eq(&other.inner)
291+
self.0.eq(&other.0)
290292
}
291293
}
292294

0 commit comments

Comments
 (0)