pub struct Shared<T> { /* private fields */ }
Expand description
A generic shared wrapper that implements the Shareable pattern
This provides a concrete implementation of the sharing pattern that can be used directly for simple cases where no custom behavior is needed.
§Examples
use turbomcp_core::shared::{Shared, Shareable};
#[derive(Debug)]
struct Counter {
value: u64,
}
impl Counter {
fn new() -> Self {
Self { value: 0 }
}
fn increment(&mut self) {
self.value += 1;
}
fn get(&self) -> u64 {
self.value
}
}
// Create a shared counter
let counter = Counter::new();
let shared = Shared::new(counter);
// Clone for use in multiple tasks
let shared1 = shared.clone();
let shared2 = shared.clone();
// Use in concurrent tasks
let handle1 = tokio::spawn(async move {
shared1.with_mut(|c| c.increment()).await;
});
let handle2 = tokio::spawn(async move {
shared2.with(|c| c.get()).await
});
Implementations§
Sourcepub async fn with<F, R>(&self, f: F) -> R
pub async fn with<F, R>(&self, f: F) -> R
Execute a closure with read access to the inner value
Sourcepub async fn with_mut<F, R>(&self, f: F) -> R
pub async fn with_mut<F, R>(&self, f: F) -> R
Execute a closure with mutable access to the inner value
Sourcepub async fn with_async<F, Fut, R>(&self, f: F) -> R
pub async fn with_async<F, Fut, R>(&self, f: F) -> R
Execute an async closure with read access to the inner value
Sourcepub async fn with_mut_async<F, Fut, R>(&self, f: F) -> R
pub async fn with_mut_async<F, Fut, R>(&self, f: F) -> R
Execute an async closure with mutable access to the inner value
Trait Implementations§
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more