Skip to main content

SharedString

Struct SharedString 

Source
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§

Source§

impl<TAllocator: Allocator> SharedString<TAllocator>

Source

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.

Source

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.

Source

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.

Source

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.

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 SharedString::as_str call.

Source

pub fn length(&self) -> Length

The length of the string.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<TAllocator: Allocator> AsRef<str> for SharedString<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 SharedString<TAllocator>

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl<TAllocator: Allocator> Clone for SharedString<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 SharedString<TAllocator>

Source§

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

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

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

Source§

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

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

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

Source§

impl<TAllocator> From<&str> for SharedString<TAllocator>
where TAllocator: Default + Allocator,

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl<TAllocator: Allocator> Hash for SharedString<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 SharedString<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> ReprC for SharedString<TAllocator>
where TAllocator: ReprC + Allocator,

Source§

const CHECK: ()

This field is used for const checks only.
Source§

impl<TAllocator: Allocator> TryClone for SharedString<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 SharedString<TAllocator>

§

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

§

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

§

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

§

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

§

impl<TAllocator> UnsafeUnpin for SharedString<TAllocator>

§

impl<TAllocator> UnwindSafe for SharedString<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> 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, 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.