ConsumableShared

Struct ConsumableShared 

Source
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§

Source§

impl<T> ConsumableShared<T>
where T: Send + 'static,

Source

pub async fn with<F, R>(&self, f: F) -> Result<R, SharedError>
where F: FnOnce(&T) -> R + Send,

Execute a closure with read access to the inner value

Returns an error if the value has been consumed.

Source

pub async fn with_mut<F, R>(&self, f: F) -> Result<R, SharedError>
where F: FnOnce(&mut T) -> R + Send,

Execute a closure with mutable access to the inner value

Returns an error if the value has been consumed.

Source

pub async fn with_async<F, Fut, R>(&self, f: F) -> Result<R, SharedError>
where F: FnOnce(&T) -> Fut + Send, Fut: Future<Output = R> + Send,

Execute an async closure with read access to the inner value

Returns an error if the value has been consumed.

Source

pub async fn with_mut_async<F, Fut, R>(&self, f: F) -> Result<R, SharedError>
where F: FnOnce(&mut T) -> Fut + Send, Fut: Future<Output = R> + Send,

Execute an async closure with mutable access to the inner value

Returns an error if the value has been consumed.

Source

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.

Source

pub async fn is_available(&self) -> bool

Check if the value is still available (not consumed)

Trait Implementations§

Source§

impl<T> Clone for ConsumableShared<T>
where T: Send + 'static,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ConsumableShared<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Shareable<T> for ConsumableShared<T>
where T: Send + 'static,

Source§

fn new(inner: T) -> Self

Create a new shared wrapper around the inner type

Auto Trait Implementations§

§

impl<T> Freeze for ConsumableShared<T>

§

impl<T> !RefUnwindSafe for ConsumableShared<T>

§

impl<T> Send for ConsumableShared<T>
where T: Send,

§

impl<T> Sync for ConsumableShared<T>
where T: Send,

§

impl<T> Unpin for ConsumableShared<T>

§

impl<T> !UnwindSafe for ConsumableShared<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V