use core::cmp::Ordering;
use core::mem::MaybeUninit;
use core::ptr::Alignment;
use crate::primitive::ArchivedUsize;
use crate::string::ArchivedString;
use crate::{Serialize, SerializeError, VerifyError};
impl Serialize for String {
type Archived = ArchivedString;
const ALIGN: Alignment = Alignment::of::<ArchivedUsize>();
unsafe fn serialize(
&self,
out: *mut MaybeUninit<ArchivedString>,
heap: *mut MaybeUninit<u8>,
) -> usize {
unsafe { (**self).serialize(out, heap) }
}
fn serialized_size(&self, offset: usize) -> Result<usize, SerializeError> {
(**self).serialized_size(offset)
}
#[inline]
unsafe fn verify(
this: *const ArchivedString,
buffer_end: *const u8,
) -> Result<(), VerifyError> {
unsafe { str::verify(this, buffer_end) }
}
}
impl PartialEq<String> for ArchivedString {
#[inline]
fn eq(&self, other: &String) -> bool {
PartialEq::eq(self.as_str(), other.as_str())
}
}
impl PartialEq<Box<str>> for ArchivedString {
#[inline]
fn eq(&self, other: &Box<str>) -> bool {
PartialEq::eq(self.as_str(), &**other)
}
}
impl PartialEq<ArchivedString> for String {
#[inline]
fn eq(&self, other: &ArchivedString) -> bool {
PartialEq::eq(other.as_str(), self.as_str())
}
}
impl PartialOrd<String> for ArchivedString {
#[inline]
fn partial_cmp(&self, other: &String) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
impl PartialOrd<Box<str>> for ArchivedString {
#[inline]
fn partial_cmp(&self, other: &Box<str>) -> Option<Ordering> {
self.as_str().partial_cmp(&**other)
}
}
impl PartialOrd<ArchivedString> for String {
#[inline]
fn partial_cmp(&self, other: &ArchivedString) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
#[cfg(test)]
mod tests {
use crate::test::roundtrip;
#[test]
fn roundtrip_string() {
roundtrip(&"".to_string());
roundtrip(&"hello world".to_string());
}
#[test]
fn roundtrip_option_string() {
roundtrip(&Some("".to_string()));
roundtrip(&Some("hello world".to_string()));
}
#[test]
fn roundtrip_result_string() {
roundtrip(&Ok::<_, ()>("".to_string()));
roundtrip(&Ok::<_, ()>("hello world".to_string()));
roundtrip(&Err::<(), _>("".to_string()));
roundtrip(&Err::<(), _>("hello world".to_string()));
}
}