pub struct ConsumableShared<T> { /* private fields */ }
Expand description
Specialized shared wrapper for types that can be consumed (like servers)
This wrapper allows the inner value to be extracted for consumption (such as running a server), after which the wrapper becomes unusable.
§Examples
use turbomcp_core::shared::{ConsumableShared, Shareable};
struct Server {
name: String,
}
impl Server {
fn new(name: String) -> Self {
Self { name }
}
fn run(self) -> String {
format!("Running server: {}", self.name)
}
fn status(&self) -> String {
format!("Server {} is ready", self.name)
}
}
let server = Server::new("test".to_string());
let shared = ConsumableShared::new(server);
let shared_clone = shared.clone();
// Check status before consumption
let status = shared.with(|s| s.status()).await?;
assert_eq!(status, "Server test is ready");
// Consume the server
let server = shared.consume().await?;
let result = server.run();
assert_eq!(result, "Running server: test");
// Wrapper is now unusable (using clone)
assert!(shared_clone.with(|s| s.status()).await.is_err());
Implementations§
Sourcepub async fn with<F, R>(&self, f: F) -> Result<R, SharedError>
pub async fn with<F, R>(&self, f: F) -> Result<R, SharedError>
Execute a closure with read access to the inner value
Returns an error if the value has been consumed.
Sourcepub async fn with_mut<F, R>(&self, f: F) -> Result<R, SharedError>
pub async fn with_mut<F, R>(&self, f: F) -> Result<R, SharedError>
Execute a closure with mutable access to the inner value
Returns an error if the value has been consumed.
Sourcepub async fn with_async<F, Fut, R>(&self, f: F) -> Result<R, SharedError>
pub async fn with_async<F, Fut, R>(&self, f: F) -> Result<R, SharedError>
Execute an async closure with read access to the inner value
Returns an error if the value has been consumed.
Sourcepub async fn with_mut_async<F, Fut, R>(&self, f: F) -> Result<R, SharedError>
pub async fn with_mut_async<F, Fut, R>(&self, f: F) -> Result<R, SharedError>
Execute an async closure with mutable access to the inner value
Returns an error if the value has been consumed.
Sourcepub async fn consume(self) -> Result<T, SharedError>
pub async fn consume(self) -> Result<T, SharedError>
Consume the inner value, making the wrapper unusable
This extracts the value from the wrapper, after which all other
operations will return SharedError::Consumed
.
Sourcepub async fn is_available(&self) -> bool
pub async fn is_available(&self) -> bool
Check if the value is still available (not consumed)