🔥 feat: Arc<Mutex> Revolution完全達成 - 全Box型統一実装

## 🎯 主要な変更
- ArrayBoxをArc<Mutex>パターンで完全再実装
- 全メソッドが`&self`で統一(push, pop, get, set等)
- Box<dyn NyashBox>引数対応でNyashから使用可能に

##  修正内容
- ArrayBox: 完全なArc<Mutex>実装に置き換え
- BufferBox: ArrayBoxとの連携修正、デバッグ出力削除
- StringBox: 新しいArrayBoxインポートに修正
- RandomBox: 新しいArrayBoxインポートに修正
- box_trait.rs: 古いArrayBox定義を削除しre-export追加

## 🧪 テスト追加
- test_buffer_box.nyash: BufferBox動作確認
- test_random_box.nyash: RandomBox動作確認
- test_new_boxes.nyash: 包括的Box機能テスト修正

##  確認済み動作
- ArrayBox: push/pop/get/set/join等全メソッド
- BufferBox: write/readAll/length
- RandomBox: choice/shuffle等配列操作
- JSONBox/RegexBox: 既に正しく実装済み

🚀 全Box型がArc<Mutex>パターンで統一完了!

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-08-10 16:46:39 +09:00
parent dff165db8d
commit 0c786a8ac6
8 changed files with 146 additions and 182 deletions

View File

