blockset-lib 0.7.0

BLOCKSET internal library
Documentation
#![cfg(test)]
use std::{collections::BTreeMap, io};

use crate::uint::u224::U224;

use super::{node_id::ForestNodeId, Forest};

pub type MemForest = [BTreeMap<U224, Vec<u8>>; 2];

impl Forest for &mut MemForest {
    fn has_block(&self, id: &ForestNodeId) -> bool {
        self[id.node_type as usize].contains_key(&id.hash)
    }

    fn get_block(&self, id: &ForestNodeId) -> io::Result<Vec<u8>> {
        self[id.node_type as usize]
            .get(&id.hash)
            .cloned()
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "file not found"))
    }

    fn set_block(&mut self, id: &ForestNodeId, value: impl Iterator<Item = u8>) -> io::Result<()> {
        self[id.node_type as usize].insert(id.hash, value.collect());
        Ok(())
    }
}