Basics
You access the state registry through reader.states() in Rust or reader.states in Python. From there you can load, find, list, and count saved states
Loading a State
load reads a state file from disk and returns the State. When verify_state is true (the default), it also verifies the state file’s internal checksum and checks the tracked file’s size, mtime, and fingerprint. It raises an error if the state does not exist
Rust
let state = reader.states().load("job-1")?;
println!("position: {}", state.position);
Python
state = reader.states.load("job-1")
# or equivalently:
state = reader.states["job-1"]
print(f"position: {state.position}")
loadraisesError::NotFoundin Rust andKeyErrorin Python when the state does not exist
Finding a State
find works like load but returns None instead of raising an error. Unlike load, it catches all errors (not just missing states), so a corrupt or unreadable state file also returns None
Rust
if let Some(state) = reader.states().find("job-1") {
println!("found at byte {}", state.position);
} else {
println!("no saved state");
}
Python
state = reader.states.find("job-1")
if state is not None:
print(f"found at byte {state.position}")
else:
print("no saved state")
Checking Existence
exists returns a boolean indicating whether a state file is present on disk. It does not parse the file; it only checks that the path exists
Rust
if reader.states().exists("job-1") {
println!("state exists");
}
Python
if "job-1" in reader.states:
print("state exists")
Listing States
names returns a StateIterator that yields the name of every saved state
Rust
for name in reader.states().names()? {
println!("{}", name?);
}
Python
for name in reader.states:
print(name)
Counting States
Rust
let count = reader.states().count()?;
println!("{count} saved states");
Python
print(f"{len(reader.states)} saved states")
Loading All States
all loads every saved state at once
Rust
let states = reader.states().all()?;
for state in &states {
println!("{}: {} bytes", state.name, state.position);
}
Python
for state in reader.states.all():
print(f"{state.name}: {state.position} bytes")
Python Dunder Protocols
In Python, the StateRegistry supports standard container protocols:
| Operation | Python Syntax | Equivalent Method |
|---|---|---|
| Load | registry["name"] | registry.load("name") |
| Check existence | "name" in registry | registry.exists("name") |
| Delete | del registry["name"] | registry.delete("name") |
| Count | len(registry) | registry.count() (Rust only) |
| Iterate | for name in registry | registry.names() |