2025-08-10 02:45:57 +00:00
|
|
|
//! BufferBox 📊 - バイナリデータ処理
|
|
|
|
|
// Nyashの箱システムによるバイナリデータ処理を提供します。
|
|
|
|
|
// 参考: 既存Boxの設計思想
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2025-08-10 02:45:57 +00:00
|
|
|
pub struct BufferBox {
|
|
|
|
|
pub data: Vec<u8>,
|
2025-08-10 03:21:24 +00:00
|
|
|
id: u64,
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BufferBox {
|
|
|
|
|
pub fn new() -> Self {
|
2025-08-10 03:21:24 +00:00
|
|
|
static mut COUNTER: u64 = 0;
|
|
|
|
|
let id = unsafe {
|
|
|
|
|
COUNTER += 1;
|
|
|
|
|
COUNTER
|
|
|
|
|
};
|
|
|
|
|
BufferBox {
|
|
|
|
|
data: Vec::new(),
|
|
|
|
|
id,
|
|
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
pub fn from_vec(data: Vec<u8>) -> Self {
|
2025-08-10 03:21:24 +00:00
|
|
|
static mut COUNTER: u64 = 0;
|
|
|
|
|
let id = unsafe {
|
|
|
|
|
COUNTER += 1;
|
|
|
|
|
COUNTER
|
|
|
|
|
};
|
|
|
|
|
BufferBox { data, id }
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.data.len()
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
2025-08-10 02:45:57 +00:00
|
|
|
pub fn as_slice(&self) -> &[u8] {
|
|
|
|
|
&self.data
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
|
|
|
|
impl NyashBox for BufferBox {
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
Box::new(self.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
|
|
|
|
StringBox::new(format!("BufferBox({} bytes)", self.data.len()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
"BufferBox"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn box_id(&self) -> u64 {
|
|
|
|
|
self.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
|
|
|
|
if let Some(other_buffer) = other.as_any().downcast_ref::<BufferBox>() {
|
|
|
|
|
BoolBox::new(self.data == other_buffer.data)
|
|
|
|
|
} else {
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|