1 unstable release

0.0.2 Apr 3, 2024

#230 in Memory management

Download history 142/week @ 2024-03-31 144/week @ 2024-04-07 12/week @ 2024-04-14 18/week @ 2024-04-21

316 downloads per month

MIT/Apache

115KB
2.5K SLoC

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);

Dependencies

~3–10MB
~70K SLoC