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>
impl<TAllocator: Allocator> ImmutableString<TAllocator>
Sourcepub fn empty() -> Result<Self, ImmutableStringError>where
TAllocator: Default,
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.
Sourcepub fn from_str_slice(text: &str) -> Result<Self, ImmutableStringError>where
TAllocator: Default,
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.
Sourcepub fn from_str_slice_and_allocator(
text: &str,
allocator: TAllocator,
) -> Result<Self, ImmutableStringError>
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.
Sourcepub fn empty_with_allocator(
allocator: TAllocator,
) -> Result<Self, ImmutableStringError>
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.
pub fn strong_count(&self) -> u32
pub fn weak_count(&self) -> u32
Sourcepub fn ptr_eq(&self, other: &Self) -> bool
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.
Sourcepub fn as_c_str(&self) -> &str
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.
Sourcepub fn downgrade(
&self,
) -> Result<WeakImmutableString<TAllocator>, MaxReferencesExceededError>
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].
Sourcepub fn abandon(self) -> Option<WeakImmutableString<TAllocator>>
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>
impl<TAllocator: Allocator> AsRef<str> for ImmutableString<TAllocator>
Source§impl<TAllocator: Allocator> Borrow<str> for ImmutableString<TAllocator>
impl<TAllocator: Allocator> Borrow<str> for ImmutableString<TAllocator>
Source§impl<TAllocator: Allocator> Clone for ImmutableString<TAllocator>
impl<TAllocator: Allocator> Clone for ImmutableString<TAllocator>
Source§impl<TAllocator: Debug + Allocator> Debug for ImmutableString<TAllocator>
impl<TAllocator: Debug + Allocator> Debug for ImmutableString<TAllocator>
Source§impl<'de, TAllocator: Allocator + Default> Deserialize<'de> for ImmutableString<TAllocator>
Available on crate feature serde only.
impl<'de, TAllocator: Allocator + Default> Deserialize<'de> for ImmutableString<TAllocator>
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<TAllocator: Allocator> Display for ImmutableString<TAllocator>
impl<TAllocator: Allocator> Display for ImmutableString<TAllocator>
impl<TAllocator: Allocator> Eq for ImmutableString<TAllocator>
Source§impl<TAllocator: Allocator> Hash for ImmutableString<TAllocator>
impl<TAllocator: Allocator> Hash for ImmutableString<TAllocator>
Source§impl<TAllocator: Allocator> PartialEq for ImmutableString<TAllocator>
impl<TAllocator: Allocator> PartialEq for ImmutableString<TAllocator>
Source§impl<TAllocator: Allocator> ReprC for ImmutableString<TAllocator>
impl<TAllocator: Allocator> ReprC for ImmutableString<TAllocator>
Source§impl<TAllocator: Allocator> Serialize for ImmutableString<TAllocator>
Available on crate feature serde only.
impl<TAllocator: Allocator> Serialize for ImmutableString<TAllocator>
serde only.