1use alloc::{borrow::Cow, ffi::CString, sync::Arc};
2use core::{convert::Infallible, ffi::CStr, str::FromStr};
3
4#[cfg(feature = "std")]
5use crate::Allocator;
6use crate::{Str, XString};
7
8impl FromStr for XString {
9 type Err = Infallible;
10 #[inline(always)]
11 fn from_str(s: &str) -> Result<Self, Self::Err> {
12 Ok(Self::new(s))
13 }
14}
15
16impl<S: Str + ?Sized> From<&S> for XString<S> {
17 #[inline(always)]
18 fn from(value: &S) -> Self {
19 Self::new(value)
20 }
21}
22
23impl<S: Str + ?Sized> From<&mut S> for XString<S> {
24 #[inline(always)]
25 fn from(value: &mut S) -> Self {
26 Self::new(value)
27 }
28}
29
30macro_rules! define_ref {
31 ($s:ty, ($($t:ty),*$(,)?)) => {$(
32 impl From<$t> for XString<$s> {
33 #[inline(always)]
34 fn from(value: $t) -> Self {
35 Self::new(&value)
36 }
37 }
38 )*};
39}
40
41define_ref!(
42 str,
43 (
44 alloc::string::String,
45 alloc::boxed::Box<str>,
46 Arc<str>,
47 Cow<'_, str>
48 )
49);
50
51define_ref!(
52 CStr,
53 (CString, alloc::boxed::Box<CStr>, Arc<CStr>, Cow<'_, CStr>)
54);
55
56#[cfg(feature = "std")]
57define_ref!(
58 std::ffi::OsStr,
59 (
60 std::ffi::OsString,
61 alloc::boxed::Box<std::ffi::OsStr>,
62 Arc<std::ffi::OsStr>,
63 Cow<'_, std::ffi::OsStr>
64 )
65);
66
67#[cfg(feature = "std")]
68define_ref!(
69 std::path::Path,
70 (
71 std::path::PathBuf,
72 alloc::boxed::Box<std::path::Path>,
73 Arc<std::path::Path>,
74 Cow<'_, std::path::Path>
75 )
76);
77
78#[cfg(feature = "std")]
79impl<S: Str + ?Sized + std::net::ToSocketAddrs, A: Allocator> std::net::ToSocketAddrs
80 for XString<S, A>
81{
82 type Iter = <S as std::net::ToSocketAddrs>::Iter;
83
84 #[inline(always)]
85 fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
86 <S as std::net::ToSocketAddrs>::to_socket_addrs(self)
87 }
88}