use core::ptr::NonNull;
use allocator_api2::alloc::Allocator;
use crate::{Arc, Box};
impl<A: Allocator + Clone> Arc<str, A> {
#[must_use]
#[inline]
pub fn as_str(&self) -> &str {
self
}
}
impl<A: Allocator + Clone> PartialEq<str> for Arc<str, A> {
#[inline]
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl<A: Allocator + Clone> PartialEq<&str> for Arc<str, A> {
#[inline]
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<A: Allocator + Clone> serde::ser::Serialize for Arc<str, A> {
fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<A: Allocator + Clone> From<Arc<str, A>> for Arc<[u8], A> {
#[inline]
fn from(s: Arc<str, A>) -> Self {
use core::mem::ManuallyDrop;
let me = ManuallyDrop::new(s);
let thin: NonNull<u8> = me.thin_ptr();
unsafe { Self::from_raw(thin) }
}
}
impl<A: Allocator + Clone> Box<str, A> {
#[must_use]
#[inline]
pub fn as_str(&self) -> &str {
self
}
#[must_use]
#[inline]
pub fn as_mut_str(&mut self) -> &mut str {
self
}
}
impl<A: Allocator + Clone> From<Box<str, A>> for Box<[u8], A> {
#[inline]
fn from(s: Box<str, A>) -> Self {
use core::mem::ManuallyDrop;
let me = ManuallyDrop::new(s);
let thin: NonNull<u8> = me.thin_ptr();
unsafe { Self::from_raw(thin) }
}
}
impl<A: Allocator + Clone> PartialEq<str> for Box<str, A> {
#[inline]
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl<A: Allocator + Clone> PartialEq<&str> for Box<str, A> {
#[inline]
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<A: Allocator + Clone> serde::ser::Serialize for Box<str, A> {
fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}