diff --git a/libtest_syntax.rlib b/libtest_syntax.rlib new file mode 100644 index 00000000..101bc938 Binary files /dev/null and b/libtest_syntax.rlib differ diff --git a/src/box_trait.rs b/src/box_trait.rs index 752b041a..c94f247a 100644 --- a/src/box_trait.rs +++ b/src/box_trait.rs @@ -8,7 +8,7 @@ use std::fmt::{Debug, Display}; use std::any::Any; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::sync::atomic::{AtomicU64, Ordering}; use std::fs; use std::path::Path; @@ -790,136 +790,8 @@ impl Display for ResultBox { } } -/// Future values in Nyash - represents async operations -#[derive(Debug)] -pub struct FutureBox { - pub result: Arc>>>, - pub is_ready: Arc>, - base: BoxBase, -} - -impl Clone for FutureBox { - fn clone(&self) -> Self { - Self { - result: Arc::clone(&self.result), - is_ready: Arc::clone(&self.is_ready), - base: BoxBase::new(), // 新しいIDを生成 - } - } -} - -impl FutureBox { - pub fn new() -> Self { - Self { - result: Arc::new(Mutex::new(None)), - is_ready: Arc::new(Mutex::new(false)), - base: BoxBase::new(), - } - } - - /// Set the result of the future - pub fn set_result(&self, value: Box) { - let mut result = self.result.lock().unwrap(); - *result = Some(value); - let mut ready = self.is_ready.lock().unwrap(); - *ready = true; - } - - /// Get the result (blocks until ready) - pub fn get(&self) -> Box { - // 簡易実装: ビジーウェイト(後でcondvarに改善) - loop { - let ready = self.is_ready.lock().unwrap(); - if *ready { - break; - } - drop(ready); - std::thread::yield_now(); - } - - let result = self.result.lock().unwrap(); - result.as_ref().unwrap().clone_box() - } - - /// Check if the future is ready - pub fn ready(&self) -> Box { - Box::new(BoolBox::new(*self.is_ready.lock().unwrap())) - } - - /// Wait and get the result (for await implementation) - pub fn wait_and_get(&self) -> Result, String> { - // 結果が準備できるまで待機 - while !*self.is_ready.lock().unwrap() { - std::thread::yield_now(); - } - - let result = self.result.lock().unwrap(); - result.as_ref() - .map(|v| v.clone_box()) - .ok_or_else(|| "Future has no result".to_string()) - } -} - -impl NyashBox for FutureBox { - fn to_string_box(&self) -> StringBox { - let ready = *self.is_ready.lock().unwrap(); - if ready { - let result = self.result.lock().unwrap(); - if let Some(value) = result.as_ref() { - StringBox::new(format!("Future(ready: {})", value.to_string_box().value)) - } else { - StringBox::new("Future(ready: void)".to_string()) - } - } else { - StringBox::new("Future(pending)".to_string()) - } - } - - fn equals(&self, other: &dyn NyashBox) -> BoolBox { - if let Some(other_future) = other.as_any().downcast_ref::() { - BoolBox::new(self.base.id == other_future.base.id) - } else { - BoolBox::new(false) - } - } - - fn type_name(&self) -> &'static str { - "FutureBox" - } - - fn clone_box(&self) -> Box { - Box::new(self.clone()) - } - -} - -impl BoxCore for FutureBox { - fn box_id(&self) -> u64 { - self.base.id - } - - fn parent_type_id(&self) -> Option { - self.base.parent_type_id - } - - fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.to_string_box().value) - } - - fn as_any(&self) -> &dyn Any { - self - } - - fn as_any_mut(&mut self) -> &mut dyn Any { - self - } -} - -impl Display for FutureBox { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.fmt_box(f) - } -} +// 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 // Re-export operation boxes from the dedicated operations module pub use crate::box_arithmetic::{AddBox, SubtractBox, MultiplyBox, DivideBox, CompareBox}; diff --git a/src/boxes/http/mod.rs b/src/boxes/http/mod.rs index af680311..3b065a05 100644 --- a/src/boxes/http/mod.rs +++ b/src/boxes/http/mod.rs @@ -8,7 +8,6 @@ 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 { diff --git a/src/boxes/regex/mod.rs b/src/boxes/regex/mod.rs index ed13c480..8b186df7 100644 --- a/src/boxes/regex/mod.rs +++ b/src/boxes/regex/mod.rs @@ -6,7 +6,7 @@ use regex::Regex; use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase}; use crate::boxes::array::ArrayBox; use std::any::Any; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::fmt::Debug; #[derive(Debug, Clone)]