Module cov::intern[][src]

String interning.

GCNO files contain a lot of repeated strings like the filenames and function names. In order to save time and memory comparing these strings, they will all be stored into the Interner class, and normal operations that does not involve the actual string content are done through the proxy Symbol handles.

use cov::Interner;

let mut interner = Interner::new();

// The interner can intern `Box<str>`s.
let symbol_1 = interner.intern("hello");
let symbol_2 = interner.intern("hello");

// Equal strings have equal symbols.
assert_eq!(symbol_1, symbol_2);

// Get back the string by indexing.
assert_eq!("hello", &interner[symbol_1]);

Serialization

A Symbol is just a plain integer, and will simply serialize to a number. To make it a write string, the Interner must be transmitted to the serializer. This is done via the with_interner() method when serializing.

extern crate serde_json;
extern crate cov;
use cov::{Interner, SerializeWithInterner};

let mut interner = Interner::new();
let a = interner.intern("one");
let b = interner.intern("two");
let c = interner.intern("three");
let value = vec![a, b, c, a];

// without the interner, the symbols will be serialized as numbers.
let serialized_without_interner = serde_json::to_string(&value)?;
assert_eq!(&serialized_without_interner, "[1,2,3,1]");

// use .with_interner() to serialize them into strings.
let serialized = serde_json::to_string(&value.with_interner(&interner))?;
assert_eq!(&serialized, r#"["one","two","three","one"]"#);

Deserialization

See deserializer::with_interner() for how to deserialize a string back to a Symbol.

Structs

Interner

The string interner.

Iter

Iterator of Interner.

Symbol

A handle to an interned string in an Interner.

WithInterner

Return type of SerializeWithInterner::with_interner().

Constants

UNKNOWN_SYMBOL

The symbol representing the string "<unknown>".

Traits

SerializeWithInterner

A data structure that may contain Symbols, which should be serialized as strings.