redoubt_buffer/
traits.rs

1// Copyright (c) 2025-2026 Federico Hoerth <memparanoid@gmail.com>
2// SPDX-License-Identifier: GPL-3.0-only
3// See LICENSE in the repository root for full license text.
4
5use crate::error::BufferError;
6
7/// Trait for buffer types that provide temporary access to their contents.
8pub trait Buffer: Send + Sync + core::fmt::Debug {
9    /// Opens the buffer for read-only access, executing the provided closure.
10    fn open(
11        &mut self,
12        f: &mut dyn FnMut(&[u8]) -> Result<(), BufferError>,
13    ) -> Result<(), BufferError>;
14
15    /// Opens the buffer for mutable access, executing the provided closure.
16    fn open_mut(
17        &mut self,
18        f: &mut dyn FnMut(&mut [u8]) -> Result<(), BufferError>,
19    ) -> Result<(), BufferError>;
20
21    /// Returns the length of the buffer in bytes.
22    fn len(&self) -> usize;
23
24    /// Returns true if the buffer has zero length.
25    fn is_empty(&self) -> bool {
26        self.len() == 0
27    }
28}