#[cfg(feature = "cstr")]
use alloc::ffi::CString;
#[cfg(all(not(feature = "std"), feature = "bytes"))]
use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
use alloc::{borrow::ToOwned, boxed::Box, string::String};
#[cfg(feature = "cstr")]
use core::ffi::CStr;
#[cfg(all(feature = "std", feature = "osstr"))]
use std::ffi::{OsStr, OsString};
#[cfg(all(feature = "std", feature = "path"))]
use std::path::{Path, PathBuf};
pub trait StringToFromBytes: ToOwned + 'static {
fn bytes_as_self(bytes: &[u8]) -> &Self;
#[inline]
fn self_as_bytes(&self) -> &[u8] {
self.self_as_raw_bytes()
}
fn self_as_raw_bytes(&self) -> &[u8];
fn empty_raw_bytes() -> &'static [u8] {
&[]
}
}
pub trait StringFromBytesMut: StringToFromBytes {
fn bytes_as_self_mut(bytes: &mut [u8]) -> &mut Self;
}
pub trait StringLike<S: ?Sized + StringToFromBytes>
where
Self: Sized,
{
fn as_ref_type(&self) -> &S;
fn as_bytes(&self) -> &[u8];
fn into_owned_type(self) -> S::Owned
where
S::Owned: From<Box<S>>;
fn to_owned_type(&self) -> S::Owned;
fn is_empty(&self) -> bool {
self.as_bytes().is_empty()
}
fn len(&self) -> usize {
self.as_bytes().len()
}
fn as_str(&self) -> &str
where
S: AsRef<str>,
{
self.as_ref_type().as_ref()
}
#[cfg(all(feature = "std", feature = "osstr"))]
fn as_os_str(&self) -> &OsStr
where
S: AsRef<OsStr>,
{
self.as_ref_type().as_ref()
}
#[cfg(all(feature = "std", feature = "path"))]
fn as_path(&self) -> &Path
where
S: AsRef<Path>,
{
self.as_ref_type().as_ref()
}
#[cfg(feature = "cstr")]
fn as_c_str(&self) -> &CStr
where
S: AsRef<CStr>,
{
self.as_ref_type().as_ref()
}
fn into_string(self) -> String
where
S::Owned: Into<String> + From<Box<S>>,
{
self.into_owned_type().into()
}
#[cfg(all(feature = "std", feature = "osstr"))]
fn into_os_string(self) -> OsString
where
S::Owned: Into<OsString> + From<Box<S>>,
{
self.into_owned_type().into()
}
#[cfg(all(feature = "std", feature = "path"))]
fn into_path_buf(self) -> PathBuf
where
S::Owned: Into<PathBuf> + From<Box<S>>,
{
self.into_owned_type().into()
}
#[cfg(feature = "cstr")]
fn into_c_string(self) -> CString
where
S::Owned: Into<CString> + From<Box<S>>,
{
self.into_owned_type().into()
}
#[cfg(feature = "bytes")]
fn into_vec_bytes(self) -> Vec<u8>
where
S::Owned: Into<Vec<u8>> + From<Box<S>>,
{
self.into_owned_type().into()
}
fn to_string(&self) -> String
where
S::Owned: Into<String>,
{
self.to_owned_type().into()
}
#[cfg(all(feature = "std", feature = "osstr"))]
fn to_os_string(&self) -> OsString
where
S::Owned: Into<OsString>,
{
self.to_owned_type().into()
}
#[cfg(all(feature = "std", feature = "path"))]
fn to_path_buf(&self) -> PathBuf
where
S::Owned: Into<PathBuf>,
{
self.to_owned_type().into()
}
#[cfg(feature = "cstr")]
fn to_c_string(&self) -> CString
where
S::Owned: Into<CString>,
{
self.to_owned_type().into()
}
#[cfg(feature = "bytes")]
fn to_vec_bytes(&self) -> Vec<u8>
where
S::Owned: Into<Vec<u8>>,
{
self.to_owned_type().into()
}
}