@@ -253,9 +253,10 @@ pub fn read_yes() -> bool {
253253 }
254254}
255255
256- /// Helper function for processing delimiter values (which could be non UTF-8)
257- /// It converts OsString to &[u8] for unix targets only
258- /// On non-unix (i.e. Windows) it will just return an error if delimiter value is not UTF-8
256+ /// Converts an `OsStr` to a UTF-8 `&[u8]`.
257+ ///
258+ /// This always succeeds on unix platforms,
259+ /// and fails on other platforms if the string can't be coerced to UTF-8.
259260pub fn os_str_as_bytes ( os_string : & OsStr ) -> mods:: error:: UResult < & [ u8 ] > {
260261 #[ cfg( unix) ]
261262 let bytes = os_string. as_bytes ( ) ;
@@ -271,13 +272,28 @@ pub fn os_str_as_bytes(os_string: &OsStr) -> mods::error::UResult<&[u8]> {
271272 Ok ( bytes)
272273}
273274
274- /// Helper function for converting a slice of bytes into an &OsStr
275- /// or OsString in non-unix targets.
275+ /// Performs a potentially lossy conversion from `OsStr` to UTF-8 bytes.
276+ ///
277+ /// This is always lossless on unix platforms,
278+ /// and wraps [`OsStr::to_string_lossy`] on non-unix platforms.
279+ pub fn os_str_as_bytes_lossy ( os_string : & OsStr ) -> Cow < [ u8 ] > {
280+ #[ cfg( unix) ]
281+ let bytes = Cow :: from ( os_string. as_bytes ( ) ) ;
282+
283+ #[ cfg( not( unix) ) ]
284+ let bytes = match os_string. to_string_lossy ( ) {
285+ Cow :: Borrowed ( slice) => Cow :: from ( slice. as_bytes ( ) ) ,
286+ Cow :: Owned ( owned) => Cow :: from ( owned. into_bytes ( ) ) ,
287+ } ;
288+
289+ bytes
290+ }
291+
292+ /// Converts a `&[u8]` to an `&OsStr`,
293+ /// or parses it as UTF-8 into an [`OsString`] on non-unix platforms.
276294///
277- /// It converts `&[u8]` to `Cow<OsStr>` for unix targets only.
278- /// On non-unix (i.e. Windows), the conversion goes through the String type
279- /// and thus undergo UTF-8 validation, making it fail if the stream contains
280- /// non-UTF-8 characters.
295+ /// This always succeeds on unix platforms,
296+ /// and fails on other platforms if the bytes can't be parsed as UTF-8.
281297pub fn os_str_from_bytes ( bytes : & [ u8 ] ) -> mods:: error:: UResult < Cow < ' _ , OsStr > > {
282298 #[ cfg( unix) ]
283299 let os_str = Cow :: Borrowed ( OsStr :: from_bytes ( bytes) ) ;
@@ -289,9 +305,10 @@ pub fn os_str_from_bytes(bytes: &[u8]) -> mods::error::UResult<Cow<'_, OsStr>> {
289305 Ok ( os_str)
290306}
291307
292- /// Helper function for making an `OsString` from a byte field
293- /// It converts `Vec<u8>` to `OsString` for unix targets only.
294- /// On non-unix (i.e. Windows) it may fail if the bytes are not valid UTF-8
308+ /// Converts a `Vec<u8>` into an `OsString`, parsing as UTF-8 on non-unix platforms.
309+ ///
310+ /// This always succeeds on unix platforms,
311+ /// and fails on other platforms if the bytes can't be parsed as UTF-8.
295312pub fn os_string_from_vec ( vec : Vec < u8 > ) -> mods:: error:: UResult < OsString > {
296313 #[ cfg( unix) ]
297314 let s = OsString :: from_vec ( vec) ;
0 commit comments