2021-06-16 12:36:33 +02:00
|
|
|
//! The `ImmutableString` type.
|
2020-05-14 12:27:22 +02:00
|
|
|
|
2020-11-16 16:10:14 +01:00
|
|
|
use crate::fn_native::{shared_make_mut, shared_take};
|
2021-06-16 12:36:33 +02:00
|
|
|
use crate::{Shared, SmartString};
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
use std::{
|
2020-05-26 08:14:03 +02:00
|
|
|
borrow::Borrow,
|
2020-07-30 07:28:25 +02:00
|
|
|
cmp::Ordering,
|
2020-05-05 09:00:10 +02:00
|
|
|
fmt,
|
2021-06-16 12:36:33 +02:00
|
|
|
hash::Hash,
|
|
|
|
iter::FromIterator,
|
2021-03-23 05:13:53 +01:00
|
|
|
ops::{Add, AddAssign, Deref, Sub, SubAssign},
|
2020-05-26 08:14:03 +02:00
|
|
|
str::FromStr,
|
2020-05-05 06:24:13 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 08:14:03 +02:00
|
|
|
/// The system immutable string type.
|
|
|
|
///
|
2021-07-14 07:58:50 +02:00
|
|
|
/// An [`ImmutableString`] wraps an [`Rc`][std::rc::Rc]`<`[`SmartString`][smartstring::SmartString]`>`
|
|
|
|
/// (or [`Arc`][std::sync::Arc]`<`[`SmartString`][smartstring::SmartString]`>` under the `sync` feature)
|
2020-05-26 08:14:03 +02:00
|
|
|
/// so that it can be simply shared and not cloned.
|
|
|
|
///
|
2020-10-27 04:30:38 +01:00
|
|
|
/// # Example
|
2020-05-26 08:14:03 +02:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use rhai::ImmutableString;
|
|
|
|
///
|
|
|
|
/// let s1: ImmutableString = "hello".into();
|
|
|
|
///
|
|
|
|
/// // No actual cloning of the string is involved below.
|
|
|
|
/// let s2 = s1.clone();
|
|
|
|
/// let s3 = s2.clone();
|
|
|
|
///
|
|
|
|
/// assert_eq!(s1, s2);
|
|
|
|
///
|
|
|
|
/// // Clones the underlying string (because it is already shared) and extracts it.
|
|
|
|
/// let mut s: String = s1.into_owned();
|
|
|
|
///
|
|
|
|
/// // Changing the clone has no impact on the previously shared version.
|
|
|
|
/// s.push_str(", world!");
|
|
|
|
///
|
|
|
|
/// // The old version still exists.
|
|
|
|
/// assert_eq!(s2, s3);
|
|
|
|
/// assert_eq!(s2.as_str(), "hello");
|
|
|
|
///
|
|
|
|
/// // Not equals!
|
|
|
|
/// assert_ne!(s2.as_str(), s.as_str());
|
|
|
|
/// assert_eq!(s, "hello, world!");
|
|
|
|
/// ```
|
2021-03-24 06:17:52 +01:00
|
|
|
#[derive(Clone, Eq, Ord, Hash, Default)]
|
2021-04-23 13:10:10 +02:00
|
|
|
pub struct ImmutableString(Shared<SmartString>);
|
2020-05-26 08:14:03 +02:00
|
|
|
|
|
|
|
impl Deref for ImmutableString {
|
2021-04-23 13:10:10 +02:00
|
|
|
type Target = SmartString;
|
2020-05-26 08:14:03 +02:00
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 13:10:10 +02:00
|
|
|
impl AsRef<SmartString> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2021-04-23 13:10:10 +02:00
|
|
|
fn as_ref(&self) -> &SmartString {
|
2020-05-26 08:14:03 +02:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:17:52 +01:00
|
|
|
impl AsRef<str> for ImmutableString {
|
|
|
|
#[inline(always)]
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 13:10:10 +02:00
|
|
|
impl Borrow<SmartString> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2021-04-23 13:10:10 +02:00
|
|
|
fn borrow(&self) -> &SmartString {
|
2020-07-30 07:28:25 +02:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 08:14:03 +02:00
|
|
|
impl Borrow<str> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn borrow(&self) -> &str {
|
|
|
|
self.0.as_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn from(value: &str) -> Self {
|
2021-10-21 11:26:43 +02:00
|
|
|
let value: SmartString = value.into();
|
|
|
|
Self(value.into())
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-30 14:12:51 +01:00
|
|
|
impl From<&String> for ImmutableString {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(value: &String) -> Self {
|
2021-10-21 11:26:43 +02:00
|
|
|
let value: SmartString = value.into();
|
|
|
|
Self(value.into())
|
2020-12-30 14:12:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 08:14:03 +02:00
|
|
|
impl From<String> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn from(value: String) -> Self {
|
2021-10-21 11:26:43 +02:00
|
|
|
let value: SmartString = value.into();
|
|
|
|
Self(value.into())
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 16:59:33 +02:00
|
|
|
#[cfg(not(feature = "no_smartstring"))]
|
2021-06-29 15:47:27 +02:00
|
|
|
impl From<&SmartString> for ImmutableString {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(value: &SmartString) -> Self {
|
2021-10-21 11:26:43 +02:00
|
|
|
Self(value.clone().into())
|
2021-06-29 15:47:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_smartstring"))]
|
2021-04-23 13:10:10 +02:00
|
|
|
impl From<SmartString> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2021-04-23 13:10:10 +02:00
|
|
|
fn from(value: SmartString) -> Self {
|
2020-05-26 08:14:03 +02:00
|
|
|
Self(value.into())
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 15:47:27 +02:00
|
|
|
impl From<&ImmutableString> for SmartString {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(value: &ImmutableString) -> Self {
|
|
|
|
value.as_str().into()
|
|
|
|
}
|
|
|
|
}
|
2021-04-23 13:10:10 +02:00
|
|
|
impl From<ImmutableString> for SmartString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2021-04-23 13:10:10 +02:00
|
|
|
fn from(mut value: ImmutableString) -> Self {
|
|
|
|
std::mem::take(shared_make_mut(&mut value.0))
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for ImmutableString {
|
|
|
|
type Err = ();
|
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2021-10-21 11:26:43 +02:00
|
|
|
let s: SmartString = s.into();
|
|
|
|
Ok(Self(s.into()))
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromIterator<char> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
|
2021-04-23 13:10:10 +02:00
|
|
|
Self(iter.into_iter().collect::<SmartString>().into())
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromIterator<&'a char> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> Self {
|
2021-04-23 13:10:10 +02:00
|
|
|
Self(iter.into_iter().cloned().collect::<SmartString>().into())
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromIterator<&'a str> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
|
2021-04-23 13:10:10 +02:00
|
|
|
Self(iter.into_iter().collect::<SmartString>().into())
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromIterator<String> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
|
2021-04-23 13:10:10 +02:00
|
|
|
Self(iter.into_iter().collect::<SmartString>().into())
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 16:59:33 +02:00
|
|
|
#[cfg(not(feature = "no_smartstring"))]
|
|
|
|
impl<'a> FromIterator<SmartString> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2021-05-08 16:59:33 +02:00
|
|
|
fn from_iter<T: IntoIterator<Item = SmartString>>(iter: T) -> Self {
|
|
|
|
Self(iter.into_iter().collect::<SmartString>().into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 09:15:54 +02:00
|
|
|
impl fmt::Display for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-04-17 09:15:54 +02:00
|
|
|
fmt::Display::fmt(self.0.as_str(), f)
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 09:15:54 +02:00
|
|
|
impl fmt::Debug for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-04-17 09:15:54 +02:00
|
|
|
fmt::Debug::fmt(self.0.as_str(), f)
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for ImmutableString {
|
|
|
|
type Output = Self;
|
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(mut self, rhs: Self) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs
|
|
|
|
} else {
|
|
|
|
self.make_mut().push_str(rhs.0.as_str());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for &ImmutableString {
|
|
|
|
type Output = ImmutableString;
|
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(self, rhs: Self) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self.clone()
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs.clone()
|
|
|
|
} else {
|
|
|
|
let mut s = self.clone();
|
|
|
|
s.make_mut().push_str(rhs.0.as_str());
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign<&ImmutableString> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add_assign(&mut self, rhs: &ImmutableString) {
|
|
|
|
if !rhs.is_empty() {
|
|
|
|
if self.is_empty() {
|
|
|
|
self.0 = rhs.0.clone();
|
|
|
|
} else {
|
|
|
|
self.make_mut().push_str(rhs.0.as_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 16:28:13 +02:00
|
|
|
impl AddAssign<ImmutableString> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-08-01 16:28:13 +02:00
|
|
|
fn add_assign(&mut self, rhs: ImmutableString) {
|
|
|
|
if !rhs.is_empty() {
|
|
|
|
if self.is_empty() {
|
|
|
|
self.0 = rhs.0;
|
|
|
|
} else {
|
|
|
|
self.make_mut().push_str(rhs.0.as_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 08:14:03 +02:00
|
|
|
impl Add<&str> for ImmutableString {
|
|
|
|
type Output = Self;
|
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(mut self, rhs: &str) -> Self::Output {
|
2021-07-24 08:11:16 +02:00
|
|
|
if !rhs.is_empty() {
|
2020-05-26 08:14:03 +02:00
|
|
|
self.make_mut().push_str(rhs);
|
|
|
|
}
|
2021-07-24 08:11:16 +02:00
|
|
|
self
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<&str> for &ImmutableString {
|
|
|
|
type Output = ImmutableString;
|
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(self, rhs: &str) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self.clone()
|
|
|
|
} else {
|
|
|
|
let mut s = self.clone();
|
|
|
|
s.make_mut().push_str(rhs);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign<&str> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add_assign(&mut self, rhs: &str) {
|
|
|
|
if !rhs.is_empty() {
|
|
|
|
self.make_mut().push_str(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<String> for ImmutableString {
|
|
|
|
type Output = Self;
|
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(mut self, rhs: String) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs.into()
|
|
|
|
} else {
|
|
|
|
self.make_mut().push_str(&rhs);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<String> for &ImmutableString {
|
|
|
|
type Output = ImmutableString;
|
|
|
|
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(self, rhs: String) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self.clone()
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs.into()
|
|
|
|
} else {
|
|
|
|
let mut s = self.clone();
|
|
|
|
s.make_mut().push_str(&rhs);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 06:29:49 +01:00
|
|
|
impl AddAssign<String> for ImmutableString {
|
2021-07-10 05:06:13 +02:00
|
|
|
#[inline]
|
2021-02-25 06:29:49 +01:00
|
|
|
fn add_assign(&mut self, rhs: String) {
|
2021-04-23 17:37:33 +02:00
|
|
|
if !rhs.is_empty() {
|
|
|
|
if self.is_empty() {
|
2021-10-21 11:26:43 +02:00
|
|
|
let rhs: SmartString = rhs.into();
|
|
|
|
self.0 = rhs.into();
|
2021-04-23 17:37:33 +02:00
|
|
|
} else {
|
|
|
|
self.make_mut().push_str(&rhs);
|
|
|
|
}
|
|
|
|
}
|
2021-02-25 06:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 08:14:03 +02:00
|
|
|
impl Add<char> for ImmutableString {
|
|
|
|
type Output = Self;
|
|
|
|
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(mut self, rhs: char) -> Self::Output {
|
|
|
|
self.make_mut().push(rhs);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<char> for &ImmutableString {
|
|
|
|
type Output = ImmutableString;
|
|
|
|
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add(self, rhs: char) -> Self::Output {
|
|
|
|
let mut s = self.clone();
|
|
|
|
s.make_mut().push(rhs);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign<char> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
fn add_assign(&mut self, rhs: char) {
|
|
|
|
self.make_mut().push(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 06:29:49 +01:00
|
|
|
impl Sub for ImmutableString {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs
|
|
|
|
} else {
|
|
|
|
self.replace(rhs.as_str(), "").into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub for &ImmutableString {
|
|
|
|
type Output = ImmutableString;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self.clone()
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs.clone()
|
|
|
|
} else {
|
|
|
|
self.replace(rhs.as_str(), "").into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubAssign<&ImmutableString> for ImmutableString {
|
|
|
|
#[inline]
|
|
|
|
fn sub_assign(&mut self, rhs: &ImmutableString) {
|
|
|
|
if !rhs.is_empty() {
|
|
|
|
if self.is_empty() {
|
|
|
|
self.0 = rhs.0.clone();
|
|
|
|
} else {
|
2021-10-21 11:26:43 +02:00
|
|
|
let rhs: SmartString = self.replace(rhs.as_str(), "").into();
|
|
|
|
self.0 = rhs.into();
|
2021-02-25 06:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubAssign<ImmutableString> for ImmutableString {
|
|
|
|
#[inline]
|
|
|
|
fn sub_assign(&mut self, rhs: ImmutableString) {
|
|
|
|
if !rhs.is_empty() {
|
|
|
|
if self.is_empty() {
|
|
|
|
self.0 = rhs.0;
|
|
|
|
} else {
|
2021-10-21 11:26:43 +02:00
|
|
|
let rhs: SmartString = self.replace(rhs.as_str(), "").into();
|
|
|
|
self.0 = rhs.into();
|
2021-02-25 06:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<String> for ImmutableString {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, rhs: String) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs.into()
|
|
|
|
} else {
|
|
|
|
self.replace(&rhs, "").into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<String> for &ImmutableString {
|
|
|
|
type Output = ImmutableString;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, rhs: String) -> Self::Output {
|
|
|
|
if rhs.is_empty() {
|
|
|
|
self.clone()
|
|
|
|
} else if self.is_empty() {
|
|
|
|
rhs.into()
|
|
|
|
} else {
|
|
|
|
self.replace(&rhs, "").into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubAssign<String> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2021-02-25 06:29:49 +01:00
|
|
|
fn sub_assign(&mut self, rhs: String) {
|
2021-10-21 11:26:43 +02:00
|
|
|
let rhs: SmartString = self.replace(&rhs, "").into();
|
|
|
|
self.0 = rhs.into();
|
2021-02-25 06:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<char> for ImmutableString {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub(self, rhs: char) -> Self::Output {
|
|
|
|
self.replace(rhs, "").into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<char> for &ImmutableString {
|
|
|
|
type Output = ImmutableString;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn sub(self, rhs: char) -> Self::Output {
|
|
|
|
self.replace(rhs, "").into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubAssign<char> for ImmutableString {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2021-02-25 06:29:49 +01:00
|
|
|
fn sub_assign(&mut self, rhs: char) {
|
2021-10-21 11:26:43 +02:00
|
|
|
let rhs: SmartString = self.replace(rhs, "").into();
|
|
|
|
self.0 = rhs.into();
|
2021-02-25 06:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 07:28:25 +02:00
|
|
|
impl<S: AsRef<str>> PartialEq<S> for ImmutableString {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-07-30 07:28:25 +02:00
|
|
|
fn eq(&self, other: &S) -> bool {
|
|
|
|
self.as_str().eq(other.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<ImmutableString> for str {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-07-30 07:28:25 +02:00
|
|
|
fn eq(&self, other: &ImmutableString) -> bool {
|
|
|
|
self.eq(other.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<ImmutableString> for String {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-07-30 07:28:25 +02:00
|
|
|
fn eq(&self, other: &ImmutableString) -> bool {
|
|
|
|
self.eq(other.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: AsRef<str>> PartialOrd<S> for ImmutableString {
|
|
|
|
fn partial_cmp(&self, other: &S) -> Option<Ordering> {
|
|
|
|
self.as_str().partial_cmp(other.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd<ImmutableString> for str {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-07-30 07:28:25 +02:00
|
|
|
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
|
|
|
|
self.partial_cmp(other.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd<ImmutableString> for String {
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2020-07-30 07:28:25 +02:00
|
|
|
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
|
|
|
|
self.as_str().partial_cmp(other.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 08:14:03 +02:00
|
|
|
impl ImmutableString {
|
2021-07-02 05:51:13 +02:00
|
|
|
/// Create a new [`ImmutableString`].
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self(SmartString::new().into())
|
|
|
|
}
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Consume the [`ImmutableString`] and convert it into a [`String`].
|
2020-05-26 08:14:03 +02:00
|
|
|
/// If there are other references to the same string, a cloned copy is returned.
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-05-26 08:14:03 +02:00
|
|
|
pub fn into_owned(mut self) -> String {
|
|
|
|
self.make_mut(); // Make sure it is unique reference
|
2021-04-23 13:10:10 +02:00
|
|
|
shared_take(self.0).into() // Should succeed
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Make sure that the [`ImmutableString`] is unique (i.e. no other outstanding references).
|
2021-04-23 13:10:10 +02:00
|
|
|
/// Then return a mutable reference to the [`SmartString`].
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline(always)]
|
2021-04-23 13:10:10 +02:00
|
|
|
pub(crate) fn make_mut(&mut self) -> &mut SmartString {
|
2020-05-26 08:14:03 +02:00
|
|
|
shared_make_mut(&mut self.0)
|
|
|
|
}
|
2021-09-27 05:09:19 +02:00
|
|
|
/// Returns `true` if the two [`ImmutableString`]'s point to the same allocation.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use rhai::ImmutableString;
|
|
|
|
///
|
|
|
|
/// let s1: ImmutableString = "hello".into();
|
|
|
|
/// let s2 = s1.clone();
|
|
|
|
/// let s3: ImmutableString = "hello".into();
|
|
|
|
///
|
|
|
|
/// assert_eq!(s1, s2);
|
|
|
|
/// assert_eq!(s1, s3);
|
|
|
|
/// assert_eq!(s2, s3);
|
|
|
|
///
|
|
|
|
/// assert!(s1.ptr_eq(&s2));
|
|
|
|
/// assert!(!s1.ptr_eq(&s3));
|
|
|
|
/// assert!(!s2.ptr_eq(&s3));
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn ptr_eq(&self, other: &Self) -> bool {
|
|
|
|
Shared::ptr_eq(&self.0, &other.0)
|
|
|
|
}
|
2020-05-26 08:14:03 +02:00
|
|
|
}
|