protobuf_parse/
rel_path.rs

1use std::ops::Deref;
2use std::path::Path;
3use std::path::PathBuf;
4
5/// Wrapper for `Path` that asserts that the path is relative.
6#[repr(transparent)]
7pub(crate) struct RelPath {
8    path: Path,
9}
10
11/// Wrapper for `PathBuf` that asserts that the path is relative.
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub(crate) struct _RelPathBuf {
14    path: PathBuf,
15}
16
17impl RelPath {
18    pub(crate) fn _new(path: &Path) -> &RelPath {
19        assert!(
20            !path.is_absolute(),
21            "path must be relative: {}",
22            path.display()
23        );
24        unsafe { &*(path as *const Path as *const RelPath) }
25    }
26
27    pub(crate) fn _to_owned(&self) -> _RelPathBuf {
28        _RelPathBuf {
29            path: self.path.to_owned(),
30        }
31    }
32}
33
34impl _RelPathBuf {
35    pub(crate) fn _new(path: PathBuf) -> _RelPathBuf {
36        assert!(
37            !path.is_absolute(),
38            "path must be relative: {}",
39            path.display()
40        );
41        _RelPathBuf { path }
42    }
43}
44
45impl Deref for RelPath {
46    type Target = Path;
47
48    fn deref(&self) -> &Self::Target {
49        &self.path
50    }
51}
52
53impl Deref for _RelPathBuf {
54    type Target = RelPath;
55
56    fn deref(&self) -> &Self::Target {
57        RelPath::_new(&self.path)
58    }
59}