2025-08-10 02:45:57 +00:00
|
|
|
|
//! FutureBox 🔄 - 非同期処理基盤
|
|
|
|
|
|
// Nyashの箱システムによる非同期処理の基盤を提供します。
|
|
|
|
|
|
// 参考: 既存Boxの設計思想
|
|
|
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
|
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
2025-08-10 03:21:24 +00:00
|
|
|
|
use std::any::Any;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
use std::sync::{Arc, Condvar, Mutex, Weak};
|
2025-08-10 02:45:57 +00:00
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub struct NyashFutureBox {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
inner: Arc<Inner>,
|
2025-08-11 12:09:14 +09:00
|
|
|
|
base: BoxBase,
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 03:41:51 +09:00
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
struct FutureState {
|
|
|
|
|
|
result: Option<Box<dyn NyashBox>>,
|
|
|
|
|
|
ready: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
struct Inner {
|
|
|
|
|
|
state: Mutex<FutureState>,
|
|
|
|
|
|
cv: Condvar,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// A weak handle to a Future's inner state.
|
|
|
|
|
|
/// Used for non-owning registries (TaskGroup/implicit group) to avoid leaks.
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
|
pub struct FutureWeak {
|
2025-09-16 03:54:44 +09:00
|
|
|
|
inner: Weak<Inner>,
|
2025-09-02 03:41:51 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
|
impl Clone for NyashFutureBox {
|
|
|
|
|
|
fn clone(&self) -> Self {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
Self {
|
|
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
|
|
base: BoxBase::new(),
|
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl NyashFutureBox {
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
|
Self {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
inner: Arc::new(Inner {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
state: Mutex::new(FutureState {
|
|
|
|
|
|
result: None,
|
|
|
|
|
|
ready: false,
|
|
|
|
|
|
}),
|
2025-09-02 03:41:51 +09:00
|
|
|
|
cv: Condvar::new(),
|
|
|
|
|
|
}),
|
2025-08-11 12:09:14 +09:00
|
|
|
|
base: BoxBase::new(),
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
|
/// Set the result of the future
|
|
|
|
|
|
pub fn set_result(&self, value: Box<dyn NyashBox>) {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
let mut st = self.inner.state.lock().unwrap();
|
|
|
|
|
|
st.result = Some(value);
|
|
|
|
|
|
st.ready = true;
|
|
|
|
|
|
self.inner.cv.notify_all();
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
|
/// Get the result (blocks until ready)
|
|
|
|
|
|
pub fn get(&self) -> Box<dyn NyashBox> {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
let mut st = self.inner.state.lock().unwrap();
|
|
|
|
|
|
while !st.ready {
|
|
|
|
|
|
st = self.inner.cv.wait(st).unwrap();
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
2025-09-02 03:41:51 +09:00
|
|
|
|
st.result.as_ref().unwrap().clone_box()
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
|
/// Check if the future is ready
|
|
|
|
|
|
pub fn ready(&self) -> bool {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
self.inner.state.lock().unwrap().ready
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
2025-09-02 03:41:51 +09:00
|
|
|
|
|
|
|
|
|
|
/// Create a non-owning weak handle to this Future's state
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub fn downgrade(&self) -> FutureWeak {
|
|
|
|
|
|
FutureWeak {
|
|
|
|
|
|
inner: Arc::downgrade(&self.inner),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl NyashBox for NyashFutureBox {
|
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
2025-08-15 14:29:47 +09:00
|
|
|
|
Box::new(self.clone())
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-15 04:29:41 +00:00
|
|
|
|
/// 仮実装: clone_boxと同じ(後で修正)
|
|
|
|
|
|
fn share_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
self.clone_box()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-10 03:21:24 +00:00
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
let ready = self.inner.state.lock().unwrap().ready;
|
2025-08-10 03:21:24 +00:00
|
|
|
|
if ready {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
let st = self.inner.state.lock().unwrap();
|
|
|
|
|
|
if let Some(value) = st.result.as_ref() {
|
2025-08-10 03:21:24 +00:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
|
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-09-02 03:41:51 +09:00
|
|
|
|
let ready = self.inner.state.lock().unwrap().ready;
|
2025-08-11 12:09:14 +09:00
|
|
|
|
if ready {
|
2025-09-02 03:41:51 +09:00
|
|
|
|
let st = self.inner.state.lock().unwrap();
|
|
|
|
|
|
if let Some(value) = st.result.as_ref() {
|
2025-08-11 12:09:14 +09:00
|
|
|
|
write!(f, "Future(ready: {})", value.to_string_box().value)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
write!(f, "Future(ready: void)")
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
write!(f, "Future(pending)")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-11 15:01:11 +09:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-02 03:41:51 +09:00
|
|
|
|
|
|
|
|
|
|
impl FutureWeak {
|
|
|
|
|
|
/// Try to upgrade and check readiness
|
|
|
|
|
|
pub(crate) fn is_ready(&self) -> Option<bool> {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.inner
|
|
|
|
|
|
.upgrade()
|
|
|
|
|
|
.map(|arc| arc.state.lock().unwrap().ready)
|
2025-09-02 03:41:51 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|