inline_flexstr/
bytes.rs

1use alloc::borrow::Cow;
2#[cfg(not(feature = "std"))]
3use alloc::vec::Vec;
4use core::str::FromStr;
5
6use crate::inline::{InlineFlexStr, TooLongForInlining, inline_partial_eq_impl};
7
8use flexstr_support::StringToFromBytes;
9
10/// Inline `[u8]` type
11pub type InlineBytes = InlineFlexStr<[u8]>;
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 [u8]> for InlineFlexStr<[u8]> {
17    type Error = TooLongForInlining;
18
19    #[inline]
20    fn try_from(s: &'s [u8]) -> Result<Self, Self::Error> {
21        InlineFlexStr::try_from_type(s)
22    }
23}
24
25impl<'s> TryFrom<&'s str> for InlineFlexStr<[u8]> {
26    type Error = TooLongForInlining;
27
28    #[inline]
29    fn try_from(s: &'s str) -> Result<Self, Self::Error> {
30        InlineFlexStr::try_from_type(s.as_bytes())
31    }
32}
33
34// *** PartialEq ***
35
36inline_partial_eq_impl!([u8], [u8]);
37inline_partial_eq_impl!(&[u8], [u8]);
38inline_partial_eq_impl!(Vec<u8>, [u8]);
39inline_partial_eq_impl!(Cow<'_, [u8]>, [u8]);
40
41// *** AsRef ***
42
43impl<S: ?Sized + StringToFromBytes> AsRef<[u8]> for InlineFlexStr<S>
44where
45    S: AsRef<[u8]>,
46{
47    fn as_ref(&self) -> &[u8] {
48        self.as_ref_type().as_ref()
49    }
50}
51
52// *** FromStr ***
53
54impl FromStr for InlineFlexStr<[u8]> {
55    type Err = TooLongForInlining;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        InlineFlexStr::try_from_type(s.as_bytes())
59    }
60}