@ -26,6 +26,19 @@ impl ArrayBox {
}
}
/// 要素を持つArrayBoxを作成
pub fn new_with_elements(elements: Vec<Box<dyn NyashBox>>) -> Self {
static mut COUNTER: u64 = 0;
let id = unsafe {
COUNTER += 1;
COUNTER
};
ArrayBox {
items: Arc::new(Mutex::new(elements)),
id,
}
}
/// 要素を追加
pub fn push(&self, item: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
self.items.lock().unwrap().push(item);
@ -46,36 +59,55 @@ impl ArrayBox {
}
/// インデックスで要素を取得
pub fn get(&self, index: usize) -> Option<Box<dyn NyashBox>> {
self.items.lock().unwrap().get(index).map(|item| item.clone_box())
pub fn get(&self, index: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
let idx = idx_box.value as usize;
let items = self.items.lock().unwrap();
match items.get(idx) {
Some(item) => item.clone_box(),
None => Box::new(crate::boxes::null_box::NullBox::new()),
}
} else {
Box::new(StringBox::new("Error: get() requires integer index"))
}
}
/// インデックスで要素を設定
pub fn set(&self, index: usize, value: Box<dyn NyashBox>) -> Result<(), String> {
let mut items = self.items.lock().unwrap();
if index < items.len() {
items[index] = value;
Ok(())
pub fn set(&self, index: Box<dyn NyashBox>, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
let idx = idx_box.value as usize;
let mut items = self.items.lock().unwrap();
if idx < items.len() {
items[idx] = value;
Box::new(StringBox::new("ok"))
} else {
Box::new(StringBox::new("Error: index out of bounds"))
}
} else {
Err(format!("Index {} out of bounds", index))
Box::new(StringBox::new("Error: set() requires integer index"))
}
}
/// 要素を削除
pub fn remove(&self, index: usize) -> Option<Box<dyn NyashBox>> {
let mut items = self.items.lock().unwrap();
if index < items.len() {
Some(items.remove(index))
pub fn remove(&self, index: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
let idx = idx_box.value as usize;
let mut items = self.items.lock().unwrap();
if idx < items.len() {
items.remove(idx)
} else {
Box::new(crate::boxes::null_box::NullBox::new())
}
} else {
None
Box::new(StringBox::new("Error: remove() requires integer index"))
}
}
/// 指定された値のインデックスを検索
pub fn indexOf(&self, value: &dyn NyashBox) -> Box<dyn NyashBox> {
pub fn indexOf(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let items = self.items.lock().unwrap();
for (i, item) in items.iter().enumerate() {
if item.equals(value).value {
if item.equals(value.as_ref()).value {
return Box::new(IntegerBox::new(i as i64));
}
}
@ -83,10 +115,10 @@ impl ArrayBox {
}
/// 指定された値が含まれているか確認
pub fn contains(&self, value: &dyn NyashBox) -> Box<dyn NyashBox> {
pub fn contains(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let items = self.items.lock().unwrap();
for item in items.iter() {
if item.equals(value).value {
if item.equals(value.as_ref()).value {
return Box::new(BoolBox::new(true));
}
}
@ -100,12 +132,17 @@ impl ArrayBox {
}
/// 文字列結合
pub fn join(&self, delimiter: &str) -> Box<dyn NyashBox> {
let items = self.items.lock().unwrap();
let strings: Vec<String> = items.iter()
.map(|item| item.to_string_box().value)
.collect();
Box::new(StringBox::new(&strings.join(delimiter)))
pub fn join(&self, delimiter: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(sep_box) = delimiter.as_any().downcast_ref::<StringBox>() {
let items = self.items.lock().unwrap();
let parts: Vec<String> = items
.iter()
.map(|item| item.to_string_box().value)
.collect();
Box::new(StringBox::new(&parts.join(&sep_box.value)))
} else {
Box::new(StringBox::new("Error: join() requires string separator"))
}
}
}

View File

@ -67,8 +67,8 @@ impl BufferBox {
/// データを書き込む
pub fn write(&self, data: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
// ArrayBoxから変換
if let Some(array_box) = data.as_any().downcast_ref::<ArrayBox>() {
// 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();
for item in items.iter() {
@ -80,7 +80,8 @@ impl BufferBox {
}
Box::new(IntegerBox::new(buffer.len() as i64))
} else {
Box::new(StringBox::new("Error: write() requires ArrayBox of integers"))
let type_name = data.type_name();
Box::new(StringBox::new(&format!("Error: write() requires ArrayBox of integers, got {}", type_name)))
}
}

View File

@ -67,7 +67,8 @@
* - 大きな配列のshuffleは処理時間が長い場合あり
*/
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox, ArrayBox};
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox};
use crate::boxes::array::ArrayBox;
use crate::boxes::math_box::FloatBox;
use std::fmt::{Debug, Display};
use std::any::Any;
@ -159,10 +160,8 @@ impl RandomBox {
}
let index = self.next_random() % (length as u64);
match array_box.get(index as usize) {
Some(element) => element,
None => Box::new(StringBox::new("Error: index out of bounds")),
}
// 新しいArrayBox.get()は既にBox<dyn NyashBox>を返すので、直接使用
array_box.get(Box::new(IntegerBox::new(index as i64)))
} else {
Box::new(StringBox::new("Error: choice() requires array input"))
}
@ -181,7 +180,9 @@ impl RandomBox {
// 元の配列の要素を全て新しい配列にコピー
for i in 0..length {
if let Some(element) = array_box.get(i as usize) {
let element = array_box.get(Box::new(IntegerBox::new(i as i64)));
// NullBoxでなければ追加
if element.type_name() != "NullBox" {
shuffled.push(element);
}
}
@ -194,7 +195,9 @@ impl RandomBox {
while !remaining_indices.is_empty() {
let random_idx = (self.next_random() % remaining_indices.len() as u64) as usize;
let actual_idx = remaining_indices.remove(random_idx);
if let Some(element) = array_box.get(actual_idx) {
let element = array_box.get(Box::new(IntegerBox::new(actual_idx as i64)));
// NullBoxでなければ追加
if element.type_name() != "NullBox" {
result.push(element);
}
}

View File

@ -60,12 +60,16 @@ impl StringBox {
/// Split string by delimiter and return ArrayBox
pub fn split(&self, delimiter: &str) -> Box<dyn NyashBox> {
use crate::box_trait::ArrayBox;
use crate::boxes::array::ArrayBox;
let parts: Vec<String> = self.value.split(delimiter).map(|s| s.to_string()).collect();
let array_elements: Vec<Box<dyn NyashBox>> = parts.into_iter()
.map(|s| Box::new(StringBox::new(s)) as Box<dyn NyashBox>)
.collect();
Box::new(ArrayBox::new_with_elements(array_elements))
let result = ArrayBox::new();
for element in array_elements {
result.push(element);
}
Box::new(result)
}
/// Find substring and return position (or -1 if not found)
@ -117,9 +121,9 @@ impl StringBox {
/// Join array elements using this string as delimiter
pub fn join(&self, array_box: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
use crate::box_trait::ArrayBox;
use crate::boxes::array::ArrayBox;
if let Some(array) = array_box.as_any().downcast_ref::<ArrayBox>() {
let strings: Vec<String> = array.elements.lock().unwrap()
let strings: Vec<String> = array.items.lock().unwrap()
.iter()
.map(|element| element.to_string_box().value)
.collect();