use crate::iter::CodePointsIter;
use crate::vtable::JsStringVTable;
use crate::{JsStr, JsString, JsStringKind};
use std::hash::{Hash, Hasher};
use std::ptr::NonNull;
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct StaticString {
vtable: JsStringVTable,
pub(crate) str: JsStr<'static>,
}
impl StaticString {
#[must_use]
pub const fn new(str: JsStr<'static>) -> Self {
Self {
vtable: JsStringVTable {
clone: static_clone,
drop: static_drop,
as_str: static_as_str,
code_points: static_code_points,
refcount: static_refcount,
len: str.len(),
kind: JsStringKind::Static,
},
str,
}
}
}
impl Hash for StaticString {
fn hash<H: Hasher>(&self, state: &mut H) {
self.str.hash(state);
}
}
impl PartialEq for StaticString {
fn eq(&self, other: &Self) -> bool {
self.str == other.str
}
}
impl Eq for StaticString {}
impl std::borrow::Borrow<JsStr<'static>> for &'static StaticString {
fn borrow(&self) -> &JsStr<'static> {
&self.str
}
}
#[inline]
pub(crate) fn static_clone(this: NonNull<JsStringVTable>) -> JsString {
unsafe { JsString::from_ptr(this) }
}
#[inline]
fn static_drop(_ptr: NonNull<JsStringVTable>) {
}
#[inline]
fn static_as_str(this: NonNull<JsStringVTable>) -> JsStr<'static> {
let this: &StaticString = unsafe { this.cast().as_ref() };
this.str
}
#[inline]
fn static_code_points(this: NonNull<JsStringVTable>) -> CodePointsIter<'static> {
CodePointsIter::new(static_as_str(this))
}
#[inline]
fn static_refcount(_ptr: NonNull<JsStringVTable>) -> Option<usize> {
None
}