#[cfg(feature = "alloc")]
use crate::inner::inner_alloc::Buf;
use crate::inner::Slice;
#[allow(unused_imports)]
use crate::sys_common::{AsInner, FromInner, IntoInner};
#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
#[cfg(feature = "alloc")]
use alloc::borrow::ToOwned;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::rc::Rc;
#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::sync::Arc;
#[cfg(feature = "alloc")]
use core::borrow::Borrow;
use core::hash::{Hash, Hasher};
use core::str;
#[allow(unused_imports)]
use core::{cmp, fmt, ops};
#[cfg(feature = "alloc")]
#[derive(Clone)]
pub struct OsString {
inner: Buf,
}
pub struct OsStr {
inner: Slice,
}
#[cfg(feature = "alloc")]
impl OsString {
pub fn new() -> OsString {
OsString {
inner: Buf::from_string(String::new()),
}
}
pub fn as_os_str(&self) -> &OsStr {
self
}
pub fn into_string(self) -> Result<String, OsString> {
self.inner
.into_string()
.map_err(|buf| OsString { inner: buf })
}
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.inner.push_slice(&s.as_ref().inner)
}
pub fn with_capacity(capacity: usize) -> OsString {
OsString {
inner: Buf::with_capacity(capacity),
}
}
pub fn clear(&mut self) {
self.inner.clear()
}
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}
pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to_fit()
}
pub fn into_boxed_os_str(self) -> Box<OsStr> {
let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
#[cfg(feature = "alloc")]
impl From<String> for OsString {
fn from(s: String) -> OsString {
OsString {
inner: Buf::from_string(s),
}
}
}
#[cfg(feature = "alloc")]
impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
fn from(s: &T) -> OsString {
s.as_ref().to_os_string()
}
}
#[cfg(feature = "alloc")]
impl ops::Index<ops::RangeFull> for OsString {
type Output = OsStr;
#[inline]
fn index(&self, _index: ops::RangeFull) -> &OsStr {
OsStr::from_inner(self.inner.as_slice())
}
}
#[cfg(feature = "alloc")]
impl ops::Deref for OsString {
type Target = OsStr;
#[inline]
fn deref(&self) -> &OsStr {
&self[..]
}
}
#[cfg(feature = "alloc")]
impl Default for OsString {
#[inline]
fn default() -> OsString {
OsString::new()
}
}
#[cfg(feature = "alloc")]
impl fmt::Debug for OsString {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, formatter)
}
}
#[cfg(feature = "alloc")]
impl PartialEq for OsString {
fn eq(&self, other: &OsString) -> bool {
self == other
}
}
#[cfg(feature = "alloc")]
impl PartialEq<str> for OsString {
fn eq(&self, other: &str) -> bool {
&**self == other
}
}
#[cfg(feature = "alloc")]
impl PartialEq<OsString> for str {
fn eq(&self, other: &OsString) -> bool {
&**other == self
}
}
#[cfg(feature = "alloc")]
impl PartialEq<&str> for OsString {
fn eq(&self, other: &&str) -> bool {
**self == **other
}
}
#[cfg(feature = "alloc")]
impl<'a> PartialEq<OsString> for &'a str {
fn eq(&self, other: &OsString) -> bool {
**other == **self
}
}
#[cfg(feature = "alloc")]
impl Eq for OsString {}
#[cfg(feature = "alloc")]
impl PartialOrd for OsString {
#[inline]
fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
(&**self).partial_cmp(&**other)
}
#[inline]
fn lt(&self, other: &OsString) -> bool {
self < other
}
#[inline]
fn le(&self, other: &OsString) -> bool {
self <= other
}
#[inline]
fn gt(&self, other: &OsString) -> bool {
self > other
}
#[inline]
fn ge(&self, other: &OsString) -> bool {
self >= other
}
}
#[cfg(feature = "alloc")]
impl PartialOrd<str> for OsString {
#[inline]
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
(&**self).partial_cmp(other)
}
}
#[cfg(feature = "alloc")]
impl Ord for OsString {
#[inline]
fn cmp(&self, other: &OsString) -> cmp::Ordering {
(&**self).cmp(&**other)
}
}
#[cfg(feature = "alloc")]
impl Hash for OsString {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(&**self).hash(state)
}
}
impl OsStr {
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &OsStr {
s.as_ref()
}
fn from_inner(inner: &Slice) -> &OsStr {
unsafe { &*(inner as *const Slice as *const OsStr) }
}
pub fn to_str(&self) -> Option<&str> {
self.inner.to_str()
}
#[cfg(feature = "alloc")]
pub fn to_string_lossy(&self) -> Cow<'_, str> {
self.inner.to_string_lossy()
}
#[cfg(feature = "alloc")]
pub fn to_os_string(&self) -> OsString {
OsString {
inner: self.inner.to_owned(),
}
}
pub fn is_empty(&self) -> bool {
self.inner.inner.is_empty()
}
pub fn len(&self) -> usize {
self.inner.inner.len()
}
#[cfg(feature = "alloc")]
pub fn into_os_string(self: Box<OsStr>) -> OsString {
let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
OsString {
inner: Buf::from_box(boxed),
}
}
fn bytes(&self) -> &[u8] {
unsafe { &*(&self.inner as *const _ as *const [u8]) }
}
pub fn display(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.inner, formatter)
}
}
#[cfg(feature = "alloc")]
impl From<&OsStr> for Box<OsStr> {
fn from(s: &OsStr) -> Box<OsStr> {
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
#[cfg(feature = "alloc")]
impl From<Box<OsStr>> for OsString {
fn from(boxed: Box<OsStr>) -> OsString {
boxed.into_os_string()
}
}
#[cfg(feature = "alloc")]
impl From<OsString> for Box<OsStr> {
fn from(s: OsString) -> Box<OsStr> {
s.into_boxed_os_str()
}
}
#[cfg(feature = "alloc")]
impl Clone for Box<OsStr> {
#[inline]
fn clone(&self) -> Self {
self.to_os_string().into_boxed_os_str()
}
}
#[cfg(feature = "alloc")]
impl From<OsString> for Arc<OsStr> {
#[inline]
fn from(s: OsString) -> Arc<OsStr> {
let arc = s.inner.into_arc();
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
}
}
#[cfg(feature = "alloc")]
impl From<&OsStr> for Arc<OsStr> {
#[inline]
fn from(s: &OsStr) -> Arc<OsStr> {
let arc = s.inner.into_arc();
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const OsStr) }
}
}
#[cfg(feature = "alloc")]
impl From<OsString> for Rc<OsStr> {
#[inline]
fn from(s: OsString) -> Rc<OsStr> {
let rc = s.inner.into_rc();
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
}
}
#[cfg(feature = "alloc")]
impl From<&OsStr> for Rc<OsStr> {
#[inline]
fn from(s: &OsStr) -> Rc<OsStr> {
let rc = s.inner.into_rc();
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const OsStr) }
}
}
#[cfg(feature = "alloc")]
impl<'a> From<OsString> for Cow<'a, OsStr> {
#[inline]
fn from(s: OsString) -> Cow<'a, OsStr> {
Cow::Owned(s)
}
}
#[cfg(feature = "alloc")]
impl<'a> From<&'a OsStr> for Cow<'a, OsStr> {
#[inline]
fn from(s: &'a OsStr) -> Cow<'a, OsStr> {
Cow::Borrowed(s)
}
}
#[cfg(feature = "alloc")]
impl<'a> From<&'a OsString> for Cow<'a, OsStr> {
#[inline]
fn from(s: &'a OsString) -> Cow<'a, OsStr> {
Cow::Borrowed(s.as_os_str())
}
}
#[cfg(feature = "alloc")]
impl<'a> From<Cow<'a, OsStr>> for OsString {
#[inline]
fn from(s: Cow<'a, OsStr>) -> Self {
s.into_owned()
}
}
#[cfg(feature = "alloc")]
impl Default for Box<OsStr> {
fn default() -> Box<OsStr> {
let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
impl Default for &OsStr {
#[inline]
fn default() -> Self {
OsStr::new("")
}
}
impl PartialEq for OsStr {
fn eq(&self, other: &OsStr) -> bool {
self.bytes().eq(other.bytes())
}
}
impl PartialEq<str> for OsStr {
fn eq(&self, other: &str) -> bool {
*self == *OsStr::new(other)
}
}
impl PartialEq<OsStr> for str {
fn eq(&self, other: &OsStr) -> bool {
*other == *OsStr::new(self)
}
}
impl Eq for OsStr {}
impl PartialOrd for OsStr {
#[inline]
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
self.bytes().partial_cmp(other.bytes())
}
#[inline]
fn lt(&self, other: &OsStr) -> bool {
self.bytes().lt(other.bytes())
}
#[inline]
fn le(&self, other: &OsStr) -> bool {
self.bytes().le(other.bytes())
}
#[inline]
fn gt(&self, other: &OsStr) -> bool {
self.bytes().gt(other.bytes())
}
#[inline]
fn ge(&self, other: &OsStr) -> bool {
self.bytes().ge(other.bytes())
}
}
impl PartialOrd<str> for OsStr {
#[inline]
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
self.partial_cmp(OsStr::new(other))
}
}
impl Ord for OsStr {
#[inline]
fn cmp(&self, other: &OsStr) -> cmp::Ordering {
self.bytes().cmp(other.bytes())
}
}
#[cfg(feature = "alloc")]
macro_rules! impl_cmp {
($lhs:ty, $rhs: ty) => {
impl<'a, 'b> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool {
<OsStr as PartialEq>::eq(self, other)
}
}
impl<'a, 'b> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool {
<OsStr as PartialEq>::eq(self, other)
}
}
impl<'a, 'b> PartialOrd<$rhs> for $lhs {
#[inline]
fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
<OsStr as PartialOrd>::partial_cmp(self, other)
}
}
impl<'a, 'b> PartialOrd<$lhs> for $rhs {
#[inline]
fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
<OsStr as PartialOrd>::partial_cmp(self, other)
}
}
};
}
#[cfg(feature = "alloc")]
impl_cmp!(OsString, OsStr);
#[cfg(feature = "alloc")]
impl_cmp!(OsString, &'a OsStr);
#[cfg(feature = "alloc")]
impl_cmp!(Cow<'a, OsStr>, OsStr);
#[cfg(feature = "alloc")]
impl_cmp!(Cow<'a, OsStr>, &'b OsStr);
#[cfg(feature = "alloc")]
impl_cmp!(Cow<'a, OsStr>, OsString);
impl Hash for OsStr {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.bytes().hash(state)
}
}
impl fmt::Debug for OsStr {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.inner, formatter)
}
}
#[cfg(feature = "alloc")]
impl Borrow<OsStr> for OsString {
fn borrow(&self) -> &OsStr {
&self[..]
}
}
#[cfg(feature = "alloc")]
impl ToOwned for OsStr {
type Owned = OsString;
fn to_owned(&self) -> OsString {
self.to_os_string()
}
}
impl AsRef<OsStr> for OsStr {
fn as_ref(&self) -> &OsStr {
self
}
}
#[cfg(feature = "alloc")]
impl AsRef<OsStr> for OsString {
fn as_ref(&self) -> &OsStr {
self
}
}
impl AsRef<OsStr> for str {
fn as_ref(&self) -> &OsStr {
OsStr::from_inner(Slice::from_str(self))
}
}
#[cfg(feature = "alloc")]
impl AsRef<OsStr> for String {
fn as_ref(&self) -> &OsStr {
(&**self).as_ref()
}
}
#[cfg(feature = "alloc")]
impl FromInner<Buf> for OsString {
fn from_inner(buf: Buf) -> OsString {
OsString { inner: buf }
}
}
#[cfg(feature = "alloc")]
impl IntoInner<Buf> for OsString {
fn into_inner(self) -> Buf {
self.inner
}
}
impl AsInner<Slice> for OsStr {
#[inline]
fn as_inner(&self) -> &Slice {
&self.inner
}
}
#[cfg(feature = "alloc")]
#[cfg(test)]
mod tests {
use super::*;
use crate::sys_common::{AsInner, IntoInner};
use alloc::rc::Rc;
use alloc::sync::Arc;
#[test]
fn test_os_string_with_capacity() {
let os_string = OsString::with_capacity(0);
assert_eq!(0, os_string.inner.into_inner().capacity());
let os_string = OsString::with_capacity(10);
assert_eq!(10, os_string.inner.into_inner().capacity());
let mut os_string = OsString::with_capacity(0);
os_string.push("abc");
assert!(os_string.inner.into_inner().capacity() >= 3);
}
#[test]
fn test_os_string_clear() {
let mut os_string = OsString::from("abc");
assert_eq!(3, os_string.inner.as_inner().len());
os_string.clear();
assert_eq!(&os_string, "");
assert_eq!(0, os_string.inner.as_inner().len());
}
#[test]
fn test_os_string_capacity() {
let os_string = OsString::with_capacity(0);
assert_eq!(0, os_string.capacity());
let os_string = OsString::with_capacity(10);
assert_eq!(10, os_string.capacity());
let mut os_string = OsString::with_capacity(0);
os_string.push("abc");
assert!(os_string.capacity() >= 3);
}
#[test]
fn test_os_string_reserve() {
let mut os_string = OsString::new();
assert_eq!(os_string.capacity(), 0);
os_string.reserve(2);
assert!(os_string.capacity() >= 2);
for _ in 0..16 {
os_string.push("a");
}
assert!(os_string.capacity() >= 16);
os_string.reserve(16);
assert!(os_string.capacity() >= 32);
os_string.push("a");
os_string.reserve(16);
assert!(os_string.capacity() >= 33)
}
#[test]
fn test_os_string_reserve_exact() {
let mut os_string = OsString::new();
assert_eq!(os_string.capacity(), 0);
os_string.reserve_exact(2);
assert!(os_string.capacity() >= 2);
for _ in 0..16 {
os_string.push("a");
}
assert!(os_string.capacity() >= 16);
os_string.reserve_exact(16);
assert!(os_string.capacity() >= 32);
os_string.push("a");
os_string.reserve_exact(16);
assert!(os_string.capacity() >= 33)
}
#[test]
fn test_os_string_default() {
let os_string: OsString = Default::default();
assert_eq!("", &os_string);
}
#[test]
fn test_os_str_is_empty() {
let mut os_string = OsString::new();
assert!(os_string.is_empty());
os_string.push("abc");
assert!(!os_string.is_empty());
os_string.clear();
assert!(os_string.is_empty());
}
#[test]
fn test_os_str_len() {
let mut os_string = OsString::new();
assert_eq!(0, os_string.len());
os_string.push("abc");
assert_eq!(3, os_string.len());
os_string.clear();
assert_eq!(0, os_string.len());
}
#[test]
fn test_os_str_default() {
let os_str: &OsStr = Default::default();
assert_eq!("", os_str);
}
#[test]
fn into_boxed() {
let orig = "Hello, world!";
let os_str = OsStr::new(orig);
let boxed: Box<OsStr> = Box::from(os_str);
let os_string = os_str.to_owned().into_boxed_os_str().into_os_string();
assert_eq!(os_str, &*boxed);
assert_eq!(&*boxed, &*os_string);
assert_eq!(&*os_string, os_str);
}
#[test]
fn boxed_default() {
let boxed = <Box<OsStr>>::default();
assert!(boxed.is_empty());
}
#[test]
fn into_rc() {
let orig = "Hello, world!";
let os_str = OsStr::new(orig);
let rc: Rc<OsStr> = Rc::from(os_str);
let arc: Arc<OsStr> = Arc::from(os_str);
assert_eq!(&*rc, os_str);
assert_eq!(&*arc, os_str);
let rc2: Rc<OsStr> = Rc::from(os_str.to_owned());
let arc2: Arc<OsStr> = Arc::from(os_str.to_owned());
assert_eq!(&*rc2, os_str);
assert_eq!(&*arc2, os_str);
}
}