2020-08-27 13:45:01 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
|
use crate::borrow::{Borrow, Cow};
|
|
|
|
|
use crate::cmp;
|
2019-12-22 17:42:04 -05:00
|
|
|
|
use crate::fmt;
|
2019-02-11 04:23:21 +09:00
|
|
|
|
use crate::hash::{Hash, Hasher};
|
2019-12-22 17:42:04 -05:00
|
|
|
|
use crate::ops;
|
2019-02-11 04:23:21 +09:00
|
|
|
|
use crate::rc::Rc;
|
2020-04-29 18:13:52 +09:00
|
|
|
|
use crate::str::FromStr;
|
2019-02-11 04:23:21 +09:00
|
|
|
|
use crate::sync::Arc;
|
2015-01-21 15:55:31 -08:00
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
|
use crate::sys::os_str::{Buf, Slice};
|
2019-12-22 17:42:04 -05:00
|
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
2015-01-21 15:55:31 -08:00
|
|
|
|
|
2016-01-07 11:58:08 +01:00
|
|
|
|
/// A type that can represent owned, mutable platform-native strings, but is
|
2016-03-10 05:14:00 -07:00
|
|
|
|
/// cheaply inter-convertible with Rust strings.
|
2016-01-07 11:58:08 +01:00
|
|
|
|
///
|
|
|
|
|
/// The need for this type arises from the fact that:
|
|
|
|
|
///
|
|
|
|
|
/// * On Unix systems, strings are often arbitrary sequences of non-zero
|
|
|
|
|
/// bytes, in many cases interpreted as UTF-8.
|
|
|
|
|
///
|
|
|
|
|
/// * On Windows, strings are often arbitrary sequences of non-zero 16-bit
|
|
|
|
|
/// values, interpreted as UTF-16 when it is valid to do so.
|
|
|
|
|
///
|
2017-06-25 14:23:43 -07:00
|
|
|
|
/// * In Rust, strings are always valid UTF-8, which may contain zeros.
|
2016-01-07 11:58:08 +01:00
|
|
|
|
///
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// `OsString` and [`OsStr`] bridge this gap by simultaneously representing Rust
|
2016-01-07 11:58:08 +01:00
|
|
|
|
/// and platform-native string values, and in particular allowing a Rust string
|
2019-02-09 21:23:30 +00:00
|
|
|
|
/// to be converted into an "OS" string with no cost if possible. A consequence
|
2018-09-17 21:10:36 -04:00
|
|
|
|
/// of this is that `OsString` instances are *not* `NUL` terminated; in order
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// to pass to e.g., Unix system call, you should create a [`CStr`].
|
2017-09-25 13:51:28 -05:00
|
|
|
|
///
|
2018-01-11 22:01:32 +01:00
|
|
|
|
/// `OsString` is to [`&OsStr`] as [`String`] is to [`&str`]: the former
|
2017-09-25 13:51:28 -05:00
|
|
|
|
/// in each pair are owned strings; the latter are borrowed
|
|
|
|
|
/// references.
|
|
|
|
|
///
|
2019-07-07 15:38:53 +02:00
|
|
|
|
/// Note, `OsString` and [`OsStr`] internally do not necessarily hold strings in
|
2018-11-10 06:59:42 +00:00
|
|
|
|
/// the form native to the platform; While on Unix, strings are stored as a
|
|
|
|
|
/// sequence of 8-bit values, on Windows, where strings are 16-bit value based
|
|
|
|
|
/// as just discussed, strings are also actually stored as a sequence of 8-bit
|
|
|
|
|
/// values, encoded in a less-strict variant of UTF-8. This is useful to
|
|
|
|
|
/// understand when handling capacity and length values.
|
|
|
|
|
///
|
2017-09-25 13:51:28 -05:00
|
|
|
|
/// # Creating an `OsString`
|
|
|
|
|
///
|
|
|
|
|
/// **From a Rust string**: `OsString` implements
|
2017-10-20 15:29:35 -04:00
|
|
|
|
/// [`From`]`<`[`String`]`>`, so you can use `my_string.from` to
|
2017-09-25 13:51:28 -05:00
|
|
|
|
/// create an `OsString` from a normal Rust string.
|
|
|
|
|
///
|
|
|
|
|
/// **From slices:** Just like you can start with an empty Rust
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [`String`] and then [`String::push_str`] `&str`
|
2017-09-25 13:51:28 -05:00
|
|
|
|
/// sub-string slices into it, you can create an empty `OsString` with
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// the [`OsString::new`] method and then push string slices into it with the
|
|
|
|
|
/// [`OsString::push`] method.
|
2017-09-25 13:51:28 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Extracting a borrowed reference to the whole OS string
|
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// You can use the [`OsString::as_os_str`] method to get an `&`[`OsStr`] from
|
2017-09-25 13:51:28 -05:00
|
|
|
|
/// an `OsString`; this is effectively a borrowed reference to the
|
|
|
|
|
/// whole string.
|
|
|
|
|
///
|
|
|
|
|
/// # Conversions
|
|
|
|
|
///
|
2017-09-26 08:56:44 -05:00
|
|
|
|
/// See the [module's toplevel documentation about conversions][conversions] for a discussion on
|
2018-04-15 16:17:18 +02:00
|
|
|
|
/// the traits which `OsString` implements for [conversions] from/to native representations.
|
2017-01-21 16:38:54 +01:00
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [`&OsStr`]: OsStr
|
|
|
|
|
/// [`&str`]: str
|
|
|
|
|
/// [`CStr`]: crate::ffi::CStr
|
2020-10-12 13:42:49 -07:00
|
|
|
|
/// [conversions]: super#conversions
|
2015-01-21 15:55:31 -08:00
|
|
|
|
#[derive(Clone)]
|
2021-02-15 02:27:20 +00:00
|
|
|
|
#[cfg_attr(not(test), rustc_diagnostic_item = "os_string_type")]
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
pub struct OsString {
|
2019-12-22 17:42:04 -05:00
|
|
|
|
inner: Buf,
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 13:51:28 -05:00
|
|
|
|
/// Borrowed reference to an OS string (see [`OsString`]).
|
|
|
|
|
///
|
|
|
|
|
/// This type represents a borrowed reference to a string in the operating system's preferred
|
|
|
|
|
/// representation.
|
|
|
|
|
///
|
2018-01-11 22:01:32 +01:00
|
|
|
|
/// `&OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed
|
2017-09-25 13:51:28 -05:00
|
|
|
|
/// references; the latter are owned strings.
|
|
|
|
|
///
|
2017-09-26 08:56:44 -05:00
|
|
|
|
/// See the [module's toplevel documentation about conversions][conversions] for a discussion on
|
2018-04-15 16:17:18 +02:00
|
|
|
|
/// the traits which `OsStr` implements for [conversions] from/to native representations.
|
2017-01-21 16:38:54 +01:00
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [`&str`]: str
|
2020-10-12 13:42:49 -07:00
|
|
|
|
/// [conversions]: super#conversions
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-19 23:15:19 +03:00
|
|
|
|
// FIXME:
|
|
|
|
|
// `OsStr::from_inner` current implementation relies
|
|
|
|
|
// on `OsStr` being layout-compatible with `Slice`.
|
|
|
|
|
// When attribute privacy is implemented, `OsStr` should be annotated as `#[repr(transparent)]`.
|
2020-07-01 15:10:51 +02:00
|
|
|
|
// Anyway, `OsStr` representation and layout are considered implementation details, are
|
2019-06-19 23:15:19 +03:00
|
|
|
|
// not documented and must not be relied upon.
|
2015-01-21 15:55:31 -08:00
|
|
|
|
pub struct OsStr {
|
2019-12-22 17:42:04 -05:00
|
|
|
|
inner: Slice,
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl OsString {
|
2015-01-27 12:20:58 -08:00
|
|
|
|
/// Constructs a new empty `OsString`.
|
2017-01-21 09:26:06 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let os_string = OsString::new();
|
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
pub fn new() -> OsString {
|
|
|
|
|
OsString { inner: Buf::from_string(String::new()) }
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Converts to an [`OsStr`] slice.
|
|
|
|
|
///
|
2017-01-21 09:26:06 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::{OsString, OsStr};
|
|
|
|
|
///
|
|
|
|
|
/// let os_string = OsString::from("foo");
|
|
|
|
|
/// let os_str = OsStr::new("foo");
|
|
|
|
|
/// assert_eq!(os_string.as_os_str(), os_str);
|
|
|
|
|
/// ```
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-03-30 15:15:27 -07:00
|
|
|
|
pub fn as_os_str(&self) -> &OsStr {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Converts the `OsString` into a [`String`] if it contains valid Unicode data.
|
2015-01-21 15:55:31 -08:00
|
|
|
|
///
|
|
|
|
|
/// On failure, ownership of the original `OsString` is returned.
|
2017-01-21 09:26:06 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let os_string = OsString::from("foo");
|
|
|
|
|
/// let string = os_string.into_string();
|
|
|
|
|
/// assert_eq!(string, Ok(String::from("foo")));
|
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
pub fn into_string(self) -> Result<String, OsString> {
|
2019-12-22 17:42:04 -05:00
|
|
|
|
self.inner.into_string().map_err(|buf| OsString { inner: buf })
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Extends the string with the given [`&OsStr`] slice.
|
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [`&OsStr`]: OsStr
|
2017-01-21 09:26:06 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut os_string = OsString::from("foo");
|
|
|
|
|
/// os_string.push("bar");
|
|
|
|
|
/// assert_eq!(&os_string, "foobar");
|
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
|
|
|
|
|
self.inner.push_slice(&s.as_ref().inner)
|
|
|
|
|
}
|
2016-02-12 12:21:57 -05:00
|
|
|
|
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// Creates a new `OsString` with the given capacity.
|
|
|
|
|
///
|
2017-08-15 21:45:21 +02:00
|
|
|
|
/// The string will be able to hold exactly `capacity` length units of other
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// OS strings without reallocating. If `capacity` is 0, the string will not
|
|
|
|
|
/// allocate.
|
2016-02-12 12:21:57 -05:00
|
|
|
|
///
|
|
|
|
|
/// See main `OsString` documentation information about encoding.
|
2017-01-21 09:26:06 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
2019-08-22 19:27:16 +02:00
|
|
|
|
/// let mut os_string = OsString::with_capacity(10);
|
2017-01-21 09:26:06 -05:00
|
|
|
|
/// let capacity = os_string.capacity();
|
|
|
|
|
///
|
|
|
|
|
/// // This push is done without reallocating
|
|
|
|
|
/// os_string.push("foo");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(capacity, os_string.capacity());
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn with_capacity(capacity: usize) -> OsString {
|
2019-12-22 17:42:04 -05:00
|
|
|
|
OsString { inner: Buf::with_capacity(capacity) }
|
2016-02-12 12:21:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Truncates the `OsString` to zero length.
|
2017-01-21 09:26:06 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut os_string = OsString::from("foo");
|
|
|
|
|
/// assert_eq!(&os_string, "foo");
|
|
|
|
|
///
|
|
|
|
|
/// os_string.clear();
|
|
|
|
|
/// assert_eq!(&os_string, "");
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
|
self.inner.clear()
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// Returns the capacity this `OsString` can hold without reallocating.
|
2016-02-12 12:21:57 -05:00
|
|
|
|
///
|
|
|
|
|
/// See `OsString` introduction for information about encoding.
|
2017-01-21 09:26:06 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
2019-08-22 19:27:16 +02:00
|
|
|
|
/// let os_string = OsString::with_capacity(10);
|
2017-01-21 09:26:06 -05:00
|
|
|
|
/// assert!(os_string.capacity() >= 10);
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
|
self.inner.capacity()
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// Reserves capacity for at least `additional` more capacity to be inserted
|
|
|
|
|
/// in the given `OsString`.
|
|
|
|
|
///
|
|
|
|
|
/// The collection may reserve more space to avoid frequent reallocations.
|
2017-03-12 16:21:34 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut s = OsString::new();
|
|
|
|
|
/// s.reserve(10);
|
|
|
|
|
/// assert!(s.capacity() >= 10);
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn reserve(&mut self, additional: usize) {
|
|
|
|
|
self.inner.reserve(additional)
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// Reserves the minimum capacity for exactly `additional` more capacity to
|
|
|
|
|
/// be inserted in the given `OsString`. Does nothing if the capacity is
|
2016-02-12 12:21:57 -05:00
|
|
|
|
/// already sufficient.
|
|
|
|
|
///
|
|
|
|
|
/// Note that the allocator may give the collection more space than it
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// requests. Therefore, capacity can not be relied upon to be precisely
|
2016-02-12 12:21:57 -05:00
|
|
|
|
/// minimal. Prefer reserve if future insertions are expected.
|
2017-03-12 16:21:58 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut s = OsString::new();
|
|
|
|
|
/// s.reserve_exact(10);
|
|
|
|
|
/// assert!(s.capacity() >= 10);
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn reserve_exact(&mut self, additional: usize) {
|
|
|
|
|
self.inner.reserve_exact(additional)
|
|
|
|
|
}
|
2017-01-31 22:46:16 -05:00
|
|
|
|
|
2017-03-10 00:23:54 -05:00
|
|
|
|
/// Shrinks the capacity of the `OsString` to match its length.
|
2017-03-12 16:22:12 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut s = OsString::from("foo");
|
|
|
|
|
///
|
|
|
|
|
/// s.reserve(100);
|
|
|
|
|
/// assert!(s.capacity() >= 100);
|
|
|
|
|
///
|
|
|
|
|
/// s.shrink_to_fit();
|
|
|
|
|
/// assert_eq!(3, s.capacity());
|
|
|
|
|
/// ```
|
2017-06-08 11:35:46 -04:00
|
|
|
|
#[stable(feature = "osstring_shrink_to_fit", since = "1.19.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2017-03-10 00:23:54 -05:00
|
|
|
|
pub fn shrink_to_fit(&mut self) {
|
|
|
|
|
self.inner.shrink_to_fit()
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-26 23:24:31 +01:00
|
|
|
|
/// Shrinks the capacity of the `OsString` with a lower bound.
|
|
|
|
|
///
|
|
|
|
|
/// The capacity will remain at least as large as both the length
|
|
|
|
|
/// and the supplied value.
|
|
|
|
|
///
|
2021-01-24 13:29:39 +01:00
|
|
|
|
/// If the current capacity is less than the lower limit, this is a no-op.
|
2018-03-26 23:24:31 +01:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(shrink_to)]
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut s = OsString::from("foo");
|
|
|
|
|
///
|
|
|
|
|
/// s.reserve(100);
|
|
|
|
|
/// assert!(s.capacity() >= 100);
|
|
|
|
|
///
|
|
|
|
|
/// s.shrink_to(10);
|
|
|
|
|
/// assert!(s.capacity() >= 10);
|
|
|
|
|
/// s.shrink_to(0);
|
|
|
|
|
/// assert!(s.capacity() >= 3);
|
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
|
2018-03-26 23:24:31 +01:00
|
|
|
|
pub fn shrink_to(&mut self, min_capacity: usize) {
|
|
|
|
|
self.inner.shrink_to(min_capacity)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 19:33:40 +02:00
|
|
|
|
/// Converts this `OsString` into a boxed [`OsStr`].
|
|
|
|
|
///
|
2017-03-12 16:22:29 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::{OsString, OsStr};
|
|
|
|
|
///
|
|
|
|
|
/// let s = OsString::from("hello");
|
|
|
|
|
///
|
|
|
|
|
/// let b: Box<OsStr> = s.into_boxed_os_str();
|
|
|
|
|
/// ```
|
2017-07-20 15:44:35 -07:00
|
|
|
|
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
|
2017-01-31 22:46:16 -05:00
|
|
|
|
pub fn into_boxed_os_str(self) -> Box<OsStr> {
|
2017-09-28 08:07:49 -04:00
|
|
|
|
let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;
|
|
|
|
|
unsafe { Box::from_raw(rw) }
|
2017-01-31 22:46:16 -05:00
|
|
|
|
}
|
2015-03-18 09:14:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl From<String> for OsString {
|
2018-07-11 13:21:10 +02:00
|
|
|
|
/// Converts a [`String`] into a [`OsString`].
|
2018-07-23 15:38:15 +02:00
|
|
|
|
///
|
2018-07-11 13:21:10 +02:00
|
|
|
|
/// The conversion copies the data, and includes an allocation on the heap.
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn from(s: String) -> OsString {
|
|
|
|
|
OsString { inner: Buf::from_string(s) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-03-09 19:10:28 -08:00
|
|
|
|
impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
|
|
|
|
|
fn from(s: &T) -> OsString {
|
2015-03-26 13:39:23 -07:00
|
|
|
|
s.as_ref().to_os_string()
|
2015-03-02 10:46:05 -08:00
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-21 19:33:27 -04:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl ops::Index<ops::RangeFull> for OsString {
|
|
|
|
|
type Output = OsStr;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index(&self, _index: ops::RangeFull) -> &OsStr {
|
2015-07-24 03:04:55 +02:00
|
|
|
|
OsStr::from_inner(self.inner.as_slice())
|
2015-03-21 19:33:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 13:13:07 -04:00
|
|
|
|
#[stable(feature = "mut_osstr", since = "1.44.0")]
|
|
|
|
|
impl ops::IndexMut<ops::RangeFull> for OsString {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut OsStr {
|
|
|
|
|
OsStr::from_inner_mut(self.inner.as_mut_slice())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl ops::Deref for OsString {
|
|
|
|
|
type Target = OsStr;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref(&self) -> &OsStr {
|
2015-02-18 14:48:57 -05:00
|
|
|
|
&self[..]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 13:13:07 -04:00
|
|
|
|
#[stable(feature = "mut_osstr", since = "1.44.0")]
|
|
|
|
|
impl ops::DerefMut for OsString {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref_mut(&mut self) -> &mut OsStr {
|
|
|
|
|
&mut self[..]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 07:38:44 +08:00
|
|
|
|
#[stable(feature = "osstring_default", since = "1.9.0")]
|
2016-03-22 00:45:08 +08:00
|
|
|
|
impl Default for OsString {
|
2016-09-11 17:00:09 +05:30
|
|
|
|
/// Constructs an empty `OsString`.
|
2016-03-22 00:45:08 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn default() -> OsString {
|
|
|
|
|
OsString::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-06-12 22:21:53 +03:00
|
|
|
|
impl fmt::Debug for OsString {
|
2019-03-01 09:34:11 +01:00
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-01-21 15:55:31 -08:00
|
|
|
|
fmt::Debug::fmt(&**self, formatter)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
impl PartialEq for OsString {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
fn eq(&self, other: &OsString) -> bool {
|
|
|
|
|
&**self == &**other
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
impl PartialEq<str> for OsString {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
fn eq(&self, other: &str) -> bool {
|
|
|
|
|
&**self == other
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
impl PartialEq<OsString> for str {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
fn eq(&self, other: &OsString) -> bool {
|
|
|
|
|
&**other == self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 14:25:43 -07:00
|
|
|
|
#[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
|
2019-03-09 19:10:28 -08:00
|
|
|
|
impl PartialEq<&str> for OsString {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2019-03-09 19:10:28 -08:00
|
|
|
|
fn eq(&self, other: &&str) -> bool {
|
2018-05-29 19:16:49 +03:00
|
|
|
|
**self == **other
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 14:25:43 -07:00
|
|
|
|
#[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
|
2018-05-29 19:16:49 +03:00
|
|
|
|
impl<'a> PartialEq<OsString> for &'a str {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2018-05-29 19:16:49 +03:00
|
|
|
|
fn eq(&self, other: &OsString) -> bool {
|
|
|
|
|
**other == **self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
impl Eq for OsString {}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
impl PartialOrd for OsString {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
|
|
|
|
|
(&**self).partial_cmp(&**other)
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn lt(&self, other: &OsString) -> bool {
|
|
|
|
|
&**self < &**other
|
|
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn le(&self, other: &OsString) -> bool {
|
|
|
|
|
&**self <= &**other
|
|
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn gt(&self, other: &OsString) -> bool {
|
|
|
|
|
&**self > &**other
|
|
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn ge(&self, other: &OsString) -> bool {
|
|
|
|
|
&**self >= &**other
|
|
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
impl PartialOrd<str> for OsString {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
|
|
|
|
|
(&**self).partial_cmp(other)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 12:20:58 -08:00
|
|
|
|
impl Ord for OsString {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn cmp(&self, other: &OsString) -> cmp::Ordering {
|
|
|
|
|
(&**self).cmp(&**other)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 20:48:07 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl Hash for OsString {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
(&**self).hash(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl OsStr {
|
2015-04-13 10:21:32 -04:00
|
|
|
|
/// Coerces into an `OsStr` slice.
|
2016-08-17 22:56:43 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsStr;
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("foo");
|
|
|
|
|
/// ```
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2015-03-30 15:15:27 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &OsStr {
|
|
|
|
|
s.as_ref()
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2015-07-24 03:04:55 +02:00
|
|
|
|
fn from_inner(inner: &Slice) -> &OsStr {
|
2020-09-08 22:26:44 -04:00
|
|
|
|
// SAFETY: OsStr is just a wrapper of Slice,
|
2020-03-16 16:12:54 -04:00
|
|
|
|
// therefore converting &Slice to &OsStr is safe.
|
2017-09-28 08:07:49 -04:00
|
|
|
|
unsafe { &*(inner as *const Slice as *const OsStr) }
|
2015-07-24 03:04:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 13:13:07 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
|
2020-09-08 22:26:44 -04:00
|
|
|
|
// SAFETY: OsStr is just a wrapper of Slice,
|
2020-03-16 16:12:54 -04:00
|
|
|
|
// therefore converting &mut Slice to &mut OsStr is safe.
|
2020-03-20 09:00:53 -04:00
|
|
|
|
// Any method that mutates OsStr must be careful not to
|
|
|
|
|
// break platform-specific encoding, in particular Wtf8 on Windows.
|
2020-03-16 13:13:07 -04:00
|
|
|
|
unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
|
2015-01-21 15:55:31 -08:00
|
|
|
|
///
|
|
|
|
|
/// This conversion may entail doing a check for UTF-8 validity.
|
2017-01-04 22:47:23 -05:00
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [`&str`]: str
|
2017-01-21 16:38:54 +01:00
|
|
|
|
///
|
2017-01-04 22:47:23 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsStr;
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("foo");
|
|
|
|
|
/// assert_eq!(os_str.to_str(), Some("foo"));
|
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
pub fn to_str(&self) -> Option<&str> {
|
|
|
|
|
self.inner.to_str()
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Converts an `OsStr` to a [`Cow`]`<`[`str`]`>`.
|
2015-01-21 15:55:31 -08:00
|
|
|
|
///
|
2018-08-11 14:09:59 -04:00
|
|
|
|
/// Any non-Unicode sequences are replaced with
|
|
|
|
|
/// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
|
2017-01-04 22:47:23 -05:00
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
|
2017-01-21 16:38:54 +01:00
|
|
|
|
///
|
2017-01-04 22:47:23 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2018-11-10 06:17:36 +00:00
|
|
|
|
/// Calling `to_string_lossy` on an `OsStr` with invalid unicode:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// // Note, due to differences in how Unix and Windows represent strings,
|
|
|
|
|
/// // we are forced to complicate this example, setting up example `OsStr`s
|
|
|
|
|
/// // with different source data and via different platform extensions.
|
|
|
|
|
/// // Understand that in reality you could end up with such example invalid
|
|
|
|
|
/// // sequences simply through collecting user command line arguments, for
|
|
|
|
|
/// // example.
|
|
|
|
|
///
|
|
|
|
|
/// #[cfg(any(unix, target_os = "redox"))] {
|
|
|
|
|
/// use std::ffi::OsStr;
|
|
|
|
|
/// use std::os::unix::ffi::OsStrExt;
|
|
|
|
|
///
|
|
|
|
|
/// // Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
|
|
|
|
|
/// // respectively. The value 0x80 is a lone continuation byte, invalid
|
|
|
|
|
/// // in a UTF-8 sequence.
|
|
|
|
|
/// let source = [0x66, 0x6f, 0x80, 0x6f];
|
|
|
|
|
/// let os_str = OsStr::from_bytes(&source[..]);
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(os_str.to_string_lossy(), "fo<66>o");
|
|
|
|
|
/// }
|
|
|
|
|
/// #[cfg(windows)] {
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
/// use std::os::windows::prelude::*;
|
|
|
|
|
///
|
|
|
|
|
/// // Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
|
|
|
|
|
/// // respectively. The value 0xD800 is a lone surrogate half, invalid
|
|
|
|
|
/// // in a UTF-16 sequence.
|
|
|
|
|
/// let source = [0x0066, 0x006f, 0xD800, 0x006f];
|
|
|
|
|
/// let os_string = OsString::from_wide(&source[..]);
|
|
|
|
|
/// let os_str = os_string.as_os_str();
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(os_str.to_string_lossy(), "fo<66>o");
|
|
|
|
|
/// }
|
2017-01-04 22:47:23 -05:00
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2019-03-01 09:34:11 +01:00
|
|
|
|
pub fn to_string_lossy(&self) -> Cow<'_, str> {
|
2015-01-21 15:55:31 -08:00
|
|
|
|
self.inner.to_string_lossy()
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Copies the slice into an owned [`OsString`].
|
|
|
|
|
///
|
2017-03-12 15:04:32 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::{OsStr, OsString};
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("foo");
|
|
|
|
|
/// let os_string = os_str.to_os_string();
|
|
|
|
|
/// assert_eq!(os_string, OsString::from("foo"));
|
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
pub fn to_os_string(&self) -> OsString {
|
|
|
|
|
OsString { inner: self.inner.to_owned() }
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 12:21:57 -05:00
|
|
|
|
/// Checks whether the `OsStr` is empty.
|
2016-08-17 22:56:43 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsStr;
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("");
|
|
|
|
|
/// assert!(os_str.is_empty());
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("foo");
|
|
|
|
|
/// assert!(!os_str.is_empty());
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
2020-01-10 18:20:40 +00:00
|
|
|
|
#[inline]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.inner.inner.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// Returns the length of this `OsStr`.
|
|
|
|
|
///
|
2018-11-10 07:35:15 +00:00
|
|
|
|
/// Note that this does **not** return the number of bytes in the string in
|
|
|
|
|
/// OS string form.
|
2016-02-12 12:21:57 -05:00
|
|
|
|
///
|
2019-08-22 13:14:42 +02:00
|
|
|
|
/// The length returned is that of the underlying storage used by `OsStr`.
|
2018-11-10 07:35:15 +00:00
|
|
|
|
/// As discussed in the [`OsString`] introduction, [`OsString`] and `OsStr`
|
|
|
|
|
/// store strings in a form best suited for cheap inter-conversion between
|
|
|
|
|
/// native-platform and Rust string forms, which may differ significantly
|
|
|
|
|
/// from both of them, including in storage size and encoding.
|
2016-08-17 22:56:43 -04:00
|
|
|
|
///
|
2018-11-10 07:35:15 +00:00
|
|
|
|
/// This number is simply useful for passing to other methods, like
|
|
|
|
|
/// [`OsString::with_capacity`] to avoid reallocations.
|
|
|
|
|
///
|
2016-08-17 22:56:43 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsStr;
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("");
|
|
|
|
|
/// assert_eq!(os_str.len(), 0);
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("foo");
|
|
|
|
|
/// assert_eq!(os_str.len(), 3);
|
|
|
|
|
/// ```
|
2020-12-28 09:13:46 +01:00
|
|
|
|
#[doc(alias = "length")]
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.inner.inner.len()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 19:33:40 +02:00
|
|
|
|
/// Converts a [`Box`]`<OsStr>` into an [`OsString`] without copying or allocating.
|
2017-07-20 15:44:35 -07:00
|
|
|
|
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
|
2017-02-13 20:37:42 -05:00
|
|
|
|
pub fn into_os_string(self: Box<OsStr>) -> OsString {
|
2017-09-28 08:07:49 -04:00
|
|
|
|
let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
|
|
|
|
|
OsString { inner: Buf::from_box(boxed) }
|
2017-02-13 20:37:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
|
/// Gets the underlying byte representation.
|
2015-01-21 15:55:31 -08:00
|
|
|
|
///
|
2020-11-03 19:26:31 +01:00
|
|
|
|
/// Note: it is *crucial* that this API is not externally public, to avoid
|
2015-01-21 15:55:31 -08:00
|
|
|
|
/// revealing the internal, platform-specific encodings.
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2020-11-03 19:26:31 +01:00
|
|
|
|
pub(crate) fn bytes(&self) -> &[u8] {
|
2017-09-30 10:01:41 -04:00
|
|
|
|
unsafe { &*(&self.inner as *const _ as *const [u8]) }
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
2020-03-11 15:53:55 -04:00
|
|
|
|
|
|
|
|
|
/// Converts this string to its ASCII lower case equivalent in-place.
|
|
|
|
|
///
|
|
|
|
|
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
|
|
|
|
|
/// but non-ASCII letters are unchanged.
|
|
|
|
|
///
|
|
|
|
|
/// To return a new lowercased value without modifying the existing one, use
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [`OsStr::to_ascii_lowercase`].
|
2020-03-11 15:53:55 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(osstring_ascii)]
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
|
|
|
|
|
///
|
|
|
|
|
/// s.make_ascii_lowercase();
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!("grÜße, jÜrgen ❤", s);
|
|
|
|
|
/// ```
|
2020-03-28 19:23:49 -04:00
|
|
|
|
#[unstable(feature = "osstring_ascii", issue = "70516")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2020-03-11 15:53:55 -04:00
|
|
|
|
pub fn make_ascii_lowercase(&mut self) {
|
|
|
|
|
self.inner.make_ascii_lowercase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Converts this string to its ASCII upper case equivalent in-place.
|
|
|
|
|
///
|
|
|
|
|
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
|
|
|
|
|
/// but non-ASCII letters are unchanged.
|
|
|
|
|
///
|
|
|
|
|
/// To return a new uppercased value without modifying the existing one, use
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// [`OsStr::to_ascii_uppercase`].
|
2020-03-11 15:53:55 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(osstring_ascii)]
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let mut s = OsString::from("Grüße, Jürgen ❤");
|
|
|
|
|
///
|
|
|
|
|
/// s.make_ascii_uppercase();
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!("GRüßE, JüRGEN ❤", s);
|
|
|
|
|
/// ```
|
2020-03-28 19:23:49 -04:00
|
|
|
|
#[unstable(feature = "osstring_ascii", issue = "70516")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2020-03-11 15:53:55 -04:00
|
|
|
|
pub fn make_ascii_uppercase(&mut self) {
|
|
|
|
|
self.inner.make_ascii_uppercase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a copy of this string where each character is mapped to its
|
|
|
|
|
/// ASCII lower case equivalent.
|
|
|
|
|
///
|
|
|
|
|
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
|
|
|
|
|
/// but non-ASCII letters are unchanged.
|
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// To lowercase the value in-place, use [`OsStr::make_ascii_lowercase`].
|
2020-03-12 12:40:57 -04:00
|
|
|
|
///
|
2020-03-11 15:53:55 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(osstring_ascii)]
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
/// let s = OsString::from("Grüße, Jürgen ❤");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
|
|
|
|
|
/// ```
|
2020-03-28 19:23:49 -04:00
|
|
|
|
#[unstable(feature = "osstring_ascii", issue = "70516")]
|
2020-03-11 15:53:55 -04:00
|
|
|
|
pub fn to_ascii_lowercase(&self) -> OsString {
|
|
|
|
|
OsString::from_inner(self.inner.to_ascii_lowercase())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a copy of this string where each character is mapped to its
|
|
|
|
|
/// ASCII upper case equivalent.
|
|
|
|
|
///
|
|
|
|
|
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
|
|
|
|
|
/// but non-ASCII letters are unchanged.
|
|
|
|
|
///
|
2020-08-13 23:19:45 +02:00
|
|
|
|
/// To uppercase the value in-place, use [`OsStr::make_ascii_uppercase`].
|
2020-03-12 12:40:57 -04:00
|
|
|
|
///
|
2020-03-11 15:53:55 -04:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(osstring_ascii)]
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
/// let s = OsString::from("Grüße, Jürgen ❤");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
|
|
|
|
|
/// ```
|
2020-03-28 19:23:49 -04:00
|
|
|
|
#[unstable(feature = "osstring_ascii", issue = "70516")]
|
2020-03-11 15:53:55 -04:00
|
|
|
|
pub fn to_ascii_uppercase(&self) -> OsString {
|
|
|
|
|
OsString::from_inner(self.inner.to_ascii_uppercase())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Checks if all characters in this string are within the ASCII range.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(osstring_ascii)]
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// let ascii = OsString::from("hello!\n");
|
|
|
|
|
/// let non_ascii = OsString::from("Grüße, Jürgen ❤");
|
|
|
|
|
///
|
|
|
|
|
/// assert!(ascii.is_ascii());
|
|
|
|
|
/// assert!(!non_ascii.is_ascii());
|
|
|
|
|
/// ```
|
2020-03-28 19:23:49 -04:00
|
|
|
|
#[unstable(feature = "osstring_ascii", issue = "70516")]
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2020-03-11 15:53:55 -04:00
|
|
|
|
pub fn is_ascii(&self) -> bool {
|
|
|
|
|
self.inner.is_ascii()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Checks that two strings are an ASCII case-insensitive match.
|
|
|
|
|
///
|
|
|
|
|
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
|
|
|
|
|
/// but without allocating and copying temporaries.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(osstring_ascii)]
|
|
|
|
|
/// use std::ffi::OsString;
|
|
|
|
|
///
|
|
|
|
|
/// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
|
|
|
|
|
/// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
|
|
|
|
|
/// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
|
|
|
|
|
/// ```
|
2020-03-28 19:23:49 -04:00
|
|
|
|
#[unstable(feature = "osstring_ascii", issue = "70516")]
|
2021-02-03 10:28:51 -05:00
|
|
|
|
pub fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> bool {
|
2020-03-11 15:53:55 -04:00
|
|
|
|
self.inner.eq_ignore_ascii_case(&other.as_ref().inner)
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 22:46:16 -05:00
|
|
|
|
#[stable(feature = "box_from_os_str", since = "1.17.0")]
|
2019-03-09 19:10:28 -08:00
|
|
|
|
impl From<&OsStr> for Box<OsStr> {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2019-03-09 19:10:28 -08:00
|
|
|
|
fn from(s: &OsStr) -> Box<OsStr> {
|
2017-09-28 08:07:49 -04:00
|
|
|
|
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
|
|
|
|
|
unsafe { Box::from_raw(rw) }
|
2017-01-31 22:46:16 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 13:03:05 -07:00
|
|
|
|
#[stable(feature = "box_from_cow", since = "1.45.0")]
|
|
|
|
|
impl From<Cow<'_, OsStr>> for Box<OsStr> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
|
|
|
|
|
match cow {
|
|
|
|
|
Cow::Borrowed(s) => Box::from(s),
|
|
|
|
|
Cow::Owned(s) => Box::from(s),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 08:38:39 +01:00
|
|
|
|
#[stable(feature = "os_string_from_box", since = "1.18.0")]
|
2017-05-20 15:32:11 -04:00
|
|
|
|
impl From<Box<OsStr>> for OsString {
|
2019-07-07 15:38:53 +02:00
|
|
|
|
/// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or
|
|
|
|
|
/// allocating.
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2017-02-13 20:37:42 -05:00
|
|
|
|
fn from(boxed: Box<OsStr>) -> OsString {
|
|
|
|
|
boxed.into_os_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 00:08:37 +01:00
|
|
|
|
#[stable(feature = "box_from_os_string", since = "1.20.0")]
|
2017-05-20 15:40:53 -04:00
|
|
|
|
impl From<OsString> for Box<OsStr> {
|
2018-07-11 13:21:10 +02:00
|
|
|
|
/// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2017-05-20 15:40:53 -04:00
|
|
|
|
fn from(s: OsString) -> Box<OsStr> {
|
|
|
|
|
s.into_boxed_os_str()
|
2017-02-13 20:37:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 11:49:32 -07:00
|
|
|
|
#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
|
|
|
|
|
impl Clone for Box<OsStr> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
self.to_os_string().into_boxed_os_str()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-27 14:11:05 +00:00
|
|
|
|
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
|
2017-11-14 12:31:07 -07:00
|
|
|
|
impl From<OsString> for Arc<OsStr> {
|
2018-07-11 13:21:10 +02:00
|
|
|
|
/// Converts a [`OsString`] into a [`Arc`]`<OsStr>` without copying or allocating.
|
2017-11-14 12:31:07 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: OsString) -> Arc<OsStr> {
|
|
|
|
|
let arc = s.inner.into_arc();
|
|
|
|
|
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-27 14:11:05 +00:00
|
|
|
|
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
|
2019-03-09 19:10:28 -08:00
|
|
|
|
impl From<&OsStr> for Arc<OsStr> {
|
2017-11-14 12:31:07 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: &OsStr) -> Arc<OsStr> {
|
|
|
|
|
let arc = s.inner.into_arc();
|
|
|
|
|
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-27 14:11:05 +00:00
|
|
|
|
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
|
2017-11-14 12:31:07 -07:00
|
|
|
|
impl From<OsString> for Rc<OsStr> {
|
2018-07-11 13:21:10 +02:00
|
|
|
|
/// Converts a [`OsString`] into a [`Rc`]`<OsStr>` without copying or allocating.
|
2017-11-14 12:31:07 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: OsString) -> Rc<OsStr> {
|
|
|
|
|
let rc = s.inner.into_rc();
|
|
|
|
|
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-27 14:11:05 +00:00
|
|
|
|
#[stable(feature = "shared_from_slice2", since = "1.24.0")]
|
2019-03-09 19:10:28 -08:00
|
|
|
|
impl From<&OsStr> for Rc<OsStr> {
|
2017-11-14 12:31:07 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: &OsStr) -> Rc<OsStr> {
|
|
|
|
|
let rc = s.inner.into_rc();
|
|
|
|
|
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 07:23:02 +01:00
|
|
|
|
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
|
2018-04-22 22:57:52 +01:00
|
|
|
|
impl<'a> From<OsString> for Cow<'a, OsStr> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: OsString) -> Cow<'a, OsStr> {
|
|
|
|
|
Cow::Owned(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 07:23:02 +01:00
|
|
|
|
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
|
2018-04-22 22:57:52 +01:00
|
|
|
|
impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
|
|
|
|
|
Cow::Borrowed(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 07:23:02 +01:00
|
|
|
|
#[stable(feature = "cow_from_osstr", since = "1.28.0")]
|
2018-04-22 22:57:52 +01:00
|
|
|
|
impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: &'a OsString) -> Cow<'a, OsStr> {
|
|
|
|
|
Cow::Borrowed(s.as_os_str())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 07:23:02 +01:00
|
|
|
|
#[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
|
2018-04-27 20:27:38 +01:00
|
|
|
|
impl<'a> From<Cow<'a, OsStr>> for OsString {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn from(s: Cow<'a, OsStr>) -> Self {
|
|
|
|
|
s.into_owned()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 22:46:16 -05:00
|
|
|
|
#[stable(feature = "box_default_extra", since = "1.17.0")]
|
|
|
|
|
impl Default for Box<OsStr> {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2017-01-31 22:46:16 -05:00
|
|
|
|
fn default() -> Box<OsStr> {
|
2017-09-28 08:07:49 -04:00
|
|
|
|
let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
|
|
|
|
|
unsafe { Box::from_raw(rw) }
|
2017-01-31 22:46:16 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 07:38:44 +08:00
|
|
|
|
#[stable(feature = "osstring_default", since = "1.9.0")]
|
2019-02-17 19:42:36 -08:00
|
|
|
|
impl Default for &OsStr {
|
2016-09-11 17:00:09 +05:30
|
|
|
|
/// Creates an empty `OsStr`.
|
2016-03-22 00:45:36 +08:00
|
|
|
|
#[inline]
|
2019-02-17 19:42:36 -08:00
|
|
|
|
fn default() -> Self {
|
2016-03-22 07:38:44 +08:00
|
|
|
|
OsStr::new("")
|
2016-03-22 00:45:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl PartialEq for OsStr {
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
fn eq(&self, other: &OsStr) -> bool {
|
|
|
|
|
self.bytes().eq(other.bytes())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl PartialEq<str> for OsStr {
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
fn eq(&self, other: &str) -> bool {
|
2015-03-30 15:15:27 -07:00
|
|
|
|
*self == *OsStr::new(other)
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl PartialEq<OsStr> for str {
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
fn eq(&self, other: &OsStr) -> bool {
|
2015-03-30 15:15:27 -07:00
|
|
|
|
*other == *OsStr::new(self)
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl Eq for OsStr {}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl PartialOrd for OsStr {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
|
|
|
|
|
self.bytes().partial_cmp(other.bytes())
|
|
|
|
|
}
|
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn lt(&self, other: &OsStr) -> bool {
|
|
|
|
|
self.bytes().lt(other.bytes())
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn le(&self, other: &OsStr) -> bool {
|
|
|
|
|
self.bytes().le(other.bytes())
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn gt(&self, other: &OsStr) -> bool {
|
|
|
|
|
self.bytes().gt(other.bytes())
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn ge(&self, other: &OsStr) -> bool {
|
|
|
|
|
self.bytes().ge(other.bytes())
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl PartialOrd<str> for OsStr {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
|
2015-03-30 15:15:27 -07:00
|
|
|
|
self.partial_cmp(OsStr::new(other))
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME (#19470): cannot provide PartialOrd<OsStr> for str until we
|
|
|
|
|
// have more flexible coherence rules.
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl Ord for OsStr {
|
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn cmp(&self, other: &OsStr) -> cmp::Ordering {
|
|
|
|
|
self.bytes().cmp(other.bytes())
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-18 16:03:42 +02:00
|
|
|
|
macro_rules! impl_cmp {
|
|
|
|
|
($lhs:ty, $rhs: ty) => {
|
|
|
|
|
#[stable(feature = "cmp_os_str", since = "1.8.0")]
|
|
|
|
|
impl<'a, 'b> PartialEq<$rhs> for $lhs {
|
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn eq(&self, other: &$rhs) -> bool {
|
|
|
|
|
<OsStr as PartialEq>::eq(self, other)
|
|
|
|
|
}
|
2016-02-18 16:03:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "cmp_os_str", since = "1.8.0")]
|
|
|
|
|
impl<'a, 'b> PartialEq<$lhs> for $rhs {
|
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn eq(&self, other: &$lhs) -> bool {
|
|
|
|
|
<OsStr as PartialEq>::eq(self, other)
|
|
|
|
|
}
|
2016-02-18 16:03:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "cmp_os_str", since = "1.8.0")]
|
|
|
|
|
impl<'a, 'b> PartialOrd<$rhs> for $lhs {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
|
|
|
|
|
<OsStr as PartialOrd>::partial_cmp(self, other)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "cmp_os_str", since = "1.8.0")]
|
|
|
|
|
impl<'a, 'b> PartialOrd<$lhs> for $rhs {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
|
|
|
|
|
<OsStr as PartialOrd>::partial_cmp(self, other)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
|
};
|
2016-02-18 16:03:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl_cmp!(OsString, OsStr);
|
|
|
|
|
impl_cmp!(OsString, &'a OsStr);
|
|
|
|
|
impl_cmp!(Cow<'a, OsStr>, OsStr);
|
|
|
|
|
impl_cmp!(Cow<'a, OsStr>, &'b OsStr);
|
|
|
|
|
impl_cmp!(Cow<'a, OsStr>, OsString);
|
|
|
|
|
|
2015-02-17 20:48:07 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl Hash for OsStr {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.bytes().hash(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-06-12 22:21:53 +03:00
|
|
|
|
impl fmt::Debug for OsStr {
|
2019-03-01 09:34:11 +01:00
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-06-12 22:21:53 +03:00
|
|
|
|
fmt::Debug::fmt(&self.inner, formatter)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl OsStr {
|
2019-03-01 09:34:11 +01:00
|
|
|
|
pub(crate) fn display(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-06-12 22:21:53 +03:00
|
|
|
|
fmt::Display::fmt(&self.inner, formatter)
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-11 23:16:32 -08:00
|
|
|
|
impl Borrow<OsStr> for OsString {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2019-12-22 17:42:04 -05:00
|
|
|
|
fn borrow(&self) -> &OsStr {
|
|
|
|
|
&self[..]
|
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-11 23:16:32 -08:00
|
|
|
|
impl ToOwned for OsStr {
|
|
|
|
|
type Owned = OsString;
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2017-04-13 02:48:46 -07:00
|
|
|
|
fn to_owned(&self) -> OsString {
|
|
|
|
|
self.to_os_string()
|
|
|
|
|
}
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2017-04-13 02:48:46 -07:00
|
|
|
|
fn clone_into(&self, target: &mut OsString) {
|
2020-03-20 15:46:40 -07:00
|
|
|
|
self.inner.clone_into(&mut target.inner)
|
2017-04-13 02:48:46 -07:00
|
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl AsRef<OsStr> for OsStr {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &OsStr {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl AsRef<OsStr> for OsString {
|
2020-01-10 19:06:18 +00:00
|
|
|
|
#[inline]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &OsStr {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl AsRef<OsStr> for str {
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &OsStr {
|
2015-07-24 03:04:55 +02:00
|
|
|
|
OsStr::from_inner(Slice::from_str(self))
|
2015-03-18 09:14:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl AsRef<OsStr> for String {
|
2019-12-09 10:39:57 +00:00
|
|
|
|
#[inline]
|
2015-03-18 09:14:54 -07:00
|
|
|
|
fn as_ref(&self) -> &OsStr {
|
2015-07-24 03:04:55 +02:00
|
|
|
|
(&**self).as_ref()
|
2015-03-18 09:14:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 15:55:31 -08:00
|
|
|
|
impl FromInner<Buf> for OsString {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
fn from_inner(buf: Buf) -> OsString {
|
|
|
|
|
OsString { inner: buf }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoInner<Buf> for OsString {
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
fn into_inner(self) -> Buf {
|
|
|
|
|
self.inner
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsInner<Slice> for OsStr {
|
2019-04-04 10:51:18 +02:00
|
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
fn as_inner(&self) -> &Slice {
|
|
|
|
|
&self.inner
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-12 12:21:57 -05:00
|
|
|
|
|
2020-04-29 18:13:52 +09:00
|
|
|
|
#[stable(feature = "osstring_from_str", since = "1.45.0")]
|
|
|
|
|
impl FromStr for OsString {
|
|
|
|
|
type Err = core::convert::Infallible;
|
|
|
|
|
|
2021-01-22 18:46:00 +01:00
|
|
|
|
#[inline]
|
2020-04-29 18:13:52 +09:00
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
Ok(OsString::from(s))
|
|
|
|
|
}
|
|
|
|
|
}
|