Crate tendrils

Crate tendrils 

Source
Expand description

§🌿 tendrils

Tiny, flexible helpers for managing shared state safely in async Rust.

§✨ Features

  • tokio (default): async Mutex / RwLock support
  • parking_lot: lightweight sync Mutex / RwLock
  • trace: adds tracing spans to lock usage
  • metrics: logs lock duration (uses saydbg)

§Example

use tendrils::with_state;
use std::sync::Arc;
use tokio::sync::Mutex;

#[derive(Default)]
struct AppState { count: u32 }

type Shared = Arc<Mutex<AppState>>;

#[tokio::test]
async fn main() {
    let state: Shared = Arc::new(Mutex::new(AppState::default()));

    with_state(&state, |s| {
        s.count += 1;
        println!("Count = {}", s.count);
    }).await;
}

Structs§

RwTendrils
A small, ergonomic handle for shared state that is often read and occasionally written: wraps Arc<RwLock<T>>.
Tendrils
A small, ergonomic handle for shared mutable state: wraps Arc<Mutex<T>> (Tokio or parking_lot depending on features).

Functions§

try_with_state
Attempts to lock a Mutex and execute a closure without blocking.
with_state
Executes a synchronous closure inside a locked Arc<Mutex<T>>.
with_state_async
Executes an async closure inside a locked Arc<Mutex<T>>, ensuring the lock is dropped before awaiting the future.
with_state_read
Executes a read-only closure inside a locked Arc<RwLock<T>>.
with_state_write
Executes a write closure inside a locked Arc<RwLock<T>>.