interprocess/local_socket/
name.rs

1mod inner;
2pub(super) mod to_name;
3pub(super) mod r#type;
4
5pub(crate) use self::inner::*;
6pub use {r#type::*, to_name::*};
7
8/// Name for a local socket.
9///
10/// Due to significant differences between how different platforms name local sockets, there needs
11/// to be a way to store and process those in a unified way while also retaining those
12/// platform-specific pecularities. `Name` exists to bridge the gap between portability and
13/// correctness, minimizing the amount of platform-dependent code in downstream programs.
14///
15/// # Creation
16/// Two traits are used to create names from basic strings: [`ToFsName`](super::ToFsName) and
17/// [`ToNsName`](super::ToNsName).
18///
19/// # Validity
20/// As mentioned in the [module-level documentation](super), not all platforms support all types of
21/// local socket names. Names pointing to filesystem locations are only supported on Unix-like
22/// systems, and names pointing to an abstract namespace reserved specifically for local sockets are
23/// only available on Linux and Windows.
24///
25/// Instances of this type cannot be constructed from unsupported values. They can, however, be
26/// constructed from invalid ones.
27#[derive(Clone, Debug, PartialEq, Eq)]
28pub struct Name<'s>(pub(crate) NameInner<'s>);
29impl Name<'_> {
30    /// Returns `true` if the name points to a dedicated local socket namespace, `false` otherwise.
31    #[inline]
32    pub fn is_namespaced(&self) -> bool { self.0.is_namespaced() }
33
34    /// Returns `true` if the name is stored as a filesystem path, `false` otherwise.
35    ///
36    /// Note that it is possible for [`.is_namespaced()`](Self::is_namespaced) and `.is_path()` to
37    /// return `true` simultaneously:
38    /// ```
39    /// # #[cfg(windows)] {
40    /// use interprocess::{local_socket::ToFsName, os::windows::local_socket::NamedPipe};
41    /// let name = r"\\.\pipe\example".to_fs_name::<NamedPipe>().unwrap();
42    /// assert!(name.is_namespaced()); // \\.\pipe\ is a namespace
43    /// assert!(name.is_path());       // \\.\pipe\example is a path
44    /// # }
45    /// ```
46    #[inline]
47    pub const fn is_path(&self) -> bool { self.0.is_path() }
48
49    /// Produces a `Name` that borrows from `self`.
50    #[inline]
51    pub fn borrow(&self) -> Name<'_> { Name(self.0.borrow()) }
52
53    /// Extends the lifetime to `'static`, cloning if necessary.
54    #[inline]
55    pub fn into_owned(self) -> Name<'static> { Name(self.0.into_owned()) }
56
57    pub(crate) fn invalid() -> Self { Self(NameInner::default()) }
58}