1 use crate::borrow::{Borrow, Cow};
4 use crate::hash::{Hash, Hasher};
7 use crate::str::FromStr;
10 use crate::sys::os_str::{Buf, Slice};
11 use crate::sys_common::{AsInner, FromInner, IntoInner};
13 /// A type that can represent owned, mutable platform-native strings, but is
14 /// cheaply inter-convertible with Rust strings.
16 /// The need for this type arises from the fact that:
18 /// * On Unix systems, strings are often arbitrary sequences of non-zero
19 /// bytes, in many cases interpreted as UTF-8.
21 /// * On Windows, strings are often arbitrary sequences of non-zero 16-bit
22 /// values, interpreted as UTF-16 when it is valid to do so.
24 /// * In Rust, strings are always valid UTF-8, which may contain zeros.
26 /// `OsString` and [`OsStr`] bridge this gap by simultaneously representing Rust
27 /// and platform-native string values, and in particular allowing a Rust string
28 /// to be converted into an "OS" string with no cost if possible. A consequence
29 /// of this is that `OsString` instances are *not* `NUL` terminated; in order
30 /// to pass to e.g., Unix system call, you should create a [`CStr`].
32 /// `OsString` is to [`&OsStr`] as [`String`] is to [`&str`]: the former
33 /// in each pair are owned strings; the latter are borrowed
36 /// Note, `OsString` and [`OsStr`] internally do not necessarily hold strings in
37 /// the form native to the platform; While on Unix, strings are stored as a
38 /// sequence of 8-bit values, on Windows, where strings are 16-bit value based
39 /// as just discussed, strings are also actually stored as a sequence of 8-bit
40 /// values, encoded in a less-strict variant of UTF-8. This is useful to
41 /// understand when handling capacity and length values.
43 /// # Creating an `OsString`
45 /// **From a Rust string**: `OsString` implements
46 /// [`From`]`<`[`String`]`>`, so you can use `my_string.from` to
47 /// create an `OsString` from a normal Rust string.
49 /// **From slices:** Just like you can start with an empty Rust
50 /// [`String`] and then [`String::push_str`] `&str`
51 /// sub-string slices into it, you can create an empty `OsString` with
52 /// the [`OsString::new`] method and then push string slices into it with the
53 /// [`OsString::push`] method.
55 /// # Extracting a borrowed reference to the whole OS string
57 /// You can use the [`OsString::as_os_str`] method to get an `&`[`OsStr`] from
58 /// an `OsString`; this is effectively a borrowed reference to the
63 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
64 /// the traits which `OsString` implements for [conversions] from/to native representations.
68 /// [`CStr`]: crate::ffi::CStr
69 /// [conversions]: index.html#conversions
71 #[stable(feature = "rust1", since = "1.0.0")]
76 /// Borrowed reference to an OS string (see [`OsString`]).
78 /// This type represents a borrowed reference to a string in the operating system's preferred
81 /// `&OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed
82 /// references; the latter are owned strings.
84 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
85 /// the traits which `OsStr` implements for [conversions] from/to native representations.
88 /// [conversions]: index.html#conversions
89 #[stable(feature = "rust1", since = "1.0.0")]
91 // `OsStr::from_inner` current implementation relies
92 // on `OsStr` being layout-compatible with `Slice`.
93 // When attribute privacy is implemented, `OsStr` should be annotated as `#[repr(transparent)]`.
94 // Anyway, `OsStr` representation and layout are considered implementation detail, are
95 // not documented and must not be relied upon.
101 /// Constructs a new empty `OsString`.
106 /// use std::ffi::OsString;
108 /// let os_string = OsString::new();
110 #[stable(feature = "rust1", since = "1.0.0")]
111 pub fn new() -> OsString {
112 OsString { inner: Buf::from_string(String::new()) }
115 /// Converts to an [`OsStr`] slice.
120 /// use std::ffi::{OsString, OsStr};
122 /// let os_string = OsString::from("foo");
123 /// let os_str = OsStr::new("foo");
124 /// assert_eq!(os_string.as_os_str(), os_str);
126 #[stable(feature = "rust1", since = "1.0.0")]
127 pub fn as_os_str(&self) -> &OsStr {
131 /// Converts the `OsString` into a [`String`] if it contains valid Unicode data.
133 /// On failure, ownership of the original `OsString` is returned.
138 /// use std::ffi::OsString;
140 /// let os_string = OsString::from("foo");
141 /// let string = os_string.into_string();
142 /// assert_eq!(string, Ok(String::from("foo")));
144 #[stable(feature = "rust1", since = "1.0.0")]
145 pub fn into_string(self) -> Result<String, OsString> {
146 self.inner.into_string().map_err(|buf| OsString { inner: buf })
149 /// Extends the string with the given [`&OsStr`] slice.
151 /// [`&OsStr`]: OsStr
156 /// use std::ffi::OsString;
158 /// let mut os_string = OsString::from("foo");
159 /// os_string.push("bar");
160 /// assert_eq!(&os_string, "foobar");
162 #[stable(feature = "rust1", since = "1.0.0")]
163 pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
164 self.inner.push_slice(&s.as_ref().inner)
167 /// Creates a new `OsString` with the given capacity.
169 /// The string will be able to hold exactly `capacity` length units of other
170 /// OS strings without reallocating. If `capacity` is 0, the string will not
173 /// See main `OsString` documentation information about encoding.
178 /// use std::ffi::OsString;
180 /// let mut os_string = OsString::with_capacity(10);
181 /// let capacity = os_string.capacity();
183 /// // This push is done without reallocating
184 /// os_string.push("foo");
186 /// assert_eq!(capacity, os_string.capacity());
188 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
189 pub fn with_capacity(capacity: usize) -> OsString {
190 OsString { inner: Buf::with_capacity(capacity) }
193 /// Truncates the `OsString` to zero length.
198 /// use std::ffi::OsString;
200 /// let mut os_string = OsString::from("foo");
201 /// assert_eq!(&os_string, "foo");
203 /// os_string.clear();
204 /// assert_eq!(&os_string, "");
206 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
207 pub fn clear(&mut self) {
211 /// Returns the capacity this `OsString` can hold without reallocating.
213 /// See `OsString` introduction for information about encoding.
218 /// use std::ffi::OsString;
220 /// let os_string = OsString::with_capacity(10);
221 /// assert!(os_string.capacity() >= 10);
223 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
224 pub fn capacity(&self) -> usize {
225 self.inner.capacity()
228 /// Reserves capacity for at least `additional` more capacity to be inserted
229 /// in the given `OsString`.
231 /// The collection may reserve more space to avoid frequent reallocations.
236 /// use std::ffi::OsString;
238 /// let mut s = OsString::new();
240 /// assert!(s.capacity() >= 10);
242 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
243 pub fn reserve(&mut self, additional: usize) {
244 self.inner.reserve(additional)
247 /// Reserves the minimum capacity for exactly `additional` more capacity to
248 /// be inserted in the given `OsString`. Does nothing if the capacity is
249 /// already sufficient.
251 /// Note that the allocator may give the collection more space than it
252 /// requests. Therefore, capacity can not be relied upon to be precisely
253 /// minimal. Prefer reserve if future insertions are expected.
258 /// use std::ffi::OsString;
260 /// let mut s = OsString::new();
261 /// s.reserve_exact(10);
262 /// assert!(s.capacity() >= 10);
264 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
265 pub fn reserve_exact(&mut self, additional: usize) {
266 self.inner.reserve_exact(additional)
269 /// Shrinks the capacity of the `OsString` to match its length.
274 /// use std::ffi::OsString;
276 /// let mut s = OsString::from("foo");
279 /// assert!(s.capacity() >= 100);
281 /// s.shrink_to_fit();
282 /// assert_eq!(3, s.capacity());
284 #[stable(feature = "osstring_shrink_to_fit", since = "1.19.0")]
285 pub fn shrink_to_fit(&mut self) {
286 self.inner.shrink_to_fit()
289 /// Shrinks the capacity of the `OsString` with a lower bound.
291 /// The capacity will remain at least as large as both the length
292 /// and the supplied value.
294 /// Panics if the current capacity is smaller than the supplied
295 /// minimum capacity.
300 /// #![feature(shrink_to)]
301 /// use std::ffi::OsString;
303 /// let mut s = OsString::from("foo");
306 /// assert!(s.capacity() >= 100);
309 /// assert!(s.capacity() >= 10);
311 /// assert!(s.capacity() >= 3);
314 #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
315 pub fn shrink_to(&mut self, min_capacity: usize) {
316 self.inner.shrink_to(min_capacity)
319 /// Converts this `OsString` into a boxed [`OsStr`].
324 /// use std::ffi::{OsString, OsStr};
326 /// let s = OsString::from("hello");
328 /// let b: Box<OsStr> = s.into_boxed_os_str();
330 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
331 pub fn into_boxed_os_str(self) -> Box<OsStr> {
332 let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;
333 unsafe { Box::from_raw(rw) }
337 #[stable(feature = "rust1", since = "1.0.0")]
338 impl From<String> for OsString {
339 /// Converts a [`String`] into a [`OsString`].
341 /// The conversion copies the data, and includes an allocation on the heap.
342 fn from(s: String) -> OsString {
343 OsString { inner: Buf::from_string(s) }
347 #[stable(feature = "rust1", since = "1.0.0")]
348 impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
349 fn from(s: &T) -> OsString {
350 s.as_ref().to_os_string()
354 #[stable(feature = "rust1", since = "1.0.0")]
355 impl ops::Index<ops::RangeFull> for OsString {
359 fn index(&self, _index: ops::RangeFull) -> &OsStr {
360 OsStr::from_inner(self.inner.as_slice())
364 #[stable(feature = "mut_osstr", since = "1.44.0")]
365 impl ops::IndexMut<ops::RangeFull> for OsString {
367 fn index_mut(&mut self, _index: ops::RangeFull) -> &mut OsStr {
368 OsStr::from_inner_mut(self.inner.as_mut_slice())
372 #[stable(feature = "rust1", since = "1.0.0")]
373 impl ops::Deref for OsString {
377 fn deref(&self) -> &OsStr {
382 #[stable(feature = "mut_osstr", since = "1.44.0")]
383 impl ops::DerefMut for OsString {
385 fn deref_mut(&mut self) -> &mut OsStr {
390 #[stable(feature = "osstring_default", since = "1.9.0")]
391 impl Default for OsString {
392 /// Constructs an empty `OsString`.
394 fn default() -> OsString {
399 #[stable(feature = "rust1", since = "1.0.0")]
400 impl fmt::Debug for OsString {
401 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
402 fmt::Debug::fmt(&**self, formatter)
406 #[stable(feature = "rust1", since = "1.0.0")]
407 impl PartialEq for OsString {
408 fn eq(&self, other: &OsString) -> bool {
413 #[stable(feature = "rust1", since = "1.0.0")]
414 impl PartialEq<str> for OsString {
415 fn eq(&self, other: &str) -> bool {
420 #[stable(feature = "rust1", since = "1.0.0")]
421 impl PartialEq<OsString> for str {
422 fn eq(&self, other: &OsString) -> bool {
427 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
428 impl PartialEq<&str> for OsString {
429 fn eq(&self, other: &&str) -> bool {
434 #[stable(feature = "os_str_str_ref_eq", since = "1.29.0")]
435 impl<'a> PartialEq<OsString> for &'a str {
436 fn eq(&self, other: &OsString) -> bool {
441 #[stable(feature = "rust1", since = "1.0.0")]
442 impl Eq for OsString {}
444 #[stable(feature = "rust1", since = "1.0.0")]
445 impl PartialOrd for OsString {
447 fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
448 (&**self).partial_cmp(&**other)
451 fn lt(&self, other: &OsString) -> bool {
455 fn le(&self, other: &OsString) -> bool {
459 fn gt(&self, other: &OsString) -> bool {
463 fn ge(&self, other: &OsString) -> bool {
468 #[stable(feature = "rust1", since = "1.0.0")]
469 impl PartialOrd<str> for OsString {
471 fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
472 (&**self).partial_cmp(other)
476 #[stable(feature = "rust1", since = "1.0.0")]
477 impl Ord for OsString {
479 fn cmp(&self, other: &OsString) -> cmp::Ordering {
480 (&**self).cmp(&**other)
484 #[stable(feature = "rust1", since = "1.0.0")]
485 impl Hash for OsString {
487 fn hash<H: Hasher>(&self, state: &mut H) {
488 (&**self).hash(state)
493 /// Coerces into an `OsStr` slice.
498 /// use std::ffi::OsStr;
500 /// let os_str = OsStr::new("foo");
503 #[stable(feature = "rust1", since = "1.0.0")]
504 pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &OsStr {
509 fn from_inner(inner: &Slice) -> &OsStr {
510 // Safety: OsStr is just a wrapper of Slice,
511 // therefore converting &Slice to &OsStr is safe.
512 unsafe { &*(inner as *const Slice as *const OsStr) }
516 fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
517 // Safety: OsStr is just a wrapper of Slice,
518 // therefore converting &mut Slice to &mut OsStr is safe.
519 // Any method that mutates OsStr must be careful not to
520 // break platform-specific encoding, in particular Wtf8 on Windows.
521 unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
524 /// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
526 /// This conversion may entail doing a check for UTF-8 validity.
533 /// use std::ffi::OsStr;
535 /// let os_str = OsStr::new("foo");
536 /// assert_eq!(os_str.to_str(), Some("foo"));
538 #[stable(feature = "rust1", since = "1.0.0")]
539 pub fn to_str(&self) -> Option<&str> {
543 /// Converts an `OsStr` to a [`Cow`]`<`[`str`]`>`.
545 /// Any non-Unicode sequences are replaced with
546 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
548 /// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
552 /// Calling `to_string_lossy` on an `OsStr` with invalid unicode:
555 /// // Note, due to differences in how Unix and Windows represent strings,
556 /// // we are forced to complicate this example, setting up example `OsStr`s
557 /// // with different source data and via different platform extensions.
558 /// // Understand that in reality you could end up with such example invalid
559 /// // sequences simply through collecting user command line arguments, for
562 /// #[cfg(any(unix, target_os = "redox"))] {
563 /// use std::ffi::OsStr;
564 /// use std::os::unix::ffi::OsStrExt;
566 /// // Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
567 /// // respectively. The value 0x80 is a lone continuation byte, invalid
568 /// // in a UTF-8 sequence.
569 /// let source = [0x66, 0x6f, 0x80, 0x6f];
570 /// let os_str = OsStr::from_bytes(&source[..]);
572 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
574 /// #[cfg(windows)] {
575 /// use std::ffi::OsString;
576 /// use std::os::windows::prelude::*;
578 /// // Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
579 /// // respectively. The value 0xD800 is a lone surrogate half, invalid
580 /// // in a UTF-16 sequence.
581 /// let source = [0x0066, 0x006f, 0xD800, 0x006f];
582 /// let os_string = OsString::from_wide(&source[..]);
583 /// let os_str = os_string.as_os_str();
585 /// assert_eq!(os_str.to_string_lossy(), "fo�o");
588 #[stable(feature = "rust1", since = "1.0.0")]
589 pub fn to_string_lossy(&self) -> Cow<'_, str> {
590 self.inner.to_string_lossy()
593 /// Copies the slice into an owned [`OsString`].
598 /// use std::ffi::{OsStr, OsString};
600 /// let os_str = OsStr::new("foo");
601 /// let os_string = os_str.to_os_string();
602 /// assert_eq!(os_string, OsString::from("foo"));
604 #[stable(feature = "rust1", since = "1.0.0")]
605 pub fn to_os_string(&self) -> OsString {
606 OsString { inner: self.inner.to_owned() }
609 /// Checks whether the `OsStr` is empty.
614 /// use std::ffi::OsStr;
616 /// let os_str = OsStr::new("");
617 /// assert!(os_str.is_empty());
619 /// let os_str = OsStr::new("foo");
620 /// assert!(!os_str.is_empty());
622 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
624 pub fn is_empty(&self) -> bool {
625 self.inner.inner.is_empty()
628 /// Returns the length of this `OsStr`.
630 /// Note that this does **not** return the number of bytes in the string in
633 /// The length returned is that of the underlying storage used by `OsStr`.
634 /// As discussed in the [`OsString`] introduction, [`OsString`] and `OsStr`
635 /// store strings in a form best suited for cheap inter-conversion between
636 /// native-platform and Rust string forms, which may differ significantly
637 /// from both of them, including in storage size and encoding.
639 /// This number is simply useful for passing to other methods, like
640 /// [`OsString::with_capacity`] to avoid reallocations.
645 /// use std::ffi::OsStr;
647 /// let os_str = OsStr::new("");
648 /// assert_eq!(os_str.len(), 0);
650 /// let os_str = OsStr::new("foo");
651 /// assert_eq!(os_str.len(), 3);
653 #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
654 pub fn len(&self) -> usize {
655 self.inner.inner.len()
658 /// Converts a [`Box`]`<OsStr>` into an [`OsString`] without copying or allocating.
659 #[stable(feature = "into_boxed_os_str", since = "1.20.0")]
660 pub fn into_os_string(self: Box<OsStr>) -> OsString {
661 let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
662 OsString { inner: Buf::from_box(boxed) }
665 /// Gets the underlying byte representation.
667 /// Note: it is *crucial* that this API is private, to avoid
668 /// revealing the internal, platform-specific encodings.
670 fn bytes(&self) -> &[u8] {
671 unsafe { &*(&self.inner as *const _ as *const [u8]) }
674 /// Converts this string to its ASCII lower case equivalent in-place.
676 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
677 /// but non-ASCII letters are unchanged.
679 /// To return a new lowercased value without modifying the existing one, use
680 /// [`OsStr::to_ascii_lowercase`].
685 /// #![feature(osstring_ascii)]
686 /// use std::ffi::OsString;
688 /// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
690 /// s.make_ascii_lowercase();
692 /// assert_eq!("grÜße, jÜrgen ❤", s);
694 #[unstable(feature = "osstring_ascii", issue = "70516")]
695 pub fn make_ascii_lowercase(&mut self) {
696 self.inner.make_ascii_lowercase()
699 /// Converts this string to its ASCII upper case equivalent in-place.
701 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
702 /// but non-ASCII letters are unchanged.
704 /// To return a new uppercased value without modifying the existing one, use
705 /// [`OsStr::to_ascii_uppercase`].
710 /// #![feature(osstring_ascii)]
711 /// use std::ffi::OsString;
713 /// let mut s = OsString::from("Grüße, Jürgen ❤");
715 /// s.make_ascii_uppercase();
717 /// assert_eq!("GRüßE, JüRGEN ❤", s);
719 #[unstable(feature = "osstring_ascii", issue = "70516")]
720 pub fn make_ascii_uppercase(&mut self) {
721 self.inner.make_ascii_uppercase()
724 /// Returns a copy of this string where each character is mapped to its
725 /// ASCII lower case equivalent.
727 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
728 /// but non-ASCII letters are unchanged.
730 /// To lowercase the value in-place, use [`OsStr::make_ascii_lowercase`].
735 /// #![feature(osstring_ascii)]
736 /// use std::ffi::OsString;
737 /// let s = OsString::from("Grüße, Jürgen ❤");
739 /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
741 #[unstable(feature = "osstring_ascii", issue = "70516")]
742 pub fn to_ascii_lowercase(&self) -> OsString {
743 OsString::from_inner(self.inner.to_ascii_lowercase())
746 /// Returns a copy of this string where each character is mapped to its
747 /// ASCII upper case equivalent.
749 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
750 /// but non-ASCII letters are unchanged.
752 /// To uppercase the value in-place, use [`OsStr::make_ascii_uppercase`].
757 /// #![feature(osstring_ascii)]
758 /// use std::ffi::OsString;
759 /// let s = OsString::from("Grüße, Jürgen ❤");
761 /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
763 #[unstable(feature = "osstring_ascii", issue = "70516")]
764 pub fn to_ascii_uppercase(&self) -> OsString {
765 OsString::from_inner(self.inner.to_ascii_uppercase())
768 /// Checks if all characters in this string are within the ASCII range.
773 /// #![feature(osstring_ascii)]
774 /// use std::ffi::OsString;
776 /// let ascii = OsString::from("hello!\n");
777 /// let non_ascii = OsString::from("Grüße, Jürgen ❤");
779 /// assert!(ascii.is_ascii());
780 /// assert!(!non_ascii.is_ascii());
782 #[unstable(feature = "osstring_ascii", issue = "70516")]
783 pub fn is_ascii(&self) -> bool {
784 self.inner.is_ascii()
787 /// Checks that two strings are an ASCII case-insensitive match.
789 /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
790 /// but without allocating and copying temporaries.
795 /// #![feature(osstring_ascii)]
796 /// use std::ffi::OsString;
798 /// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
799 /// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
800 /// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
802 #[unstable(feature = "osstring_ascii", issue = "70516")]
803 pub fn eq_ignore_ascii_case<S: ?Sized + AsRef<OsStr>>(&self, other: &S) -> bool {
804 self.inner.eq_ignore_ascii_case(&other.as_ref().inner)
808 #[stable(feature = "box_from_os_str", since = "1.17.0")]
809 impl From<&OsStr> for Box<OsStr> {
810 fn from(s: &OsStr) -> Box<OsStr> {
811 let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
812 unsafe { Box::from_raw(rw) }
816 #[stable(feature = "box_from_cow", since = "1.45.0")]
817 impl From<Cow<'_, OsStr>> for Box<OsStr> {
819 fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
821 Cow::Borrowed(s) => Box::from(s),
822 Cow::Owned(s) => Box::from(s),
827 #[stable(feature = "os_string_from_box", since = "1.18.0")]
828 impl From<Box<OsStr>> for OsString {
829 /// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or
831 fn from(boxed: Box<OsStr>) -> OsString {
832 boxed.into_os_string()
836 #[stable(feature = "box_from_os_string", since = "1.20.0")]
837 impl From<OsString> for Box<OsStr> {
838 /// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
839 fn from(s: OsString) -> Box<OsStr> {
840 s.into_boxed_os_str()
844 #[stable(feature = "more_box_slice_clone", since = "1.29.0")]
845 impl Clone for Box<OsStr> {
847 fn clone(&self) -> Self {
848 self.to_os_string().into_boxed_os_str()
852 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
853 impl From<OsString> for Arc<OsStr> {
854 /// Converts a [`OsString`] into a [`Arc`]`<OsStr>` without copying or allocating.
856 fn from(s: OsString) -> Arc<OsStr> {
857 let arc = s.inner.into_arc();
858 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
862 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
863 impl From<&OsStr> for Arc<OsStr> {
865 fn from(s: &OsStr) -> Arc<OsStr> {
866 let arc = s.inner.into_arc();
867 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
871 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
872 impl From<OsString> for Rc<OsStr> {
873 /// Converts a [`OsString`] into a [`Rc`]`<OsStr>` without copying or allocating.
875 fn from(s: OsString) -> Rc<OsStr> {
876 let rc = s.inner.into_rc();
877 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
881 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
882 impl From<&OsStr> for Rc<OsStr> {
884 fn from(s: &OsStr) -> Rc<OsStr> {
885 let rc = s.inner.into_rc();
886 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
890 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
891 impl<'a> From<OsString> for Cow<'a, OsStr> {
893 fn from(s: OsString) -> Cow<'a, OsStr> {
898 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
899 impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
901 fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
906 #[stable(feature = "cow_from_osstr", since = "1.28.0")]
907 impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
909 fn from(s: &'a OsString) -> Cow<'a, OsStr> {
910 Cow::Borrowed(s.as_os_str())
914 #[stable(feature = "osstring_from_cow_osstr", since = "1.28.0")]
915 impl<'a> From<Cow<'a, OsStr>> for OsString {
917 fn from(s: Cow<'a, OsStr>) -> Self {
922 #[stable(feature = "box_default_extra", since = "1.17.0")]
923 impl Default for Box<OsStr> {
924 fn default() -> Box<OsStr> {
925 let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
926 unsafe { Box::from_raw(rw) }
930 #[stable(feature = "osstring_default", since = "1.9.0")]
931 impl Default for &OsStr {
932 /// Creates an empty `OsStr`.
934 fn default() -> Self {
939 #[stable(feature = "rust1", since = "1.0.0")]
940 impl PartialEq for OsStr {
942 fn eq(&self, other: &OsStr) -> bool {
943 self.bytes().eq(other.bytes())
947 #[stable(feature = "rust1", since = "1.0.0")]
948 impl PartialEq<str> for OsStr {
950 fn eq(&self, other: &str) -> bool {
951 *self == *OsStr::new(other)
955 #[stable(feature = "rust1", since = "1.0.0")]
956 impl PartialEq<OsStr> for str {
958 fn eq(&self, other: &OsStr) -> bool {
959 *other == *OsStr::new(self)
963 #[stable(feature = "rust1", since = "1.0.0")]
966 #[stable(feature = "rust1", since = "1.0.0")]
967 impl PartialOrd for OsStr {
969 fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
970 self.bytes().partial_cmp(other.bytes())
973 fn lt(&self, other: &OsStr) -> bool {
974 self.bytes().lt(other.bytes())
977 fn le(&self, other: &OsStr) -> bool {
978 self.bytes().le(other.bytes())
981 fn gt(&self, other: &OsStr) -> bool {
982 self.bytes().gt(other.bytes())
985 fn ge(&self, other: &OsStr) -> bool {
986 self.bytes().ge(other.bytes())
990 #[stable(feature = "rust1", since = "1.0.0")]
991 impl PartialOrd<str> for OsStr {
993 fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
994 self.partial_cmp(OsStr::new(other))
998 // FIXME (#19470): cannot provide PartialOrd<OsStr> for str until we
999 // have more flexible coherence rules.
1001 #[stable(feature = "rust1", since = "1.0.0")]
1002 impl Ord for OsStr {
1004 fn cmp(&self, other: &OsStr) -> cmp::Ordering {
1005 self.bytes().cmp(other.bytes())
1009 macro_rules! impl_cmp {
1010 ($lhs:ty, $rhs: ty) => {
1011 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1012 impl<'a, 'b> PartialEq<$rhs> for $lhs {
1014 fn eq(&self, other: &$rhs) -> bool {
1015 <OsStr as PartialEq>::eq(self, other)
1019 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1020 impl<'a, 'b> PartialEq<$lhs> for $rhs {
1022 fn eq(&self, other: &$lhs) -> bool {
1023 <OsStr as PartialEq>::eq(self, other)
1027 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1028 impl<'a, 'b> PartialOrd<$rhs> for $lhs {
1030 fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
1031 <OsStr as PartialOrd>::partial_cmp(self, other)
1035 #[stable(feature = "cmp_os_str", since = "1.8.0")]
1036 impl<'a, 'b> PartialOrd<$lhs> for $rhs {
1038 fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
1039 <OsStr as PartialOrd>::partial_cmp(self, other)
1045 impl_cmp!(OsString, OsStr);
1046 impl_cmp!(OsString, &'a OsStr);
1047 impl_cmp!(Cow<'a, OsStr>, OsStr);
1048 impl_cmp!(Cow<'a, OsStr>, &'b OsStr);
1049 impl_cmp!(Cow<'a, OsStr>, OsString);
1051 #[stable(feature = "rust1", since = "1.0.0")]
1052 impl Hash for OsStr {
1054 fn hash<H: Hasher>(&self, state: &mut H) {
1055 self.bytes().hash(state)
1059 #[stable(feature = "rust1", since = "1.0.0")]
1060 impl fmt::Debug for OsStr {
1061 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1062 fmt::Debug::fmt(&self.inner, formatter)
1067 pub(crate) fn display(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1068 fmt::Display::fmt(&self.inner, formatter)
1072 #[stable(feature = "rust1", since = "1.0.0")]
1073 impl Borrow<OsStr> for OsString {
1074 fn borrow(&self) -> &OsStr {
1079 #[stable(feature = "rust1", since = "1.0.0")]
1080 impl ToOwned for OsStr {
1081 type Owned = OsString;
1082 fn to_owned(&self) -> OsString {
1085 fn clone_into(&self, target: &mut OsString) {
1086 self.inner.clone_into(&mut target.inner)
1090 #[stable(feature = "rust1", since = "1.0.0")]
1091 impl AsRef<OsStr> for OsStr {
1092 fn as_ref(&self) -> &OsStr {
1097 #[stable(feature = "rust1", since = "1.0.0")]
1098 impl AsRef<OsStr> for OsString {
1100 fn as_ref(&self) -> &OsStr {
1105 #[stable(feature = "rust1", since = "1.0.0")]
1106 impl AsRef<OsStr> for str {
1108 fn as_ref(&self) -> &OsStr {
1109 OsStr::from_inner(Slice::from_str(self))
1113 #[stable(feature = "rust1", since = "1.0.0")]
1114 impl AsRef<OsStr> for String {
1116 fn as_ref(&self) -> &OsStr {
1121 impl FromInner<Buf> for OsString {
1122 fn from_inner(buf: Buf) -> OsString {
1123 OsString { inner: buf }
1127 impl IntoInner<Buf> for OsString {
1128 fn into_inner(self) -> Buf {
1133 impl AsInner<Slice> for OsStr {
1135 fn as_inner(&self) -> &Slice {
1140 #[stable(feature = "osstring_from_str", since = "1.45.0")]
1141 impl FromStr for OsString {
1142 type Err = core::convert::Infallible;
1144 fn from_str(s: &str) -> Result<Self, Self::Err> {
1145 Ok(OsString::from(s))
1152 use crate::sys_common::{AsInner, IntoInner};
1155 use crate::sync::Arc;
1158 fn test_os_string_with_capacity() {
1159 let os_string = OsString::with_capacity(0);
1160 assert_eq!(0, os_string.inner.into_inner().capacity());
1162 let os_string = OsString::with_capacity(10);
1163 assert_eq!(10, os_string.inner.into_inner().capacity());
1165 let mut os_string = OsString::with_capacity(0);
1166 os_string.push("abc");
1167 assert!(os_string.inner.into_inner().capacity() >= 3);
1171 fn test_os_string_clear() {
1172 let mut os_string = OsString::from("abc");
1173 assert_eq!(3, os_string.inner.as_inner().len());
1176 assert_eq!(&os_string, "");
1177 assert_eq!(0, os_string.inner.as_inner().len());
1181 fn test_os_string_capacity() {
1182 let os_string = OsString::with_capacity(0);
1183 assert_eq!(0, os_string.capacity());
1185 let os_string = OsString::with_capacity(10);
1186 assert_eq!(10, os_string.capacity());
1188 let mut os_string = OsString::with_capacity(0);
1189 os_string.push("abc");
1190 assert!(os_string.capacity() >= 3);
1194 fn test_os_string_reserve() {
1195 let mut os_string = OsString::new();
1196 assert_eq!(os_string.capacity(), 0);
1198 os_string.reserve(2);
1199 assert!(os_string.capacity() >= 2);
1202 os_string.push("a");
1205 assert!(os_string.capacity() >= 16);
1206 os_string.reserve(16);
1207 assert!(os_string.capacity() >= 32);
1209 os_string.push("a");
1211 os_string.reserve(16);
1212 assert!(os_string.capacity() >= 33)
1216 fn test_os_string_reserve_exact() {
1217 let mut os_string = OsString::new();
1218 assert_eq!(os_string.capacity(), 0);
1220 os_string.reserve_exact(2);
1221 assert!(os_string.capacity() >= 2);
1224 os_string.push("a");
1227 assert!(os_string.capacity() >= 16);
1228 os_string.reserve_exact(16);
1229 assert!(os_string.capacity() >= 32);
1231 os_string.push("a");
1233 os_string.reserve_exact(16);
1234 assert!(os_string.capacity() >= 33)
1238 fn test_os_string_default() {
1239 let os_string: OsString = Default::default();
1240 assert_eq!("", &os_string);
1244 fn test_os_str_is_empty() {
1245 let mut os_string = OsString::new();
1246 assert!(os_string.is_empty());
1248 os_string.push("abc");
1249 assert!(!os_string.is_empty());
1252 assert!(os_string.is_empty());
1256 fn test_os_str_len() {
1257 let mut os_string = OsString::new();
1258 assert_eq!(0, os_string.len());
1260 os_string.push("abc");
1261 assert_eq!(3, os_string.len());
1264 assert_eq!(0, os_string.len());
1268 fn test_os_str_default() {
1269 let os_str: &OsStr = Default::default();
1270 assert_eq!("", os_str);
1275 let orig = "Hello, world!";
1276 let os_str = OsStr::new(orig);
1277 let boxed: Box<OsStr> = Box::from(os_str);
1278 let os_string = os_str.to_owned().into_boxed_os_str().into_os_string();
1279 assert_eq!(os_str, &*boxed);
1280 assert_eq!(&*boxed, &*os_string);
1281 assert_eq!(&*os_string, os_str);
1285 fn boxed_default() {
1286 let boxed = <Box<OsStr>>::default();
1287 assert!(boxed.is_empty());
1291 fn test_os_str_clone_into() {
1292 let mut os_string = OsString::with_capacity(123);
1293 os_string.push("hello");
1294 let os_str = OsStr::new("bonjour");
1295 os_str.clone_into(&mut os_string);
1296 assert_eq!(os_str, os_string);
1297 assert!(os_string.capacity() >= 123);
1302 let orig = "Hello, world!";
1303 let os_str = OsStr::new(orig);
1304 let rc: Rc<OsStr> = Rc::from(os_str);
1305 let arc: Arc<OsStr> = Arc::from(os_str);
1307 assert_eq!(&*rc, os_str);
1308 assert_eq!(&*arc, os_str);
1310 let rc2: Rc<OsStr> = Rc::from(os_str.to_owned());
1311 let arc2: Arc<OsStr> = Arc::from(os_str.to_owned());
1313 assert_eq!(&*rc2, os_str);
1314 assert_eq!(&*arc2, os_str);