use std::borrow::Cow;
use std::ffi::CStr;
use std::ffi::CString;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::io::IoSlice;
use std::io::IoSliceMut;
use std::ops::Deref;
use std::path::Path;
use std::path::PathBuf;
#[derive(Debug)]
pub(super) enum BytesInner<'a> {
Bytes(&'a [u8]),
OsStr(&'a OsStr),
}
#[derive(Debug)]
pub struct Bytes<'a>(pub(super) BytesInner<'a>);
pub trait ToBytes {
#[must_use]
fn to_bytes(&self) -> Bytes<'_>;
}
impl ToBytes for [u8] {
#[inline]
fn to_bytes(&self) -> Bytes<'_> {
Bytes(BytesInner::Bytes(self))
}
}
#[cfg_attr(print_bytes_docs_rs, doc(cfg(feature = "min_const_generics")))]
#[cfg(any(feature = "const_generics", feature = "min_const_generics"))]
impl<const N: usize> ToBytes for [u8; N] {
#[inline]
fn to_bytes(&self) -> Bytes<'_> {
self[..].to_bytes()
}
}
impl<T> ToBytes for Cow<'_, T>
where
T: ?Sized + ToBytes + ToOwned,
T::Owned: ToBytes,
{
#[inline]
fn to_bytes(&self) -> Bytes<'_> {
(**self).to_bytes()
}
}
impl ToBytes for OsStr {
#[inline]
fn to_bytes(&self) -> Bytes<'_> {
Bytes(BytesInner::OsStr(self))
}
}
macro_rules! r#impl {
( $type:ty , $convert_method:ident ) => {
impl ToBytes for $type {
#[inline]
fn to_bytes(&self) -> Bytes<'_> {
ToBytes::to_bytes(self.$convert_method())
}
}
};
}
r#impl!(CStr, to_bytes);
r#impl!(CString, as_c_str);
r#impl!(IoSlice<'_>, deref);
r#impl!(IoSliceMut<'_>, deref);
r#impl!(OsString, as_os_str);
r#impl!(Path, as_os_str);
r#impl!(PathBuf, as_path);
r#impl!(Vec::<u8>, as_slice);