oceanpkg_shared/ext/
ffi.rs

1use std::ffi::OsStr;
2
3/// Extended functionality for
4/// [`OsStr`](https://doc.rust-lang.org/std/ffi/struct.OsStr.html).
5pub trait OsStrExt {
6    /// Attempts to retrieve the underlying bytes of `self`.
7    fn try_as_bytes(&self) -> Option<&[u8]>;
8}
9
10impl OsStrExt for OsStr {
11    #[inline]
12    fn try_as_bytes(&self) -> Option<&[u8]> {
13        #[cfg(unix)] {
14            use std::os::unix::ffi::OsStrExt;
15            Some(self.as_bytes())
16        }
17
18        // To get the bytes on non-Unix platforms, `OsStr` needs to be converted
19        // to a `str` first.
20        #[cfg(not(unix))] {
21            s.to_str().map(|s| s.as_bytes())
22        }
23    }
24}