4 use crate::borrow
::{Borrow, Cow}
;
7 use crate::hash
::{Hash, Hasher}
;
10 use crate::str::FromStr
;
13 use crate::sys
::os_str
::{Buf, Slice}
;
14 use crate::sys_common
::{AsInner, FromInner, IntoInner}
;
16 /// A type that can represent owned, mutable platform-native strings, but is
17 /// cheaply inter-convertible with Rust strings.
19 /// The need for this type arises from the fact that:
21 /// * On Unix systems, strings are often arbitrary sequences of non-zero
22 /// bytes, in many cases interpreted as UTF-8.
24 /// * On Windows, strings are often arbitrary sequences of non-zero 16-bit
25 /// values, interpreted as UTF-16 when it is valid to do so.
27 /// * In Rust, strings are always valid UTF-8, which may contain zeros.
29 /// `OsString` and [`OsStr`] bridge this gap by simultaneously representing Rust
30 /// and platform-native string values, and in particular allowing a Rust string
31 /// to be converted into an "OS" string with no cost if possible. A consequence
32 /// of this is that `OsString` instances are *not* `NUL` terminated; in order
33 /// to pass to e.g., Unix system call, you should create a [`CStr`].
35 /// `OsString` is to [`&OsStr`] as [`String`] is to [`&str`]: the former
36 /// in each pair are owned strings; the latter are borrowed
39 /// Note, `OsString` and [`OsStr`] internally do not necessarily hold strings in
40 /// the form native to the platform; While on Unix, strings are stored as a
41 /// sequence of 8-bit values, on Windows, where strings are 16-bit value based
42 /// as just discussed, strings are also actually stored as a sequence of 8-bit
43 /// values, encoded in a less-strict variant of UTF-8. This is useful to
44 /// understand when handling capacity and length values.
46 /// # Creating an `OsString`
48 /// **From a Rust string**: `OsString` implements
49 /// [`From`]`<`[`String`]`>`, so you can use `my_string.from` to
50 /// create an `OsString` from a normal Rust string.
52 /// **From slices:** Just like you can start with an empty Rust
53 /// [`String`] and then [`String::push_str`] `&str`
54 /// sub-string slices into it, you can create an empty `OsString` with
55 /// the [`OsString::new`] method and then push string slices into it with the
56 /// [`OsString::push`] method.
58 /// # Extracting a borrowed reference to the whole OS string
60 /// You can use the [`OsString::as_os_str`] method to get an `&`[`OsStr`] from
61 /// an `OsString`; this is effectively a borrowed reference to the
66 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
67 /// the traits which `OsString` implements for [conversions] from/to native representations.
71 /// [`CStr`]: crate::ffi::CStr
72 /// [conversions]: super#conversions
74 #[stable(feature = "rust1", since = "1.0.0")]
79 /// Borrowed reference to an OS string (see [`OsString`]).
81 /// This type represents a borrowed reference to a string in the operating system's preferred
84 /// `&OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed
85 /// references; the latter are owned strings.
87 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
88 /// the traits which `OsStr` implements for [conversions] from/to native representations.
91 /// [conversions]: super#conversions
92 #[stable(feature = "rust1", since = "1.0.0")]
94 // `OsStr::from_inner` current implementation relies
95 // on `OsStr` being layout-compatible with `Slice`.
96 // When attribute privacy is implemented, `OsStr` should be annotated as `#[repr(transparent)]`.
97 // Anyway, `OsStr` representation and layout are considered implementation details, are
98 // not documented and must not be relied upon.
104 /// Constructs a new empty `OsString`.
109 /// use std::ffi::OsString;
111 /// let os_string = OsString::new();
113 #[stable(feature = "rust1", since = "1.0.0")]
115 pub fn new() -> OsString
{
116 OsString { inner: Buf::from_string(String::new()) }
119 /// Converts to an [`OsStr`] slice.
124 /// use std::ffi::{OsString, OsStr};
126 /// let os_string = OsString::from("foo");
127 /// let os_str = OsStr::new("foo");
128 /// assert_eq!(os_string.as_os_str(), os_str);
130 #[stable(feature = "rust1", since = "1.0.0")]
132 pub fn as_os_str(&self) -> &OsStr
{
136 /// Converts the `OsString` into a [`String`] if it contains valid Unicode data.
138 /// On failure, ownership of the original `OsString` is returned.
143 /// use std::ffi::OsString;
145 /// let os_string = OsString::from("foo");
146 /// let string = os_string.into_string();
147 /// assert_eq!(string, Ok(String::from("foo")));
149 #[stable(feature = "rust1", since = "1.0.0")]
151 pub fn into_string(self) -> Result
<String
, OsString
> {
152 self.inner
.into_string().map_err(|buf
| OsString { inner: buf }
)
155 /// Extends the string with the given [`&OsStr`] slice.
157 /// [`&OsStr`]: OsStr
162 /// use std::ffi::OsString;
164 /// let mut os_string = OsString::from("foo");
165 /// os_string.push("bar");
166 /// assert_eq!(&os_string, "foobar");
168 #[stable(feature = "rust1", since = "1.0.0")]
170 pub fn push
<T
: AsRef
<OsStr
>>(&mut self, s
: T
) {
171 self.inner
.push_slice(&s
.as_ref().inner
)
174 /// Creates a new `OsString` with the given capacity.
176 /// The string will be able to hold exactly `capacity` length units of other
177 /// OS strings without reallocating. If `capacity` is 0, the string will not
180 /// See main `OsString` documentation information about encoding.
185 /// use std::ffi::OsString;
187 /// let mut os_string = OsString::with_capacity(10);
188 /// let capacity = os_string.capacity();
190 /// // This push is done without reallocating
191 /// os_string.push("foo");
193 /// assert_eq!(capacity, os_string.capacity());
195 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
197 pub fn with_capacity(capacity
: usize) -> OsString
{
198 OsString { inner: Buf::with_capacity(capacity) }
201 /// Truncates the `OsString` to zero length.
206 /// use std::ffi::OsString;
208 /// let mut os_string = OsString::from("foo");
209 /// assert_eq!(&os_string, "foo");
211 /// os_string.clear();
212 /// assert_eq!(&os_string, "");
214 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
216 pub fn clear(&mut self) {
220 /// Returns the capacity this `OsString` can hold without reallocating.
222 /// See `OsString` introduction for information about encoding.
227 /// use std::ffi::OsString;
229 /// let os_string = OsString::with_capacity(10);
230 /// assert!(os_string.capacity() >= 10);
232 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
234 pub fn capacity(&self) -> usize {
235 self.inner
.capacity()
238 /// Reserves capacity for at least `additional` more capacity to be inserted
239 /// in the given `OsString`.
241 /// The collection may reserve more space to avoid frequent reallocations.
246 /// use std::ffi::OsString;
248 /// let mut s = OsString::new();
250 /// assert!(s.capacity() >= 10);
252 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
254 pub fn reserve(&mut self, additional
: usize) {
255 self.inner
.reserve(additional
)
258 /// Reserves the minimum capacity for exactly `additional` more capacity to
259 /// be inserted in the given `OsString`. Does nothing if the capacity is
260 /// already sufficient.
262 /// Note that the allocator may give the collection more space than it
263 /// requests. Therefore, capacity can not be relied upon to be precisely
264 /// minimal. Prefer reserve if future insertions are expected.
269 /// use std::ffi::OsString;
271 /// let mut s = OsString::new();
272 /// s.reserve_exact(10);
273 /// assert!(s.capacity() >= 10);
275 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
277 pub fn reserve_exact(&mut self, additional
: usize) {
278 self.inner
.reserve_exact(additional
)
281 /// Shrinks the capacity of the `OsString` to match its length.
286 /// use std::ffi::OsString;
288 /// let mut s = OsString::from("foo");
291 /// assert!(s.capacity() >= 100);
293 /// s.shrink_to_fit();
294 /// assert_eq!(3, s.capacity());
296 #[stable(feature = "osstring_shrink_to_fit", since = "1.19.0")]
298 pub fn shrink_to_fit(&mut self) {
299 self.inner
.shrink_to_fit()
302 /// Shrinks the capacity of the `OsString` with a lower bound.
304 /// The capacity will remain at least as large as both the length
305 /// and the supplied value.
307 /// If the current capacity is less than the lower limit, this is a no-op.
312 /// #![feature(shrink_to)]
313 /// use std::ffi::OsString;
315 /// let mut s = OsString::from("foo");
318 /// assert!(s.capacity() >= 100);
321 /// assert!(s.capacity() >= 10);
323 /// assert!(s.capacity() >= 3);
326 #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
327 pub fn shrink_to(&mut self, min_capacity
: usize) {
328 self.inner
.shrink_to(min_capacity
)
331 /// Converts this `OsString` into a boxed [`OsStr`].
336 /// use std::ffi::{OsString, OsStr};
338 /// let s = OsString::from("hello");
340 /// let b: Box<OsStr> = s.into_boxed_os_str();
342 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
343 pub fn into_boxed_os_str(self) -> Box
<OsStr
> {
344 let rw
= Box
::into_raw(self.inner
.into_box()) as *mut OsStr
;
345 unsafe { Box::from_raw(rw) }
349 #[stable(feature = "rust1", since = "1.0.0")]
350 impl From
<String
> for OsString
{
351 /// Converts a [`String`] into a [`OsString`].
353 /// The conversion copies the data, and includes an allocation on the heap.
355 fn from(s
: String
) -> OsString
{
356 OsString { inner: Buf::from_string(s) }
360 #[stable(feature = "rust1", since = "1.0.0")]
361 impl<T
: ?Sized
+ AsRef
<OsStr
>> From
<&T
> for OsString
{
362 fn from(s
: &T
) -> OsString
{
363 s
.as_ref().to_os_string()
367 #[stable(feature = "rust1", since = "1.0.0")]
368 impl ops
::Index
<ops
::RangeFull
> for OsString
{
372 fn index(&self, _index
: ops
::RangeFull
) -> &OsStr
{
373 OsStr
::from_inner(self.inner
.as_slice())
377 #[stable(feature = "mut_osstr", since = "1.44.0")]
378 impl ops
::IndexMut
<ops
::RangeFull
> for OsString
{
380 fn index_mut(&mut self, _index
: ops
::RangeFull
) -> &mut OsStr
{
381 OsStr
::from_inner_mut(self.inner
.as_mut_slice())
385 #[stable(feature = "rust1", since = "1.0.0")]
386 impl ops
::Deref
for OsString
{
390 fn deref(&self) -> &OsStr
{
395 #[stable(feature = "mut_osstr", since = "1.44.0")]
396 impl ops
::DerefMut
for OsString
{
398 fn deref_mut(&mut self) -> &mut OsStr
{
403 #[stable(feature = "osstring_default", since = "1.9.0")]
404 impl Default
for OsString
{
405 /// Constructs an empty `OsString`.
407 fn default() -> OsString
{
412 #[stable(feature = "rust1", since = "1.0.0")]
413 impl fmt
::Debug
for OsString
{
414 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
415 fmt
::Debug
::fmt(&**self, formatter
)
419 #[stable(feature = "rust1", since = "1.0.0")]
420 impl PartialEq
for OsString
{
422 fn eq(&self, other
: &OsString
) -> bool
{
427 #[stable(feature = "rust1", since = "1.0.0")]
428 impl PartialEq
<str> for OsString
{
430 fn eq(&self, other
: &str) -> bool
{
435 #[stable(feature = "rust1", since = "1.0.0")]
436 impl PartialEq
<OsString
> for str {
438 fn eq(&self, other
: &OsString
) -> bool
{
443 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
444 impl PartialEq
<&str> for OsString
{
446 fn eq(&self, other
: &&str) -> bool
{
451 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
452 impl<'a
> PartialEq
<OsString
> for &'a
str {
454 fn eq(&self, other
: &OsString
) -> bool
{
459 #[stable(feature = "rust1", since = "1.0.0")]
460 impl Eq
for OsString {}
462 #[stable(feature = "rust1", since = "1.0.0")]
463 impl PartialOrd
for OsString
{
465 fn partial_cmp(&self, other
: &OsString
) -> Option
<cmp
::Ordering
> {
466 (&**self).partial_cmp(&**other
)
469 fn lt(&self, other
: &OsString
) -> bool
{
473 fn le(&self, other
: &OsString
) -> bool
{
477 fn gt(&self, other
: &OsString
) -> bool
{
481 fn ge(&self, other
: &OsString
) -> bool
{
486 #[stable(feature = "rust1", since = "1.0.0")]
487 impl PartialOrd
<str> for OsString
{
489 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
490 (&**self).partial_cmp(other
)
494 #[stable(feature = "rust1", since = "1.0.0")]
495 impl Ord
for OsString
{
497 fn cmp(&self, other
: &OsString
) -> cmp
::Ordering
{
498 (&**self).cmp(&**other
)
502 #[stable(feature = "rust1", since = "1.0.0")]
503 impl Hash
for OsString
{
505 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
506 (&**self).hash(state
)
511 /// Coerces into an `OsStr` slice.
516 /// use std::ffi::OsStr;
518 /// let os_str = OsStr::new("foo");
521 #[stable(feature = "rust1", since = "1.0.0")]
522 pub fn new
<S
: AsRef
<OsStr
> + ?Sized
>(s
: &S
) -> &OsStr
{
527 fn from_inner(inner
: &Slice
) -> &OsStr
{
528 // SAFETY: OsStr is just a wrapper of Slice,
529 // therefore converting &Slice to &OsStr is safe.
530 unsafe { &*(inner as *const Slice as *const OsStr) }
534 fn from_inner_mut(inner
: &mut Slice
) -> &mut OsStr
{
535 // SAFETY: OsStr is just a wrapper of Slice,
536 // therefore converting &mut Slice to &mut OsStr is safe.
537 // Any method that mutates OsStr must be careful not to
538 // break platform-specific encoding, in particular Wtf8 on Windows.
539 unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
542 /// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
544 /// This conversion may entail doing a check for UTF-8 validity.
551 /// use std::ffi::OsStr;
553 /// let os_str = OsStr::new("foo");
554 /// assert_eq!(os_str.to_str(), Some("foo"));
556 #[stable(feature = "rust1", since = "1.0.0")]
558 pub fn to_str(&self) -> Option
<&str> {
562 /// Converts an `OsStr` to a [`Cow`]`<`[`str`]`>`.
564 /// Any non-Unicode sequences are replaced with
565 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
567 /// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
571 /// Calling `to_string_lossy` on an `OsStr` with invalid unicode:
574 /// // Note, due to differences in how Unix and Windows represent strings,
575 /// // we are forced to complicate this example, setting up example `OsStr`s
576 /// // with different source data and via different platform extensions.
577 /// // Understand that in reality you could end up with such example invalid
578 /// // sequences simply through collecting user command line arguments, for
581 /// #[cfg(any(unix, target_os = "redox"))] {
582 /// use std::ffi::OsStr;
583 /// use std::os::unix::ffi::OsStrExt;
585 /// // Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
586 /// // respectively. The value 0x80 is a lone continuation byte, invalid
587 /// // in a UTF-8 sequence.
588 /// let source = [0x66, 0x6f, 0x80, 0x6f];
589 /// let os_str = OsStr::from_bytes(&source[..]);
591 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
593 /// #[cfg(windows)] {
594 /// use std::ffi::OsString;
595 /// use std::os::windows::prelude::*;
597 /// // Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
598 /// // respectively. The value 0xD800 is a lone surrogate half, invalid
599 /// // in a UTF-16 sequence.
600 /// let source = [0x0066, 0x006f, 0xD800, 0x006f];
601 /// let os_string = OsString::from_wide(&source[..]);
602 /// let os_str = os_string.as_os_str();
604 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
607 #[stable(feature = "rust1", since = "1.0.0")]
609 pub fn to_string_lossy(&self) -> Cow
<'_
, str> {
610 self.inner
.to_string_lossy()
613 /// Copies the slice into an owned [`OsString`].
618 /// use std::ffi::{OsStr, OsString};
620 /// let os_str = OsStr::new("foo");
621 /// let os_string = os_str.to_os_string();
622 /// assert_eq!(os_string, OsString::from("foo"));
624 #[stable(feature = "rust1", since = "1.0.0")]
626 pub fn to_os_string(&self) -> OsString
{
627 OsString { inner: self.inner.to_owned() }
630 /// Checks whether the `OsStr` is empty.
635 /// use std::ffi::OsStr;
637 /// let os_str = OsStr::new("");
638 /// assert!(os_str.is_empty());
640 /// let os_str = OsStr::new("foo");
641 /// assert!(!os_str.is_empty());
643 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
645 pub fn is_empty(&self) -> bool
{
646 self.inner
.inner
.is_empty()
649 /// Returns the length of this `OsStr`.
651 /// Note that this does **not** return the number of bytes in the string in
654 /// The length returned is that of the underlying storage used by `OsStr`.
655 /// As discussed in the [`OsString`] introduction, [`OsString`] and `OsStr`
656 /// store strings in a form best suited for cheap inter-conversion between
657 /// native-platform and Rust string forms, which may differ significantly
658 /// from both of them, including in storage size and encoding.
660 /// This number is simply useful for passing to other methods, like
661 /// [`OsString::with_capacity`] to avoid reallocations.
666 /// use std::ffi::OsStr;
668 /// let os_str = OsStr::new("");
669 /// assert_eq!(os_str.len(), 0);
671 /// let os_str = OsStr::new("foo");
672 /// assert_eq!(os_str.len(), 3);
674 #[doc(alias = "length")]
675 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
677 pub fn len(&self) -> usize {
678 self.inner
.inner
.len()
681 /// Converts a [`Box`]`<OsStr>` into an [`OsString`] without copying or allocating.
682 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
683 pub fn into_os_string(self: Box
<OsStr
>) -> OsString
{
684 let boxed
= unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) }
;
685 OsString { inner: Buf::from_box(boxed) }
688 /// Gets the underlying byte representation.
690 /// Note: it is *crucial* that this API is not externally public, to avoid
691 /// revealing the internal, platform-specific encodings.
693 pub(crate) fn bytes(&self) -> &[u8] {
694 unsafe { &*(&self.inner as *const _ as *const [u8]) }
697 /// Converts this string to its ASCII lower case equivalent in-place.
699 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
700 /// but non-ASCII letters are unchanged.
702 /// To return a new lowercased value without modifying the existing one, use
703 /// [`OsStr::to_ascii_lowercase`].
708 /// #![feature(osstring_ascii)]
709 /// use std::ffi::OsString;
711 /// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
713 /// s.make_ascii_lowercase();
715 /// assert_eq!("grÜße, jÜrgen ❤", s);
717 #[unstable(feature = "osstring_ascii", issue = "70516")]
719 pub fn make_ascii_lowercase(&mut self) {
720 self.inner
.make_ascii_lowercase()
723 /// Converts this string to its ASCII upper case equivalent in-place.
725 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
726 /// but non-ASCII letters are unchanged.
728 /// To return a new uppercased value without modifying the existing one, use
729 /// [`OsStr::to_ascii_uppercase`].
734 /// #![feature(osstring_ascii)]
735 /// use std::ffi::OsString;
737 /// let mut s = OsString::from("Grüße, Jürgen ❤");
739 /// s.make_ascii_uppercase();
741 /// assert_eq!("GRüßE, JüRGEN ❤", s);
743 #[unstable(feature = "osstring_ascii", issue = "70516")]
745 pub fn make_ascii_uppercase(&mut self) {
746 self.inner
.make_ascii_uppercase()
749 /// Returns a copy of this string where each character is mapped to its
750 /// ASCII lower case equivalent.
752 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
753 /// but non-ASCII letters are unchanged.
755 /// To lowercase the value in-place, use [`OsStr::make_ascii_lowercase`].
760 /// #![feature(osstring_ascii)]
761 /// use std::ffi::OsString;
762 /// let s = OsString::from("Grüße, Jürgen ❤");
764 /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
766 #[unstable(feature = "osstring_ascii", issue = "70516")]
767 pub fn to_ascii_lowercase(&self) -> OsString
{
768 OsString
::from_inner(self.inner
.to_ascii_lowercase())
771 /// Returns a copy of this string where each character is mapped to its
772 /// ASCII upper case equivalent.
774 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
775 /// but non-ASCII letters are unchanged.
777 /// To uppercase the value in-place, use [`OsStr::make_ascii_uppercase`].
782 /// #![feature(osstring_ascii)]
783 /// use std::ffi::OsString;
784 /// let s = OsString::from("Grüße, Jürgen ❤");
786 /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
788 #[unstable(feature = "osstring_ascii", issue = "70516")]
789 pub fn to_ascii_uppercase(&self) -> OsString
{
790 OsString
::from_inner(self.inner
.to_ascii_uppercase())
793 /// Checks if all characters in this string are within the ASCII range.
798 /// #![feature(osstring_ascii)]
799 /// use std::ffi::OsString;
801 /// let ascii = OsString::from("hello!\n");
802 /// let non_ascii = OsString::from("Grüße, Jürgen ❤");
804 /// assert!(ascii.is_ascii());
805 /// assert!(!non_ascii.is_ascii());
807 #[unstable(feature = "osstring_ascii", issue = "70516")]
809 pub fn is_ascii(&self) -> bool
{
810 self.inner
.is_ascii()
813 /// Checks that two strings are an ASCII case-insensitive match.
815 /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
816 /// but without allocating and copying temporaries.
821 /// #![feature(osstring_ascii)]
822 /// use std::ffi::OsString;
824 /// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
825 /// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
826 /// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
828 #[unstable(feature = "osstring_ascii", issue = "70516")]
829 pub fn eq_ignore_ascii_case
<S
: AsRef
<OsStr
>>(&self, other
: S
) -> bool
{
830 self.inner
.eq_ignore_ascii_case(&other
.as_ref().inner
)
834 #[stable(feature = "box_from_os_str", since = "1.17.0")]
835 impl From
<&OsStr
> for Box
<OsStr
> {
837 fn from(s
: &OsStr
) -> Box
<OsStr
> {
838 let rw
= Box
::into_raw(s
.inner
.into_box()) as *mut OsStr
;
839 unsafe { Box::from_raw(rw) }
843 #[stable(feature = "box_from_cow", since = "1.45.0")]
844 impl From
<Cow
<'_
, OsStr
>> for Box
<OsStr
> {
846 fn from(cow
: Cow
<'_
, OsStr
>) -> Box
<OsStr
> {
848 Cow
::Borrowed(s
) => Box
::from(s
),
849 Cow
::Owned(s
) => Box
::from(s
),
854 #[stable(feature = "os_string_from_box", since = "1.18.0")]
855 impl From
<Box
<OsStr
>> for OsString
{
856 /// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or
859 fn from(boxed
: Box
<OsStr
>) -> OsString
{
860 boxed
.into_os_string()
864 #[stable(feature = "box_from_os_string", since = "1.20.0")]
865 impl From
<OsString
> for Box
<OsStr
> {
866 /// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
868 fn from(s
: OsString
) -> Box
<OsStr
> {
869 s
.into_boxed_os_str()
873 #[stable(feature = "more_box_slice_clone", since = "1.29.0")]
874 impl Clone
for Box
<OsStr
> {
876 fn clone(&self) -> Self {
877 self.to_os_string().into_boxed_os_str()
881 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
882 impl From
<OsString
> for Arc
<OsStr
> {
883 /// Converts a [`OsString`] into a [`Arc`]`<OsStr>` without copying or allocating.
885 fn from(s
: OsString
) -> Arc
<OsStr
> {
886 let arc
= s
.inner
.into_arc();
887 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
891 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
892 impl From
<&OsStr
> for Arc
<OsStr
> {
894 fn from(s
: &OsStr
) -> Arc
<OsStr
> {
895 let arc
= s
.inner
.into_arc();
896 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
900 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
901 impl From
<OsString
> for Rc
<OsStr
> {
902 /// Converts a [`OsString`] into a [`Rc`]`<OsStr>` without copying or allocating.
904 fn from(s
: OsString
) -> Rc
<OsStr
> {
905 let rc
= s
.inner
.into_rc();
906 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
910 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
911 impl From
<&OsStr
> for Rc
<OsStr
> {
913 fn from(s
: &OsStr
) -> Rc
<OsStr
> {
914 let rc
= s
.inner
.into_rc();
915 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
919 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
920 impl<'a
> From
<OsString
> for Cow
<'a
, OsStr
> {
922 fn from(s
: OsString
) -> Cow
<'a
, OsStr
> {
927 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
928 impl<'a
> From
<&'a OsStr
> for Cow
<'a
, OsStr
> {
930 fn from(s
: &'a OsStr
) -> Cow
<'a
, OsStr
> {
935 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
936 impl<'a
> From
<&'a OsString
> for Cow
<'a
, OsStr
> {
938 fn from(s
: &'a OsString
) -> Cow
<'a
, OsStr
> {
939 Cow
::Borrowed(s
.as_os_str())
943 #[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
944 impl<'a
> From
<Cow
<'a
, OsStr
>> for OsString
{
946 fn from(s
: Cow
<'a
, OsStr
>) -> Self {
951 #[stable(feature = "box_default_extra", since = "1.17.0")]
952 impl Default
for Box
<OsStr
> {
954 fn default() -> Box
<OsStr
> {
955 let rw
= Box
::into_raw(Slice
::empty_box()) as *mut OsStr
;
956 unsafe { Box::from_raw(rw) }
960 #[stable(feature = "osstring_default", since = "1.9.0")]
961 impl Default
for &OsStr
{
962 /// Creates an empty `OsStr`.
964 fn default() -> Self {
969 #[stable(feature = "rust1", since = "1.0.0")]
970 impl PartialEq
for OsStr
{
972 fn eq(&self, other
: &OsStr
) -> bool
{
973 self.bytes().eq(other
.bytes())
977 #[stable(feature = "rust1", since = "1.0.0")]
978 impl PartialEq
<str> for OsStr
{
980 fn eq(&self, other
: &str) -> bool
{
981 *self == *OsStr
::new(other
)
985 #[stable(feature = "rust1", since = "1.0.0")]
986 impl PartialEq
<OsStr
> for str {
988 fn eq(&self, other
: &OsStr
) -> bool
{
989 *other
== *OsStr
::new(self)
993 #[stable(feature = "rust1", since = "1.0.0")]
996 #[stable(feature = "rust1", since = "1.0.0")]
997 impl PartialOrd
for OsStr
{
999 fn partial_cmp(&self, other
: &OsStr
) -> Option
<cmp
::Ordering
> {
1000 self.bytes().partial_cmp(other
.bytes())
1003 fn lt(&self, other
: &OsStr
) -> bool
{
1004 self.bytes().lt(other
.bytes())
1007 fn le(&self, other
: &OsStr
) -> bool
{
1008 self.bytes().le(other
.bytes())
1011 fn gt(&self, other
: &OsStr
) -> bool
{
1012 self.bytes().gt(other
.bytes())
1015 fn ge(&self, other
: &OsStr
) -> bool
{
1016 self.bytes().ge(other
.bytes())
1020 #[stable(feature = "rust1", since = "1.0.0")]
1021 impl PartialOrd
<str> for OsStr
{
1023 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
1024 self.partial_cmp(OsStr
::new(other
))
1028 // FIXME (#19470): cannot provide PartialOrd<OsStr> for str until we
1029 // have more flexible coherence rules.
1031 #[stable(feature = "rust1", since = "1.0.0")]
1032 impl Ord
for OsStr
{
1034 fn cmp(&self, other
: &OsStr
) -> cmp
::Ordering
{
1035 self.bytes().cmp(other
.bytes())
1039 macro_rules
! impl_cmp
{
1040 ($lhs
:ty
, $rhs
: ty
) => {
1041 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1042 impl<'a
, 'b
> PartialEq
<$rhs
> for $lhs
{
1044 fn eq(&self, other
: &$rhs
) -> bool
{
1045 <OsStr
as PartialEq
>::eq(self, other
)
1049 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1050 impl<'a
, 'b
> PartialEq
<$lhs
> for $rhs
{
1052 fn eq(&self, other
: &$lhs
) -> bool
{
1053 <OsStr
as PartialEq
>::eq(self, other
)
1057 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1058 impl<'a
, 'b
> PartialOrd
<$rhs
> for $lhs
{
1060 fn partial_cmp(&self, other
: &$rhs
) -> Option
<cmp
::Ordering
> {
1061 <OsStr
as PartialOrd
>::partial_cmp(self, other
)
1065 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1066 impl<'a
, 'b
> PartialOrd
<$lhs
> for $rhs
{
1068 fn partial_cmp(&self, other
: &$lhs
) -> Option
<cmp
::Ordering
> {
1069 <OsStr
as PartialOrd
>::partial_cmp(self, other
)
1075 impl_cmp
!(OsString
, OsStr
);
1076 impl_cmp
!(OsString
, &'a OsStr
);
1077 impl_cmp
!(Cow
<'a
, OsStr
>, OsStr
);
1078 impl_cmp
!(Cow
<'a
, OsStr
>, &'b OsStr
);
1079 impl_cmp
!(Cow
<'a
, OsStr
>, OsString
);
1081 #[stable(feature = "rust1", since = "1.0.0")]
1082 impl Hash
for OsStr
{
1084 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
1085 self.bytes().hash(state
)
1089 #[stable(feature = "rust1", since = "1.0.0")]
1090 impl fmt
::Debug
for OsStr
{
1091 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1092 fmt
::Debug
::fmt(&self.inner
, formatter
)
1097 pub(crate) fn display(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1098 fmt
::Display
::fmt(&self.inner
, formatter
)
1102 #[stable(feature = "rust1", since = "1.0.0")]
1103 impl Borrow
<OsStr
> for OsString
{
1105 fn borrow(&self) -> &OsStr
{
1110 #[stable(feature = "rust1", since = "1.0.0")]
1111 impl ToOwned
for OsStr
{
1112 type Owned
= OsString
;
1114 fn to_owned(&self) -> OsString
{
1118 fn clone_into(&self, target
: &mut OsString
) {
1119 self.inner
.clone_into(&mut target
.inner
)
1123 #[stable(feature = "rust1", since = "1.0.0")]
1124 impl AsRef
<OsStr
> for OsStr
{
1126 fn as_ref(&self) -> &OsStr
{
1131 #[stable(feature = "rust1", since = "1.0.0")]
1132 impl AsRef
<OsStr
> for OsString
{
1134 fn as_ref(&self) -> &OsStr
{
1139 #[stable(feature = "rust1", since = "1.0.0")]
1140 impl AsRef
<OsStr
> for str {
1142 fn as_ref(&self) -> &OsStr
{
1143 OsStr
::from_inner(Slice
::from_str(self))
1147 #[stable(feature = "rust1", since = "1.0.0")]
1148 impl AsRef
<OsStr
> for String
{
1150 fn as_ref(&self) -> &OsStr
{
1155 impl FromInner
<Buf
> for OsString
{
1157 fn from_inner(buf
: Buf
) -> OsString
{
1158 OsString { inner: buf }
1162 impl IntoInner
<Buf
> for OsString
{
1164 fn into_inner(self) -> Buf
{
1169 impl AsInner
<Slice
> for OsStr
{
1171 fn as_inner(&self) -> &Slice
{
1176 #[stable(feature = "osstring_from_str", since = "1.45.0")]
1177 impl FromStr
for OsString
{
1178 type Err
= core
::convert
::Infallible
;
1181 fn from_str(s
: &str) -> Result
<Self, Self::Err
> {
1182 Ok(OsString
::from(s
))