use std::{borrow::Cow, ops::Deref, rc::Rc, sync::Arc};
use super::{StringExtT, StringT};
use crate::{impl_for_shared_ref, impl_for_wrapper, wrapper};
macro_rules! impl_for_string {
($($ty:ty),*) => {
$(
impl_for_string!(@INTERNAL $ty);
impl_for_string!(@INTERNAL &$ty);
impl_for_string!(@INTERNAL &mut $ty);
impl_for_string!(@INTERNAL &&$ty);
impl_for_string!(@INTERNAL &&mut $ty);
impl_for_string!(@INTERNAL &mut &$ty);
impl_for_string!(@INTERNAL &mut &mut $ty);
impl_for_string!(@INTERNAL &&&$ty);
)*
};
(@INTERNAL $ty:ty) => {
impl StringT for $ty {
#[inline]
fn encode_to_buf(self, string: &mut Vec<u8>) {
string.extend(self.as_bytes());
}
#[inline]
fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
string.extend(self.as_bytes());
string.extend(separator.as_bytes());
}
#[inline]
fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
string.extend(self.as_bytes());
}
#[inline]
fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
string.extend(self.as_bytes());
string.extend(separator.as_bytes());
}
}
impl StringExtT for $ty {}
}
}
impl_for_string! {
&str,
Arc<str>,
Rc<str>,
Box<str>,
Cow<'_, str>,
String
}
impl StringT for char {
#[inline]
fn encode_to_buf(self, string: &mut Vec<u8>) {
match self.len_utf8() {
1 => string.push(self as u8),
_ => string.extend(self.encode_utf8(&mut [0; 4]).as_bytes()),
}
}
#[inline]
fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
self.encode_to_buf(string);
string.extend(separator.as_bytes());
}
#[inline]
fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
match self.len_utf8() {
1 => string.extend([self as u8]),
_ => string.extend(self.encode_utf8(&mut [0; 4]).as_bytes()),
}
}
fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
self.encode_to_bytes_buf(string);
string.extend(separator.as_bytes());
}
}
impl StringExtT for char {}
impl_for_shared_ref!(COPIED: char);
#[macro_export]
macro_rules! str_wrapper {
(str = $data:expr) => {
$crate::string::general::string::StrWrapper { inner: $data }
};
(string = $data:expr) => {
$crate::string::general::string::StringWrapper { inner: $data }
};
}
wrapper! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub StrWrapper<T>(pub T)
}
wrapper! {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub StringWrapper<T>(pub T)
}
impl_for_wrapper!(STRING_T: impl<T> StringT for StrWrapper<T> where T: Deref<Target = str>);
impl_for_wrapper!(STRING_T: impl<T> StringT for StringWrapper<T> where T: Deref<Target = String>);
impl_for_wrapper!(STRING_EXT_T: impl<T> StringExtT for StrWrapper<T> where T: Deref<Target = str>);
impl_for_wrapper!(STRING_EXT_T: impl<T> StringExtT for StringWrapper<T> where T: Deref<Target = String>);