+ /// Yield this `OsStr` as a byte slice.
+ ///
+ /// # Platform behavior
+ ///
+ /// On Unix systems, this is a no-op.
+ ///
+ /// On Windows systems, this returns `None` unless the `OsStr` is
+ /// valid unicode, in which case it produces UTF-8-encoded
+ /// data. This may entail checking validity.
+ #[unstable(feature = "convert", reason = "recently added")]
+ pub fn to_bytes(&self) -> Option<&[u8]> {
+ if cfg!(windows) {
+ self.to_str().map(|s| s.as_bytes())
+ } else {
+ Some(self.bytes())
+ }
+ }
+
+ /// Create a `CString` containing this `OsStr` data.
+ ///
+ /// Fails if the `OsStr` contains interior nulls.
+ ///
+ /// This is a convenience for creating a `CString` from
+ /// `self.to_bytes()`, and inherits the platform behavior of the
+ /// `to_bytes` method.
+ #[unstable(feature = "convert", reason = "recently added")]
+ pub fn to_cstring(&self) -> Option<CString> {
+ self.to_bytes().and_then(|b| CString::new(b).ok())
+ }
+