2025-08-09 15:14:44 +09:00
|
|
|
|
/*!
|
|
|
|
|
|
* Nyash Box Trait System - Everything is Box in Rust
|
2025-09-17 07:43:07 +09:00
|
|
|
|
*
|
2025-08-09 15:14:44 +09:00
|
|
|
|
* This module implements the core "Everything is Box" philosophy using Rust's
|
|
|
|
|
|
* ownership system and trait system. Every value in Nyash is a Box that
|
|
|
|
|
|
* implements the NyashBox trait.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
use std::any::Any;
|
2025-09-25 03:46:37 +09:00
|
|
|
|
use std::fmt::Debug;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
|
|
use std::sync::Arc;
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-08-14 10:05:42 +00:00
|
|
|
|
// 🔥 新しい型エイリアス - 将来的にBox<dyn NyashBox>を全て置き換える
|
|
|
|
|
|
pub type SharedNyashBox = Arc<dyn NyashBox>;
|
|
|
|
|
|
|
2025-08-11 11:05:53 +09:00
|
|
|
|
/// 🔥 BoxBase + BoxCore革命 - 統一ID生成システム
|
|
|
|
|
|
/// CharmFlow教訓を活かした互換性保証の基盤
|
|
|
|
|
|
pub fn next_box_id() -> u64 {
|
|
|
|
|
|
static COUNTER: AtomicU64 = AtomicU64::new(1);
|
|
|
|
|
|
COUNTER.fetch_add(1, Ordering::Relaxed)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-15 10:52:35 +00:00
|
|
|
|
/// 🔥 Phase 8.8: pack透明化システム - ビルトインBox判定リスト
|
|
|
|
|
|
/// ユーザーは`pack`を一切意識せず、`from BuiltinBox()`で自動的に内部のpack機能が呼ばれる
|
|
|
|
|
|
pub const BUILTIN_BOXES: &[&str] = &[
|
2025-09-17 07:43:07 +09:00
|
|
|
|
"StringBox",
|
|
|
|
|
|
"IntegerBox",
|
|
|
|
|
|
"BoolBox",
|
|
|
|
|
|
"NullBox",
|
|
|
|
|
|
"ArrayBox",
|
|
|
|
|
|
"MapBox",
|
2025-09-27 08:45:25 +09:00
|
|
|
|
"MissingBox",
|
2025-09-17 07:43:07 +09:00
|
|
|
|
"FileBox",
|
|
|
|
|
|
"ResultBox",
|
|
|
|
|
|
"FutureBox",
|
|
|
|
|
|
"ChannelBox",
|
|
|
|
|
|
"MathBox",
|
|
|
|
|
|
"FloatBox",
|
|
|
|
|
|
"TimeBox",
|
|
|
|
|
|
"DateTimeBox",
|
|
|
|
|
|
"TimerBox",
|
|
|
|
|
|
"RandomBox",
|
|
|
|
|
|
"SoundBox",
|
|
|
|
|
|
"DebugBox",
|
|
|
|
|
|
"MethodBox",
|
|
|
|
|
|
"ConsoleBox",
|
|
|
|
|
|
"BufferBox",
|
|
|
|
|
|
"RegexBox",
|
|
|
|
|
|
"JSONBox",
|
|
|
|
|
|
"StreamBox",
|
|
|
|
|
|
"HTTPClientBox",
|
|
|
|
|
|
"IntentBox",
|
|
|
|
|
|
"P2PBox",
|
|
|
|
|
|
"SocketBox",
|
|
|
|
|
|
"HTTPServerBox",
|
|
|
|
|
|
"HTTPRequestBox",
|
|
|
|
|
|
"HTTPResponseBox",
|
|
|
|
|
|
"JitConfigBox",
|
2025-08-15 10:52:35 +00:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/// 🔥 ビルトインBox判定関数 - pack透明化システムの核心
|
|
|
|
|
|
/// ユーザー側: `from StringBox()` → 内部的に `StringBox.pack()` 自動呼び出し
|
|
|
|
|
|
pub fn is_builtin_box(box_name: &str) -> bool {
|
|
|
|
|
|
BUILTIN_BOXES.contains(&box_name)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-11 11:05:53 +09:00
|
|
|
|
/// 🏗️ BoxBase - 全てのBox型の共通基盤構造体
|
|
|
|
|
|
/// Phase 2: 統一的な基盤データを提供
|
2025-08-11 15:01:11 +09:00
|
|
|
|
/// 🔥 Phase 1: ビルトインBox継承システム - 最小限拡張
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2025-08-11 11:05:53 +09:00
|
|
|
|
pub struct BoxBase {
|
|
|
|
|
|
pub id: u64,
|
2025-08-11 15:01:11 +09:00
|
|
|
|
pub parent_type_id: Option<std::any::TypeId>, // ビルトインBox継承用
|
2025-08-11 11:05:53 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl BoxBase {
|
|
|
|
|
|
/// 新しいBoxBase作成 - 安全なID生成
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
|
Self {
|
|
|
|
|
|
id: next_box_id(),
|
2025-08-11 15:01:11 +09:00
|
|
|
|
parent_type_id: None, // ビルトインBox: 継承なし
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
|
/// ビルトインBox継承用コンストラクタ
|
|
|
|
|
|
pub fn with_parent_type(parent_type_id: std::any::TypeId) -> Self {
|
|
|
|
|
|
Self {
|
|
|
|
|
|
id: next_box_id(),
|
|
|
|
|
|
parent_type_id: Some(parent_type_id),
|
2025-08-11 11:05:53 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 🎯 BoxCore - Box型共通メソッドの統一インターフェース
|
|
|
|
|
|
/// Phase 2: 重複コードを削減する中核トレイト
|
2025-08-11 15:01:11 +09:00
|
|
|
|
/// 🔥 Phase 2: ビルトインBox継承システム対応
|
2025-08-11 11:05:53 +09:00
|
|
|
|
pub trait BoxCore: Send + Sync {
|
|
|
|
|
|
/// ボックスの一意ID取得
|
|
|
|
|
|
fn box_id(&self) -> u64;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
|
/// 継承元の型ID取得(ビルトインBox継承用)
|
|
|
|
|
|
fn parent_type_id(&self) -> Option<std::any::TypeId>;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 11:05:53 +09:00
|
|
|
|
/// Display実装のための統一フォーマット
|
|
|
|
|
|
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
|
/// Any変換(ダウンキャスト用)
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
|
/// Anyミュータブル変換(ダウンキャスト用)
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
2025-08-11 11:05:53 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// The fundamental trait that all Nyash values must implement.
|
|
|
|
|
|
/// This embodies the "Everything is Box" philosophy with Rust's type safety.
|
2025-08-11 11:05:53 +09:00
|
|
|
|
pub trait NyashBox: BoxCore + Debug {
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Convert this box to a string representation (equivalent to Python's toString())
|
|
|
|
|
|
fn to_string_box(&self) -> StringBox;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Check equality with another box (equivalent to Python's equals())
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Get the type name of this box for debugging
|
2025-08-11 11:05:53 +09:00
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
|
std::any::type_name::<Self>()
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Clone this box (equivalent to Python's copy())
|
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox>;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-15 04:29:41 +00:00
|
|
|
|
/// Share this box (state-preserving reference sharing)
|
|
|
|
|
|
fn share_box(&self) -> Box<dyn NyashBox>;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-22 12:09:06 +09:00
|
|
|
|
/// Identity hint: boxes that wrap external/stateful handles should override to return true.
|
2025-09-17 07:43:07 +09:00
|
|
|
|
fn is_identity(&self) -> bool {
|
|
|
|
|
|
false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 12:09:06 +09:00
|
|
|
|
/// Helper: pick share or clone based on identity semantics.
|
|
|
|
|
|
fn clone_or_share(&self) -> Box<dyn NyashBox> {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if self.is_identity() {
|
|
|
|
|
|
self.share_box()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
self.clone_box()
|
|
|
|
|
|
}
|
2025-08-22 12:09:06 +09:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-14 10:05:42 +00:00
|
|
|
|
/// Arc参照を返す新しいcloneメソッド(参照共有)
|
|
|
|
|
|
fn clone_arc(&self) -> SharedNyashBox {
|
|
|
|
|
|
Arc::from(self.clone_box())
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// 🌟 TypeBox革命: Get type information as a Box
|
|
|
|
|
|
// Everything is Box極限実現 - 型情報もBoxとして取得!
|
|
|
|
|
|
// TODO: 次のステップで完全実装
|
|
|
|
|
|
// fn get_type_box(&self) -> std::sync::Arc<crate::type_box::TypeBox>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-25 03:46:37 +09:00
|
|
|
|
// ===== Basic Box Types (Re-exported from basic module) =====
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-09-25 03:46:37 +09:00
|
|
|
|
// Re-export all basic box types from the dedicated basic module
|
2025-11-21 06:25:17 +09:00
|
|
|
|
pub use crate::boxes::basic::{BoolBox, ErrorBox, FileBox, IntegerBox, StringBox, VoidBox};
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-09-25 03:46:37 +09:00
|
|
|
|
// Old Box implementations have been moved to separate files
|
2025-08-10 16:46:39 +09:00
|
|
|
|
// ArrayBox is now defined in boxes::array module
|
|
|
|
|
|
pub use crate::boxes::array::ArrayBox;
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-08-15 02:24:34 +00:00
|
|
|
|
// FutureBox is now implemented in src/boxes/future/mod.rs using RwLock pattern
|
|
|
|
|
|
// and re-exported from src/boxes/mod.rs as both NyashFutureBox and FutureBox
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-08-14 13:32:06 +00:00
|
|
|
|
// Re-export operation boxes from the dedicated operations module
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub use crate::box_arithmetic::{
|
|
|
|
|
|
AddBox, CompareBox, DivideBox, ModuloBox, MultiplyBox, SubtractBox,
|
|
|
|
|
|
};
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use super::*;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_string_box_creation() {
|
|
|
|
|
|
let s = StringBox::new("Hello, Rust!");
|
|
|
|
|
|
assert_eq!(s.value, "Hello, Rust!");
|
|
|
|
|
|
assert_eq!(s.type_name(), "StringBox");
|
|
|
|
|
|
assert_eq!(s.to_string_box().value, "Hello, Rust!");
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_integer_box_creation() {
|
|
|
|
|
|
let i = IntegerBox::new(42);
|
|
|
|
|
|
assert_eq!(i.value, 42);
|
|
|
|
|
|
assert_eq!(i.type_name(), "IntegerBox");
|
|
|
|
|
|
assert_eq!(i.to_string_box().value, "42");
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_bool_box_creation() {
|
|
|
|
|
|
let b = BoolBox::new(true);
|
|
|
|
|
|
assert_eq!(b.value, true);
|
|
|
|
|
|
assert_eq!(b.type_name(), "BoolBox");
|
|
|
|
|
|
assert_eq!(b.to_string_box().value, "true");
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_box_equality() {
|
|
|
|
|
|
let s1 = StringBox::new("test");
|
|
|
|
|
|
let s2 = StringBox::new("test");
|
|
|
|
|
|
let s3 = StringBox::new("different");
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
assert!(s1.equals(&s2).value);
|
|
|
|
|
|
assert!(!s1.equals(&s3).value);
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_add_box_integers() {
|
|
|
|
|
|
let left = Box::new(IntegerBox::new(5)) as Box<dyn NyashBox>;
|
|
|
|
|
|
let right = Box::new(IntegerBox::new(3)) as Box<dyn NyashBox>;
|
|
|
|
|
|
let add = AddBox::new(left, right);
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
let result = add.execute();
|
|
|
|
|
|
let result_int = result.as_any().downcast_ref::<IntegerBox>().unwrap();
|
|
|
|
|
|
assert_eq!(result_int.value, 8);
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_add_box_strings() {
|
|
|
|
|
|
let left = Box::new(StringBox::new("Hello, ")) as Box<dyn NyashBox>;
|
|
|
|
|
|
let right = Box::new(StringBox::new("Rust!")) as Box<dyn NyashBox>;
|
|
|
|
|
|
let add = AddBox::new(left, right);
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
let result = add.execute();
|
|
|
|
|
|
let result_str = result.as_any().downcast_ref::<StringBox>().unwrap();
|
|
|
|
|
|
assert_eq!(result_str.value, "Hello, Rust!");
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_box_ids_unique() {
|
|
|
|
|
|
let s1 = StringBox::new("test");
|
|
|
|
|
|
let s2 = StringBox::new("test");
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Same content but different IDs
|
|
|
|
|
|
assert_ne!(s1.box_id(), s2.box_id());
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_void_box() {
|
|
|
|
|
|
let v = VoidBox::new();
|
|
|
|
|
|
assert_eq!(v.type_name(), "VoidBox");
|
|
|
|
|
|
assert_eq!(v.to_string_box().value, "void");
|
|
|
|
|
|
}
|
2025-08-22 12:09:06 +09:00
|
|
|
|
}
|