use core_types::BufferId;
use memmap2::Mmap;

mod config;
mod header;
mod rate_limiter;
mod reader;
mod segment_io;
mod writer;

pub use config::{ShmRateLimitConfig, ShmTransportConfig};
use rate_limiter::TokenBucket;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SharedMemoryBufferRef {
    pub buffer_id: BufferId,
    pub offset: usize,
    pub len: usize,
}

#[derive(Debug)]
pub struct SharedMemoryLease {
    mmap: Mmap,
    offset: usize,
    len: usize,
}

impl SharedMemoryLease {
    pub fn as_slice(&self) -> &[u8] {
        &self.mmap[self.offset..self.offset + self.len]
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }
}

pub struct SharedMemoryWriter {
    cfg: ShmTransportConfig,
    next_buffer_id: u64,
    rate_limiter: Option<TokenBucket>,
}

pub struct SharedMemoryReader {
    cfg: ShmTransportConfig,
}
