2015-01-21 15:55:31 -08:00
|
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2016-08-22 19:47:38 +00:00
|
|
|
|
use borrow::{Borrow, Cow};
|
2017-06-12 22:21:53 +03:00
|
|
|
|
use fmt;
|
2015-01-21 15:55:31 -08:00
|
|
|
|
use mem;
|
|
|
|
|
use ops;
|
|
|
|
|
use cmp;
|
2015-02-17 20:48:07 -08:00
|
|
|
|
use hash::{Hash, Hasher};
|
2015-01-21 15:55:31 -08:00
|
|
|
|
|
|
|
|
|
use sys::os_str::{Buf, Slice};
|
|
|
|
|
use sys_common::{AsInner, IntoInner, FromInner};
|
|
|
|
|
|
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.
|
|
|
|
|
///
|
|
|
|
|
/// * In Rust, strings are always valid UTF-8, but may contain zeros.
|
|
|
|
|
///
|
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
|
|
|
|
|
/// to be converted into an "OS" string with no cost.
|
2017-01-21 16:38:54 +01:00
|
|
|
|
///
|
|
|
|
|
/// [`OsStr`]: struct.OsStr.html
|
2015-01-21 15:55:31 -08:00
|
|
|
|
#[derive(Clone)]
|
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 {
|
|
|
|
|
inner: Buf
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Slices into OS strings (see [`OsString`]).
|
|
|
|
|
///
|
|
|
|
|
/// [`OsString`]: struct.OsString.html
|
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 OsStr {
|
|
|
|
|
inner: Slice
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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")]
|
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.
|
|
|
|
|
///
|
|
|
|
|
/// [`OsStr`]: struct.OsStr.html
|
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")]
|
|
|
|
|
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
|
|
|
|
///
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
|
|
|
|
///
|
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")]
|
2015-01-21 15:55:31 -08:00
|
|
|
|
pub fn into_string(self) -> Result<String, OsString> {
|
|
|
|
|
self.inner.into_string().map_err(|buf| OsString { inner: buf} )
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// Extends the string with the given [`&OsStr`] slice.
|
|
|
|
|
///
|
|
|
|
|
/// [`&OsStr`]: struct.OsStr.html
|
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")]
|
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.
|
|
|
|
|
///
|
|
|
|
|
/// The string will be able to hold exactly `capacity` lenth units of other
|
|
|
|
|
/// 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;
|
|
|
|
|
///
|
|
|
|
|
/// let mut os_string = OsString::with_capacity(10);
|
|
|
|
|
/// 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")]
|
2016-02-12 12:21:57 -05:00
|
|
|
|
pub fn with_capacity(capacity: usize) -> OsString {
|
|
|
|
|
OsString {
|
|
|
|
|
inner: Buf::with_capacity(capacity)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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")]
|
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;
|
|
|
|
|
///
|
|
|
|
|
/// let mut os_string = OsString::with_capacity(10);
|
|
|
|
|
/// assert!(os_string.capacity() >= 10);
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
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")]
|
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
|
|
|
|
|
/// requests. Therefore capacity can not be relied upon to be precisely
|
|
|
|
|
/// 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")]
|
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
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(osstring_shrink_to_fit)]
|
|
|
|
|
///
|
|
|
|
|
/// 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-03-10 00:23:54 -05:00
|
|
|
|
#[unstable(feature = "osstring_shrink_to_fit", issue = "40421")]
|
|
|
|
|
pub fn shrink_to_fit(&mut self) {
|
|
|
|
|
self.inner.shrink_to_fit()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 19:33:40 +02:00
|
|
|
|
/// Converts this `OsString` into a boxed [`OsStr`].
|
|
|
|
|
///
|
|
|
|
|
/// [`OsStr`]: struct.OsStr.html
|
2017-03-12 16:22:29 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(into_boxed_os_str)]
|
|
|
|
|
///
|
|
|
|
|
/// use std::ffi::{OsString, OsStr};
|
|
|
|
|
///
|
|
|
|
|
/// let s = OsString::from("hello");
|
|
|
|
|
///
|
|
|
|
|
/// let b: Box<OsStr> = s.into_boxed_os_str();
|
|
|
|
|
/// ```
|
2017-02-13 20:37:42 -05:00
|
|
|
|
#[unstable(feature = "into_boxed_os_str", issue = "40380")]
|
2017-01-31 22:46:16 -05:00
|
|
|
|
pub fn into_boxed_os_str(self) -> Box<OsStr> {
|
|
|
|
|
unsafe { mem::transmute(self.inner.into_box()) }
|
|
|
|
|
}
|
2015-03-18 09:14:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl From<String> for OsString {
|
|
|
|
|
fn from(s: String) -> OsString {
|
|
|
|
|
OsString { inner: Buf::from_string(s) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-26 13:39:23 -07:00
|
|
|
|
impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for OsString {
|
|
|
|
|
fn from(s: &'a T) -> OsString {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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]
|
|
|
|
|
fn lt(&self, other: &OsString) -> bool { &**self < &**other }
|
|
|
|
|
#[inline]
|
|
|
|
|
fn le(&self, other: &OsString) -> bool { &**self <= &**other }
|
|
|
|
|
#[inline]
|
|
|
|
|
fn gt(&self, other: &OsString) -> bool { &**self > &**other }
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ge(&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 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");
|
|
|
|
|
/// ```
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-24 03:04:55 +02:00
|
|
|
|
fn from_inner(inner: &Slice) -> &OsStr {
|
|
|
|
|
unsafe { mem::transmute(inner) }
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
///
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// [`&str`]: ../../std/primitive.str.html
|
|
|
|
|
///
|
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")]
|
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
|
|
|
|
///
|
|
|
|
|
/// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
|
2017-01-04 22:47:23 -05:00
|
|
|
|
///
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// [`Cow`]: ../../std/borrow/enum.Cow.html
|
|
|
|
|
/// [`str`]: ../../std/primitive.str.html
|
|
|
|
|
///
|
2017-01-04 22:47:23 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Calling `to_string_lossy` on an `OsStr` with valid unicode:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::ffi::OsStr;
|
|
|
|
|
///
|
|
|
|
|
/// let os_str = OsStr::new("foo");
|
|
|
|
|
/// assert_eq!(os_str.to_string_lossy(), "foo");
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Had `os_str` contained invalid unicode, the `to_string_lossy` call might
|
|
|
|
|
/// have returned `"fo<66>"`.
|
2015-03-02 10:46:05 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-28 23:24:05 -08: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`].
|
|
|
|
|
///
|
|
|
|
|
/// [`OsString`]: struct.OsString.html
|
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")]
|
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")]
|
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`.
|
|
|
|
|
///
|
|
|
|
|
/// Note that this does **not** return the number of bytes in this string
|
2017-05-24 19:33:40 +02:00
|
|
|
|
/// as, for example, OS strings on Windows are encoded as a list of [`u16`]
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// rather than a list of bytes. This number is simply useful for passing to
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// other methods like [`OsString::with_capacity`] to avoid reallocations.
|
2016-02-12 12:21:57 -05:00
|
|
|
|
///
|
2016-04-07 10:42:53 -07:00
|
|
|
|
/// See `OsStr` introduction for more information about encoding.
|
2016-08-17 22:56:43 -04:00
|
|
|
|
///
|
2017-05-24 19:33:40 +02:00
|
|
|
|
/// [`u16`]: ../primitive.u16.html
|
2017-01-21 16:38:54 +01:00
|
|
|
|
/// [`OsString::with_capacity`]: struct.OsString.html#method.with_capacity
|
|
|
|
|
///
|
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);
|
|
|
|
|
/// ```
|
2016-04-07 10:42:53 -07:00
|
|
|
|
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
|
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.
|
|
|
|
|
///
|
|
|
|
|
/// [`Box`]: ../boxed/struct.Box.html
|
|
|
|
|
/// [`OsString`]: struct.OsString.html
|
2017-02-13 20:37:42 -05:00
|
|
|
|
#[unstable(feature = "into_boxed_os_str", issue = "40380")]
|
|
|
|
|
pub fn into_os_string(self: Box<OsStr>) -> OsString {
|
|
|
|
|
let inner: Box<Slice> = unsafe { mem::transmute(self) };
|
|
|
|
|
OsString { inner: Buf::from_box(inner) }
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
|
/// Gets the underlying byte representation.
|
2015-01-21 15:55:31 -08:00
|
|
|
|
///
|
|
|
|
|
/// Note: it is *crucial* that this API is private, to avoid
|
|
|
|
|
/// revealing the internal, platform-specific encodings.
|
|
|
|
|
fn bytes(&self) -> &[u8] {
|
|
|
|
|
unsafe { mem::transmute(&self.inner) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 22:46:16 -05:00
|
|
|
|
#[stable(feature = "box_from_os_str", since = "1.17.0")]
|
|
|
|
|
impl<'a> From<&'a OsStr> for Box<OsStr> {
|
|
|
|
|
fn from(s: &'a OsStr) -> Box<OsStr> {
|
|
|
|
|
unsafe { mem::transmute(s.inner.into_box()) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2017-02-13 20:37:42 -05:00
|
|
|
|
fn from(boxed: Box<OsStr>) -> OsString {
|
|
|
|
|
boxed.into_os_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-20 08:38:39 +01:00
|
|
|
|
#[stable(feature = "box_from_os_string", since = "1.18.0")]
|
2017-05-20 15:40:53 -04:00
|
|
|
|
impl From<OsString> for Box<OsStr> {
|
|
|
|
|
fn from(s: OsString) -> Box<OsStr> {
|
|
|
|
|
s.into_boxed_os_str()
|
2017-02-13 20:37:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 22:46:16 -05:00
|
|
|
|
#[stable(feature = "box_default_extra", since = "1.17.0")]
|
|
|
|
|
impl Default for Box<OsStr> {
|
|
|
|
|
fn default() -> Box<OsStr> {
|
|
|
|
|
unsafe { mem::transmute(Slice::empty_box()) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 07:38:44 +08:00
|
|
|
|
#[stable(feature = "osstring_default", since = "1.9.0")]
|
2016-03-22 00:45:36 +08:00
|
|
|
|
impl<'a> Default for &'a OsStr {
|
2016-09-11 17:00:09 +05:30
|
|
|
|
/// Creates an empty `OsStr`.
|
2016-03-22 00:45:36 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn default() -> &'a OsStr {
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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]
|
|
|
|
|
fn lt(&self, other: &OsStr) -> bool { self.bytes().lt(other.bytes()) }
|
|
|
|
|
#[inline]
|
|
|
|
|
fn le(&self, other: &OsStr) -> bool { self.bytes().le(other.bytes()) }
|
|
|
|
|
#[inline]
|
|
|
|
|
fn gt(&self, other: &OsStr) -> bool { self.bytes().gt(other.bytes()) }
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ge(&self, other: &OsStr) -> bool { self.bytes().ge(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 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]
|
|
|
|
|
fn cmp(&self, other: &OsStr) -> cmp::Ordering { self.bytes().cmp(other.bytes()) }
|
|
|
|
|
}
|
|
|
|
|
|
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]
|
|
|
|
|
fn eq(&self, other: &$rhs) -> bool { <OsStr as PartialEq>::eq(self, other) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "cmp_os_str", since = "1.8.0")]
|
|
|
|
|
impl<'a, 'b> PartialEq<$lhs> for $rhs {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn eq(&self, other: &$lhs) -> bool { <OsStr as PartialEq>::eq(self, other) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
fmt::Debug::fmt(&self.inner, formatter)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl OsStr {
|
|
|
|
|
pub(crate) fn display(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
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 {
|
2015-02-18 15:48:40 -08: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;
|
2017-04-13 02:48:46 -07:00
|
|
|
|
fn to_owned(&self) -> OsString {
|
|
|
|
|
self.to_os_string()
|
|
|
|
|
}
|
|
|
|
|
fn clone_into(&self, target: &mut OsString) {
|
|
|
|
|
target.clear();
|
|
|
|
|
target.push(self);
|
|
|
|
|
}
|
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 {
|
|
|
|
|
fn as_ref(&self) -> &OsStr {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl AsRef<OsStr> for OsString {
|
|
|
|
|
fn as_ref(&self) -> &OsStr {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl AsRef<OsStr> for str {
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
fn from_inner(buf: Buf) -> OsString {
|
|
|
|
|
OsString { inner: buf }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoInner<Buf> for OsString {
|
|
|
|
|
fn into_inner(self) -> Buf {
|
|
|
|
|
self.inner
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsInner<Slice> for OsStr {
|
|
|
|
|
fn as_inner(&self) -> &Slice {
|
|
|
|
|
&self.inner
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-12 12:21:57 -05:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use sys_common::{AsInner, IntoInner};
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_string_with_capacity() {
|
|
|
|
|
let os_string = OsString::with_capacity(0);
|
|
|
|
|
assert_eq!(0, os_string.inner.into_inner().capacity());
|
|
|
|
|
|
|
|
|
|
let os_string = OsString::with_capacity(10);
|
|
|
|
|
assert_eq!(10, os_string.inner.into_inner().capacity());
|
|
|
|
|
|
|
|
|
|
let mut os_string = OsString::with_capacity(0);
|
|
|
|
|
os_string.push("abc");
|
|
|
|
|
assert!(os_string.inner.into_inner().capacity() >= 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_string_clear() {
|
|
|
|
|
let mut os_string = OsString::from("abc");
|
|
|
|
|
assert_eq!(3, os_string.inner.as_inner().len());
|
|
|
|
|
|
|
|
|
|
os_string.clear();
|
|
|
|
|
assert_eq!(&os_string, "");
|
|
|
|
|
assert_eq!(0, os_string.inner.as_inner().len());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_string_capacity() {
|
|
|
|
|
let os_string = OsString::with_capacity(0);
|
|
|
|
|
assert_eq!(0, os_string.capacity());
|
|
|
|
|
|
|
|
|
|
let os_string = OsString::with_capacity(10);
|
|
|
|
|
assert_eq!(10, os_string.capacity());
|
|
|
|
|
|
|
|
|
|
let mut os_string = OsString::with_capacity(0);
|
|
|
|
|
os_string.push("abc");
|
|
|
|
|
assert!(os_string.capacity() >= 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_string_reserve() {
|
|
|
|
|
let mut os_string = OsString::new();
|
|
|
|
|
assert_eq!(os_string.capacity(), 0);
|
|
|
|
|
|
|
|
|
|
os_string.reserve(2);
|
|
|
|
|
assert!(os_string.capacity() >= 2);
|
|
|
|
|
|
|
|
|
|
for _ in 0..16 {
|
|
|
|
|
os_string.push("a");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert!(os_string.capacity() >= 16);
|
|
|
|
|
os_string.reserve(16);
|
|
|
|
|
assert!(os_string.capacity() >= 32);
|
|
|
|
|
|
|
|
|
|
os_string.push("a");
|
|
|
|
|
|
|
|
|
|
os_string.reserve(16);
|
|
|
|
|
assert!(os_string.capacity() >= 33)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_string_reserve_exact() {
|
|
|
|
|
let mut os_string = OsString::new();
|
|
|
|
|
assert_eq!(os_string.capacity(), 0);
|
|
|
|
|
|
|
|
|
|
os_string.reserve_exact(2);
|
|
|
|
|
assert!(os_string.capacity() >= 2);
|
|
|
|
|
|
|
|
|
|
for _ in 0..16 {
|
|
|
|
|
os_string.push("a");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert!(os_string.capacity() >= 16);
|
|
|
|
|
os_string.reserve_exact(16);
|
|
|
|
|
assert!(os_string.capacity() >= 32);
|
|
|
|
|
|
|
|
|
|
os_string.push("a");
|
|
|
|
|
|
|
|
|
|
os_string.reserve_exact(16);
|
|
|
|
|
assert!(os_string.capacity() >= 33)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 00:45:08 +08:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_string_default() {
|
|
|
|
|
let os_string: OsString = Default::default();
|
|
|
|
|
assert_eq!("", &os_string);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 12:21:57 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_str_is_empty() {
|
|
|
|
|
let mut os_string = OsString::new();
|
|
|
|
|
assert!(os_string.is_empty());
|
|
|
|
|
|
|
|
|
|
os_string.push("abc");
|
|
|
|
|
assert!(!os_string.is_empty());
|
|
|
|
|
|
|
|
|
|
os_string.clear();
|
|
|
|
|
assert!(os_string.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_str_len() {
|
|
|
|
|
let mut os_string = OsString::new();
|
|
|
|
|
assert_eq!(0, os_string.len());
|
|
|
|
|
|
|
|
|
|
os_string.push("abc");
|
|
|
|
|
assert_eq!(3, os_string.len());
|
|
|
|
|
|
|
|
|
|
os_string.clear();
|
|
|
|
|
assert_eq!(0, os_string.len());
|
|
|
|
|
}
|
2016-03-22 00:45:36 +08:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_str_default() {
|
|
|
|
|
let os_str: &OsStr = Default::default();
|
|
|
|
|
assert_eq!("", os_str);
|
|
|
|
|
}
|
2017-01-31 22:46:16 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn into_boxed() {
|
|
|
|
|
let orig = "Hello, world!";
|
|
|
|
|
let os_str = OsStr::new(orig);
|
2017-02-13 20:37:42 -05:00
|
|
|
|
let boxed: Box<OsStr> = Box::from(os_str);
|
|
|
|
|
let os_string = os_str.to_owned().into_boxed_os_str().into_os_string();
|
|
|
|
|
assert_eq!(os_str, &*boxed);
|
|
|
|
|
assert_eq!(&*boxed, &*os_string);
|
|
|
|
|
assert_eq!(&*os_string, os_str);
|
2017-01-31 22:46:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn boxed_default() {
|
|
|
|
|
let boxed = <Box<OsStr>>::default();
|
|
|
|
|
assert!(boxed.is_empty());
|
|
|
|
|
}
|
2017-04-13 02:48:46 -07:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_os_str_clone_into() {
|
|
|
|
|
let mut os_string = OsString::with_capacity(123);
|
|
|
|
|
os_string.push("hello");
|
|
|
|
|
let os_str = OsStr::new("bonjour");
|
|
|
|
|
os_str.clone_into(&mut os_string);
|
|
|
|
|
assert_eq!(os_str, os_string);
|
|
|
|
|
assert!(os_string.capacity() >= 123);
|
|
|
|
|
}
|
2016-02-12 12:21:57 -05:00
|
|
|
|
}
|