Phase 9.75-B: Convert ArrayBox, MapBox, BufferBox from Arc<Mutex> to RwLock
Co-authored-by: moe-charm <217100418+moe-charm@users.noreply.github.com>
This commit is contained in:
@ -31,31 +31,30 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, BoxCore, BoxBase};
|
||||
use crate::boxes::array::ArrayBox;
|
||||
use std::any::Any;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::RwLock;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BufferBox {
|
||||
data: Arc<Mutex<Vec<u8>>>,
|
||||
data: RwLock<Vec<u8>>,
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl BufferBox {
|
||||
pub fn new() -> Self {
|
||||
BufferBox {
|
||||
data: Arc::new(Mutex::new(Vec::new())),
|
||||
data: RwLock::new(Vec::new()),
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Rust向けヘルパー: バッファ長をusizeで取得(テスト用)
|
||||
pub fn len(&self) -> usize {
|
||||
self.data.lock().unwrap().len()
|
||||
self.data.read().unwrap().len()
|
||||
}
|
||||
|
||||
pub fn from_vec(data: Vec<u8>) -> Self {
|
||||
BufferBox {
|
||||
data: Arc::new(Mutex::new(data)),
|
||||
data: RwLock::new(data),
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -64,8 +63,8 @@ impl BufferBox {
|
||||
pub fn write(&self, data: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
// ArrayBoxから変換 - use crate::boxes::array::ArrayBox directly
|
||||
if let Some(array_box) = data.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
let mut buffer = self.data.lock().unwrap();
|
||||
let items = array_box.items.lock().unwrap();
|
||||
let mut buffer = self.data.write().unwrap();
|
||||
let items = array_box.items.read().unwrap();
|
||||
for item in items.iter() {
|
||||
if let Some(int_box) = item.as_any().downcast_ref::<IntegerBox>() {
|
||||
if int_box.value >= 0 && int_box.value <= 255 {
|
||||
@ -82,7 +81,7 @@ impl BufferBox {
|
||||
|
||||
/// すべてのデータを読み取る
|
||||
pub fn readAll(&self) -> Box<dyn NyashBox> {
|
||||
let buffer = self.data.lock().unwrap();
|
||||
let buffer = self.data.read().unwrap();
|
||||
let array = ArrayBox::new();
|
||||
for &byte in buffer.iter() {
|
||||
array.push(Box::new(IntegerBox::new(byte as i64)));
|
||||
@ -93,7 +92,7 @@ impl BufferBox {
|
||||
/// 指定バイト数読み取る
|
||||
pub fn read(&self, count: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let Some(count_int) = count.as_any().downcast_ref::<IntegerBox>() {
|
||||
let mut buffer = self.data.lock().unwrap();
|
||||
let mut buffer = self.data.write().unwrap();
|
||||
let count = count_int.value.min(buffer.len() as i64) as usize;
|
||||
let array = ArrayBox::new();
|
||||
|
||||
@ -110,20 +109,20 @@ impl BufferBox {
|
||||
|
||||
/// バッファをクリア
|
||||
pub fn clear(&self) -> Box<dyn NyashBox> {
|
||||
self.data.lock().unwrap().clear();
|
||||
self.data.write().unwrap().clear();
|
||||
Box::new(StringBox::new("ok"))
|
||||
}
|
||||
|
||||
/// データサイズを取得
|
||||
pub fn length(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(IntegerBox::new(self.data.lock().unwrap().len() as i64))
|
||||
Box::new(IntegerBox::new(self.data.read().unwrap().len() as i64))
|
||||
}
|
||||
|
||||
/// 他のBufferBoxを追加
|
||||
pub fn append(&self, other: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let Some(other_buffer) = other.as_any().downcast_ref::<BufferBox>() {
|
||||
let mut self_data = self.data.lock().unwrap();
|
||||
let other_data = other_buffer.data.lock().unwrap();
|
||||
let mut self_data = self.data.write().unwrap();
|
||||
let other_data = other_buffer.data.read().unwrap();
|
||||
self_data.extend_from_slice(&other_data);
|
||||
Box::new(IntegerBox::new(self_data.len() as i64))
|
||||
} else {
|
||||
@ -137,7 +136,7 @@ impl BufferBox {
|
||||
start.as_any().downcast_ref::<IntegerBox>(),
|
||||
end.as_any().downcast_ref::<IntegerBox>()
|
||||
) {
|
||||
let data = self.data.lock().unwrap();
|
||||
let data = self.data.read().unwrap();
|
||||
let start = (start_int.value as usize).min(data.len());
|
||||
let end = (end_int.value as usize).min(data.len());
|
||||
|
||||
@ -153,6 +152,14 @@ impl BufferBox {
|
||||
}
|
||||
}
|
||||
|
||||
// Clone implementation for BufferBox (needed since RwLock doesn't auto-derive Clone)
|
||||
impl Clone for BufferBox {
|
||||
fn clone(&self) -> Self {
|
||||
let data = self.data.read().unwrap();
|
||||
BufferBox::from_vec(data.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for BufferBox {
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
@ -163,7 +170,7 @@ impl BoxCore for BufferBox {
|
||||
}
|
||||
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let data = self.data.lock().unwrap();
|
||||
let data = self.data.read().unwrap();
|
||||
write!(f, "BufferBox({} bytes)", data.len())
|
||||
}
|
||||
|
||||
@ -188,7 +195,7 @@ impl NyashBox for BufferBox {
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
let data = self.data.lock().unwrap();
|
||||
let data = self.data.read().unwrap();
|
||||
StringBox::new(format!("BufferBox({} bytes)", data.len()))
|
||||
}
|
||||
|
||||
@ -200,9 +207,9 @@ impl NyashBox for BufferBox {
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_buffer) = other.as_any().downcast_ref::<BufferBox>() {
|
||||
// Arc<Mutex>の内容を比較
|
||||
let self_data = self.data.lock().unwrap();
|
||||
let other_data = other_buffer.data.lock().unwrap();
|
||||
// RwLock内容を比較
|
||||
let self_data = self.data.read().unwrap();
|
||||
let other_data = other_buffer.data.read().unwrap();
|
||||
BoolBox::new(*self_data == *other_data)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
|
||||
Reference in New Issue
Block a user