diff --git a/src/boxes/future/mod.rs b/src/boxes/future/mod.rs index 67338bac..8d0615f6 100644 --- a/src/boxes/future/mod.rs +++ b/src/boxes/future/mod.rs @@ -2,7 +2,7 @@ // Nyashの箱システムによる非同期処理の基盤を提供します。 // 参考: 既存Boxの設計思想 -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use std::any::Any; use std::future::Future; use std::pin::Pin; @@ -12,7 +12,7 @@ use std::sync::{Arc, Mutex}; pub struct NyashFutureBox { pub result: Arc>>>, pub is_ready: Arc>, - id: u64, + base: BoxBase, } impl Clone for NyashFutureBox { @@ -20,22 +20,17 @@ impl Clone for NyashFutureBox { Self { result: Arc::clone(&self.result), is_ready: Arc::clone(&self.is_ready), - id: self.id, + base: self.base.clone(), } } } impl NyashFutureBox { pub fn new() -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; Self { result: Arc::new(Mutex::new(None)), is_ready: Arc::new(Mutex::new(false)), - id, + base: BoxBase::new(), } } @@ -96,19 +91,42 @@ impl NyashBox for NyashFutureBox { "NyashFutureBox" } - fn box_id(&self) -> u64 { - self.id - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_future) = other.as_any().downcast_ref::() { - BoolBox::new(self.id == other_future.id) + BoolBox::new(self.base.id() == other_future.base.id()) } else { BoolBox::new(false) } } } +impl BoxCore for NyashFutureBox { + fn box_id(&self) -> u64 { + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let ready = *self.is_ready.lock().unwrap(); + if ready { + let result = self.result.lock().unwrap(); + if let Some(value) = result.as_ref() { + write!(f, "Future(ready: {})", value.to_string_box().value) + } else { + write!(f, "Future(ready: void)") + } + } else { + write!(f, "Future(pending)") + } + } +} + +impl std::fmt::Display for NyashFutureBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) + } +} + // Export NyashFutureBox as FutureBox for consistency pub type FutureBox = NyashFutureBox; diff --git a/src/boxes/http/mod.rs b/src/boxes/http/mod.rs index 9dc1c25e..9db85cfa 100644 --- a/src/boxes/http/mod.rs +++ b/src/boxes/http/mod.rs @@ -5,24 +5,21 @@ // NOTE: HTTPサポートは現在開発中です。 // reqwestクレートの依存関係のため、一時的に無効化されています。 -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use crate::boxes::map_box::MapBox; use std::any::Any; use std::sync::{Arc, Mutex}; #[derive(Debug, Clone)] pub struct HttpClientBox { - id: u64, + base: BoxBase, } impl HttpClientBox { pub fn new() -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - HttpClientBox { id } + HttpClientBox { + base: BoxBase::new() + } } /// HTTP GETリクエスト(スタブ) @@ -57,7 +54,7 @@ impl NyashBox for HttpClientBox { } fn to_string_box(&self) -> StringBox { - StringBox::new(format!("HttpClientBox(id: {})", self.id)) + StringBox::new(format!("HttpClientBox(id: {})", self.base.id())) } fn as_any(&self) -> &dyn Any { @@ -68,15 +65,28 @@ impl NyashBox for HttpClientBox { "HttpClientBox" } - fn box_id(&self) -> u64 { - self.id - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_http) = other.as_any().downcast_ref::() { - BoolBox::new(self.id == other_http.id) + BoolBox::new(self.base.id() == other_http.base.id()) } else { BoolBox::new(false) } } +} + +impl BoxCore for HttpClientBox { + fn box_id(&self) -> u64 { + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "HttpClientBox(id: {})", self.base.id()) + } +} + +impl std::fmt::Display for HttpClientBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) + } } \ No newline at end of file diff --git a/src/boxes/intent_box.rs b/src/boxes/intent_box.rs index 3712d528..1dd52c47 100644 --- a/src/boxes/intent_box.rs +++ b/src/boxes/intent_box.rs @@ -25,7 +25,7 @@ * ``` */ -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use std::any::Any; use std::sync::{Arc, Mutex}; use std::fmt::{self, Debug}; @@ -121,14 +121,14 @@ impl Transport for LocalTransport { /// IntentBox - 通信世界を定義 #[derive(Clone)] pub struct IntentBox { - id: u64, + base: BoxBase, transport: Arc>>, } impl Debug for IntentBox { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("IntentBox") - .field("id", &self.id) + .field("id", &self.base.id()) .field("transport", &"") .finish() } @@ -137,28 +137,16 @@ impl Debug for IntentBox { impl IntentBox { /// デフォルト(ローカル)通信世界を作成 pub fn new() -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - IntentBox { - id, + base: BoxBase::new(), transport: Arc::new(Mutex::new(Box::new(LocalTransport::new()))), } } /// カスタムトランスポートで通信世界を作成 pub fn new_with_transport(transport: Box) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - IntentBox { - id, + base: BoxBase::new(), transport: Arc::new(Mutex::new(transport)), } } @@ -185,7 +173,7 @@ impl NyashBox for IntentBox { fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_intent) = other.as_any().downcast_ref::() { - BoolBox::new(self.id == other_intent.id) + BoolBox::new(self.base.id() == other_intent.base.id()) } else { BoolBox::new(false) } @@ -203,8 +191,22 @@ impl NyashBox for IntentBox { self } +} + +impl BoxCore for IntentBox { fn box_id(&self) -> u64 { - self.id + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let transport = self.transport.lock().unwrap(); + write!(f, "IntentBox[{}]", transport.transport_type()) + } +} + +impl std::fmt::Display for IntentBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) } } diff --git a/src/boxes/p2p_box.rs b/src/boxes/p2p_box.rs index d2c40bef..f2266b88 100644 --- a/src/boxes/p2p_box.rs +++ b/src/boxes/p2p_box.rs @@ -33,7 +33,7 @@ * ``` */ -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use crate::boxes::intent_box::IntentBox; pub use crate::boxes::intent_box::Message; use crate::boxes::map_box::MapBox; @@ -47,7 +47,7 @@ pub type ListenerFn = Box; /// P2PBox内部実装 #[derive(Debug)] struct P2PBoxInner { - id: u64, + base: BoxBase, node_id: String, intent_box: Arc, listeners: Arc>>>, @@ -62,14 +62,8 @@ pub struct P2PBox { impl P2PBox { /// 新しいP2PBoxノードを作成 pub fn new(node_id: String, intent_box: Arc) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - let inner = Arc::new(P2PBoxInner { - id, + base: BoxBase::new(), node_id, intent_box: intent_box.clone(), listeners: Arc::new(Mutex::new(HashMap::new())), @@ -150,7 +144,7 @@ impl NyashBox for P2PBox { fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_p2p) = other.as_any().downcast_ref::() { - BoolBox::new(self.inner.id == other_p2p.inner.id) + BoolBox::new(self.inner.base.id() == other_p2p.inner.base.id()) } else { BoolBox::new(false) } @@ -168,7 +162,20 @@ impl NyashBox for P2PBox { self } +} + +impl BoxCore for P2PBox { fn box_id(&self) -> u64 { - self.inner.id + self.inner.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "P2PBox[{}]", self.inner.node_id) + } +} + +impl std::fmt::Display for P2PBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) } } \ No newline at end of file diff --git a/src/boxes/regex/mod.rs b/src/boxes/regex/mod.rs index 776af813..e3f11a1d 100644 --- a/src/boxes/regex/mod.rs +++ b/src/boxes/regex/mod.rs @@ -3,7 +3,7 @@ // 参考: 既存Boxの設計思想 use regex::Regex; -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use crate::boxes::array::ArrayBox; use std::any::Any; use std::sync::{Arc, Mutex}; @@ -13,21 +13,16 @@ use std::fmt::Debug; pub struct RegexBox { regex: Arc, pattern: Arc, - id: u64, + base: BoxBase, } impl RegexBox { pub fn new(pattern: &str) -> Result { - static mut COUNTER: u64 = 0; let regex = Regex::new(pattern)?; - let id = unsafe { - COUNTER += 1; - COUNTER - }; Ok(RegexBox { regex: Arc::new(regex), pattern: Arc::new(pattern.to_string()), - id, + base: BoxBase::new(), }) } pub fn is_match(&self, text: &str) -> bool { @@ -103,9 +98,6 @@ impl NyashBox for RegexBox { "RegexBox" } - fn box_id(&self) -> u64 { - self.id - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_regex) = other.as_any().downcast_ref::() { @@ -115,3 +107,19 @@ impl NyashBox for RegexBox { } } } + +impl BoxCore for RegexBox { + fn box_id(&self) -> u64 { + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "RegexBox({})", self.pattern.as_str()) + } +} + +impl std::fmt::Display for RegexBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) + } +} diff --git a/src/boxes/result/mod.rs b/src/boxes/result/mod.rs index 615b85a9..fb7ea560 100644 --- a/src/boxes/result/mod.rs +++ b/src/boxes/result/mod.rs @@ -2,7 +2,7 @@ // Nyashの箱システムによるエラー処理を提供します。 // 参考: 既存Boxの設計思想 -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore}; use std::any::Any; #[derive(Debug)] @@ -59,13 +59,6 @@ impl NyashBox for NyashResultBox { "NyashResultBox" } - fn box_id(&self) -> u64 { - // For enum variants, we use the contained value's ID - match self { - NyashResultBox::Ok(val) => val.box_id(), - NyashResultBox::Err(err) => err.box_id(), - } - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_result) = other.as_any().downcast_ref::() { @@ -80,6 +73,29 @@ impl NyashBox for NyashResultBox { } } +impl BoxCore for NyashResultBox { + fn box_id(&self) -> u64 { + // For enum variants, we use the contained value's ID + match self { + NyashResultBox::Ok(val) => val.box_id(), + NyashResultBox::Err(err) => err.box_id(), + } + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + NyashResultBox::Ok(val) => write!(f, "Ok({})", val.to_string_box().value), + NyashResultBox::Err(err) => write!(f, "Err({})", err.to_string_box().value), + } + } +} + +impl std::fmt::Display for NyashResultBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) + } +} + // Export NyashResultBox as ResultBox for compatibility pub type ResultBox = NyashResultBox; diff --git a/src/boxes/stream/mod.rs b/src/boxes/stream/mod.rs index 356e5216..ac598c36 100644 --- a/src/boxes/stream/mod.rs +++ b/src/boxes/stream/mod.rs @@ -2,7 +2,7 @@ // Nyashの箱システムによるストリーミング処理を提供します。 // 参考: 既存Boxの設計思想 -use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, BoxCore, BoxBase}; use crate::boxes::buffer::BufferBox; use crate::boxes::array::ArrayBox; use std::any::Any; @@ -13,33 +13,23 @@ use std::io::{Read, Write, Result}; pub struct NyashStreamBox { buffer: Arc>>, position: Arc>, - id: u64, + base: BoxBase, } impl NyashStreamBox { pub fn new() -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; NyashStreamBox { buffer: Arc::new(Mutex::new(Vec::new())), position: Arc::new(Mutex::new(0)), - id, + base: BoxBase::new(), } } pub fn from_data(data: Vec) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; NyashStreamBox { buffer: Arc::new(Mutex::new(data)), position: Arc::new(Mutex::new(0)), - id, + base: BoxBase::new(), } } @@ -165,9 +155,6 @@ impl NyashBox for NyashStreamBox { "NyashStreamBox" } - fn box_id(&self) -> u64 { - self.id - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_stream) = other.as_any().downcast_ref::() { @@ -182,5 +169,23 @@ impl NyashBox for NyashStreamBox { } } +impl BoxCore for NyashStreamBox { + fn box_id(&self) -> u64 { + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let buffer = self.buffer.lock().unwrap(); + let position = self.position.lock().unwrap(); + write!(f, "NyashStreamBox({} bytes, pos: {})", buffer.len(), *position) + } +} + +impl std::fmt::Display for NyashStreamBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) + } +} + // Export NyashStreamBox as StreamBox for consistency pub type StreamBox = NyashStreamBox; diff --git a/src/channel_box.rs b/src/channel_box.rs index 0aa4a211..1e34e151 100644 --- a/src/channel_box.rs +++ b/src/channel_box.rs @@ -5,7 +5,7 @@ * Everything is Box哲学に基づくP2P通信システム */ -use crate::box_trait::{NyashBox, StringBox, VoidBox}; +use crate::box_trait::{NyashBox, StringBox, VoidBox, BoxCore, BoxBase}; use std::collections::HashMap; use std::sync::{Arc, Mutex, Weak}; use std::fmt::{Debug, Display}; @@ -26,25 +26,19 @@ pub struct ChannelBox { /// メッセージハンドラー handlers: Arc) -> Box + Send>>>>, - /// チャンネルID - id: u64, + /// Box基底 + base: BoxBase, } impl ChannelBox { /// 新しいチャンネルを作成 pub fn new(sender: &str, receiver: &str) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - Self { sender_name: sender.to_string(), receiver_name: receiver.to_string(), linked_boxes: Arc::new(Mutex::new(HashMap::new())), handlers: Arc::new(Mutex::new(HashMap::new())), - id, + base: BoxBase::new(), } } @@ -141,14 +135,21 @@ impl NyashBox for ChannelBox { self } +} + +impl BoxCore for ChannelBox { fn box_id(&self) -> u64 { - self.id + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Channel({} >> {})", self.sender_name, self.receiver_name) } } impl Display for ChannelBox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.to_string_box().value) + self.fmt_box(f) } } @@ -157,7 +158,7 @@ impl Debug for ChannelBox { f.debug_struct("ChannelBox") .field("sender_name", &self.sender_name) .field("receiver_name", &self.receiver_name) - .field("id", &self.id) + .field("id", &self.base.id()) .finish() } } @@ -167,21 +168,15 @@ impl Debug for ChannelBox { pub struct MessageBox { pub sender: String, pub content: String, - pub timestamp: u64, + base: BoxBase, } impl MessageBox { pub fn new(sender: &str, content: &str) -> Self { - static mut COUNTER: u64 = 0; - let timestamp = unsafe { - COUNTER += 1; - COUNTER - }; - Self { sender: sender.to_string(), content: content.to_string(), - timestamp, + base: BoxBase::new(), } } } @@ -192,7 +187,7 @@ impl NyashBox for MessageBox { } fn to_string_box(&self) -> StringBox { - StringBox::new(&format!("[{}] {}: {}", self.timestamp, self.sender, self.content)) + StringBox::new(&format!("[{}] {}: {}", self.base.id(), self.sender, self.content)) } fn clone_box(&self) -> Box { @@ -214,13 +209,20 @@ impl NyashBox for MessageBox { self } +} + +impl BoxCore for MessageBox { fn box_id(&self) -> u64 { - self.timestamp + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[{}] {}: {}", self.base.id(), self.sender, self.content) } } impl Display for MessageBox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.to_string_box().value) + self.fmt_box(f) } } \ No newline at end of file diff --git a/src/exception_box.rs b/src/exception_box.rs index 12cd4053..e0f12f5f 100644 --- a/src/exception_box.rs +++ b/src/exception_box.rs @@ -4,7 +4,7 @@ * Everything is Box哲学に基づく例外システム */ -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use std::any::Any; use std::collections::HashMap; @@ -14,35 +14,25 @@ pub struct ErrorBox { pub message: String, pub stack_trace: Vec, pub cause: Option>, - id: u64, + base: BoxBase, } impl ErrorBox { pub fn new(message: &str) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; Self { message: message.to_string(), stack_trace: Vec::new(), cause: None, - id, + base: BoxBase::new(), } } pub fn with_cause(message: &str, cause: ErrorBox) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; Self { message: message.to_string(), stack_trace: Vec::new(), cause: Some(Box::new(cause)), - id, + base: BoxBase::new(), } } @@ -68,9 +58,6 @@ impl NyashBox for ErrorBox { StringBox::new(format!("ErrorBox({})", self.message)) } - fn box_id(&self) -> u64 { - self.id - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_error) = other.as_any().downcast_ref::() { @@ -89,6 +76,22 @@ impl NyashBox for ErrorBox { } } +impl BoxCore for ErrorBox { + fn box_id(&self) -> u64 { + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "ErrorBox({})", self.message) + } +} + +impl std::fmt::Display for ErrorBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.fmt_box(f) + } +} + /// 例外タイプの判定ユーティリティ pub fn is_exception_type(exception: &dyn NyashBox, type_name: &str) -> bool { match type_name { diff --git a/src/instance.rs b/src/instance.rs index 1b98227e..2df5bbe2 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -5,7 +5,7 @@ * Everything is Box哲学に基づくオブジェクト指向システム */ -use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase}; use crate::ast::ASTNode; use std::collections::HashMap; use std::fmt::{Debug, Display}; @@ -24,8 +24,8 @@ pub struct InstanceBox { /// メソッド定義(ClassBoxから共有) pub methods: Arc>, - /// インスタンスID - id: u64, + /// Box基底 + base: BoxBase, /// 解放済みフラグ finalized: Arc>, @@ -33,12 +33,6 @@ pub struct InstanceBox { impl InstanceBox { pub fn new(class_name: String, fields: Vec, methods: HashMap) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - // フィールドをVoidBoxで初期化 let mut field_map = HashMap::new(); for field in fields { @@ -49,7 +43,7 @@ impl InstanceBox { class_name, fields: Arc::new(Mutex::new(field_map)), methods: Arc::new(methods), - id, + base: BoxBase::new(), finalized: Arc::new(Mutex::new(false)), } } @@ -143,13 +137,13 @@ impl InstanceBox { impl NyashBox for InstanceBox { fn to_string_box(&self) -> StringBox { - StringBox::new(format!("<{} instance #{}>", self.class_name, self.id)) + StringBox::new(format!("<{} instance #{}>", self.class_name, self.base.id())) } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_instance) = other.as_any().downcast_ref::() { // 同じインスタンスIDなら等しい - BoolBox::new(self.id == other_instance.id) + BoolBox::new(self.base.id() == other_instance.base.id()) } else { BoolBox::new(false) } @@ -168,14 +162,21 @@ impl NyashBox for InstanceBox { self } +} + +impl BoxCore for InstanceBox { fn box_id(&self) -> u64 { - self.id + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "<{} instance #{}>", self.class_name, self.base.id()) } } impl Display for InstanceBox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "<{} instance>", self.class_name) + self.fmt_box(f) } } diff --git a/src/method_box.rs b/src/method_box.rs index bc8354d1..1b4b9533 100644 --- a/src/method_box.rs +++ b/src/method_box.rs @@ -5,7 +5,7 @@ * ChatGPT先生のアドバイスを全面採用 */ -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use crate::ast::ASTNode; use crate::instance::InstanceBox; use std::fmt::{Debug, Display}; @@ -46,19 +46,13 @@ pub struct MethodBox { /// メソッド定義(キャッシュ用) pub method_def: Option, - /// ユニークID - id: u64, + /// Box基底 + base: BoxBase, } impl MethodBox { /// 新しいMethodBoxを作成 pub fn new(instance: Box, method_name: String) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - // メソッド定義をキャッシュ(可能であれば) let method_def = if let Some(inst) = instance.as_any().downcast_ref::() { inst.get_method(&method_name).and_then(|ast| { @@ -81,7 +75,7 @@ impl MethodBox { instance: Arc::new(Mutex::new(instance)), method_name, method_def, - id, + base: BoxBase::new(), } } @@ -129,14 +123,21 @@ impl NyashBox for MethodBox { self } +} + +impl BoxCore for MethodBox { fn box_id(&self) -> u64 { - self.id + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "", self.method_name) } } impl Display for MethodBox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "", self.method_name) + self.fmt_box(f) } } diff --git a/src/type_box.rs b/src/type_box.rs index f0c400dc..a5c473e9 100644 --- a/src/type_box.rs +++ b/src/type_box.rs @@ -5,7 +5,7 @@ * ジェネリクス基盤を提供する革命的システム */ -use crate::box_trait::{NyashBox, StringBox, BoolBox}; +use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use std::collections::HashMap; use std::sync::Arc; use std::fmt::{Debug, Display}; @@ -72,19 +72,13 @@ pub struct TypeBox { /// ビルトイン型かどうか pub is_builtin: bool, - /// ユニークID - id: u64, + /// Box基底 + base: BoxBase, } impl TypeBox { /// 新しいTypeBoxを作成 pub fn new(name: &str) -> Self { - static mut COUNTER: u64 = 0; - let id = unsafe { - COUNTER += 1; - COUNTER - }; - Self { name: name.to_string(), fields: HashMap::new(), @@ -93,7 +87,7 @@ impl TypeBox { type_parameters: Vec::new(), concrete_types: HashMap::new(), is_builtin: false, - id, + base: BoxBase::new(), } } @@ -264,14 +258,21 @@ impl NyashBox for TypeBox { self } +} + +impl BoxCore for TypeBox { fn box_id(&self) -> u64 { - self.id + self.base.id() + } + + fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "", self.full_name()) } } impl Display for TypeBox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "", self.full_name()) + self.fmt_box(f) } }