tokio_dbus/object_path/object_path_buf.rs
1use std::{borrow::Borrow, ops::Deref};
2
3use super::ObjectPath;
4
5/// A validated owned object path.
6///
7/// The following rules define a [valid object path]. Implementations must not
8/// send or accept messages with invalid object paths.
9///
10/// [valid object path]: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path
11///
12/// * The path may be of any length.
13/// * The path must begin with an ASCII '/' (integer 47) character, and must
14/// consist of elements separated by slash characters.
15/// * Each element must only contain the ASCII characters "[A-Z][a-z][0-9]_"
16/// * No element may be the empty string.
17/// * Multiple '/' characters cannot occur in sequence.
18/// * A trailing '/' character is not allowed unless the path is the root path
19/// (a single '/' character).
20#[derive(Clone, PartialEq, Eq)]
21#[repr(transparent)]
22pub struct ObjectPathBuf(Vec<u8>);
23
24impl ObjectPathBuf {
25 /// Construct an owned object path from its raw underlying vector.
26 ///
27 /// # Safety
28 ///
29 /// The caller must ensure that the vector contains a valid object path.
30 #[inline]
31 pub(super) unsafe fn from_raw_vec(data: Vec<u8>) -> Self {
32 Self(data)
33 }
34
35 #[inline]
36 fn to_object_path(&self) -> &ObjectPath {
37 // SAFETY: This type ensures during construction that the object path it
38 // contains is valid.
39 unsafe { ObjectPath::new_unchecked(&self.0) }
40 }
41}
42
43impl Deref for ObjectPathBuf {
44 type Target = ObjectPath;
45
46 #[inline]
47 fn deref(&self) -> &Self::Target {
48 self.to_object_path()
49 }
50}
51
52impl Borrow<ObjectPath> for ObjectPathBuf {
53 #[inline]
54 fn borrow(&self) -> &ObjectPath {
55 self
56 }
57}
58
59impl AsRef<ObjectPath> for ObjectPathBuf {
60 #[inline]
61 fn as_ref(&self) -> &ObjectPath {
62 self
63 }
64}