rhai/src/immutable_string.rs

544 lines
13 KiB
Rust
Raw Normal View History

2021-06-16 12:36:33 +02:00
//! The `ImmutableString` type.
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,
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-26 08:14:03 +02:00
/// The system immutable string type.
///
2020-11-20 09:52:28 +01:00
/// An [`ImmutableString`] wraps an [`Rc`][std::rc::Rc]`<`[`String`]`>`
/// (or [`Arc`][std::sync::Arc]`<`[`String`]`>` 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)]
pub struct ImmutableString(Shared<SmartString>);
2020-05-26 08:14:03 +02:00
impl Deref for ImmutableString {
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
}
}
impl AsRef<SmartString> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
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
}
}
impl Borrow<SmartString> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
fn borrow(&self) -> &SmartString {
&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 {
Self(Into::<SmartString>::into(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 {
Self(Into::<SmartString>::into(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 {
Self(Into::<SmartString>::into(value).into())
2020-05-26 08:14:03 +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 {
Self(Into::<SmartString>::into(value.as_str()).into())
}
}
#[cfg(not(feature = "no_smartstring"))]
impl From<SmartString> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
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()
}
}
impl From<ImmutableString> for SmartString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
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> {
Ok(Self(Into::<SmartString>::into(s).into()))
2020-05-26 08:14:03 +02:00
}
}
impl FromIterator<char> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-05-26 08:14:03 +02:00
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
Self(iter.into_iter().collect::<SmartString>().into())
2020-05-26 08:14:03 +02:00
}
}
impl<'a> FromIterator<&'a char> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-05-26 08:14:03 +02:00
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> Self {
Self(iter.into_iter().cloned().collect::<SmartString>().into())
2020-05-26 08:14:03 +02:00
}
}
impl<'a> FromIterator<&'a str> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-05-26 08:14:03 +02:00
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
Self(iter.into_iter().collect::<SmartString>().into())
2020-05-26 08:14:03 +02:00
}
}
impl<'a> FromIterator<String> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-05-26 08:14:03 +02:00
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
Self(iter.into_iter().collect::<SmartString>().into())
2020-05-26 08:14:03 +02:00
}
}
#[cfg(not(feature = "no_smartstring"))]
impl<'a> FromIterator<SmartString> for ImmutableString {
#[inline(always)]
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());
}
}
}
}
impl AddAssign<ImmutableString> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline]
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 {
if rhs.is_empty() {
self
} else {
self.make_mut().push_str(rhs);
self
}
}
}
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 {
2020-10-08 16:25:50 +02:00
#[inline(always)]
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
}
}
}
impl AddAssign<String> for ImmutableString {
#[inline(always)]
fn add_assign(&mut self, rhs: String) {
2021-04-23 17:37:33 +02:00
if !rhs.is_empty() {
if self.is_empty() {
self.0 = Into::<SmartString>::into(rhs).into();
} else {
self.make_mut().push_str(&rhs);
}
}
}
}
2020-05-26 08:14:03 +02:00
impl Add<char> for ImmutableString {
type Output = Self;
2020-10-08 16:25:50 +02:00
#[inline(always)]
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;
2020-10-08 16:25:50 +02:00
#[inline(always)]
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 {
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-05-26 08:14:03 +02:00
fn add_assign(&mut self, rhs: char) {
self.make_mut().push(rhs);
}
}
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 {
self.0 = Into::<SmartString>::into(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;
} else {
self.0 = Into::<SmartString>::into(self.replace(rhs.as_str(), "")).into();
}
}
}
}
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 {
#[inline(always)]
fn sub_assign(&mut self, rhs: String) {
self.0 = Into::<SmartString>::into(self.replace(&rhs, "")).into();
}
}
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 {
#[inline(always)]
fn sub_assign(&mut self, rhs: char) {
self.0 = Into::<SmartString>::into(self.replace(rhs, "")).into();
}
}
impl<S: AsRef<str>> PartialEq<S> for ImmutableString {
2020-10-08 16:25:50 +02:00
#[inline(always)]
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)]
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)]
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)]
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)]
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 {
/// 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.
2020-10-08 16:25:50 +02:00
#[inline(always)]
2020-05-26 08:14:03 +02:00
pub fn into_owned(mut self) -> String {
self.make_mut(); // Make sure it is unique reference
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).
/// Then return a mutable reference to the [`SmartString`].
2020-10-08 16:25:50 +02:00
#[inline(always)]
pub(crate) fn make_mut(&mut self) -> &mut SmartString {
2020-05-26 08:14:03 +02:00
shared_make_mut(&mut self.0)
}
}