use crate::buffer::{BTRFS_MAX_LEVEL, ExtentBuffer};
pub struct BtrfsPath {
pub nodes: [Option<ExtentBuffer>; BTRFS_MAX_LEVEL],
pub slots: [usize; BTRFS_MAX_LEVEL],
pub lowest_level: u8,
}
impl BtrfsPath {
#[must_use]
pub fn new() -> Self {
Self {
nodes: Default::default(),
slots: [0; BTRFS_MAX_LEVEL],
lowest_level: 0,
}
}
pub fn release(&mut self) {
for node in &mut self.nodes {
*node = None;
}
self.slots = [0; BTRFS_MAX_LEVEL];
}
#[must_use]
pub fn leaf(&self) -> Option<&ExtentBuffer> {
self.nodes[0].as_ref()
}
#[must_use]
pub fn leaf_slot(&self) -> usize {
self.slots[0]
}
}
impl Default for BtrfsPath {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_path_is_empty() {
let path = BtrfsPath::new();
assert!(path.leaf().is_none());
assert_eq!(path.leaf_slot(), 0);
assert_eq!(path.lowest_level, 0);
}
#[test]
fn release_clears_all() {
let mut path = BtrfsPath::new();
path.slots[0] = 5;
path.nodes[0] = Some(ExtentBuffer::new_zeroed(4096, 0));
path.release();
assert!(path.leaf().is_none());
assert_eq!(path.slots[0], 0);
}
}