[−][src]Struct string_cache::Atom
Represents a string that has been interned.
While the type definition for Atom
indicates that it generic on a particular
implementation of an atom set, you don't need to worry about this. Atoms can be static
and come from a StaticAtomSet
generated by the string_cache_codegen
crate, or they
can be dynamic and created by you on an EmptyStaticAtomSet
.
Atom
implements Clone
but not Copy
, since internally atoms are reference-counted;
this means that you may need to .clone()
an atom to keep copies to it in different
places, or when passing it to a function that takes an Atom
rather than an &Atom
.
Creating an atom at runtime
If you use string_cache_codegen
to generate a precomputed list of atoms, your code
may then do something like read data from somewhere and extract tokens that need to be
compared to the atoms. In this case, you can use Atom::from(&str)
or
Atom::from(String)
. These create a reference-counted atom which will be
automatically freed when all references to it are dropped.
This means that your application can safely have a loop which tokenizes data, creates atoms from the tokens, and compares the atoms to a predefined set of keywords, without running the risk of arbitrary memory consumption from creating large numbers of atoms — as long as your application does not store clones of the atoms it creates along the way.
For example, the following is safe and will not consume arbitrary amounts of memory:
let untrusted_data = "large amounts of text ..."; for token in untrusted_data.split_whitespace() { let atom = Atom::from(token); // interns the string if atom == Atom::from("keyword") { // handle that keyword } else if atom == Atom::from("another_keyword") { // handle that keyword } else { println!("unknown keyword"); } } // atom is dropped here, so it is not kept around in memory
Methods
impl<Static: StaticAtomSet> Atom<Static>
[src]
impl<Static: StaticAtomSet> Atom<Static>
[src]
pub fn to_ascii_uppercase(&self) -> Self
[src]
Like to_ascii_uppercase
.
pub fn to_ascii_lowercase(&self) -> Self
[src]
Like to_ascii_lowercase
.
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
[src]
Like eq_ignore_ascii_case
.
pub fn eq_str_ignore_ascii_case(&self, other: &str) -> bool
[src]
Like eq_ignore_ascii_case
, but takes an unhashed string as other
.
Trait Implementations
impl<Static> Drop for Atom<Static>
[src]
impl<Static: StaticAtomSet> AsRef<str> for Atom<Static>
[src]
impl<'a, Static: StaticAtomSet> From<Cow<'a, str>> for Atom<Static>
[src]
impl<'a, Static: StaticAtomSet> From<&'a Atom<Static>> for Atom<Static>
[src]
impl<'a, Static: StaticAtomSet> From<&'a str> for Atom<Static>
[src]
impl<Static: StaticAtomSet> From<String> for Atom<Static>
[src]
impl<Static: StaticAtomSet> Clone for Atom<Static>
[src]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<Static: StaticAtomSet> Default for Atom<Static>
[src]
impl<Static: Eq> Eq for Atom<Static>
[src]
impl<Static: StaticAtomSet> Ord for Atom<Static>
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
1.21.0[src]
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<Static: PartialEq> PartialEq<Atom<Static>> for Atom<Static>
[src]
impl<Static: StaticAtomSet> PartialEq<str> for Atom<Static>
[src]
impl<Static: StaticAtomSet> PartialEq<Atom<Static>> for str
[src]
fn eq(&self, other: &Atom<Static>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<Static: StaticAtomSet> PartialEq<String> for Atom<Static>
[src]
impl<Static: StaticAtomSet> PartialOrd<Atom<Static>> for Atom<Static>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<Static: StaticAtomSet> Display for Atom<Static>
[src]
impl<Static: StaticAtomSet> Debug for Atom<Static>
[src]
impl<Static: StaticAtomSet> Deref for Atom<Static>
[src]
impl<Static: StaticAtomSet> Hash for Atom<Static>
[src]
fn hash<H>(&self, state: &mut H) where
H: Hasher,
[src]
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<Static: StaticAtomSet> Serialize for Atom<Static>
[src]
impl<'a, Static: StaticAtomSet> Deserialize<'a> for Atom<Static>
[src]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'a>,
[src]
D: Deserializer<'a>,
impl<Static: StaticAtomSet> PrecomputedHash for Atom<Static>
[src]
fn precomputed_hash(&self) -> u32
[src]
Auto Trait Implementations
impl<Static> Send for Atom<Static> where
Static: Send,
Static: Send,
impl<Static> Sync for Atom<Static> where
Static: Sync,
Static: Sync,
impl<Static> Unpin for Atom<Static> where
Static: Unpin,
Static: Unpin,
impl<Static> UnwindSafe for Atom<Static> where
Static: UnwindSafe,
Static: UnwindSafe,
impl<Static> RefUnwindSafe for Atom<Static> where
Static: RefUnwindSafe,
Static: RefUnwindSafe,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,