Skip to main content

strict_path/path/strict_path/
traits.rs

1//! Standard trait impls for `StrictPath`: `Display`, `Debug`, `PartialEq`, `Eq`, `PartialOrd`,
2//! `Ord`, `Hash`.
3//!
4//! Equality and ordering are based solely on the underlying system path, not the marker type.
5//! This lets paths with different markers be compared when the application needs to deduplicate
6//! or sort — while the type system still prevents mixing them in security-sensitive operations.
7use super::StrictPath;
8use std::cmp::Ordering;
9use std::fmt;
10use std::hash::{Hash, Hasher};
11use std::path::Path;
12
13impl<Marker> fmt::Debug for StrictPath<Marker> {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        f.debug_struct("StrictPath")
16            .field("path", &self.path())
17            .field("boundary", &self.boundary_path())
18            .field("marker", &std::any::type_name::<Marker>())
19            .finish()
20    }
21}
22
23impl<Marker> PartialEq for StrictPath<Marker> {
24    #[inline]
25    fn eq(&self, other: &Self) -> bool {
26        self.path() == other.path()
27    }
28}
29
30impl<Marker> Eq for StrictPath<Marker> {}
31
32impl<Marker> Hash for StrictPath<Marker> {
33    #[inline]
34    fn hash<H: Hasher>(&self, state: &mut H) {
35        self.path().hash(state);
36    }
37}
38
39impl<Marker> PartialOrd for StrictPath<Marker> {
40    #[inline]
41    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
42        Some(self.cmp(other))
43    }
44}
45
46impl<Marker> Ord for StrictPath<Marker> {
47    #[inline]
48    fn cmp(&self, other: &Self) -> Ordering {
49        self.path().cmp(other.path())
50    }
51}
52
53impl<T: AsRef<Path>, Marker> PartialEq<T> for StrictPath<Marker> {
54    fn eq(&self, other: &T) -> bool {
55        self.path() == other.as_ref()
56    }
57}
58
59impl<T: AsRef<Path>, Marker> PartialOrd<T> for StrictPath<Marker> {
60    fn partial_cmp(&self, other: &T) -> Option<Ordering> {
61        Some(self.path().cmp(other.as_ref()))
62    }
63}
64
65#[cfg(feature = "virtual-path")]
66impl<Marker> PartialEq<crate::path::virtual_path::VirtualPath<Marker>> for StrictPath<Marker> {
67    #[inline]
68    fn eq(&self, other: &crate::path::virtual_path::VirtualPath<Marker>) -> bool {
69        self.path() == other.as_unvirtual().path()
70    }
71}