Str

Enum Str 

Source
pub enum Str {
    Static(&'static str),
    Rc(Arc<String>),
    Owned(String),
}
Expand description

A string type that can hold either a 'static string slice, an Arc<String> for cheap cloning, or an owned String.

This enum is useful when you need to store string data that may be static, shared, or owned without incurring unnecessary allocations.

§Variants

  • Static(&'static str) – A reference to a compile‑time constant string.
  • Rc(Arc<String>) – A reference‑counted pointer to a String, allowing cheap cloning of the underlying data.
  • Owned(String) – An owned heap‑allocated String.

Variants§

§

Static(&'static str)

A reference to a compile‑time constant string.

§

Rc(Arc<String>)

A reference‑counted pointer to a String.

§

Owned(String)

An owned heap‑allocated String.

Implementations§

Source§

impl Str

Source

pub fn as_str(&self) -> &str

Returns a string slice (&str) that references the data inside the Str, regardless of which variant it holds.

§Examples
use anyval::Str;
let s = Str::from_static("hello");
assert_eq!(s.as_str(), "hello");
Source

pub fn from_static(s: &'static str) -> Self

Constructs a Str from a 'static string slice.

This does not allocate; the variant stored is Str::Static.

§Examples
use anyval::Str;
let s = Str::from_static("static");
assert!(s.is_static());
Source

pub fn from_owned(s: String) -> Self

Constructs a Str from an owned String.

The String is moved into the Str::Owned variant.

§Examples
use anyval::Str;
let original = String::from("owned");
let s = Str::from_owned(original);
// `original` can no longer be used here
Source

pub fn from_rc(s: Arc<String>) -> Self

Constructs a Str from an Arc<String>.

The Arc is moved into the Str::Rc variant, allowing cheap cloning.

§Examples
use std::sync::Arc;
use anyval::Str;
let arc = Arc::new(String::from("shared"));
let s = Str::from_rc(arc.clone());
assert!(s.is_rc());
Source

pub fn take(self) -> String

Consumes the Str and returns an owned String.

  • For Str::Static, the static slice is cloned into a new String.
  • For Str::Rc, the underlying String is cloned.
  • For Str::Owned, the inner String is returned without allocation.
§Examples
use anyval::Str;
let s = Str::from_owned(String::from("owned"));
let owned = s.take(); // `s` is moved
assert_eq!(owned, "owned");
Source

pub fn into_string(self) -> String

Alias for [take]; consumes the Str and returns an owned String.

This method exists for API symmetry with other types that provide an into_* conversion.

Source

pub fn is_static(&self) -> bool

Returns true if the Str is the Static variant.

§Examples
use anyval::Str;
let s = Str::from_static("static");
assert!(s.is_static());
Source

pub fn is_owned(&self) -> bool

Returns true if the Str is the Owned variant.

§Examples
use anyval::Str;
let s = Str::from_owned(String::from("owned"));
assert!(s.is_owned());
Source

pub fn is_rc(&self) -> bool

Returns true if the Str is the Rc variant.

§Examples
use anyval::Str;
use std::sync::Arc;
let s = Str::from_rc(Arc::new(String::from("shared")));
assert!(s.is_rc());

Trait Implementations§

Source§

impl Clone for Str

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Str

Source§

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

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

impl Display for Str

Source§

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

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

impl From<&'static str> for Str

Source§

fn from(s: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<Arc<String>> for Str

Source§

fn from(s: Arc<String>) -> Self

Converts to this type from the input type.
Source§

impl From<Str> for String

Source§

fn from(s: Str) -> Self

Converts to this type from the input type.
Source§

impl From<Str> for Value

Source§

fn from(s: Str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Str

Source§

fn from(s: String) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Str

§

impl RefUnwindSafe for Str

§

impl Send for Str

§

impl Sync for Str

§

impl Unpin for Str

§

impl UnwindSafe for Str

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> 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.