Struct string_cache::Atom[][src]

pub struct Atom<Static> { /* fields omitted */ }
Expand description

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

Implementations

Get the hash of the string as it is stored in the set.

Like eq_ignore_ascii_case, but takes an unhashed string as other.

Trait Implementations

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Return the precomputed hash for this item.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.