use core::borrow::{Borrow, BorrowMut};
use core::mem::ManuallyDrop;
use allocator_api2::alloc::Allocator;
use widestring::Utf16Str as WideUtf16Str;
use super::Utf16Str;
use crate::{Arc, Box, Rc};
macro_rules! impl_utf16_str_smart_ptr_extras {
($Ty:ident) => {
impl<A: Allocator + Clone> PartialEq<WideUtf16Str> for $Ty<Utf16Str, A> {
#[inline]
fn eq(&self, other: &WideUtf16Str) -> bool {
(**self).as_widestring_utf16_str() == other
}
}
impl<A: Allocator + Clone> PartialEq<&WideUtf16Str> for $Ty<Utf16Str, A> {
#[inline]
fn eq(&self, other: &&WideUtf16Str) -> bool {
(**self).as_widestring_utf16_str() == *other
}
}
impl<A: Allocator + Clone> AsRef<WideUtf16Str> for $Ty<Utf16Str, A> {
#[inline]
fn as_ref(&self) -> &WideUtf16Str {
(**self).as_widestring_utf16_str()
}
}
impl<A: Allocator + Clone> Borrow<WideUtf16Str> for $Ty<Utf16Str, A> {
#[inline]
fn borrow(&self) -> &WideUtf16Str {
(**self).as_widestring_utf16_str()
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<A: Allocator + Clone> serde::ser::Serialize for $Ty<Utf16Str, A> {
fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str((**self).as_widestring_utf16_str())
}
}
};
}
impl_utf16_str_smart_ptr_extras!(Arc);
impl_utf16_str_smart_ptr_extras!(Box);
impl_utf16_str_smart_ptr_extras!(Rc);
impl<A: Allocator + Clone> AsMut<WideUtf16Str> for Box<Utf16Str, A> {
#[inline]
fn as_mut(&mut self) -> &mut WideUtf16Str {
(**self).as_mut_widestring_utf16_str()
}
}
impl<A: Allocator + Clone> BorrowMut<WideUtf16Str> for Box<Utf16Str, A> {
#[inline]
fn borrow_mut(&mut self) -> &mut WideUtf16Str {
(**self).as_mut_widestring_utf16_str()
}
}
impl<A: Allocator + Clone> From<Arc<Utf16Str, A>> for Arc<[u16], A> {
#[inline]
fn from(s: Arc<Utf16Str, A>) -> Self {
let me = ManuallyDrop::new(s);
unsafe { Self::from_raw(me.thin_ptr()) }
}
}
impl<A: Allocator + Clone> From<Box<Utf16Str, A>> for Box<[u16], A> {
#[inline]
fn from(s: Box<Utf16Str, A>) -> Self {
let me = ManuallyDrop::new(s);
unsafe { Self::from_raw(me.thin_ptr()) }
}
}
impl<A: Allocator + Clone> From<Rc<Utf16Str, A>> for Rc<[u16], A> {
#[inline]
fn from(s: Rc<Utf16Str, A>) -> Self {
let me = ManuallyDrop::new(s);
unsafe { Self::from_raw(me.thin_ptr()) }
}
}