🛠️ Phase C: SocketBox Arc<RwLock> + syntax fixes for multiple Box types
Co-authored-by: moe-charm <217100418+moe-charm@users.noreply.github.com>
This commit is contained in:
@ -93,9 +93,6 @@ impl NyashBox for BoolBox {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl BoxCore for BoolBox {
|
||||
fn box_id(&self) -> u64 {
|
||||
|
||||
@ -377,14 +377,13 @@ impl NyashBox for DebugBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -89,9 +89,6 @@ impl NyashBox for IntegerBox {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl BoxCore for IntegerBox {
|
||||
fn box_id(&self) -> u64 {
|
||||
|
||||
@ -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::<MapBox>() {
|
||||
|
||||
@ -314,14 +314,13 @@ impl NyashBox for MathBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_math) = other.as_any().downcast_ref::<MathBox>() {
|
||||
@ -387,14 +386,13 @@ impl NyashBox for FloatBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_float) = other.as_any().downcast_ref::<FloatBox>() {
|
||||
@ -486,14 +484,13 @@ impl NyashBox for RangeBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_range) = other.as_any().downcast_ref::<RangeBox>() {
|
||||
|
||||
@ -267,14 +267,13 @@ impl NyashBox for RandomBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_random) = other.as_any().downcast_ref::<RandomBox>() {
|
||||
|
||||
@ -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<Option<TcpListener>>,
|
||||
listener: Arc<RwLock<Option<TcpListener>>>, // Arc追加
|
||||
// TCP Client/Connected Socket
|
||||
stream: RwLock<Option<TcpStream>>,
|
||||
stream: Arc<RwLock<Option<TcpStream>>>, // Arc追加
|
||||
// Connection state
|
||||
is_server: RwLock<bool>,
|
||||
is_connected: RwLock<bool>,
|
||||
is_server: Arc<RwLock<bool>>, // Arc追加
|
||||
is_connected: Arc<RwLock<bool>>, // 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<dyn NyashBox> {
|
||||
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 {
|
||||
|
||||
@ -197,14 +197,13 @@ impl NyashBox for TimeBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_time) = other.as_any().downcast_ref::<TimeBox>() {
|
||||
@ -374,14 +373,13 @@ impl NyashBox for DateTimeBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_dt) = other.as_any().downcast_ref::<DateTimeBox>() {
|
||||
@ -464,14 +462,13 @@ impl NyashBox for TimerBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_timer) = other.as_any().downcast_ref::<TimerBox>() {
|
||||
|
||||
Reference in New Issue
Block a user