Skip to main content

ImmutableString

Struct ImmutableString 

Source
pub struct ImmutableString<TAllocator: Allocator> { /* private fields */ }
Expand description

This struct is a smart pointer around a string. Internally keeps reference counters for both strong and weak references. Cloning the struct is therefore cheap.

NOTE: the immutable string internally keeps one byte more than the specified data. This last byte is always set to \0 and thus the string is suitable to use as a C-style string, by calling ImmutableString::as_c_str() method.

Implementations§

Source§

impl<TAllocator: Allocator> ImmutableString<TAllocator>

Source

pub fn empty() -> Result<Self, ImmutableStringError>
where TAllocator: Default,

Creates a new, empty ImmutableString.

This function allocates under the hood, since all immutable strings are backed by smart pointer.

§Errors

For details see ImmutableStringError.

Source

pub fn from_str_slice(text: &str) -> Result<Self, ImmutableStringError>
where TAllocator: Default,

Creates a new ImmutableString out of passed string slice.

This function allocates under the hood, since all immutable strings are backed by smart pointer. It also copies the input into the output’s buffer.

§Errors

For details see ImmutableStringError.

Source

pub fn from_str_slice_and_allocator( text: &str, allocator: TAllocator, ) -> Result<Self, ImmutableStringError>

Creates a new ImmutableString out of passed string slice and allocator.

This function allocates under the hood, since all immutable strings are backed by smart pointer. It also copies the input into the output’s buffer.

§Errors

For details see ImmutableStringError.

Source

pub fn empty_with_allocator( allocator: TAllocator, ) -> Result<Self, ImmutableStringError>

Creates a new, empty ImmutableString with a given allocator.

This function allocates under the hood, since all immutable strings are backed by smart pointer.

§Errors

For details see ImmutableStringError.

Source

pub fn strong_count(&self) -> u32

Source

pub fn weak_count(&self) -> u32

Source

pub fn ptr_eq(&self, other: &Self) -> bool

Compares the underlying raw pointers of the two strings.

Useful for determining whether the two strings have the same allocation, not only data.

Source

pub fn as_str(&self) -> &str

Returns the underlying string.

Source

pub fn as_c_str(&self) -> &str

Returns the underlying string as C-string.

Meaning the string has an additional 0 at the end of the buffer. In particular, the C-string returned by this method has length +1 compared to ImmutableString::as_str call.

Source

pub fn length(&self) -> Length

The length of the string.

Source

pub fn downgrade( &self, ) -> Result<WeakImmutableString<TAllocator>, MaxReferencesExceededError>

Creates a new WeakImmutableString out of current.

§Errors

Returns MaxReferencesExceededError if the weak reference count is too high. Cannot exceed [osom_lib_arc::consts::MAX_REFERENCES].

Source

pub fn abandon(self) -> Option<WeakImmutableString<TAllocator>>

Abandons current ImmutableString.

This function returns None if the underlying strong reference counter is still positive. Otherwise it returns the final WeakImmutableString reference.

Trait Implementations§

Source§

impl<TAllocator: Allocator> AsRef<str> for ImmutableString<TAllocator>

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<TAllocator: Allocator> Borrow<str> for ImmutableString<TAllocator>

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl<TAllocator: Allocator> Clone for ImmutableString<TAllocator>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<TAllocator: Debug + Allocator> Debug for ImmutableString<TAllocator>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, TAllocator: Allocator + Default> Deserialize<'de> for ImmutableString<TAllocator>

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<TAllocator: Allocator> Display for ImmutableString<TAllocator>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<TAllocator: Allocator> Eq for ImmutableString<TAllocator>

Source§

impl<TAllocator: Allocator> Hash for ImmutableString<TAllocator>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<TAllocator: Allocator> PartialEq for ImmutableString<TAllocator>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<TAllocator: Allocator> ReprC for ImmutableString<TAllocator>

Source§

const CHECK: ()

This field is used for const checks only.
Source§

impl<TAllocator: Allocator> Serialize for ImmutableString<TAllocator>

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<TAllocator: Allocator> TryClone for ImmutableString<TAllocator>

Source§

type Error = MaxReferencesExceededError

Source§

fn try_clone(&self) -> Result<Self, Self::Error>

Tries to clone the type. Read more

Auto Trait Implementations§

§

impl<TAllocator> Freeze for ImmutableString<TAllocator>

§

impl<TAllocator> RefUnwindSafe for ImmutableString<TAllocator>
where TAllocator: RefUnwindSafe,

§

impl<TAllocator> Send for ImmutableString<TAllocator>
where TAllocator: Send,

§

impl<TAllocator> Sync for ImmutableString<TAllocator>
where TAllocator: Sync,

§

impl<TAllocator> Unpin for ImmutableString<TAllocator>
where TAllocator: Unpin,

§

impl<TAllocator> UnsafeUnpin for ImmutableString<TAllocator>

§

impl<TAllocator> UnwindSafe for ImmutableString<TAllocator>
where TAllocator: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.