1use crate::{Bstr, Ustr, Ustring};
2use std::{
3 borrow::Cow,
4 ffi::{CStr, CString, OsStr, OsString},
5 fmt::Debug,
6 ops::Deref,
7 os::unix::ffi::{OsStrExt, OsStringExt},
8 path::{Path, PathBuf},
9};
10
11pub trait IntoUstr<'a>: Debug {
26 fn into_ustr(self) -> Cow<'a, Ustr>;
28}
29
30impl<'a> IntoUstr<'a> for Cow<'a, Ustr> {
31 fn into_ustr(self) -> Cow<'a, Ustr> {
32 self
33 }
34}
35
36impl<'a> IntoUstr<'a> for &'a Cow<'a, Ustr> {
37 fn into_ustr(self) -> Cow<'a, Ustr> {
38 self.deref().into_ustr()
39 }
40}
41
42impl<'a> IntoUstr<'a> for &'a Ustr {
43 fn into_ustr(self) -> Cow<'a, Ustr> {
44 Cow::Borrowed(self)
45 }
46}
47
48impl<'a> IntoUstr<'a> for &'a Ustring {
49 fn into_ustr(self) -> Cow<'a, Ustr> {
50 self.deref().into_ustr()
51 }
52}
53
54impl IntoUstr<'static> for Ustring {
55 fn into_ustr(self) -> Cow<'static, Ustr> {
56 Cow::Owned(self)
57 }
58}
59
60impl<'a> IntoUstr<'a> for &'a [u8] {
61 fn into_ustr(self) -> Cow<'a, Ustr> {
62 if let Some(s) = Ustr::from_bytes(self) {
63 return Cow::Borrowed(s);
64 }
65 Cow::Owned(Ustring::from_vec(self.to_owned()))
66 }
67}
68
69impl<'a> IntoUstr<'a> for &'a Bstr {
70 fn into_ustr(self) -> Cow<'a, Ustr> {
71 self.as_bytes().into_ustr()
72 }
73}
74
75impl<'a> IntoUstr<'a> for Vec<u8> {
76 fn into_ustr(self) -> Cow<'a, Ustr> {
77 Cow::Owned(Ustring::from_vec(self))
78 }
79}
80
81impl<'a> IntoUstr<'a> for &'a str {
82 fn into_ustr(self) -> Cow<'a, Ustr> {
83 self.as_bytes().into_ustr()
84 }
85}
86
87impl<'a> IntoUstr<'a> for String {
88 fn into_ustr(self) -> Cow<'a, Ustr> {
89 self.into_bytes().into_ustr()
90 }
91}
92
93impl<'a> IntoUstr<'a> for &'a CStr {
94 fn into_ustr(self) -> Cow<'a, Ustr> {
95 Cow::Borrowed(Ustr::from_c_str(self))
96 }
97}
98
99impl<'a> IntoUstr<'a> for CString {
100 fn into_ustr(self) -> Cow<'a, Ustr> {
101 Cow::Owned(Ustring::from_c_string(self))
102 }
103}
104
105impl<'a> IntoUstr<'a> for &'a OsStr {
106 fn into_ustr(self) -> Cow<'a, Ustr> {
107 self.as_bytes().into_ustr()
108 }
109}
110
111impl<'a> IntoUstr<'a> for OsString {
112 fn into_ustr(self) -> Cow<'a, Ustr> {
113 self.into_vec().into_ustr()
114 }
115}
116
117impl<'a> IntoUstr<'a> for &'a Path {
118 fn into_ustr(self) -> Cow<'a, Ustr> {
119 self.as_os_str().into_ustr()
120 }
121}
122
123impl<'a> IntoUstr<'a> for PathBuf {
124 fn into_ustr(self) -> Cow<'a, Ustr> {
125 self.into_os_string().into_ustr()
126 }
127}