From 8d9c8b5899a809eb1e4b0398a4658e1e9e8c3e84 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 15 Aug 2025 04:39:00 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Phase=20C:=20MapBox=20&=20Buffer?= =?UTF-8?q?Box=20Arc=20state=20sharing=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: moe-charm <217100418+moe-charm@users.noreply.github.com> --- src/boxes/buffer/mod.rs | 24 ++++++++++++++++-------- src/boxes/map_box.rs | 23 ++++++++++++++--------- src/boxes/string_box.rs | 3 --- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/boxes/buffer/mod.rs b/src/boxes/buffer/mod.rs index d256afe6..043540a8 100644 --- a/src/boxes/buffer/mod.rs +++ b/src/boxes/buffer/mod.rs @@ -31,18 +31,18 @@ use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, BoxCore, BoxBase}; use crate::boxes::array::ArrayBox; use std::any::Any; -use std::sync::RwLock; +use std::sync::{Arc, RwLock}; // Arc追加 use std::fmt::{Debug, Display}; pub struct BufferBox { - data: RwLock>, + data: Arc>>, // Arc追加 base: BoxBase, } impl BufferBox { pub fn new() -> Self { BufferBox { - data: RwLock::new(Vec::new()), + data: Arc::new(RwLock::new(Vec::new())), // Arc::new追加 base: BoxBase::new(), } } @@ -54,7 +54,7 @@ impl BufferBox { pub fn from_vec(data: Vec) -> Self { BufferBox { - data: RwLock::new(data), + data: Arc::new(RwLock::new(data)), // Arc::new追加 base: BoxBase::new(), } } @@ -155,8 +155,12 @@ impl BufferBox { // Clone implementation for BufferBox (needed since RwLock doesn't auto-derive Clone) impl Clone for BufferBox { fn clone(&self) -> Self { - let data = self.data.read().unwrap(); - BufferBox::from_vec(data.clone()) + // ディープコピー(独立インスタンス) + let data_guard = self.data.read().unwrap(); + BufferBox { + data: Arc::new(RwLock::new(data_guard.clone())), // 新しいArc + base: BoxBase::new(), + } } } @@ -194,9 +198,13 @@ impl NyashBox for BufferBox { Box::new(self.clone()) } - /// 仮実装: clone_boxと同じ(後で修正) + /// 🎯 状态共享的核心实现 fn share_box(&self) -> Box { - self.clone_box() + let new_instance = BufferBox { + data: Arc::clone(&self.data), // Arcクローンで状態共有 + base: BoxBase::new(), // 新しいID + }; + Box::new(new_instance) } fn to_string_box(&self) -> StringBox { diff --git a/src/boxes/map_box.rs b/src/boxes/map_box.rs index 974330f8..9543e23b 100644 --- a/src/boxes/map_box.rs +++ b/src/boxes/map_box.rs @@ -108,18 +108,18 @@ use crate::boxes::ArrayBox; use std::fmt::{Debug, Display}; use std::any::Any; use std::collections::HashMap; -use std::sync::RwLock; +use std::sync::{Arc, RwLock}; // Arc追加 /// キーバリューストアを表すBox pub struct MapBox { - data: RwLock>>, + data: Arc>>>, // Arc追加 base: BoxBase, } impl MapBox { pub fn new() -> Self { Self { - data: RwLock::new(HashMap::new()), + data: Arc::new(RwLock::new(HashMap::new())), // Arc::new追加 base: BoxBase::new(), } } @@ -225,12 +225,13 @@ impl MapBox { // Clone implementation for MapBox (needed since RwLock doesn't auto-derive Clone) impl Clone for MapBox { fn clone(&self) -> Self { - let data = self.data.read().unwrap(); - let cloned_data: HashMap> = data.iter() - .map(|(k, v)| (k.clone(), v.clone_box())) + // ディープコピー(独立インスタンス) + let data_guard = self.data.read().unwrap(); + let cloned_data: HashMap> = data_guard.iter() + .map(|(k, v)| (k.clone(), v.clone_box())) // 要素もディープコピー .collect(); MapBox { - data: RwLock::new(cloned_data), + data: Arc::new(RwLock::new(cloned_data)), // 新しいArc base: BoxBase::new(), } } @@ -274,9 +275,13 @@ impl NyashBox for MapBox { Box::new(self.clone()) } - /// 仮実装: clone_boxと同じ(後で修正) + /// 🎯 状態共有の核心実装 fn share_box(&self) -> Box { - self.clone_box() + let new_instance = MapBox { + data: Arc::clone(&self.data), // Arcクローンで状態共有 + base: BoxBase::new(), // 新しいID + }; + Box::new(new_instance) } } diff --git a/src/boxes/string_box.rs b/src/boxes/string_box.rs index 91afa3a7..9186023f 100644 --- a/src/boxes/string_box.rs +++ b/src/boxes/string_box.rs @@ -157,9 +157,6 @@ impl NyashBox for StringBox { self.clone_box() } } - - -} impl BoxCore for StringBox { fn box_id(&self) -> u64 {