use super::ffi;
use std::{
fmt::{Debug, Display},
hash::Hash,
mem::MaybeUninit,
};
pub struct ClipperText(pub(crate) ffi::ClipperText);
impl ClipperText {
#[inline(always)]
pub fn as_str(&self) -> &str {
self.as_ref()
}
}
impl AsRef<str> for ClipperText {
fn as_ref(&self) -> &str {
unsafe {
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
self.0.text as *const u8,
self.0.length,
))
}
}
}
impl Debug for ClipperText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self.as_ref(), f)
}
}
impl Display for ClipperText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.as_ref(), f)
}
}
impl From<ClipperText> for Box<str> {
fn from(text: ClipperText) -> Self {
text.as_ref().into()
}
}
impl Drop for ClipperText {
fn drop(&mut self) {
unsafe {
ffi::clipper_free_tagged_data(ffi::TaggedClipperData {
tag: ffi::ClipperTag::Text,
data: MaybeUninit::new(ffi::ClipperData { text: self.0 }),
})
}
}
}
impl Eq for ClipperText {}
impl PartialEq for ClipperText {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}
impl PartialEq<&str> for ClipperText {
fn eq(&self, other: &&str) -> bool {
self.as_ref() == *other
}
}
impl PartialEq<ClipperText> for &str {
fn eq(&self, other: &ClipperText) -> bool {
other.as_ref() == *self
}
}
impl PartialEq<str> for ClipperText {
fn eq(&self, other: &str) -> bool {
self.as_ref() == other
}
}
impl PartialEq<ClipperText> for str {
fn eq(&self, other: &ClipperText) -> bool {
self == other.as_ref()
}
}
impl PartialEq<String> for ClipperText {
fn eq(&self, other: &String) -> bool {
self.as_ref() == other
}
}
impl PartialEq<ClipperText> for String {
fn eq(&self, other: &ClipperText) -> bool {
self == other.as_ref()
}
}
impl Ord for ClipperText {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_ref().cmp(other.as_ref())
}
}
impl PartialOrd for ClipperText {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialOrd<str> for ClipperText {
fn partial_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
Some(self.as_ref().cmp(other))
}
}
impl PartialOrd<ClipperText> for str {
fn partial_cmp(&self, other: &ClipperText) -> Option<std::cmp::Ordering> {
Some(self.cmp(other.as_ref()))
}
}
impl PartialOrd<String> for ClipperText {
fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
Some(self.as_ref().cmp(other))
}
}
impl PartialOrd<ClipperText> for String {
fn partial_cmp(&self, other: &ClipperText) -> Option<std::cmp::Ordering> {
Some(self.as_str().cmp(other.as_str()))
}
}
impl Hash for ClipperText {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_ref().hash(state)
}
}