global_paths/
global-paths.rs

1use std::borrow::Cow;
2use std::path::Path;
3
4use interner::global::{GlobalPath, PathPool, StaticPooledPath};
5
6static PATH_POOL: PathPool = PathPool::new();
7// Because there is no way to get a Path in a const context, we have to use the
8// lazy option, which is a little more verbose due to it accepting both owned or
9static STATIC_PATH: StaticPooledPath = PATH_POOL.get_static_with(|| Cow::Borrowed(Path::new("a")));
10
11fn main() {
12    // Get a path from the static instance. This will keep the pooled path
13    // alive for the duration of the process.
14    STATIC_PATH.get();
15    // Request the same path directly from the pool.
16    let a_again = PATH_POOL.get(Path::new("a"));
17
18    // The two instances are pointing to the same instance.
19    assert!(GlobalPath::ptr_eq(&*STATIC_PATH, &a_again));
20
21    // Verify the pool still contains "a" even after dropping our local
22    // instances. This is due to STATIC_PATH still holding a reference.
23    drop(a_again);
24    let pooled: Vec<GlobalPath> = PATH_POOL.pooled();
25    assert_eq!(pooled.len(), 1);
26    assert_eq!(pooled[0], Path::new("a"));
27}
28
29#[test]
30fn runs() {
31    main();
32}