diff --git a/src/boxes/bool_box.rs b/src/boxes/bool_box.rs index 25f2cd7b..f68bd195 100644 --- a/src/boxes/bool_box.rs +++ b/src/boxes/bool_box.rs @@ -93,9 +93,6 @@ impl NyashBox for BoolBox { self.clone_box() } } - - -} impl BoxCore for BoolBox { fn box_id(&self) -> u64 { diff --git a/src/boxes/debug_box.rs b/src/boxes/debug_box.rs index 2a6ac757..f752dca1 100644 --- a/src/boxes/debug_box.rs +++ b/src/boxes/debug_box.rs @@ -377,14 +377,13 @@ impl NyashBox for DebugBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } } \ No newline at end of file diff --git a/src/boxes/integer_box.rs b/src/boxes/integer_box.rs index 5090acac..65e9e966 100644 --- a/src/boxes/integer_box.rs +++ b/src/boxes/integer_box.rs @@ -89,9 +89,6 @@ impl NyashBox for IntegerBox { self.clone_box() } } - - -} impl BoxCore for IntegerBox { fn box_id(&self) -> u64 { diff --git a/src/boxes/map_box.rs b/src/boxes/map_box.rs index 9543e23b..27a10b62 100644 --- a/src/boxes/map_box.rs +++ b/src/boxes/map_box.rs @@ -283,7 +283,6 @@ impl NyashBox for MapBox { }; Box::new(new_instance) } -} fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_map) = other.as_any().downcast_ref::() { diff --git a/src/boxes/math_box.rs b/src/boxes/math_box.rs index 25d451a6..5502a38c 100644 --- a/src/boxes/math_box.rs +++ b/src/boxes/math_box.rs @@ -314,14 +314,13 @@ impl NyashBox for MathBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_math) = other.as_any().downcast_ref::() { @@ -387,14 +386,13 @@ impl NyashBox for FloatBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_float) = other.as_any().downcast_ref::() { @@ -486,14 +484,13 @@ impl NyashBox for RangeBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_range) = other.as_any().downcast_ref::() { diff --git a/src/boxes/random_box.rs b/src/boxes/random_box.rs index 97d79050..86b06ba9 100644 --- a/src/boxes/random_box.rs +++ b/src/boxes/random_box.rs @@ -267,14 +267,13 @@ impl NyashBox for RandomBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_random) = other.as_any().downcast_ref::() { diff --git a/src/boxes/socket_box.rs b/src/boxes/socket_box.rs index 6e4066a8..4882c6c1 100644 --- a/src/boxes/socket_box.rs +++ b/src/boxes/socket_box.rs @@ -38,7 +38,7 @@ use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use std::any::Any; use std::net::{TcpListener, TcpStream}; use std::io::{Write, BufRead, BufReader}; -use std::sync::RwLock; +use std::sync::{Arc, RwLock}; // Arc追加 use std::time::Duration; /// TCP/UDP ソケット操作を提供するBox @@ -46,26 +46,26 @@ use std::time::Duration; pub struct SocketBox { base: BoxBase, // TCP Server - listener: RwLock>, + listener: Arc>>, // Arc追加 // TCP Client/Connected Socket - stream: RwLock>, + stream: Arc>>, // Arc追加 // Connection state - is_server: RwLock, - is_connected: RwLock, + is_server: Arc>, // Arc追加 + is_connected: Arc>, // Arc追加 } impl Clone for SocketBox { fn clone(&self) -> Self { - // State-preserving clone implementation following RwLock pattern + // ディープコピー(独立インスタンス) let is_server_val = *self.is_server.read().unwrap(); let is_connected_val = *self.is_connected.read().unwrap(); Self { base: BoxBase::new(), // New unique ID for clone - listener: RwLock::new(None), // Start fresh for clone - stream: RwLock::new(None), // Start fresh for clone - is_server: RwLock::new(is_server_val), - is_connected: RwLock::new(is_connected_val), + listener: Arc::new(RwLock::new(None)), // 新しいArc + stream: Arc::new(RwLock::new(None)), // 新しいArc + is_server: Arc::new(RwLock::new(is_server_val)), // 状態のみコピー + is_connected: Arc::new(RwLock::new(is_connected_val)), // 状態のみコピー } } } @@ -74,10 +74,10 @@ impl SocketBox { pub fn new() -> Self { Self { base: BoxBase::new(), - listener: RwLock::new(None), - stream: RwLock::new(None), - is_server: RwLock::new(false), - is_connected: RwLock::new(false), + listener: Arc::new(RwLock::new(None)), // Arc::new追加 + stream: Arc::new(RwLock::new(None)), // Arc::new追加 + is_server: Arc::new(RwLock::new(false)), // Arc::new追加 + is_connected: Arc::new(RwLock::new(false)), // Arc::new追加 } } @@ -385,9 +385,16 @@ impl NyashBox for SocketBox { Box::new(self.clone()) } - /// 仮実装: clone_boxと同じ(後で修正) + /// 🎯 状態共有の核心実装 - SocketBox状態保持問題の根本解決 fn share_box(&self) -> Box { - self.clone_box() + let new_instance = SocketBox { + base: BoxBase::new(), // 新しいID + listener: Arc::clone(&self.listener), // 状態共有 + stream: Arc::clone(&self.stream), // 状態共有 + is_server: Arc::clone(&self.is_server), // 状態共有 + is_connected: Arc::clone(&self.is_connected), // 状態共有 + }; + Box::new(new_instance) } fn to_string_box(&self) -> StringBox { diff --git a/src/boxes/time_box.rs b/src/boxes/time_box.rs index 2913dcd1..4c161851 100644 --- a/src/boxes/time_box.rs +++ b/src/boxes/time_box.rs @@ -197,14 +197,13 @@ impl NyashBox for TimeBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_time) = other.as_any().downcast_ref::() { @@ -374,14 +373,13 @@ impl NyashBox for DateTimeBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_dt) = other.as_any().downcast_ref::() { @@ -464,14 +462,13 @@ impl NyashBox for TimerBox { } fn clone_box(&self) -> Box { + Box::new(self.clone()) + } /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box { self.clone_box() } - - Box::new(self.clone()) - } fn equals(&self, other: &dyn NyashBox) -> BoolBox { if let Some(other_timer) = other.as_any().downcast_ref::() {