interprocess/local_socket/name/
to_name.rs1use {
2 super::{
3 r#type::{NamespacedNameType, PathNameType},
4 Name,
5 },
6 std::{
7 borrow::Cow,
8 ffi::{CStr, CString, OsStr, OsString},
9 io,
10 path::{Path, PathBuf},
11 str,
12 },
13};
14
15macro_rules! trivial_string_impl {
16 (
17 $cvttrait:ident<$str:ident>
18 :: $mtd:ident<$nttrait:ident>
19 for $($tgt:ty => $via:ident :: $ctor:ident)+
20 ) => {$(
21 impl<'s> $cvttrait<'s, $str> for $tgt {
22 #[inline]
23 fn $mtd<T: $nttrait<$str>>(self) -> io::Result<Name<'s>> {
24 $via::$ctor(self).$mtd::<T>()
25 }
26 }
27 )+};
28}
29
30pub trait ToFsName<'s, S: ToOwned + ?Sized> {
32 fn to_fs_name<NT: PathNameType<S>>(self) -> io::Result<Name<'s>>;
36}
37
38pub trait ToNsName<'s, S: ToOwned + ?Sized> {
40 fn to_ns_name<NT: NamespacedNameType<S>>(self) -> io::Result<Name<'s>>;
44}
45
46#[allow(dead_code)]
47fn err(s: &'static str) -> io::Error { io::Error::new(io::ErrorKind::Unsupported, s) }
48
49impl<'s> ToFsName<'s, OsStr> for &'s Path {
50 #[inline]
51 fn to_fs_name<FT: PathNameType<OsStr>>(self) -> io::Result<Name<'s>> {
52 FT::map(Cow::Borrowed(self.as_os_str()))
53 }
54}
55impl<'s> ToFsName<'s, OsStr> for PathBuf {
56 #[inline]
57 fn to_fs_name<FT: PathNameType<OsStr>>(self) -> io::Result<Name<'s>> {
58 FT::map(Cow::Owned(self.into_os_string()))
59 }
60}
61trivial_string_impl! { ToFsName<OsStr>::to_fs_name<PathNameType> for
62 &'s str => Path ::new
63 String => PathBuf::from
64 &'s OsStr => Path ::new
65 OsString => PathBuf::from
66}
67
68impl<'s> ToNsName<'s, OsStr> for &'s OsStr {
69 #[inline]
70 fn to_ns_name<NT: NamespacedNameType<OsStr>>(self) -> io::Result<Name<'s>> {
71 NT::map(Cow::Borrowed(self))
72 }
73}
74impl<'s> ToNsName<'s, OsStr> for OsString {
75 #[inline]
76 fn to_ns_name<NT: NamespacedNameType<OsStr>>(self) -> io::Result<Name<'s>> {
77 NT::map(Cow::Owned(self))
78 }
79}
80trivial_string_impl! { ToNsName<OsStr>::to_ns_name<NamespacedNameType> for
81 &'s str => OsStr ::new
82 String => OsString::from
83}
84
85impl<'s> ToFsName<'s, CStr> for &'s CStr {
86 #[inline]
87 fn to_fs_name<FT: PathNameType<CStr>>(self) -> io::Result<Name<'s>> {
88 FT::map(Cow::Borrowed(self))
89 }
90}
91impl<'s> ToFsName<'s, CStr> for CString {
92 #[inline]
93 fn to_fs_name<FT: PathNameType<CStr>>(self) -> io::Result<Name<'s>> {
94 FT::map(Cow::Owned(self))
95 }
96}
97
98impl<'s> ToNsName<'s, CStr> for &'s CStr {
99 #[inline]
100 fn to_ns_name<NT: NamespacedNameType<CStr>>(self) -> io::Result<Name<'s>> {
101 NT::map(Cow::Borrowed(self))
102 }
103}
104impl<'s> ToNsName<'s, CStr> for CString {
105 #[inline]
106 fn to_ns_name<NT: NamespacedNameType<CStr>>(self) -> io::Result<Name<'s>> {
107 NT::map(Cow::Owned(self))
108 }
109}