chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt

This commit is contained in:
Selfhosting Dev
2025-09-17 07:43:07 +09:00
parent fcf8ce1f3c
commit adbb0201a9
385 changed files with 35622 additions and 15004 deletions

View File

@ -2,39 +2,39 @@
// Nyashの箱システムによる配列・リスト操作を提供します。
// RwLockパターンで内部可変性を実現Phase 9.75-B Arc<Mutex>削除)
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, BoxCore, BoxBase};
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
use std::any::Any;
use std::sync::{Arc, RwLock};
use std::fmt::Display;
use std::sync::{Arc, RwLock};
pub struct ArrayBox {
pub items: Arc<RwLock<Vec<Box<dyn NyashBox>>>>, // Arc追加
pub items: Arc<RwLock<Vec<Box<dyn NyashBox>>>>, // Arc追加
base: BoxBase,
}
impl ArrayBox {
/// 新しいArrayBoxを作成
pub fn new() -> Self {
ArrayBox {
items: Arc::new(RwLock::new(Vec::new())), // Arc::new追加
ArrayBox {
items: Arc::new(RwLock::new(Vec::new())), // Arc::new追加
base: BoxBase::new(),
}
}
/// 要素を持つArrayBoxを作成
pub fn new_with_elements(elements: Vec<Box<dyn NyashBox>>) -> Self {
ArrayBox {
items: Arc::new(RwLock::new(elements)), // Arc::new追加
ArrayBox {
items: Arc::new(RwLock::new(elements)), // Arc::new追加
base: BoxBase::new(),
}
}
/// 要素を追加
pub fn push(&self, item: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
self.items.write().unwrap().push(item);
Box::new(StringBox::new("ok"))
}
/// 最後の要素を取り出す
pub fn pop(&self) -> Box<dyn NyashBox> {
match self.items.write().unwrap().pop() {
@ -42,7 +42,7 @@ impl ArrayBox {
None => Box::new(crate::boxes::null_box::NullBox::new()),
}
}
/// 要素数を取得
pub fn length(&self) -> Box<dyn NyashBox> {
Box::new(IntegerBox::new(self.items.read().unwrap().len() as i64))
@ -52,7 +52,7 @@ impl ArrayBox {
pub fn len(&self) -> usize {
self.items.read().unwrap().len()
}
/// インデックスで要素を取得
pub fn get(&self, index: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
@ -61,7 +61,11 @@ impl ArrayBox {
match items.get(idx) {
Some(item) => {
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
if item.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some() {
if item
.as_any()
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
.is_some()
{
return item.share_box();
}
item.clone_box()
@ -72,7 +76,7 @@ impl ArrayBox {
Box::new(StringBox::new("Error: get() requires integer index"))
}
}
/// インデックスで要素を設定
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>() {
@ -92,7 +96,7 @@ impl ArrayBox {
Box::new(StringBox::new("Error: set() requires integer index"))
}
}
/// 要素を削除
pub fn remove(&self, index: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(idx_box) = index.as_any().downcast_ref::<IntegerBox>() {
@ -107,7 +111,7 @@ impl ArrayBox {
Box::new(StringBox::new("Error: remove() requires integer index"))
}
}
/// 指定された値のインデックスを検索
pub fn indexOf(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let items = self.items.read().unwrap();
@ -118,7 +122,7 @@ impl ArrayBox {
}
Box::new(IntegerBox::new(-1))
}
/// 指定された値が含まれているか確認
pub fn contains(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let items = self.items.read().unwrap();
@ -129,13 +133,13 @@ impl ArrayBox {
}
Box::new(BoolBox::new(false))
}
/// 配列を空にする
pub fn clear(&self) -> Box<dyn NyashBox> {
self.items.write().unwrap().clear();
Box::new(StringBox::new("ok"))
}
/// 文字列結合
pub fn join(&self, delimiter: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(sep_box) = delimiter.as_any().downcast_ref::<StringBox>() {
@ -149,66 +153,78 @@ impl ArrayBox {
Box::new(StringBox::new("Error: join() requires string separator"))
}
}
/// 配列をソート(昇順)
pub fn sort(&self) -> Box<dyn NyashBox> {
let mut items = self.items.write().unwrap();
// Numeric values first, then string values
items.sort_by(|a, b| {
use std::cmp::Ordering;
// Try to compare as numbers first
if let (Some(a_int), Some(b_int)) = (
a.as_any().downcast_ref::<IntegerBox>(),
b.as_any().downcast_ref::<IntegerBox>()
b.as_any().downcast_ref::<IntegerBox>(),
) {
return a_int.value.cmp(&b_int.value);
}
// Try FloatBox comparison
if let (Some(a_float), Some(b_float)) = (
a.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>(),
b.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>()
a.as_any()
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
b.as_any()
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
) {
return a_float.value.partial_cmp(&b_float.value).unwrap_or(Ordering::Equal);
return a_float
.value
.partial_cmp(&b_float.value)
.unwrap_or(Ordering::Equal);
}
// Mixed numeric types
if let (Some(a_int), Some(b_float)) = (
a.as_any().downcast_ref::<IntegerBox>(),
b.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>()
b.as_any()
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
) {
return (a_int.value as f64).partial_cmp(&b_float.value).unwrap_or(Ordering::Equal);
return (a_int.value as f64)
.partial_cmp(&b_float.value)
.unwrap_or(Ordering::Equal);
}
if let (Some(a_float), Some(b_int)) = (
a.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>(),
b.as_any().downcast_ref::<IntegerBox>()
a.as_any()
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
b.as_any().downcast_ref::<IntegerBox>(),
) {
return a_float.value.partial_cmp(&(b_int.value as f64)).unwrap_or(Ordering::Equal);
return a_float
.value
.partial_cmp(&(b_int.value as f64))
.unwrap_or(Ordering::Equal);
}
// Fall back to string comparison
let a_str = a.to_string_box().value;
let b_str = b.to_string_box().value;
a_str.cmp(&b_str)
});
Box::new(StringBox::new("ok"))
}
/// 配列を反転
pub fn reverse(&self) -> Box<dyn NyashBox> {
let mut items = self.items.write().unwrap();
items.reverse();
Box::new(StringBox::new("ok"))
}
/// 部分配列を取得
pub fn slice(&self, start: Box<dyn NyashBox>, end: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let items = self.items.read().unwrap();
// Extract start and end indices
let start_idx = if let Some(start_int) = start.as_any().downcast_ref::<IntegerBox>() {
if start_int.value < 0 {
@ -217,9 +233,11 @@ impl ArrayBox {
start_int.value as usize
}
} else {
return Box::new(StringBox::new("Error: slice() start index must be an integer"));
return Box::new(StringBox::new(
"Error: slice() start index must be an integer",
));
};
let end_idx = if let Some(end_int) = end.as_any().downcast_ref::<IntegerBox>() {
if end_int.value < 0 {
items.len()
@ -227,26 +245,32 @@ impl ArrayBox {
(end_int.value as usize).min(items.len())
}
} else {
return Box::new(StringBox::new("Error: slice() end index must be an integer"));
return Box::new(StringBox::new(
"Error: slice() end index must be an integer",
));
};
// Validate indices
if start_idx > items.len() || start_idx > end_idx {
return Box::new(ArrayBox::new());
}
// Create slice
let slice_items: Vec<Box<dyn NyashBox>> = items[start_idx..end_idx]
.iter()
.map(|item| {
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
if item.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some() {
if item
.as_any()
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
.is_some()
{
return item.share_box();
}
item.clone_box()
})
.collect();
Box::new(ArrayBox::new_with_elements(slice_items))
}
}
@ -256,18 +280,23 @@ impl Clone for ArrayBox {
fn clone(&self) -> Self {
// ディープコピー(独立インスタンス)
let items_guard = self.items.read().unwrap();
let cloned_items: Vec<Box<dyn NyashBox>> = items_guard.iter()
let cloned_items: Vec<Box<dyn NyashBox>> = items_guard
.iter()
.map(|item| {
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
if item.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some() {
if item
.as_any()
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
.is_some()
{
return item.share_box();
}
item.clone_box()
}) // 要素もディープコピー(ハンドルは共有)
}) // 要素もディープコピー(ハンドルは共有)
.collect();
ArrayBox {
items: Arc::new(RwLock::new(cloned_items)), // 新しいArc
items: Arc::new(RwLock::new(cloned_items)), // 新しいArc
base: BoxBase::new(),
}
}
@ -277,23 +306,24 @@ impl BoxCore for ArrayBox {
fn box_id(&self) -> u64 {
self.base.id
}
fn parent_type_id(&self) -> Option<std::any::TypeId> {
self.base.parent_type_id
}
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let items = self.items.read().unwrap();
let strings: Vec<String> = items.iter()
let strings: Vec<String> = items
.iter()
.map(|item| item.to_string_box().value)
.collect();
write!(f, "[{}]", strings.join(", "))
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
@ -306,49 +336,50 @@ impl Display for ArrayBox {
}
impl NyashBox for ArrayBox {
fn is_identity(&self) -> bool { true }
fn is_identity(&self) -> bool {
true
}
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 🎯 状態共有の核心実装
fn share_box(&self) -> Box<dyn NyashBox> {
let new_instance = ArrayBox {
items: Arc::clone(&self.items), // Arcクローンで状態共有
base: BoxBase::new(), // 新しいID
items: Arc::clone(&self.items), // Arcクローンで状態共有
base: BoxBase::new(), // 新しいID
};
Box::new(new_instance)
}
fn to_string_box(&self) -> StringBox {
let items = self.items.read().unwrap();
let strings: Vec<String> = items.iter()
let strings: Vec<String> = items
.iter()
.map(|item| item.to_string_box().value)
.collect();
StringBox::new(format!("[{}]", strings.join(", ")))
}
fn type_name(&self) -> &'static str {
"ArrayBox"
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_array) = other.as_any().downcast_ref::<ArrayBox>() {
let self_items = self.items.read().unwrap();
let other_items = other_array.items.read().unwrap();
if self_items.len() != other_items.len() {
return BoolBox::new(false);
}
for (a, b) in self_items.iter().zip(other_items.iter()) {
if !a.equals(b.as_ref()).value {
return BoolBox::new(false);
}
}
BoolBox::new(true)
} else {
BoolBox::new(false)