pub struct SharedString<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 shared 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 SharedString::as_c_str() method.
Implementations§
Sourcepub fn empty() -> Result<Self, SharedStringError>where
TAllocator: Default,
pub fn empty() -> Result<Self, SharedStringError>where
TAllocator: Default,
Creates a new, empty SharedString.
This function allocates under the hood, since all shared strings are backed by smart pointer.
§Errors
For details see SharedStringError.
Sourcepub fn from_str_slice(text: &str) -> Result<Self, SharedStringError>where
TAllocator: Default,
pub fn from_str_slice(text: &str) -> Result<Self, SharedStringError>where
TAllocator: Default,
Creates a new SharedString out of passed string slice.
This function allocates under the hood, since all shared strings are backed by smart pointer. It also copies the input into the output’s buffer.
§Errors
For details see SharedStringError.
Sourcepub fn from_str_slice_and_allocator(
text: &str,
allocator: TAllocator,
) -> Result<Self, SharedStringError>
pub fn from_str_slice_and_allocator( text: &str, allocator: TAllocator, ) -> Result<Self, SharedStringError>
Creates a new SharedString out of passed string slice and allocator.
This function allocates under the hood, since all shared strings are backed by smart pointer. It also copies the input into the output’s buffer.
§Errors
For details see SharedStringError.
Sourcepub fn empty_with_allocator(
allocator: TAllocator,
) -> Result<Self, SharedStringError>
pub fn empty_with_allocator( allocator: TAllocator, ) -> Result<Self, SharedStringError>
Creates a new, empty SharedString with a given allocator.
This function allocates under the hood, since all shared strings are backed by smart pointer.
§Errors
For details see SharedStringError.
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 SharedString::as_str
call.
Sourcepub fn downgrade(
&self,
) -> Result<WeakSharedString<TAllocator>, MaxReferencesExceededError>
pub fn downgrade( &self, ) -> Result<WeakSharedString<TAllocator>, MaxReferencesExceededError>
Creates a new WeakSharedString 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<WeakSharedString<TAllocator>>
pub fn abandon(self) -> Option<WeakSharedString<TAllocator>>
Abandons current SharedString.
This function returns None if the underlying strong reference counter
is still positive. Otherwise it returns the final WeakSharedString
reference.