macro_rules! impl_utf16_str_common {
($Ty:ident) => {
impl<A: allocator_api2::alloc::Allocator + Clone> $Ty<A> {
#[inline]
fn u16_len(&self) -> usize {
unsafe { $crate::arena::alloc_prefixed::read_prefix_len(self.ptr) }
}
#[must_use]
#[inline]
pub fn as_utf16_str(&self) -> &widestring::Utf16Str {
let len = self.u16_len();
unsafe {
let units = core::slice::from_raw_parts(self.ptr.as_ptr(), len);
widestring::Utf16Str::from_slice_unchecked(units)
}
}
#[must_use]
#[inline]
pub fn len(&self) -> usize {
self.u16_len()
}
#[must_use]
#[inline]
pub fn is_empty(&self) -> bool {
self.u16_len() == 0
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> Drop for $Ty<A> {
#[inline]
fn drop(&mut self) {
unsafe {
let _ref: $crate::internal::chunk_ref::ChunkRef<A> = $crate::internal::chunk_ref::ChunkRef::from_value_ptr(self.ptr);
}
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> Unpin for $Ty<A> {}
impl<A: allocator_api2::alloc::Allocator + Clone> core::ops::Deref for $Ty<A> {
type Target = widestring::Utf16Str;
#[inline]
fn deref(&self) -> &widestring::Utf16Str {
self.as_utf16_str()
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> AsRef<widestring::Utf16Str> for $Ty<A> {
#[inline]
fn as_ref(&self) -> &widestring::Utf16Str {
self.as_utf16_str()
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> core::borrow::Borrow<widestring::Utf16Str> for $Ty<A> {
#[inline]
fn borrow(&self) -> &widestring::Utf16Str {
self.as_utf16_str()
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> core::fmt::Debug for $Ty<A> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(self.as_utf16_str(), f)
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> core::fmt::Display for $Ty<A> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(self.as_utf16_str(), f)
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> PartialEq for $Ty<A> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_utf16_str() == other.as_utf16_str()
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> Eq for $Ty<A> {}
impl<A: allocator_api2::alloc::Allocator + Clone> PartialOrd for $Ty<A> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> Ord for $Ty<A> {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.as_utf16_str().cmp(other.as_utf16_str())
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> core::hash::Hash for $Ty<A> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.as_utf16_str().hash(state);
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> core::fmt::Pointer for $Ty<A> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Pointer::fmt(&self.ptr.as_ptr(), f)
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> PartialEq<widestring::Utf16Str> for $Ty<A> {
#[inline]
fn eq(&self, other: &widestring::Utf16Str) -> bool {
self.as_utf16_str() == other
}
}
impl<A: allocator_api2::alloc::Allocator + Clone> PartialEq<&widestring::Utf16Str> for $Ty<A> {
#[inline]
fn eq(&self, other: &&widestring::Utf16Str) -> bool {
self.as_utf16_str() == *other
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<A: allocator_api2::alloc::Allocator + Clone> serde::ser::Serialize for $Ty<A> {
fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self.as_utf16_str())
}
}
};
}
pub(crate) use impl_utf16_str_common;