Skip to main content

runifold_tool/
state.rs

1use std::{ops::Deref, sync::Arc};
2
3/// Shared application state injected into a typed Tool handler.
4///
5/// State is host-only. It is not represented in the Tool's input schema,
6/// model arguments, transcript, or write-ahead Effect input.
7pub struct State<T: ?Sized>(Arc<T>);
8
9impl<T> State<T> {
10    /// Wraps owned application state for shared injection.
11    pub fn new(value: T) -> Self {
12        Self(Arc::new(value))
13    }
14}
15
16impl<T: ?Sized> State<T> {
17    /// Wraps existing shared application state.
18    pub const fn from_shared(value: Arc<T>) -> Self {
19        Self(value)
20    }
21
22    /// Returns the shared state allocation.
23    pub fn shared(&self) -> &Arc<T> {
24        &self.0
25    }
26
27    /// Consumes the wrapper and returns the shared state allocation.
28    pub fn into_shared(self) -> Arc<T> {
29        self.0
30    }
31}
32
33impl<T: ?Sized> Clone for State<T> {
34    fn clone(&self) -> Self {
35        Self(self.0.clone())
36    }
37}
38
39impl<T: ?Sized> Deref for State<T> {
40    type Target = T;
41
42    fn deref(&self) -> &Self::Target {
43        &self.0
44    }
45}
46
47impl<T: ?Sized> std::fmt::Debug for State<T> {
48    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        formatter
50            .debug_tuple("State")
51            .field(&std::any::type_name::<T>())
52            .finish()
53    }
54}