#[cfg(test)]
#[path = "static_string.tests.rs"]
mod tests;
#[cfg(feature = "alloc")]
use std_alloc::{borrow::Cow, string::String};
mod sealed {
pub trait Sealed {}
impl Sealed for &'static str {}
#[cfg(feature = "alloc")]
impl Sealed for super::Cow<'static, str> {}
#[cfg(feature = "alloc")]
impl Sealed for super::String {}
}
pub trait StaticString: sealed::Sealed + Sized + Send + Sync + 'static {
#[cfg(feature = "alloc")]
fn into_cow(self) -> Cow<'static, str>;
#[cfg(feature = "alloc")]
fn into_owned(self) -> String;
fn try_borrow(&self) -> Option<&'static str>;
}
impl StaticString for &'static str {
#[cfg(feature = "alloc")]
#[inline(always)]
fn into_cow(self) -> Cow<'static, str> {
Cow::Borrowed(self)
}
#[cfg(feature = "alloc")]
#[inline(always)]
fn into_owned(self) -> String {
String::from(self)
}
#[inline(always)]
fn try_borrow(&self) -> Option<&'static str> {
Some(self)
}
}
#[cfg(feature = "alloc")]
impl StaticString for Cow<'static, str> {
#[inline(always)]
fn into_cow(self) -> Cow<'static, str> {
self
}
#[inline]
fn into_owned(self) -> String {
self.into_owned()
}
#[inline]
fn try_borrow(&self) -> Option<&'static str> {
match self {
Cow::Borrowed(value) => Some(value),
Cow::Owned(_) => None,
}
}
}
#[cfg(feature = "alloc")]
impl StaticString for String {
#[inline(always)]
fn into_cow(self) -> Cow<'static, str> {
Cow::Owned(self)
}
#[inline(always)]
fn into_owned(self) -> String {
self
}
#[inline(always)]
fn try_borrow(&self) -> Option<&'static str> {
None
}
}