Skip to main content

buffer_core/
buffer.rs

1use std::sync::Arc;
2
3use core_types::BufferId;
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub enum BufferKind {
7    Owned,
8    Shared,
9}
10
11#[derive(Clone, Debug)]
12pub struct Buffer {
13    pub id: BufferId,
14    pub kind: BufferKind,
15    bytes: Arc<[u8]>,
16}
17
18impl Buffer {
19    pub fn from_vec(id: BufferId, data: Vec<u8>) -> Self {
20        Self {
21            id,
22            kind: BufferKind::Owned,
23            bytes: Arc::from(data.into_boxed_slice()),
24        }
25    }
26
27    pub fn len(&self) -> usize {
28        self.bytes.len()
29    }
30
31    pub fn is_empty(&self) -> bool {
32        self.bytes.is_empty()
33    }
34
35    pub fn as_slice(&self) -> &[u8] {
36        &self.bytes
37    }
38}