🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一: - BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現 - FileBox: Arc<Mutex<File>>でファイルハンドル管理 - JSONBox: Arc<Mutex<Value>>でJSON値を保持 - HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理 - StreamBox: Arc<Mutex>でストリームバッファと位置を管理 - RegexBox: Arc<Regex>で軽量ラッパー実装 各Boxに実用的なメソッドも追加: - BufferBox: write, read, readAll, clear, length, append, slice - FileBox: read, write, exists, delete, copy - JSONBox: parse, stringify, get, set, has, keys - HttpClientBox: get, post, put, delete, request - StreamBox: write, read, position, length, reset - RegexBox: test, find, findAll, replace, split interpreterに新Box用のメソッド実行を追加: - data_methods.rs: BufferBox, JSONBox, RegexBox - network_methods.rs: HttpClientBox, StreamBox これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました! 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -2,14 +2,16 @@
|
||||
// Nyashの箱システムによるストリーミング処理を提供します。
|
||||
// 参考: 既存Boxの設計思想
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox};
|
||||
use crate::boxes::buffer::BufferBox;
|
||||
use std::any::Any;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::io::{Read, Write, Result};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NyashStreamBox {
|
||||
pub buffer: Vec<u8>,
|
||||
pub position: usize,
|
||||
buffer: Arc<Mutex<Vec<u8>>>,
|
||||
position: Arc<Mutex<usize>>,
|
||||
id: u64,
|
||||
}
|
||||
|
||||
@ -21,8 +23,8 @@ impl NyashStreamBox {
|
||||
COUNTER
|
||||
};
|
||||
NyashStreamBox {
|
||||
buffer: Vec::new(),
|
||||
position: 0,
|
||||
buffer: Arc::new(Mutex::new(Vec::new())),
|
||||
position: Arc::new(Mutex::new(0)),
|
||||
id,
|
||||
}
|
||||
}
|
||||
@ -34,54 +36,109 @@ impl NyashStreamBox {
|
||||
COUNTER
|
||||
};
|
||||
NyashStreamBox {
|
||||
buffer: data,
|
||||
position: 0,
|
||||
buffer: Arc::new(Mutex::new(data)),
|
||||
position: Arc::new(Mutex::new(0)),
|
||||
id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let available = self.buffer.len().saturating_sub(self.position);
|
||||
pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
|
||||
let buffer = self.buffer.lock().unwrap();
|
||||
let mut position = self.position.lock().unwrap();
|
||||
|
||||
let available = buffer.len().saturating_sub(*position);
|
||||
let to_read = buf.len().min(available);
|
||||
|
||||
if to_read == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
buf[..to_read].copy_from_slice(&self.buffer[self.position..self.position + to_read]);
|
||||
self.position += to_read;
|
||||
buf[..to_read].copy_from_slice(&buffer[*position..*position + to_read]);
|
||||
*position += to_read;
|
||||
Ok(to_read)
|
||||
}
|
||||
|
||||
pub fn write(&mut self, buf: &[u8]) -> Result<()> {
|
||||
self.buffer.extend_from_slice(buf);
|
||||
pub fn write(&self, buf: &[u8]) -> Result<()> {
|
||||
let mut buffer = self.buffer.lock().unwrap();
|
||||
buffer.extend_from_slice(buf);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.buffer.len()
|
||||
self.buffer.lock().unwrap().len()
|
||||
}
|
||||
|
||||
pub fn position(&self) -> usize {
|
||||
self.position
|
||||
*self.position.lock().unwrap()
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.position = 0;
|
||||
pub fn reset(&self) {
|
||||
*self.position.lock().unwrap() = 0;
|
||||
}
|
||||
|
||||
/// ストリームに書き込み
|
||||
pub fn stream_write(&self, data: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
// BufferBoxから変換
|
||||
if let Some(buffer_box) = data.as_any().downcast_ref::<BufferBox>() {
|
||||
let buffer_data = buffer_box.data.lock().unwrap();
|
||||
match self.write(&buffer_data) {
|
||||
Ok(()) => Box::new(StringBox::new("ok")),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error writing to stream: {}", e))),
|
||||
}
|
||||
} else if let Some(string_box) = data.as_any().downcast_ref::<StringBox>() {
|
||||
match self.write(string_box.value.as_bytes()) {
|
||||
Ok(()) => Box::new(StringBox::new("ok")),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error writing to stream: {}", e))),
|
||||
}
|
||||
} else {
|
||||
Box::new(StringBox::new("Error: write() requires BufferBox or StringBox"))
|
||||
}
|
||||
}
|
||||
|
||||
/// ストリームから読み込み
|
||||
pub fn stream_read(&self, count: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let Some(count_int) = count.as_any().downcast_ref::<IntegerBox>() {
|
||||
let count_val = count_int.value as usize;
|
||||
let mut buf = vec![0u8; count_val];
|
||||
|
||||
match self.read(&mut buf) {
|
||||
Ok(bytes_read) => {
|
||||
buf.truncate(bytes_read);
|
||||
Box::new(BufferBox::from_vec(buf))
|
||||
},
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error reading from stream: {}", e))),
|
||||
}
|
||||
} else {
|
||||
Box::new(StringBox::new("Error: read() requires integer count"))
|
||||
}
|
||||
}
|
||||
|
||||
/// 現在位置を取得
|
||||
pub fn get_position(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(IntegerBox::new(self.position() as i64))
|
||||
}
|
||||
|
||||
/// バッファサイズを取得
|
||||
pub fn get_length(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(IntegerBox::new(self.len() as i64))
|
||||
}
|
||||
|
||||
/// ストリームをリセット
|
||||
pub fn stream_reset(&self) -> Box<dyn NyashBox> {
|
||||
self.reset();
|
||||
Box::new(StringBox::new("ok"))
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for NyashStreamBox {
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(NyashStreamBox {
|
||||
buffer: self.buffer.clone(),
|
||||
position: self.position,
|
||||
id: self.id,
|
||||
})
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("NyashStreamBox({} bytes, pos: {})", self.buffer.len(), self.position))
|
||||
let buffer = self.buffer.lock().unwrap();
|
||||
let position = self.position.lock().unwrap();
|
||||
StringBox::new(format!("NyashStreamBox({} bytes, pos: {})", buffer.len(), *position))
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
@ -98,27 +155,16 @@ impl NyashBox for NyashStreamBox {
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_stream) = other.as_any().downcast_ref::<NyashStreamBox>() {
|
||||
BoolBox::new(self.buffer == other_stream.buffer && self.position == other_stream.position)
|
||||
let self_buffer = self.buffer.lock().unwrap();
|
||||
let self_position = self.position.lock().unwrap();
|
||||
let other_buffer = other_stream.buffer.lock().unwrap();
|
||||
let other_position = other_stream.position.lock().unwrap();
|
||||
BoolBox::new(*self_buffer == *other_buffer && *self_position == *other_position)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep the original generic StreamBox for compatibility
|
||||
pub struct StreamBox<R: Read, W: Write> {
|
||||
pub reader: R,
|
||||
pub writer: W,
|
||||
}
|
||||
|
||||
impl<R: Read, W: Write> StreamBox<R, W> {
|
||||
pub fn new(reader: R, writer: W) -> Self {
|
||||
StreamBox { reader, writer }
|
||||
}
|
||||
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
self.reader.read(buf)
|
||||
}
|
||||
pub fn write(&mut self, buf: &[u8]) -> Result<()> {
|
||||
self.writer.write_all(buf)
|
||||
}
|
||||
}
|
||||
// Export NyashStreamBox as StreamBox for consistency
|
||||
pub type StreamBox = NyashStreamBox;
|
||||
|
||||
Reference in New Issue
Block a user