4 use crate::borrow
::{Borrow, Cow}
;
7 use crate::hash
::{Hash, Hasher}
;
8 use crate::iter
::{Extend, FromIterator}
;
11 use crate::str::FromStr
;
14 use crate::sys
::os_str
::{Buf, Slice}
;
15 use crate::sys_common
::{AsInner, FromInner, IntoInner}
;
17 /// A type that can represent owned, mutable platform-native strings, but is
18 /// cheaply inter-convertible with Rust strings.
20 /// The need for this type arises from the fact that:
22 /// * On Unix systems, strings are often arbitrary sequences of non-zero
23 /// bytes, in many cases interpreted as UTF-8.
25 /// * On Windows, strings are often arbitrary sequences of non-zero 16-bit
26 /// values, interpreted as UTF-16 when it is valid to do so.
28 /// * In Rust, strings are always valid UTF-8, which may contain zeros.
30 /// `OsString` and [`OsStr`] bridge this gap by simultaneously representing Rust
31 /// and platform-native string values, and in particular allowing a Rust string
32 /// to be converted into an "OS" string with no cost if possible. A consequence
33 /// of this is that `OsString` instances are *not* `NUL` terminated; in order
34 /// to pass to e.g., Unix system call, you should create a [`CStr`].
36 /// `OsString` is to [`&OsStr`] as [`String`] is to [`&str`]: the former
37 /// in each pair are owned strings; the latter are borrowed
40 /// Note, `OsString` and [`OsStr`] internally do not necessarily hold strings in
41 /// the form native to the platform; While on Unix, strings are stored as a
42 /// sequence of 8-bit values, on Windows, where strings are 16-bit value based
43 /// as just discussed, strings are also actually stored as a sequence of 8-bit
44 /// values, encoded in a less-strict variant of UTF-8. This is useful to
45 /// understand when handling capacity and length values.
47 /// # Creating an `OsString`
49 /// **From a Rust string**: `OsString` implements
50 /// [`From`]`<`[`String`]`>`, so you can use `my_string.from` to
51 /// create an `OsString` from a normal Rust string.
53 /// **From slices:** Just like you can start with an empty Rust
54 /// [`String`] and then [`String::push_str`] `&str`
55 /// sub-string slices into it, you can create an empty `OsString` with
56 /// the [`OsString::new`] method and then push string slices into it with the
57 /// [`OsString::push`] method.
59 /// # Extracting a borrowed reference to the whole OS string
61 /// You can use the [`OsString::as_os_str`] method to get an `&`[`OsStr`] from
62 /// an `OsString`; this is effectively a borrowed reference to the
67 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
68 /// the traits which `OsString` implements for [conversions] from/to native representations.
72 /// [`CStr`]: crate::ffi::CStr
73 /// [conversions]: super#conversions
75 #[cfg_attr(not(test), rustc_diagnostic_item = "OsString")]
76 #[stable(feature = "rust1", since = "1.0.0")]
81 /// Allows extension traits within `std`.
82 #[unstable(feature = "sealed", issue = "none")]
83 impl crate::sealed
::Sealed
for OsString {}
85 /// Borrowed reference to an OS string (see [`OsString`]).
87 /// This type represents a borrowed reference to a string in the operating system's preferred
90 /// `&OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed
91 /// references; the latter are owned strings.
93 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
94 /// the traits which `OsStr` implements for [conversions] from/to native representations.
97 /// [conversions]: super#conversions
98 #[cfg_attr(not(test), rustc_diagnostic_item = "OsStr")]
99 #[stable(feature = "rust1", since = "1.0.0")]
101 // `OsStr::from_inner` current implementation relies
102 // on `OsStr` being layout-compatible with `Slice`.
103 // When attribute privacy is implemented, `OsStr` should be annotated as `#[repr(transparent)]`.
104 // Anyway, `OsStr` representation and layout are considered implementation details, are
105 // not documented and must not be relied upon.
110 /// Allows extension traits within `std`.
111 #[unstable(feature = "sealed", issue = "none")]
112 impl crate::sealed
::Sealed
for OsStr {}
115 /// Constructs a new empty `OsString`.
120 /// use std::ffi::OsString;
122 /// let os_string = OsString::new();
124 #[stable(feature = "rust1", since = "1.0.0")]
126 pub fn new() -> OsString
{
127 OsString { inner: Buf::from_string(String::new()) }
130 /// Converts to an [`OsStr`] slice.
135 /// use std::ffi::{OsString, OsStr};
137 /// let os_string = OsString::from("foo");
138 /// let os_str = OsStr::new("foo");
139 /// assert_eq!(os_string.as_os_str(), os_str);
141 #[stable(feature = "rust1", since = "1.0.0")]
143 pub fn as_os_str(&self) -> &OsStr
{
147 /// Converts the `OsString` into a [`String`] if it contains valid Unicode data.
149 /// On failure, ownership of the original `OsString` is returned.
154 /// use std::ffi::OsString;
156 /// let os_string = OsString::from("foo");
157 /// let string = os_string.into_string();
158 /// assert_eq!(string, Ok(String::from("foo")));
160 #[stable(feature = "rust1", since = "1.0.0")]
162 pub fn into_string(self) -> Result
<String
, OsString
> {
163 self.inner
.into_string().map_err(|buf
| OsString { inner: buf }
)
166 /// Extends the string with the given [`&OsStr`] slice.
168 /// [`&OsStr`]: OsStr
173 /// use std::ffi::OsString;
175 /// let mut os_string = OsString::from("foo");
176 /// os_string.push("bar");
177 /// assert_eq!(&os_string, "foobar");
179 #[stable(feature = "rust1", since = "1.0.0")]
181 pub fn push
<T
: AsRef
<OsStr
>>(&mut self, s
: T
) {
182 self.inner
.push_slice(&s
.as_ref().inner
)
185 /// Creates a new `OsString` with the given capacity.
187 /// The string will be able to hold exactly `capacity` length units of other
188 /// OS strings without reallocating. If `capacity` is 0, the string will not
191 /// See main `OsString` documentation information about encoding.
196 /// use std::ffi::OsString;
198 /// let mut os_string = OsString::with_capacity(10);
199 /// let capacity = os_string.capacity();
201 /// // This push is done without reallocating
202 /// os_string.push("foo");
204 /// assert_eq!(capacity, os_string.capacity());
206 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
208 pub fn with_capacity(capacity
: usize) -> OsString
{
209 OsString { inner: Buf::with_capacity(capacity) }
212 /// Truncates the `OsString` to zero length.
217 /// use std::ffi::OsString;
219 /// let mut os_string = OsString::from("foo");
220 /// assert_eq!(&os_string, "foo");
222 /// os_string.clear();
223 /// assert_eq!(&os_string, "");
225 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
227 pub fn clear(&mut self) {
231 /// Returns the capacity this `OsString` can hold without reallocating.
233 /// See `OsString` introduction for information about encoding.
238 /// use std::ffi::OsString;
240 /// let os_string = OsString::with_capacity(10);
241 /// assert!(os_string.capacity() >= 10);
243 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
245 pub fn capacity(&self) -> usize {
246 self.inner
.capacity()
249 /// Reserves capacity for at least `additional` more capacity to be inserted
250 /// in the given `OsString`.
252 /// The collection may reserve more space to avoid frequent reallocations.
257 /// use std::ffi::OsString;
259 /// let mut s = OsString::new();
261 /// assert!(s.capacity() >= 10);
263 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
265 pub fn reserve(&mut self, additional
: usize) {
266 self.inner
.reserve(additional
)
269 /// Reserves the minimum capacity for exactly `additional` more capacity to
270 /// be inserted in the given `OsString`. Does nothing if the capacity is
271 /// already sufficient.
273 /// Note that the allocator may give the collection more space than it
274 /// requests. Therefore, capacity can not be relied upon to be precisely
275 /// minimal. Prefer reserve if future insertions are expected.
280 /// use std::ffi::OsString;
282 /// let mut s = OsString::new();
283 /// s.reserve_exact(10);
284 /// assert!(s.capacity() >= 10);
286 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
288 pub fn reserve_exact(&mut self, additional
: usize) {
289 self.inner
.reserve_exact(additional
)
292 /// Shrinks the capacity of the `OsString` to match its length.
297 /// use std::ffi::OsString;
299 /// let mut s = OsString::from("foo");
302 /// assert!(s.capacity() >= 100);
304 /// s.shrink_to_fit();
305 /// assert_eq!(3, s.capacity());
307 #[stable(feature = "osstring_shrink_to_fit", since = "1.19.0")]
309 pub fn shrink_to_fit(&mut self) {
310 self.inner
.shrink_to_fit()
313 /// Shrinks the capacity of the `OsString` with a lower bound.
315 /// The capacity will remain at least as large as both the length
316 /// and the supplied value.
318 /// If the current capacity is less than the lower limit, this is a no-op.
323 /// #![feature(shrink_to)]
324 /// use std::ffi::OsString;
326 /// let mut s = OsString::from("foo");
329 /// assert!(s.capacity() >= 100);
332 /// assert!(s.capacity() >= 10);
334 /// assert!(s.capacity() >= 3);
337 #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
338 pub fn shrink_to(&mut self, min_capacity
: usize) {
339 self.inner
.shrink_to(min_capacity
)
342 /// Converts this `OsString` into a boxed [`OsStr`].
347 /// use std::ffi::{OsString, OsStr};
349 /// let s = OsString::from("hello");
351 /// let b: Box<OsStr> = s.into_boxed_os_str();
353 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
354 pub fn into_boxed_os_str(self) -> Box
<OsStr
> {
355 let rw
= Box
::into_raw(self.inner
.into_box()) as *mut OsStr
;
356 unsafe { Box::from_raw(rw) }
360 #[stable(feature = "rust1", since = "1.0.0")]
361 impl From
<String
> for OsString
{
362 /// Converts a [`String`] into a [`OsString`].
364 /// The conversion copies the data, and includes an allocation on the heap.
366 fn from(s
: String
) -> OsString
{
367 OsString { inner: Buf::from_string(s) }
371 #[stable(feature = "rust1", since = "1.0.0")]
372 impl<T
: ?Sized
+ AsRef
<OsStr
>> From
<&T
> for OsString
{
373 fn from(s
: &T
) -> OsString
{
374 s
.as_ref().to_os_string()
378 #[stable(feature = "rust1", since = "1.0.0")]
379 impl ops
::Index
<ops
::RangeFull
> for OsString
{
383 fn index(&self, _index
: ops
::RangeFull
) -> &OsStr
{
384 OsStr
::from_inner(self.inner
.as_slice())
388 #[stable(feature = "mut_osstr", since = "1.44.0")]
389 impl ops
::IndexMut
<ops
::RangeFull
> for OsString
{
391 fn index_mut(&mut self, _index
: ops
::RangeFull
) -> &mut OsStr
{
392 OsStr
::from_inner_mut(self.inner
.as_mut_slice())
396 #[stable(feature = "rust1", since = "1.0.0")]
397 impl ops
::Deref
for OsString
{
401 fn deref(&self) -> &OsStr
{
406 #[stable(feature = "mut_osstr", since = "1.44.0")]
407 impl ops
::DerefMut
for OsString
{
409 fn deref_mut(&mut self) -> &mut OsStr
{
414 #[stable(feature = "osstring_default", since = "1.9.0")]
415 impl Default
for OsString
{
416 /// Constructs an empty `OsString`.
418 fn default() -> OsString
{
423 #[stable(feature = "rust1", since = "1.0.0")]
424 impl fmt
::Debug
for OsString
{
425 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
426 fmt
::Debug
::fmt(&**self, formatter
)
430 #[stable(feature = "rust1", since = "1.0.0")]
431 impl PartialEq
for OsString
{
433 fn eq(&self, other
: &OsString
) -> bool
{
438 #[stable(feature = "rust1", since = "1.0.0")]
439 impl PartialEq
<str> for OsString
{
441 fn eq(&self, other
: &str) -> bool
{
446 #[stable(feature = "rust1", since = "1.0.0")]
447 impl PartialEq
<OsString
> for str {
449 fn eq(&self, other
: &OsString
) -> bool
{
454 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
455 impl PartialEq
<&str> for OsString
{
457 fn eq(&self, other
: &&str) -> bool
{
462 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
463 impl<'a
> PartialEq
<OsString
> for &'a
str {
465 fn eq(&self, other
: &OsString
) -> bool
{
470 #[stable(feature = "rust1", since = "1.0.0")]
471 impl Eq
for OsString {}
473 #[stable(feature = "rust1", since = "1.0.0")]
474 impl PartialOrd
for OsString
{
476 fn partial_cmp(&self, other
: &OsString
) -> Option
<cmp
::Ordering
> {
477 (&**self).partial_cmp(&**other
)
480 fn lt(&self, other
: &OsString
) -> bool
{
484 fn le(&self, other
: &OsString
) -> bool
{
488 fn gt(&self, other
: &OsString
) -> bool
{
492 fn ge(&self, other
: &OsString
) -> bool
{
497 #[stable(feature = "rust1", since = "1.0.0")]
498 impl PartialOrd
<str> for OsString
{
500 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
501 (&**self).partial_cmp(other
)
505 #[stable(feature = "rust1", since = "1.0.0")]
506 impl Ord
for OsString
{
508 fn cmp(&self, other
: &OsString
) -> cmp
::Ordering
{
509 (&**self).cmp(&**other
)
513 #[stable(feature = "rust1", since = "1.0.0")]
514 impl Hash
for OsString
{
516 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
517 (&**self).hash(state
)
522 /// Coerces into an `OsStr` slice.
527 /// use std::ffi::OsStr;
529 /// let os_str = OsStr::new("foo");
532 #[stable(feature = "rust1", since = "1.0.0")]
533 pub fn new
<S
: AsRef
<OsStr
> + ?Sized
>(s
: &S
) -> &OsStr
{
538 fn from_inner(inner
: &Slice
) -> &OsStr
{
539 // SAFETY: OsStr is just a wrapper of Slice,
540 // therefore converting &Slice to &OsStr is safe.
541 unsafe { &*(inner as *const Slice as *const OsStr) }
545 fn from_inner_mut(inner
: &mut Slice
) -> &mut OsStr
{
546 // SAFETY: OsStr is just a wrapper of Slice,
547 // therefore converting &mut Slice to &mut OsStr is safe.
548 // Any method that mutates OsStr must be careful not to
549 // break platform-specific encoding, in particular Wtf8 on Windows.
550 unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
553 /// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
555 /// This conversion may entail doing a check for UTF-8 validity.
562 /// use std::ffi::OsStr;
564 /// let os_str = OsStr::new("foo");
565 /// assert_eq!(os_str.to_str(), Some("foo"));
567 #[stable(feature = "rust1", since = "1.0.0")]
569 pub fn to_str(&self) -> Option
<&str> {
573 /// Converts an `OsStr` to a [`Cow`]`<`[`str`]`>`.
575 /// Any non-Unicode sequences are replaced with
576 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
578 /// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
582 /// Calling `to_string_lossy` on an `OsStr` with invalid unicode:
585 /// // Note, due to differences in how Unix and Windows represent strings,
586 /// // we are forced to complicate this example, setting up example `OsStr`s
587 /// // with different source data and via different platform extensions.
588 /// // Understand that in reality you could end up with such example invalid
589 /// // sequences simply through collecting user command line arguments, for
592 /// #[cfg(any(unix, target_os = "redox"))] {
593 /// use std::ffi::OsStr;
594 /// use std::os::unix::ffi::OsStrExt;
596 /// // Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
597 /// // respectively. The value 0x80 is a lone continuation byte, invalid
598 /// // in a UTF-8 sequence.
599 /// let source = [0x66, 0x6f, 0x80, 0x6f];
600 /// let os_str = OsStr::from_bytes(&source[..]);
602 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
604 /// #[cfg(windows)] {
605 /// use std::ffi::OsString;
606 /// use std::os::windows::prelude::*;
608 /// // Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
609 /// // respectively. The value 0xD800 is a lone surrogate half, invalid
610 /// // in a UTF-16 sequence.
611 /// let source = [0x0066, 0x006f, 0xD800, 0x006f];
612 /// let os_string = OsString::from_wide(&source[..]);
613 /// let os_str = os_string.as_os_str();
615 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
618 #[stable(feature = "rust1", since = "1.0.0")]
620 pub fn to_string_lossy(&self) -> Cow
<'_
, str> {
621 self.inner
.to_string_lossy()
624 /// Copies the slice into an owned [`OsString`].
629 /// use std::ffi::{OsStr, OsString};
631 /// let os_str = OsStr::new("foo");
632 /// let os_string = os_str.to_os_string();
633 /// assert_eq!(os_string, OsString::from("foo"));
635 #[stable(feature = "rust1", since = "1.0.0")]
637 pub fn to_os_string(&self) -> OsString
{
638 OsString { inner: self.inner.to_owned() }
641 /// Checks whether the `OsStr` is empty.
646 /// use std::ffi::OsStr;
648 /// let os_str = OsStr::new("");
649 /// assert!(os_str.is_empty());
651 /// let os_str = OsStr::new("foo");
652 /// assert!(!os_str.is_empty());
654 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
656 pub fn is_empty(&self) -> bool
{
657 self.inner
.inner
.is_empty()
660 /// Returns the length of this `OsStr`.
662 /// Note that this does **not** return the number of bytes in the string in
665 /// The length returned is that of the underlying storage used by `OsStr`.
666 /// As discussed in the [`OsString`] introduction, [`OsString`] and `OsStr`
667 /// store strings in a form best suited for cheap inter-conversion between
668 /// native-platform and Rust string forms, which may differ significantly
669 /// from both of them, including in storage size and encoding.
671 /// This number is simply useful for passing to other methods, like
672 /// [`OsString::with_capacity`] to avoid reallocations.
677 /// use std::ffi::OsStr;
679 /// let os_str = OsStr::new("");
680 /// assert_eq!(os_str.len(), 0);
682 /// let os_str = OsStr::new("foo");
683 /// assert_eq!(os_str.len(), 3);
685 #[doc(alias = "length")]
686 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
688 pub fn len(&self) -> usize {
689 self.inner
.inner
.len()
692 /// Converts a [`Box`]`<OsStr>` into an [`OsString`] without copying or allocating.
693 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
694 pub fn into_os_string(self: Box
<OsStr
>) -> OsString
{
695 let boxed
= unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) }
;
696 OsString { inner: Buf::from_box(boxed) }
699 /// Gets the underlying byte representation.
701 /// Note: it is *crucial* that this API is not externally public, to avoid
702 /// revealing the internal, platform-specific encodings.
704 pub(crate) fn bytes(&self) -> &[u8] {
705 unsafe { &*(&self.inner as *const _ as *const [u8]) }
708 /// Converts this string to its ASCII lower case equivalent in-place.
710 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
711 /// but non-ASCII letters are unchanged.
713 /// To return a new lowercased value without modifying the existing one, use
714 /// [`OsStr::to_ascii_lowercase`].
719 /// #![feature(osstring_ascii)]
720 /// use std::ffi::OsString;
722 /// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
724 /// s.make_ascii_lowercase();
726 /// assert_eq!("grÜße, jÜrgen ❤", s);
728 #[unstable(feature = "osstring_ascii", issue = "70516")]
730 pub fn make_ascii_lowercase(&mut self) {
731 self.inner
.make_ascii_lowercase()
734 /// Converts this string to its ASCII upper case equivalent in-place.
736 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
737 /// but non-ASCII letters are unchanged.
739 /// To return a new uppercased value without modifying the existing one, use
740 /// [`OsStr::to_ascii_uppercase`].
745 /// #![feature(osstring_ascii)]
746 /// use std::ffi::OsString;
748 /// let mut s = OsString::from("Grüße, Jürgen ❤");
750 /// s.make_ascii_uppercase();
752 /// assert_eq!("GRüßE, JüRGEN ❤", s);
754 #[unstable(feature = "osstring_ascii", issue = "70516")]
756 pub fn make_ascii_uppercase(&mut self) {
757 self.inner
.make_ascii_uppercase()
760 /// Returns a copy of this string where each character is mapped to its
761 /// ASCII lower case equivalent.
763 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
764 /// but non-ASCII letters are unchanged.
766 /// To lowercase the value in-place, use [`OsStr::make_ascii_lowercase`].
771 /// #![feature(osstring_ascii)]
772 /// use std::ffi::OsString;
773 /// let s = OsString::from("Grüße, Jürgen ❤");
775 /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
777 #[unstable(feature = "osstring_ascii", issue = "70516")]
778 pub fn to_ascii_lowercase(&self) -> OsString
{
779 OsString
::from_inner(self.inner
.to_ascii_lowercase())
782 /// Returns a copy of this string where each character is mapped to its
783 /// ASCII upper case equivalent.
785 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
786 /// but non-ASCII letters are unchanged.
788 /// To uppercase the value in-place, use [`OsStr::make_ascii_uppercase`].
793 /// #![feature(osstring_ascii)]
794 /// use std::ffi::OsString;
795 /// let s = OsString::from("Grüße, Jürgen ❤");
797 /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
799 #[unstable(feature = "osstring_ascii", issue = "70516")]
800 pub fn to_ascii_uppercase(&self) -> OsString
{
801 OsString
::from_inner(self.inner
.to_ascii_uppercase())
804 /// Checks if all characters in this string are within the ASCII range.
809 /// #![feature(osstring_ascii)]
810 /// use std::ffi::OsString;
812 /// let ascii = OsString::from("hello!\n");
813 /// let non_ascii = OsString::from("Grüße, Jürgen ❤");
815 /// assert!(ascii.is_ascii());
816 /// assert!(!non_ascii.is_ascii());
818 #[unstable(feature = "osstring_ascii", issue = "70516")]
820 pub fn is_ascii(&self) -> bool
{
821 self.inner
.is_ascii()
824 /// Checks that two strings are an ASCII case-insensitive match.
826 /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
827 /// but without allocating and copying temporaries.
832 /// #![feature(osstring_ascii)]
833 /// use std::ffi::OsString;
835 /// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
836 /// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
837 /// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
839 #[unstable(feature = "osstring_ascii", issue = "70516")]
840 pub fn eq_ignore_ascii_case
<S
: AsRef
<OsStr
>>(&self, other
: S
) -> bool
{
841 self.inner
.eq_ignore_ascii_case(&other
.as_ref().inner
)
845 #[stable(feature = "box_from_os_str", since = "1.17.0")]
846 impl From
<&OsStr
> for Box
<OsStr
> {
848 fn from(s
: &OsStr
) -> Box
<OsStr
> {
849 let rw
= Box
::into_raw(s
.inner
.into_box()) as *mut OsStr
;
850 unsafe { Box::from_raw(rw) }
854 #[stable(feature = "box_from_cow", since = "1.45.0")]
855 impl From
<Cow
<'_
, OsStr
>> for Box
<OsStr
> {
857 fn from(cow
: Cow
<'_
, OsStr
>) -> Box
<OsStr
> {
859 Cow
::Borrowed(s
) => Box
::from(s
),
860 Cow
::Owned(s
) => Box
::from(s
),
865 #[stable(feature = "os_string_from_box", since = "1.18.0")]
866 impl From
<Box
<OsStr
>> for OsString
{
867 /// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or
870 fn from(boxed
: Box
<OsStr
>) -> OsString
{
871 boxed
.into_os_string()
875 #[stable(feature = "box_from_os_string", since = "1.20.0")]
876 impl From
<OsString
> for Box
<OsStr
> {
877 /// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
879 fn from(s
: OsString
) -> Box
<OsStr
> {
880 s
.into_boxed_os_str()
884 #[stable(feature = "more_box_slice_clone", since = "1.29.0")]
885 impl Clone
for Box
<OsStr
> {
887 fn clone(&self) -> Self {
888 self.to_os_string().into_boxed_os_str()
892 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
893 impl From
<OsString
> for Arc
<OsStr
> {
894 /// Converts a [`OsString`] into a [`Arc`]`<OsStr>` without copying or allocating.
896 fn from(s
: OsString
) -> Arc
<OsStr
> {
897 let arc
= s
.inner
.into_arc();
898 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
902 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
903 impl From
<&OsStr
> for Arc
<OsStr
> {
905 fn from(s
: &OsStr
) -> Arc
<OsStr
> {
906 let arc
= s
.inner
.into_arc();
907 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
911 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
912 impl From
<OsString
> for Rc
<OsStr
> {
913 /// Converts a [`OsString`] into a [`Rc`]`<OsStr>` without copying or allocating.
915 fn from(s
: OsString
) -> Rc
<OsStr
> {
916 let rc
= s
.inner
.into_rc();
917 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
921 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
922 impl From
<&OsStr
> for Rc
<OsStr
> {
924 fn from(s
: &OsStr
) -> Rc
<OsStr
> {
925 let rc
= s
.inner
.into_rc();
926 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
930 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
931 impl<'a
> From
<OsString
> for Cow
<'a
, OsStr
> {
933 fn from(s
: OsString
) -> Cow
<'a
, OsStr
> {
938 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
939 impl<'a
> From
<&'a OsStr
> for Cow
<'a
, OsStr
> {
941 fn from(s
: &'a OsStr
) -> Cow
<'a
, OsStr
> {
946 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
947 impl<'a
> From
<&'a OsString
> for Cow
<'a
, OsStr
> {
949 fn from(s
: &'a OsString
) -> Cow
<'a
, OsStr
> {
950 Cow
::Borrowed(s
.as_os_str())
954 #[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
955 impl<'a
> From
<Cow
<'a
, OsStr
>> for OsString
{
957 fn from(s
: Cow
<'a
, OsStr
>) -> Self {
962 #[stable(feature = "box_default_extra", since = "1.17.0")]
963 impl Default
for Box
<OsStr
> {
965 fn default() -> Box
<OsStr
> {
966 let rw
= Box
::into_raw(Slice
::empty_box()) as *mut OsStr
;
967 unsafe { Box::from_raw(rw) }
971 #[stable(feature = "osstring_default", since = "1.9.0")]
972 impl Default
for &OsStr
{
973 /// Creates an empty `OsStr`.
975 fn default() -> Self {
980 #[stable(feature = "rust1", since = "1.0.0")]
981 impl PartialEq
for OsStr
{
983 fn eq(&self, other
: &OsStr
) -> bool
{
984 self.bytes().eq(other
.bytes())
988 #[stable(feature = "rust1", since = "1.0.0")]
989 impl PartialEq
<str> for OsStr
{
991 fn eq(&self, other
: &str) -> bool
{
992 *self == *OsStr
::new(other
)
996 #[stable(feature = "rust1", since = "1.0.0")]
997 impl PartialEq
<OsStr
> for str {
999 fn eq(&self, other
: &OsStr
) -> bool
{
1000 *other
== *OsStr
::new(self)
1004 #[stable(feature = "rust1", since = "1.0.0")]
1005 impl Eq
for OsStr {}
1007 #[stable(feature = "rust1", since = "1.0.0")]
1008 impl PartialOrd
for OsStr
{
1010 fn partial_cmp(&self, other
: &OsStr
) -> Option
<cmp
::Ordering
> {
1011 self.bytes().partial_cmp(other
.bytes())
1014 fn lt(&self, other
: &OsStr
) -> bool
{
1015 self.bytes().lt(other
.bytes())
1018 fn le(&self, other
: &OsStr
) -> bool
{
1019 self.bytes().le(other
.bytes())
1022 fn gt(&self, other
: &OsStr
) -> bool
{
1023 self.bytes().gt(other
.bytes())
1026 fn ge(&self, other
: &OsStr
) -> bool
{
1027 self.bytes().ge(other
.bytes())
1031 #[stable(feature = "rust1", since = "1.0.0")]
1032 impl PartialOrd
<str> for OsStr
{
1034 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
1035 self.partial_cmp(OsStr
::new(other
))
1039 // FIXME (#19470): cannot provide PartialOrd<OsStr> for str until we
1040 // have more flexible coherence rules.
1042 #[stable(feature = "rust1", since = "1.0.0")]
1043 impl Ord
for OsStr
{
1045 fn cmp(&self, other
: &OsStr
) -> cmp
::Ordering
{
1046 self.bytes().cmp(other
.bytes())
1050 macro_rules
! impl_cmp
{
1051 ($lhs
:ty
, $rhs
: ty
) => {
1052 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1053 impl<'a
, 'b
> PartialEq
<$rhs
> for $lhs
{
1055 fn eq(&self, other
: &$rhs
) -> bool
{
1056 <OsStr
as PartialEq
>::eq(self, other
)
1060 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1061 impl<'a
, 'b
> PartialEq
<$lhs
> for $rhs
{
1063 fn eq(&self, other
: &$lhs
) -> bool
{
1064 <OsStr
as PartialEq
>::eq(self, other
)
1068 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1069 impl<'a
, 'b
> PartialOrd
<$rhs
> for $lhs
{
1071 fn partial_cmp(&self, other
: &$rhs
) -> Option
<cmp
::Ordering
> {
1072 <OsStr
as PartialOrd
>::partial_cmp(self, other
)
1076 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1077 impl<'a
, 'b
> PartialOrd
<$lhs
> for $rhs
{
1079 fn partial_cmp(&self, other
: &$lhs
) -> Option
<cmp
::Ordering
> {
1080 <OsStr
as PartialOrd
>::partial_cmp(self, other
)
1086 impl_cmp
!(OsString
, OsStr
);
1087 impl_cmp
!(OsString
, &'a OsStr
);
1088 impl_cmp
!(Cow
<'a
, OsStr
>, OsStr
);
1089 impl_cmp
!(Cow
<'a
, OsStr
>, &'b OsStr
);
1090 impl_cmp
!(Cow
<'a
, OsStr
>, OsString
);
1092 #[stable(feature = "rust1", since = "1.0.0")]
1093 impl Hash
for OsStr
{
1095 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
1096 self.bytes().hash(state
)
1100 #[stable(feature = "rust1", since = "1.0.0")]
1101 impl fmt
::Debug
for OsStr
{
1102 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1103 fmt
::Debug
::fmt(&self.inner
, formatter
)
1108 pub(crate) fn display(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1109 fmt
::Display
::fmt(&self.inner
, formatter
)
1113 #[stable(feature = "rust1", since = "1.0.0")]
1114 impl Borrow
<OsStr
> for OsString
{
1116 fn borrow(&self) -> &OsStr
{
1121 #[stable(feature = "rust1", since = "1.0.0")]
1122 impl ToOwned
for OsStr
{
1123 type Owned
= OsString
;
1125 fn to_owned(&self) -> OsString
{
1129 fn clone_into(&self, target
: &mut OsString
) {
1130 self.inner
.clone_into(&mut target
.inner
)
1134 #[stable(feature = "rust1", since = "1.0.0")]
1135 impl AsRef
<OsStr
> for OsStr
{
1137 fn as_ref(&self) -> &OsStr
{
1142 #[stable(feature = "rust1", since = "1.0.0")]
1143 impl AsRef
<OsStr
> for OsString
{
1145 fn as_ref(&self) -> &OsStr
{
1150 #[stable(feature = "rust1", since = "1.0.0")]
1151 impl AsRef
<OsStr
> for str {
1153 fn as_ref(&self) -> &OsStr
{
1154 OsStr
::from_inner(Slice
::from_str(self))
1158 #[stable(feature = "rust1", since = "1.0.0")]
1159 impl AsRef
<OsStr
> for String
{
1161 fn as_ref(&self) -> &OsStr
{
1166 impl FromInner
<Buf
> for OsString
{
1168 fn from_inner(buf
: Buf
) -> OsString
{
1169 OsString { inner: buf }
1173 impl IntoInner
<Buf
> for OsString
{
1175 fn into_inner(self) -> Buf
{
1180 impl AsInner
<Slice
> for OsStr
{
1182 fn as_inner(&self) -> &Slice
{
1187 #[stable(feature = "osstring_from_str", since = "1.45.0")]
1188 impl FromStr
for OsString
{
1189 type Err
= core
::convert
::Infallible
;
1192 fn from_str(s
: &str) -> Result
<Self, Self::Err
> {
1193 Ok(OsString
::from(s
))
1197 #[stable(feature = "osstring_extend", since = "1.52.0")]
1198 impl Extend
<OsString
> for OsString
{
1200 fn extend
<T
: IntoIterator
<Item
= OsString
>>(&mut self, iter
: T
) {
1207 #[stable(feature = "osstring_extend", since = "1.52.0")]
1208 impl<'a
> Extend
<&'a OsStr
> for OsString
{
1210 fn extend
<T
: IntoIterator
<Item
= &'a OsStr
>>(&mut self, iter
: T
) {
1217 #[stable(feature = "osstring_extend", since = "1.52.0")]
1218 impl<'a
> Extend
<Cow
<'a
, OsStr
>> for OsString
{
1220 fn extend
<T
: IntoIterator
<Item
= Cow
<'a
, OsStr
>>>(&mut self, iter
: T
) {
1227 #[stable(feature = "osstring_extend", since = "1.52.0")]
1228 impl FromIterator
<OsString
> for OsString
{
1230 fn from_iter
<I
: IntoIterator
<Item
= OsString
>>(iter
: I
) -> Self {
1231 let mut iterator
= iter
.into_iter();
1233 // Because we're iterating over `OsString`s, we can avoid at least
1234 // one allocation by getting the first string from the iterator
1235 // and appending to it all the subsequent strings.
1236 match iterator
.next() {
1237 None
=> OsString
::new(),
1239 buf
.extend(iterator
);
1246 #[stable(feature = "osstring_extend", since = "1.52.0")]
1247 impl<'a
> FromIterator
<&'a OsStr
> for OsString
{
1249 fn from_iter
<I
: IntoIterator
<Item
= &'a OsStr
>>(iter
: I
) -> Self {
1250 let mut buf
= Self::new();
1258 #[stable(feature = "osstring_extend", since = "1.52.0")]
1259 impl<'a
> FromIterator
<Cow
<'a
, OsStr
>> for OsString
{
1261 fn from_iter
<I
: IntoIterator
<Item
= Cow
<'a
, OsStr
>>>(iter
: I
) -> Self {
1262 let mut iterator
= iter
.into_iter();
1264 // Because we're iterating over `OsString`s, we can avoid at least
1265 // one allocation by getting the first owned string from the iterator
1266 // and appending to it all the subsequent strings.
1267 match iterator
.next() {
1268 None
=> OsString
::new(),
1269 Some(Cow
::Owned(mut buf
)) => {
1270 buf
.extend(iterator
);
1273 Some(Cow
::Borrowed(buf
)) => {
1274 let mut buf
= OsString
::from(buf
);
1275 buf
.extend(iterator
);