1- #![ allow( private_interfaces) ]
21use crate :: Error ;
32use alloc:: string:: String ;
43
5- pub ( crate ) struct SealedFilename ;
6-
7- /// This trait is implemented for types that can be used as a filename when loading new
8- /// [`Library`](crate::Library) instances.
9- ///
10- /// It is currently sealed and cannot be implemented or its methods called by users of this crate.
11- pub trait AsFilename {
4+ pub ( crate ) trait Sealed {
125 #[ cfg( windows) ]
136 #[ doc( hidden) ]
147 fn windows_filename < R > (
158 self ,
16- sealed : SealedFilename ,
179 function : impl FnOnce ( * const u16 ) -> Result < R , crate :: Error > ,
1810 ) -> Result < R , crate :: Error > ;
1911
2012 #[ cfg( unix) ]
2113 #[ doc( hidden) ]
2214 fn posix_filename < R > (
2315 self ,
24- sealed : SealedFilename ,
2516 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , crate :: Error > ,
2617 ) -> Result < R , crate :: Error > ;
2718}
2819
29- impl AsFilename for & str {
20+ /// This trait is implemented for types that can be used as a filename when loading new
21+ /// [`Library`](crate::Library) instances.
22+ ///
23+ /// It is currently sealed and cannot be implemented or its methods called by users of this crate.
24+ #[ allow( private_bounds) ]
25+ pub trait AsFilename : Sealed { }
26+
27+ impl AsFilename for & str { }
28+ impl Sealed for & str {
3029 #[ cfg( windows) ]
3130 fn windows_filename < R > (
3231 self ,
33- _seal : SealedFilename ,
3432 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
3533 ) -> Result < R , Error > {
3634 let utf16: alloc:: vec:: Vec < u16 > = if crate :: util:: check_null_bytes ( self . as_bytes ( ) ) ? {
@@ -44,7 +42,6 @@ impl AsFilename for &str {
4442 #[ cfg( unix) ]
4543 fn posix_filename < R > (
4644 self ,
47- _seal : SealedFilename ,
4845 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
4946 ) -> Result < R , Error > {
5047 if crate :: util:: check_null_bytes ( self . as_bytes ( ) ) ? {
@@ -56,40 +53,38 @@ impl AsFilename for &str {
5653 }
5754}
5855
59- impl AsFilename for & String {
56+ impl AsFilename for & String { }
57+ impl Sealed for & String {
6058 #[ cfg( windows) ]
6159 fn windows_filename < R > (
6260 self ,
63- seal : SealedFilename ,
6461 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
6562 ) -> Result < R , Error > {
66- self . as_str ( ) . windows_filename ( seal , function)
63+ self . as_str ( ) . windows_filename ( function)
6764 }
6865
6966 #[ cfg( unix) ]
7067 fn posix_filename < R > (
7168 self ,
72- seal : SealedFilename ,
7369 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
7470 ) -> Result < R , Error > {
75- self . as_str ( ) . posix_filename ( seal , function)
71+ self . as_str ( ) . posix_filename ( function)
7672 }
7773}
7874
79- impl AsFilename for String {
75+ impl AsFilename for String { }
76+ impl Sealed for String {
8077 #[ cfg( windows) ]
8178 fn windows_filename < R > (
8279 self ,
83- seal : SealedFilename ,
8480 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
8581 ) -> Result < R , Error > {
86- self . as_str ( ) . windows_filename ( seal , function)
82+ self . as_str ( ) . windows_filename ( function)
8783 }
8884
8985 #[ cfg( unix) ]
9086 fn posix_filename < R > (
9187 mut self ,
92- _seal : SealedFilename ,
9388 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
9489 ) -> Result < R , Error > {
9590 if crate :: util:: check_null_bytes ( self . as_bytes ( ) ) ? {
@@ -104,15 +99,15 @@ impl AsFilename for String {
10499#[ cfg( feature = "std" ) ]
105100#[ cfg_attr( libloading_docs, doc( cfg( feature = "std" ) ) ) ]
106101mod std {
107- use super :: { AsFilename , SealedFilename } ;
102+ use super :: { Sealed , AsFilename } ;
108103 use crate :: Error ;
109104 use std:: ffi:: { OsStr , OsString } ;
110105
111- impl AsFilename for & OsStr {
106+ impl AsFilename for & OsStr { }
107+ impl Sealed for & OsStr {
112108 #[ cfg( windows) ]
113109 fn windows_filename < R > (
114110 self ,
115- _seal : SealedFilename ,
116111 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
117112 ) -> Result < R , Error > {
118113 use std:: os:: windows:: ffi:: OsStrExt ;
@@ -128,7 +123,6 @@ mod std {
128123 #[ cfg( unix) ]
129124 fn posix_filename < R > (
130125 self ,
131- _seal : SealedFilename ,
132126 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
133127 ) -> Result < R , Error > {
134128 let bytes = std:: os:: unix:: ffi:: OsStrExt :: as_bytes ( self ) ;
@@ -141,44 +135,42 @@ mod std {
141135 }
142136 }
143137
144- impl AsFilename for & OsString {
138+ impl AsFilename for & OsString { }
139+ impl Sealed for & OsString {
145140 #[ cfg( windows) ]
146141 fn windows_filename < R > (
147142 self ,
148- seal : SealedFilename ,
149143 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
150144 ) -> Result < R , Error > {
151- self . as_os_str ( ) . windows_filename ( seal , function)
145+ self . as_os_str ( ) . windows_filename ( function)
152146 }
153147
154148 #[ cfg( unix) ]
155149 fn posix_filename < R > (
156150 self ,
157- seal : SealedFilename ,
158151 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
159152 ) -> Result < R , Error > {
160- self . as_os_str ( ) . posix_filename ( seal , function)
153+ self . as_os_str ( ) . posix_filename ( function)
161154 }
162155 }
163156
164- impl AsFilename for OsString {
157+ impl AsFilename for OsString { }
158+ impl Sealed for OsString {
165159 #[ cfg( windows) ]
166160 fn windows_filename < R > (
167161 self ,
168- seal : SealedFilename ,
169162 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
170163 ) -> Result < R , Error > {
171164 // This is the best we can do.
172165 // There is no into_wide for windows.
173166 // The internal repr is wtf-8 and this is different
174167 // from LCPWSTR that we need for the ffi calls.
175- self . as_os_str ( ) . windows_filename ( seal , function)
168+ self . as_os_str ( ) . windows_filename ( function)
176169 }
177170
178171 #[ cfg( unix) ]
179172 fn posix_filename < R > (
180173 self ,
181- _seal : SealedFilename ,
182174 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
183175 ) -> Result < R , Error > {
184176 let mut bytes = std:: os:: unix:: ffi:: OsStringExt :: into_vec ( self ) ;
@@ -191,63 +183,60 @@ mod std {
191183 }
192184 }
193185
194- impl AsFilename for std:: path:: PathBuf {
186+ impl AsFilename for std:: path:: PathBuf { }
187+ impl Sealed for std:: path:: PathBuf {
195188 #[ cfg( windows) ]
196189 fn windows_filename < R > (
197190 self ,
198- seal : SealedFilename ,
199191 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
200192 ) -> Result < R , Error > {
201- self . into_os_string ( ) . windows_filename ( seal , function)
193+ self . into_os_string ( ) . windows_filename ( function)
202194 }
203195
204196 #[ cfg( unix) ]
205197 fn posix_filename < R > (
206198 self ,
207- seal : SealedFilename ,
208199 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
209200 ) -> Result < R , Error > {
210- self . into_os_string ( ) . posix_filename ( seal , function)
201+ self . into_os_string ( ) . posix_filename ( function)
211202 }
212203 }
213204
214- impl AsFilename for & std:: path:: PathBuf {
205+ impl AsFilename for & std:: path:: PathBuf { }
206+ impl Sealed for & std:: path:: PathBuf {
215207 #[ cfg( windows) ]
216208 fn windows_filename < R > (
217209 self ,
218- seal : SealedFilename ,
219210 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
220211 ) -> Result < R , Error > {
221- self . as_os_str ( ) . windows_filename ( seal , function)
212+ self . as_os_str ( ) . windows_filename ( function)
222213 }
223214
224215 #[ cfg( unix) ]
225216 fn posix_filename < R > (
226217 self ,
227- seal : SealedFilename ,
228218 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
229219 ) -> Result < R , Error > {
230- self . as_os_str ( ) . posix_filename ( seal , function)
220+ self . as_os_str ( ) . posix_filename ( function)
231221 }
232222 }
233223
234- impl AsFilename for & std:: path:: Path {
224+ impl AsFilename for & std:: path:: Path { }
225+ impl Sealed for & std:: path:: Path {
235226 #[ cfg( windows) ]
236227 fn windows_filename < R > (
237228 self ,
238- seal : SealedFilename ,
239229 function : impl FnOnce ( * const u16 ) -> Result < R , Error > ,
240230 ) -> Result < R , Error > {
241- self . as_os_str ( ) . windows_filename ( seal , function)
231+ self . as_os_str ( ) . windows_filename ( function)
242232 }
243233
244234 #[ cfg( unix) ]
245235 fn posix_filename < R > (
246236 self ,
247- seal : SealedFilename ,
248237 function : impl FnOnce ( * const core:: ffi:: c_char ) -> Result < R , Error > ,
249238 ) -> Result < R , Error > {
250- self . as_os_str ( ) . posix_filename ( seal , function)
239+ self . as_os_str ( ) . posix_filename ( function)
251240 }
252241 }
253242}
0 commit comments