Crate refuse_pool

source ·
Expand description

Garbage-collected “interned” strings.

Interning is a process of making many equal things share the same underlying resource. This crate introduces two types that are powered by the Refuse garbage collector:

  • RootString: A Root<String>-like type that ensures all instances of the same exact byte sequence refer to the same allocation.
  • RefString: A Ref<String> type that is a reference to a RootString.
use refuse::CollectionGuard;
use refuse_pool::{RefString, RootString};

let a = RootString::from("a");
let a_again = RootString::from(String::from("a"));

// Both a and a_again point to the same underlying storage.
assert_eq!(a.root_count(), 2);
// Comparing two RootStrings is cheap.
assert_eq!(a, a_again);

// a_ref can be used to gain a reference to a string,
// but only until the string is unreachable.
let a_ref = a.downgrade();

let mut guard = CollectionGuard::acquire();
assert_eq!(a_ref.load(&guard), Some("a"));

drop(a);
drop(a_again);
guard.collect();
assert_eq!(a_ref.load(&guard), None);

Structs§

  • A weak reference to a garbage collected, interned string.
  • A “root” reference to a garbage collected, interned string.