1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! # filock
//!
//! Thread-safe, closure-based file locking for Rust using `flock(2)`.
//!
//! `filock` provides a simple API for concurrent file access where locks are
//! held for exactly the duration of a closure. This eliminates lock leaks and
//! makes concurrent file access safe and predictable.
//!
//! ## Features
//!
//! - **Closure-based API** — locks are held for exactly the closure's duration
//! - **Thread-safe** — `FileStore` is `Send + Sync`
//! - **Deadlock-free** — by construction within the same process
//! - **Condvar-based waiting** — no spin-waiting, efficient blocking
//! - **Non-blocking try-locks** — `try_with_read` / `try_with_write`
//! - **Timeout support** — `with_read_timeout` / `with_write_timeout`
//! - **Minimal dependencies** — only `libc`
//! - **Advisory locking** — works with other processes using `flock(2)`
//!
//! ## Quick Start
//!
//! ```no_run
//! use filock::FileStore;
//!
//! let store = FileStore::new("/tmp/myapp").unwrap();
//!
//! // Write data
//! store.with_write("data.json", |file| {
//! use std::io::Write;
//! writeln!(file, "{{\"key\": \"value\"}}")?;
//! Ok(())
//! }).unwrap();
//!
//! // Read data
//! let content: String = store.with_read("data.json", |file| {
//! use std::io::Read;
//! let mut buf = String::new();
//! let mut reader = std::io::BufReader::new(file);
//! reader.read_to_string(&mut buf)?;
//! Ok(buf)
//! }).unwrap();
//! ```
pub use ;
pub use FileStore;
/// Version of this crate.
pub const VERSION: &str = env!;