2025-08-10 02:45:57 +00:00
|
|
|
//! FutureBox 🔄 - 非同期処理基盤
|
|
|
|
|
// Nyashの箱システムによる非同期処理の基盤を提供します。
|
|
|
|
|
// 参考: 既存Boxの設計思想
|
|
|
|
|
|
2025-08-11 12:09:14 +09:00
|
|
|
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
2025-08-10 03:21:24 +00:00
|
|
|
use std::any::Any;
|
2025-08-10 02:45:57 +00:00
|
|
|
use std::future::Future;
|
|
|
|
|
use std::pin::Pin;
|
2025-08-15 01:21:37 +00:00
|
|
|
use std::sync::RwLock;
|
2025-08-10 02:45:57 +00:00
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct NyashFutureBox {
|
2025-08-15 01:21:37 +00:00
|
|
|
pub result: RwLock<Option<Box<dyn NyashBox>>>,
|
|
|
|
|
pub is_ready: RwLock<bool>,
|
2025-08-11 12:09:14 +09:00
|
|
|
base: BoxBase,
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clone for NyashFutureBox {
|
|
|
|
|
fn clone(&self) -> Self {
|
2025-08-15 01:21:37 +00:00
|
|
|
let result_val = self.result.read().unwrap().clone();
|
|
|
|
|
let is_ready_val = *self.is_ready.read().unwrap();
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
Self {
|
2025-08-15 01:21:37 +00:00
|
|
|
result: RwLock::new(result_val),
|
|
|
|
|
is_ready: RwLock::new(is_ready_val),
|
2025-08-11 15:01:11 +09:00
|
|
|
base: BoxBase::new(), // Create a new base with unique ID for the clone
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NyashFutureBox {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
2025-08-15 01:21:37 +00:00
|
|
|
result: RwLock::new(None),
|
|
|
|
|
is_ready: RwLock::new(false),
|
2025-08-11 12:09:14 +09:00
|
|
|
base: BoxBase::new(),
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the result of the future
|
|
|
|
|
pub fn set_result(&self, value: Box<dyn NyashBox>) {
|
2025-08-15 01:21:37 +00:00
|
|
|
let mut result = self.result.write().unwrap();
|
2025-08-10 03:21:24 +00:00
|
|
|
*result = Some(value);
|
2025-08-15 01:21:37 +00:00
|
|
|
let mut ready = self.is_ready.write().unwrap();
|
2025-08-10 03:21:24 +00:00
|
|
|
*ready = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the result (blocks until ready)
|
|
|
|
|
pub fn get(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
// Simple busy wait (could be improved with condvar)
|
|
|
|
|
loop {
|
2025-08-15 01:21:37 +00:00
|
|
|
let ready = self.is_ready.read().unwrap();
|
2025-08-10 03:21:24 +00:00
|
|
|
if *ready {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
drop(ready);
|
|
|
|
|
std::thread::yield_now();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 01:21:37 +00:00
|
|
|
let result = self.result.read().unwrap();
|
2025-08-10 03:21:24 +00:00
|
|
|
result.as_ref().unwrap().clone_box()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check if the future is ready
|
|
|
|
|
pub fn ready(&self) -> bool {
|
2025-08-15 01:21:37 +00:00
|
|
|
*self.is_ready.read().unwrap()
|
2025-08-10 03:21:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NyashBox for NyashFutureBox {
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
Box::new(self.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
2025-08-15 01:21:37 +00:00
|
|
|
let ready = *self.is_ready.read().unwrap();
|
2025-08-10 03:21:24 +00:00
|
|
|
if ready {
|
2025-08-15 01:21:37 +00:00
|
|
|
let result = self.result.read().unwrap();
|
2025-08-10 03:21:24 +00:00
|
|
|
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 type_name(&self) -> &'static str {
|
|
|
|
|
"NyashFutureBox"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
|
|
|
|
if let Some(other_future) = other.as_any().downcast_ref::<NyashFutureBox>() {
|
2025-08-11 15:01:11 +09:00
|
|
|
BoolBox::new(self.base.id == other_future.base.id)
|
2025-08-10 03:21:24 +00:00
|
|
|
} else {
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 12:09:14 +09:00
|
|
|
impl BoxCore for NyashFutureBox {
|
|
|
|
|
fn box_id(&self) -> u64 {
|
2025-08-11 15:01:11 +09:00
|
|
|
self.base.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
|
|
|
|
self.base.parent_type_id
|
2025-08-11 12:09:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2025-08-15 01:21:37 +00:00
|
|
|
let ready = *self.is_ready.read().unwrap();
|
2025-08-11 12:09:14 +09:00
|
|
|
if ready {
|
2025-08-15 01:21:37 +00:00
|
|
|
let result = self.result.read().unwrap();
|
2025-08-11 12:09:14 +09:00
|
|
|
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)")
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-11 15:01:11 +09:00
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
2025-08-11 12:09:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for NyashFutureBox {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
self.fmt_box(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
// Export NyashFutureBox as FutureBox for consistency
|
|
|
|
|
pub type FutureBox = NyashFutureBox;
|
2025-08-10 02:45:57 +00:00
|
|
|
|
🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一:
- BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現
- FileBox: Arc<Mutex<File>>でファイルハンドル管理
- JSONBox: Arc<Mutex<Value>>でJSON値を保持
- HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理
- StreamBox: Arc<Mutex>でストリームバッファと位置を管理
- RegexBox: Arc<Regex>で軽量ラッパー実装
各Boxに実用的なメソッドも追加:
- BufferBox: write, read, readAll, clear, length, append, slice
- FileBox: read, write, exists, delete, copy
- JSONBox: parse, stringify, get, set, has, keys
- HttpClientBox: get, post, put, delete, request
- StreamBox: write, read, position, length, reset
- RegexBox: test, find, findAll, replace, split
interpreterに新Box用のメソッド実行を追加:
- data_methods.rs: BufferBox, JSONBox, RegexBox
- network_methods.rs: HttpClientBox, StreamBox
これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 13:03:42 +09:00
|
|
|
impl FutureBox {
|
|
|
|
|
/// wait_and_get()の実装 - await演算子で使用
|
|
|
|
|
pub fn wait_and_get(&self) -> Result<Box<dyn NyashBox>, String> {
|
|
|
|
|
Ok(self.get())
|
2025-08-10 02:45:57 +00:00
|
|
|
}
|
|
|
|
|
}
|