liteboxfs 0.2.0

A modern POSIX filesystem in a SQLite database
Documentation
use std::collections::HashSet;

/// A table for allocating `u64` values.
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct IdPool {
    /// The highest used ID value (the high water mark).
    highest: u64,

    /// A set of unused ID values below the high water mark.
    unused: HashSet<u64>,
}

impl IdPool {
    /// Return a new empty `IdPool`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the next unused ID from the table.
    pub fn next(&mut self) -> u64 {
        match self.unused.iter().next().copied() {
            Some(id) => {
                self.unused.remove(&id);
                id
            }
            None => {
                self.highest += 1;
                self.highest
            }
        }
    }

    /// Return whether the given `id` is in the table.
    pub fn contains(&self, id: u64) -> bool {
        id <= self.highest && !self.unused.contains(&id)
    }

    /// Return the given `id` back to the table.
    ///
    /// This returns `true` if the value was returned or `false` if it was unused.
    pub fn recycle(&mut self, id: u64) -> bool {
        if !self.contains(id) {
            return false;
        }
        self.unused.insert(id);
        true
    }
}