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
74 #[cfg_attr(not(test), rustc_diagnostic_item = "OsString")]
75 #[stable(feature = "rust1", since = "1.0.0")]
80 /// Allows extension traits within `std`.
81 #[unstable(feature = "sealed", issue = "none")]
82 impl crate::sealed
::Sealed
for OsString {}
84 /// Borrowed reference to an OS string (see [`OsString`]).
86 /// This type represents a borrowed reference to a string in the operating system's preferred
89 /// `&OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed
90 /// references; the latter are owned strings.
92 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
93 /// the traits which `OsStr` implements for [conversions] from/to native representations.
96 /// [conversions]: super#conversions
97 #[cfg_attr(not(test), rustc_diagnostic_item = "OsStr")]
98 #[stable(feature = "rust1", since = "1.0.0")]
100 // `OsStr::from_inner` current implementation relies
101 // on `OsStr` being layout-compatible with `Slice`.
102 // When attribute privacy is implemented, `OsStr` should be annotated as `#[repr(transparent)]`.
103 // Anyway, `OsStr` representation and layout are considered implementation details, are
104 // not documented and must not be relied upon.
109 /// Allows extension traits within `std`.
110 #[unstable(feature = "sealed", issue = "none")]
111 impl crate::sealed
::Sealed
for OsStr {}
114 /// Constructs a new empty `OsString`.
119 /// use std::ffi::OsString;
121 /// let os_string = OsString::new();
123 #[stable(feature = "rust1", since = "1.0.0")]
125 pub fn new() -> OsString
{
126 OsString { inner: Buf::from_string(String::new()) }
129 /// Converts to an [`OsStr`] slice.
134 /// use std::ffi::{OsString, OsStr};
136 /// let os_string = OsString::from("foo");
137 /// let os_str = OsStr::new("foo");
138 /// assert_eq!(os_string.as_os_str(), os_str);
140 #[stable(feature = "rust1", since = "1.0.0")]
142 pub fn as_os_str(&self) -> &OsStr
{
146 /// Converts the `OsString` into a [`String`] if it contains valid Unicode data.
148 /// On failure, ownership of the original `OsString` is returned.
153 /// use std::ffi::OsString;
155 /// let os_string = OsString::from("foo");
156 /// let string = os_string.into_string();
157 /// assert_eq!(string, Ok(String::from("foo")));
159 #[stable(feature = "rust1", since = "1.0.0")]
161 pub fn into_string(self) -> Result
<String
, OsString
> {
162 self.inner
.into_string().map_err(|buf
| OsString { inner: buf }
)
165 /// Extends the string with the given [`&OsStr`] slice.
167 /// [`&OsStr`]: OsStr
172 /// use std::ffi::OsString;
174 /// let mut os_string = OsString::from("foo");
175 /// os_string.push("bar");
176 /// assert_eq!(&os_string, "foobar");
178 #[stable(feature = "rust1", since = "1.0.0")]
180 pub fn push
<T
: AsRef
<OsStr
>>(&mut self, s
: T
) {
181 self.inner
.push_slice(&s
.as_ref().inner
)
184 /// Creates a new `OsString` with the given capacity.
186 /// The string will be able to hold exactly `capacity` length units of other
187 /// OS strings without reallocating. If `capacity` is 0, the string will not
190 /// See main `OsString` documentation information about encoding.
195 /// use std::ffi::OsString;
197 /// let mut os_string = OsString::with_capacity(10);
198 /// let capacity = os_string.capacity();
200 /// // This push is done without reallocating
201 /// os_string.push("foo");
203 /// assert_eq!(capacity, os_string.capacity());
205 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
207 pub fn with_capacity(capacity
: usize) -> OsString
{
208 OsString { inner: Buf::with_capacity(capacity) }
211 /// Truncates the `OsString` to zero length.
216 /// use std::ffi::OsString;
218 /// let mut os_string = OsString::from("foo");
219 /// assert_eq!(&os_string, "foo");
221 /// os_string.clear();
222 /// assert_eq!(&os_string, "");
224 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
226 pub fn clear(&mut self) {
230 /// Returns the capacity this `OsString` can hold without reallocating.
232 /// See `OsString` introduction for information about encoding.
237 /// use std::ffi::OsString;
239 /// let os_string = OsString::with_capacity(10);
240 /// assert!(os_string.capacity() >= 10);
242 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
244 pub fn capacity(&self) -> usize {
245 self.inner
.capacity()
248 /// Reserves capacity for at least `additional` more capacity to be inserted
249 /// in the given `OsString`.
251 /// The collection may reserve more space to avoid frequent reallocations.
256 /// use std::ffi::OsString;
258 /// let mut s = OsString::new();
260 /// assert!(s.capacity() >= 10);
262 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
264 pub fn reserve(&mut self, additional
: usize) {
265 self.inner
.reserve(additional
)
268 /// Reserves the minimum capacity for exactly `additional` more capacity to
269 /// be inserted in the given `OsString`. Does nothing if the capacity is
270 /// already sufficient.
272 /// Note that the allocator may give the collection more space than it
273 /// requests. Therefore, capacity can not be relied upon to be precisely
274 /// minimal. Prefer reserve if future insertions are expected.
279 /// use std::ffi::OsString;
281 /// let mut s = OsString::new();
282 /// s.reserve_exact(10);
283 /// assert!(s.capacity() >= 10);
285 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
287 pub fn reserve_exact(&mut self, additional
: usize) {
288 self.inner
.reserve_exact(additional
)
291 /// Shrinks the capacity of the `OsString` to match its length.
296 /// use std::ffi::OsString;
298 /// let mut s = OsString::from("foo");
301 /// assert!(s.capacity() >= 100);
303 /// s.shrink_to_fit();
304 /// assert_eq!(3, s.capacity());
306 #[stable(feature = "osstring_shrink_to_fit", since = "1.19.0")]
308 pub fn shrink_to_fit(&mut self) {
309 self.inner
.shrink_to_fit()
312 /// Shrinks the capacity of the `OsString` with a lower bound.
314 /// The capacity will remain at least as large as both the length
315 /// and the supplied value.
317 /// If the current capacity is less than the lower limit, this is a no-op.
322 /// #![feature(shrink_to)]
323 /// use std::ffi::OsString;
325 /// let mut s = OsString::from("foo");
328 /// assert!(s.capacity() >= 100);
331 /// assert!(s.capacity() >= 10);
333 /// assert!(s.capacity() >= 3);
336 #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
337 pub fn shrink_to(&mut self, min_capacity
: usize) {
338 self.inner
.shrink_to(min_capacity
)
341 /// Converts this `OsString` into a boxed [`OsStr`].
346 /// use std::ffi::{OsString, OsStr};
348 /// let s = OsString::from("hello");
350 /// let b: Box<OsStr> = s.into_boxed_os_str();
352 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
353 pub fn into_boxed_os_str(self) -> Box
<OsStr
> {
354 let rw
= Box
::into_raw(self.inner
.into_box()) as *mut OsStr
;
355 unsafe { Box::from_raw(rw) }
359 #[stable(feature = "rust1", since = "1.0.0")]
360 impl From
<String
> for OsString
{
361 /// Converts a [`String`] into a [`OsString`].
363 /// This conversion does not allocate or copy memory.
365 fn from(s
: String
) -> OsString
{
366 OsString { inner: Buf::from_string(s) }
370 #[stable(feature = "rust1", since = "1.0.0")]
371 impl<T
: ?Sized
+ AsRef
<OsStr
>> From
<&T
> for OsString
{
372 fn from(s
: &T
) -> OsString
{
373 s
.as_ref().to_os_string()
377 #[stable(feature = "rust1", since = "1.0.0")]
378 impl ops
::Index
<ops
::RangeFull
> for OsString
{
382 fn index(&self, _index
: ops
::RangeFull
) -> &OsStr
{
383 OsStr
::from_inner(self.inner
.as_slice())
387 #[stable(feature = "mut_osstr", since = "1.44.0")]
388 impl ops
::IndexMut
<ops
::RangeFull
> for OsString
{
390 fn index_mut(&mut self, _index
: ops
::RangeFull
) -> &mut OsStr
{
391 OsStr
::from_inner_mut(self.inner
.as_mut_slice())
395 #[stable(feature = "rust1", since = "1.0.0")]
396 impl ops
::Deref
for OsString
{
400 fn deref(&self) -> &OsStr
{
405 #[stable(feature = "mut_osstr", since = "1.44.0")]
406 impl ops
::DerefMut
for OsString
{
408 fn deref_mut(&mut self) -> &mut OsStr
{
413 #[stable(feature = "osstring_default", since = "1.9.0")]
414 impl Default
for OsString
{
415 /// Constructs an empty `OsString`.
417 fn default() -> OsString
{
422 #[stable(feature = "rust1", since = "1.0.0")]
423 impl Clone
for OsString
{
425 fn clone(&self) -> Self {
426 OsString { inner: self.inner.clone() }
430 fn clone_from(&mut self, source
: &Self) {
431 self.inner
.clone_from(&source
.inner
)
435 #[stable(feature = "rust1", since = "1.0.0")]
436 impl fmt
::Debug
for OsString
{
437 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
438 fmt
::Debug
::fmt(&**self, formatter
)
442 #[stable(feature = "rust1", since = "1.0.0")]
443 impl PartialEq
for OsString
{
445 fn eq(&self, other
: &OsString
) -> bool
{
450 #[stable(feature = "rust1", since = "1.0.0")]
451 impl PartialEq
<str> for OsString
{
453 fn eq(&self, other
: &str) -> bool
{
458 #[stable(feature = "rust1", since = "1.0.0")]
459 impl PartialEq
<OsString
> for str {
461 fn eq(&self, other
: &OsString
) -> bool
{
466 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
467 impl PartialEq
<&str> for OsString
{
469 fn eq(&self, other
: &&str) -> bool
{
474 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
475 impl<'a
> PartialEq
<OsString
> for &'a
str {
477 fn eq(&self, other
: &OsString
) -> bool
{
482 #[stable(feature = "rust1", since = "1.0.0")]
483 impl Eq
for OsString {}
485 #[stable(feature = "rust1", since = "1.0.0")]
486 impl PartialOrd
for OsString
{
488 fn partial_cmp(&self, other
: &OsString
) -> Option
<cmp
::Ordering
> {
489 (&**self).partial_cmp(&**other
)
492 fn lt(&self, other
: &OsString
) -> bool
{
496 fn le(&self, other
: &OsString
) -> bool
{
500 fn gt(&self, other
: &OsString
) -> bool
{
504 fn ge(&self, other
: &OsString
) -> bool
{
509 #[stable(feature = "rust1", since = "1.0.0")]
510 impl PartialOrd
<str> for OsString
{
512 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
513 (&**self).partial_cmp(other
)
517 #[stable(feature = "rust1", since = "1.0.0")]
518 impl Ord
for OsString
{
520 fn cmp(&self, other
: &OsString
) -> cmp
::Ordering
{
521 (&**self).cmp(&**other
)
525 #[stable(feature = "rust1", since = "1.0.0")]
526 impl Hash
for OsString
{
528 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
529 (&**self).hash(state
)
534 /// Coerces into an `OsStr` slice.
539 /// use std::ffi::OsStr;
541 /// let os_str = OsStr::new("foo");
544 #[stable(feature = "rust1", since = "1.0.0")]
545 pub fn new
<S
: AsRef
<OsStr
> + ?Sized
>(s
: &S
) -> &OsStr
{
550 fn from_inner(inner
: &Slice
) -> &OsStr
{
551 // SAFETY: OsStr is just a wrapper of Slice,
552 // therefore converting &Slice to &OsStr is safe.
553 unsafe { &*(inner as *const Slice as *const OsStr) }
557 fn from_inner_mut(inner
: &mut Slice
) -> &mut OsStr
{
558 // SAFETY: OsStr is just a wrapper of Slice,
559 // therefore converting &mut Slice to &mut OsStr is safe.
560 // Any method that mutates OsStr must be careful not to
561 // break platform-specific encoding, in particular Wtf8 on Windows.
562 unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
565 /// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
567 /// This conversion may entail doing a check for UTF-8 validity.
574 /// use std::ffi::OsStr;
576 /// let os_str = OsStr::new("foo");
577 /// assert_eq!(os_str.to_str(), Some("foo"));
579 #[stable(feature = "rust1", since = "1.0.0")]
581 pub fn to_str(&self) -> Option
<&str> {
585 /// Converts an `OsStr` to a [`Cow`]`<`[`str`]`>`.
587 /// Any non-Unicode sequences are replaced with
588 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
590 /// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
594 /// Calling `to_string_lossy` on an `OsStr` with invalid unicode:
597 /// // Note, due to differences in how Unix and Windows represent strings,
598 /// // we are forced to complicate this example, setting up example `OsStr`s
599 /// // with different source data and via different platform extensions.
600 /// // Understand that in reality you could end up with such example invalid
601 /// // sequences simply through collecting user command line arguments, for
605 /// use std::ffi::OsStr;
606 /// use std::os::unix::ffi::OsStrExt;
608 /// // Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
609 /// // respectively. The value 0x80 is a lone continuation byte, invalid
610 /// // in a UTF-8 sequence.
611 /// let source = [0x66, 0x6f, 0x80, 0x6f];
612 /// let os_str = OsStr::from_bytes(&source[..]);
614 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
616 /// #[cfg(windows)] {
617 /// use std::ffi::OsString;
618 /// use std::os::windows::prelude::*;
620 /// // Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
621 /// // respectively. The value 0xD800 is a lone surrogate half, invalid
622 /// // in a UTF-16 sequence.
623 /// let source = [0x0066, 0x006f, 0xD800, 0x006f];
624 /// let os_string = OsString::from_wide(&source[..]);
625 /// let os_str = os_string.as_os_str();
627 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
630 #[stable(feature = "rust1", since = "1.0.0")]
632 pub fn to_string_lossy(&self) -> Cow
<'_
, str> {
633 self.inner
.to_string_lossy()
636 /// Copies the slice into an owned [`OsString`].
641 /// use std::ffi::{OsStr, OsString};
643 /// let os_str = OsStr::new("foo");
644 /// let os_string = os_str.to_os_string();
645 /// assert_eq!(os_string, OsString::from("foo"));
647 #[stable(feature = "rust1", since = "1.0.0")]
649 pub fn to_os_string(&self) -> OsString
{
650 OsString { inner: self.inner.to_owned() }
653 /// Checks whether the `OsStr` is empty.
658 /// use std::ffi::OsStr;
660 /// let os_str = OsStr::new("");
661 /// assert!(os_str.is_empty());
663 /// let os_str = OsStr::new("foo");
664 /// assert!(!os_str.is_empty());
666 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
668 pub fn is_empty(&self) -> bool
{
669 self.inner
.inner
.is_empty()
672 /// Returns the length of this `OsStr`.
674 /// Note that this does **not** return the number of bytes in the string in
677 /// The length returned is that of the underlying storage used by `OsStr`.
678 /// As discussed in the [`OsString`] introduction, [`OsString`] and `OsStr`
679 /// store strings in a form best suited for cheap inter-conversion between
680 /// native-platform and Rust string forms, which may differ significantly
681 /// from both of them, including in storage size and encoding.
683 /// This number is simply useful for passing to other methods, like
684 /// [`OsString::with_capacity`] to avoid reallocations.
689 /// use std::ffi::OsStr;
691 /// let os_str = OsStr::new("");
692 /// assert_eq!(os_str.len(), 0);
694 /// let os_str = OsStr::new("foo");
695 /// assert_eq!(os_str.len(), 3);
697 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
699 pub fn len(&self) -> usize {
700 self.inner
.inner
.len()
703 /// Converts a [`Box`]`<OsStr>` into an [`OsString`] without copying or allocating.
704 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
705 pub fn into_os_string(self: Box
<OsStr
>) -> OsString
{
706 let boxed
= unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) }
;
707 OsString { inner: Buf::from_box(boxed) }
710 /// Gets the underlying byte representation.
712 /// Note: it is *crucial* that this API is not externally public, to avoid
713 /// revealing the internal, platform-specific encodings.
715 pub(crate) fn bytes(&self) -> &[u8] {
716 unsafe { &*(&self.inner as *const _ as *const [u8]) }
719 /// Converts this string to its ASCII lower case equivalent in-place.
721 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
722 /// but non-ASCII letters are unchanged.
724 /// To return a new lowercased value without modifying the existing one, use
725 /// [`OsStr::to_ascii_lowercase`].
730 /// use std::ffi::OsString;
732 /// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
734 /// s.make_ascii_lowercase();
736 /// assert_eq!("grÜße, jÜrgen ❤", s);
738 #[stable(feature = "osstring_ascii", since = "1.53.0")]
740 pub fn make_ascii_lowercase(&mut self) {
741 self.inner
.make_ascii_lowercase()
744 /// Converts this string to its ASCII upper case equivalent in-place.
746 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
747 /// but non-ASCII letters are unchanged.
749 /// To return a new uppercased value without modifying the existing one, use
750 /// [`OsStr::to_ascii_uppercase`].
755 /// use std::ffi::OsString;
757 /// let mut s = OsString::from("Grüße, Jürgen ❤");
759 /// s.make_ascii_uppercase();
761 /// assert_eq!("GRüßE, JüRGEN ❤", s);
763 #[stable(feature = "osstring_ascii", since = "1.53.0")]
765 pub fn make_ascii_uppercase(&mut self) {
766 self.inner
.make_ascii_uppercase()
769 /// Returns a copy of this string where each character is mapped to its
770 /// ASCII lower case equivalent.
772 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
773 /// but non-ASCII letters are unchanged.
775 /// To lowercase the value in-place, use [`OsStr::make_ascii_lowercase`].
780 /// use std::ffi::OsString;
781 /// let s = OsString::from("Grüße, Jürgen ❤");
783 /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
785 #[stable(feature = "osstring_ascii", since = "1.53.0")]
786 pub fn to_ascii_lowercase(&self) -> OsString
{
787 OsString
::from_inner(self.inner
.to_ascii_lowercase())
790 /// Returns a copy of this string where each character is mapped to its
791 /// ASCII upper case equivalent.
793 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
794 /// but non-ASCII letters are unchanged.
796 /// To uppercase the value in-place, use [`OsStr::make_ascii_uppercase`].
801 /// use std::ffi::OsString;
802 /// let s = OsString::from("Grüße, Jürgen ❤");
804 /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
806 #[stable(feature = "osstring_ascii", since = "1.53.0")]
807 pub fn to_ascii_uppercase(&self) -> OsString
{
808 OsString
::from_inner(self.inner
.to_ascii_uppercase())
811 /// Checks if all characters in this string are within the ASCII range.
816 /// use std::ffi::OsString;
818 /// let ascii = OsString::from("hello!\n");
819 /// let non_ascii = OsString::from("Grüße, Jürgen ❤");
821 /// assert!(ascii.is_ascii());
822 /// assert!(!non_ascii.is_ascii());
824 #[stable(feature = "osstring_ascii", since = "1.53.0")]
826 pub fn is_ascii(&self) -> bool
{
827 self.inner
.is_ascii()
830 /// Checks that two strings are an ASCII case-insensitive match.
832 /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
833 /// but without allocating and copying temporaries.
838 /// use std::ffi::OsString;
840 /// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
841 /// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
842 /// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
844 #[stable(feature = "osstring_ascii", since = "1.53.0")]
845 pub fn eq_ignore_ascii_case
<S
: AsRef
<OsStr
>>(&self, other
: S
) -> bool
{
846 self.inner
.eq_ignore_ascii_case(&other
.as_ref().inner
)
850 #[stable(feature = "box_from_os_str", since = "1.17.0")]
851 impl From
<&OsStr
> for Box
<OsStr
> {
853 fn from(s
: &OsStr
) -> Box
<OsStr
> {
854 let rw
= Box
::into_raw(s
.inner
.into_box()) as *mut OsStr
;
855 unsafe { Box::from_raw(rw) }
859 #[stable(feature = "box_from_cow", since = "1.45.0")]
860 impl From
<Cow
<'_
, OsStr
>> for Box
<OsStr
> {
862 fn from(cow
: Cow
<'_
, OsStr
>) -> Box
<OsStr
> {
864 Cow
::Borrowed(s
) => Box
::from(s
),
865 Cow
::Owned(s
) => Box
::from(s
),
870 #[stable(feature = "os_string_from_box", since = "1.18.0")]
871 impl From
<Box
<OsStr
>> for OsString
{
872 /// Converts a [`Box`]`<`[`OsStr`]`>` into an [`OsString`] without copying or
875 fn from(boxed
: Box
<OsStr
>) -> OsString
{
876 boxed
.into_os_string()
880 #[stable(feature = "box_from_os_string", since = "1.20.0")]
881 impl From
<OsString
> for Box
<OsStr
> {
882 /// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
884 fn from(s
: OsString
) -> Box
<OsStr
> {
885 s
.into_boxed_os_str()
889 #[stable(feature = "more_box_slice_clone", since = "1.29.0")]
890 impl Clone
for Box
<OsStr
> {
892 fn clone(&self) -> Self {
893 self.to_os_string().into_boxed_os_str()
897 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
898 impl From
<OsString
> for Arc
<OsStr
> {
899 /// Converts a [`OsString`] into a [`Arc`]`<OsStr>` without copying or allocating.
901 fn from(s
: OsString
) -> Arc
<OsStr
> {
902 let arc
= s
.inner
.into_arc();
903 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
907 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
908 impl From
<&OsStr
> for Arc
<OsStr
> {
910 fn from(s
: &OsStr
) -> Arc
<OsStr
> {
911 let arc
= s
.inner
.into_arc();
912 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
916 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
917 impl From
<OsString
> for Rc
<OsStr
> {
918 /// Converts a [`OsString`] into a [`Rc`]`<OsStr>` without copying or allocating.
920 fn from(s
: OsString
) -> Rc
<OsStr
> {
921 let rc
= s
.inner
.into_rc();
922 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
926 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
927 impl From
<&OsStr
> for Rc
<OsStr
> {
929 fn from(s
: &OsStr
) -> Rc
<OsStr
> {
930 let rc
= s
.inner
.into_rc();
931 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
935 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
936 impl<'a
> From
<OsString
> for Cow
<'a
, OsStr
> {
938 fn from(s
: OsString
) -> Cow
<'a
, OsStr
> {
943 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
944 impl<'a
> From
<&'a OsStr
> for Cow
<'a
, OsStr
> {
946 fn from(s
: &'a OsStr
) -> Cow
<'a
, OsStr
> {
951 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
952 impl<'a
> From
<&'a OsString
> for Cow
<'a
, OsStr
> {
954 fn from(s
: &'a OsString
) -> Cow
<'a
, OsStr
> {
955 Cow
::Borrowed(s
.as_os_str())
959 #[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
960 impl<'a
> From
<Cow
<'a
, OsStr
>> for OsString
{
962 fn from(s
: Cow
<'a
, OsStr
>) -> Self {
967 #[stable(feature = "box_default_extra", since = "1.17.0")]
968 impl Default
for Box
<OsStr
> {
970 fn default() -> Box
<OsStr
> {
971 let rw
= Box
::into_raw(Slice
::empty_box()) as *mut OsStr
;
972 unsafe { Box::from_raw(rw) }
976 #[stable(feature = "osstring_default", since = "1.9.0")]
977 impl Default
for &OsStr
{
978 /// Creates an empty `OsStr`.
980 fn default() -> Self {
985 #[stable(feature = "rust1", since = "1.0.0")]
986 impl PartialEq
for OsStr
{
988 fn eq(&self, other
: &OsStr
) -> bool
{
989 self.bytes().eq(other
.bytes())
993 #[stable(feature = "rust1", since = "1.0.0")]
994 impl PartialEq
<str> for OsStr
{
996 fn eq(&self, other
: &str) -> bool
{
997 *self == *OsStr
::new(other
)
1001 #[stable(feature = "rust1", since = "1.0.0")]
1002 impl PartialEq
<OsStr
> for str {
1004 fn eq(&self, other
: &OsStr
) -> bool
{
1005 *other
== *OsStr
::new(self)
1009 #[stable(feature = "rust1", since = "1.0.0")]
1010 impl Eq
for OsStr {}
1012 #[stable(feature = "rust1", since = "1.0.0")]
1013 impl PartialOrd
for OsStr
{
1015 fn partial_cmp(&self, other
: &OsStr
) -> Option
<cmp
::Ordering
> {
1016 self.bytes().partial_cmp(other
.bytes())
1019 fn lt(&self, other
: &OsStr
) -> bool
{
1020 self.bytes().lt(other
.bytes())
1023 fn le(&self, other
: &OsStr
) -> bool
{
1024 self.bytes().le(other
.bytes())
1027 fn gt(&self, other
: &OsStr
) -> bool
{
1028 self.bytes().gt(other
.bytes())
1031 fn ge(&self, other
: &OsStr
) -> bool
{
1032 self.bytes().ge(other
.bytes())
1036 #[stable(feature = "rust1", since = "1.0.0")]
1037 impl PartialOrd
<str> for OsStr
{
1039 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
1040 self.partial_cmp(OsStr
::new(other
))
1044 // FIXME (#19470): cannot provide PartialOrd<OsStr> for str until we
1045 // have more flexible coherence rules.
1047 #[stable(feature = "rust1", since = "1.0.0")]
1048 impl Ord
for OsStr
{
1050 fn cmp(&self, other
: &OsStr
) -> cmp
::Ordering
{
1051 self.bytes().cmp(other
.bytes())
1055 macro_rules
! impl_cmp
{
1056 ($lhs
:ty
, $rhs
: ty
) => {
1057 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1058 impl<'a
, 'b
> PartialEq
<$rhs
> for $lhs
{
1060 fn eq(&self, other
: &$rhs
) -> bool
{
1061 <OsStr
as PartialEq
>::eq(self, other
)
1065 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1066 impl<'a
, 'b
> PartialEq
<$lhs
> for $rhs
{
1068 fn eq(&self, other
: &$lhs
) -> bool
{
1069 <OsStr
as PartialEq
>::eq(self, other
)
1073 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1074 impl<'a
, 'b
> PartialOrd
<$rhs
> for $lhs
{
1076 fn partial_cmp(&self, other
: &$rhs
) -> Option
<cmp
::Ordering
> {
1077 <OsStr
as PartialOrd
>::partial_cmp(self, other
)
1081 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1082 impl<'a
, 'b
> PartialOrd
<$lhs
> for $rhs
{
1084 fn partial_cmp(&self, other
: &$lhs
) -> Option
<cmp
::Ordering
> {
1085 <OsStr
as PartialOrd
>::partial_cmp(self, other
)
1091 impl_cmp
!(OsString
, OsStr
);
1092 impl_cmp
!(OsString
, &'a OsStr
);
1093 impl_cmp
!(Cow
<'a
, OsStr
>, OsStr
);
1094 impl_cmp
!(Cow
<'a
, OsStr
>, &'b OsStr
);
1095 impl_cmp
!(Cow
<'a
, OsStr
>, OsString
);
1097 #[stable(feature = "rust1", since = "1.0.0")]
1098 impl Hash
for OsStr
{
1100 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
1101 self.bytes().hash(state
)
1105 #[stable(feature = "rust1", since = "1.0.0")]
1106 impl fmt
::Debug
for OsStr
{
1107 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1108 fmt
::Debug
::fmt(&self.inner
, formatter
)
1113 pub(crate) fn display(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1114 fmt
::Display
::fmt(&self.inner
, formatter
)
1118 #[stable(feature = "rust1", since = "1.0.0")]
1119 impl Borrow
<OsStr
> for OsString
{
1121 fn borrow(&self) -> &OsStr
{
1126 #[stable(feature = "rust1", since = "1.0.0")]
1127 impl ToOwned
for OsStr
{
1128 type Owned
= OsString
;
1130 fn to_owned(&self) -> OsString
{
1134 fn clone_into(&self, target
: &mut OsString
) {
1135 self.inner
.clone_into(&mut target
.inner
)
1139 #[stable(feature = "rust1", since = "1.0.0")]
1140 impl AsRef
<OsStr
> for OsStr
{
1142 fn as_ref(&self) -> &OsStr
{
1147 #[stable(feature = "rust1", since = "1.0.0")]
1148 impl AsRef
<OsStr
> for OsString
{
1150 fn as_ref(&self) -> &OsStr
{
1155 #[stable(feature = "rust1", since = "1.0.0")]
1156 impl AsRef
<OsStr
> for str {
1158 fn as_ref(&self) -> &OsStr
{
1159 OsStr
::from_inner(Slice
::from_str(self))
1163 #[stable(feature = "rust1", since = "1.0.0")]
1164 impl AsRef
<OsStr
> for String
{
1166 fn as_ref(&self) -> &OsStr
{
1171 impl FromInner
<Buf
> for OsString
{
1173 fn from_inner(buf
: Buf
) -> OsString
{
1174 OsString { inner: buf }
1178 impl IntoInner
<Buf
> for OsString
{
1180 fn into_inner(self) -> Buf
{
1185 impl AsInner
<Slice
> for OsStr
{
1187 fn as_inner(&self) -> &Slice
{
1192 #[stable(feature = "osstring_from_str", since = "1.45.0")]
1193 impl FromStr
for OsString
{
1194 type Err
= core
::convert
::Infallible
;
1197 fn from_str(s
: &str) -> Result
<Self, Self::Err
> {
1198 Ok(OsString
::from(s
))
1202 #[stable(feature = "osstring_extend", since = "1.52.0")]
1203 impl Extend
<OsString
> for OsString
{
1205 fn extend
<T
: IntoIterator
<Item
= OsString
>>(&mut self, iter
: T
) {
1212 #[stable(feature = "osstring_extend", since = "1.52.0")]
1213 impl<'a
> Extend
<&'a OsStr
> for OsString
{
1215 fn extend
<T
: IntoIterator
<Item
= &'a OsStr
>>(&mut self, iter
: T
) {
1222 #[stable(feature = "osstring_extend", since = "1.52.0")]
1223 impl<'a
> Extend
<Cow
<'a
, OsStr
>> for OsString
{
1225 fn extend
<T
: IntoIterator
<Item
= Cow
<'a
, OsStr
>>>(&mut self, iter
: T
) {
1232 #[stable(feature = "osstring_extend", since = "1.52.0")]
1233 impl FromIterator
<OsString
> for OsString
{
1235 fn from_iter
<I
: IntoIterator
<Item
= OsString
>>(iter
: I
) -> Self {
1236 let mut iterator
= iter
.into_iter();
1238 // Because we're iterating over `OsString`s, we can avoid at least
1239 // one allocation by getting the first string from the iterator
1240 // and appending to it all the subsequent strings.
1241 match iterator
.next() {
1242 None
=> OsString
::new(),
1244 buf
.extend(iterator
);
1251 #[stable(feature = "osstring_extend", since = "1.52.0")]
1252 impl<'a
> FromIterator
<&'a OsStr
> for OsString
{
1254 fn from_iter
<I
: IntoIterator
<Item
= &'a OsStr
>>(iter
: I
) -> Self {
1255 let mut buf
= Self::new();
1263 #[stable(feature = "osstring_extend", since = "1.52.0")]
1264 impl<'a
> FromIterator
<Cow
<'a
, OsStr
>> for OsString
{
1266 fn from_iter
<I
: IntoIterator
<Item
= Cow
<'a
, OsStr
>>>(iter
: I
) -> Self {
1267 let mut iterator
= iter
.into_iter();
1269 // Because we're iterating over `OsString`s, we can avoid at least
1270 // one allocation by getting the first owned string from the iterator
1271 // and appending to it all the subsequent strings.
1272 match iterator
.next() {
1273 None
=> OsString
::new(),
1274 Some(Cow
::Owned(mut buf
)) => {
1275 buf
.extend(iterator
);
1278 Some(Cow
::Borrowed(buf
)) => {
1279 let mut buf
= OsString
::from(buf
);
1280 buf
.extend(iterator
);