Skip to main content

inline_flexstr/
osstr.rs

1use alloc::borrow::Cow;
2use core::str::FromStr;
3use std::ffi::{OsStr, OsString};
4use std::path::Path;
5
6use crate::inline::{InlineFlexStr, TooLongForInlining, inline_partial_eq_impl};
7
8use flexstr_support::StringToFromBytes;
9
10/// Inline `OsStr` type
11pub type InlineOsStr = InlineFlexStr<OsStr>;
12
13// *** TryFrom for InlineFlexStr ***
14
15// NOTE: Cannot be implemented generically because of impl<T, U> TryFrom<U> for T where U: Into<T>
16impl<'s> TryFrom<&'s OsStr> for InlineFlexStr<OsStr> {
17    type Error = TooLongForInlining;
18
19    #[inline]
20    fn try_from(s: &'s OsStr) -> Result<Self, Self::Error> {
21        InlineFlexStr::try_from_type(s)
22    }
23}
24
25impl<'s> TryFrom<&'s str> for InlineFlexStr<OsStr> {
26    type Error = TooLongForInlining;
27
28    #[inline]
29    fn try_from(s: &'s str) -> Result<Self, Self::Error> {
30        InlineFlexStr::try_from_type(OsStr::new(s))
31    }
32}
33
34impl<'s> TryFrom<&'s Path> for InlineFlexStr<OsStr> {
35    type Error = TooLongForInlining;
36
37    #[inline]
38    fn try_from(p: &'s Path) -> Result<Self, Self::Error> {
39        InlineFlexStr::try_from_type(p.as_os_str())
40    }
41}
42
43// *** PartialEq ***
44
45inline_partial_eq_impl!(OsStr, OsStr);
46inline_partial_eq_impl!(&OsStr, OsStr);
47inline_partial_eq_impl!(OsString, OsStr);
48inline_partial_eq_impl!(Cow<'_, OsStr>, OsStr);
49
50// *** AsRef ***
51
52impl<S: ?Sized + StringToFromBytes> AsRef<OsStr> for InlineFlexStr<S>
53where
54    S: AsRef<OsStr>,
55{
56    fn as_ref(&self) -> &OsStr {
57        self.as_ref_type().as_ref()
58    }
59}
60
61// *** FromStr ***
62
63impl FromStr for InlineFlexStr<OsStr> {
64    type Err = TooLongForInlining;
65
66    fn from_str(s: &str) -> Result<Self, Self::Err> {
67        InlineFlexStr::try_from_type(OsStr::new(s))
68    }
69}