tyco

Crates.iotyco
lib.rstyco
version0.0.9
sourcesrc
created_at2024-04-23 11:02:38.288115
updated_at2024-04-23 11:02:38.288115
descriptionMacro for generating scoped TYped COntexts
homepage
repositoryhttps://github.com/Flowneee/tyco
max_upload_size
id1217416
size51,913
Andrei Kononov (Flowneee)

documentation

README

tyco - crate for creating scoped TYped COntexts.

Crates.io docs.rs CI

This crate allows you to define and store typed information in current execution context (sync or async) and pass it down call stack without explicitely speicfying it in function arguments. This is helpful for implementing some additional functionality without main business logic messing up your main business logic code.

It is inspired by and similar to opentelemetry::Context, how loggers stored id log/slog/tracing, using TLS for storing and accessing values. Unlike opentelemetry::Context, which is basically HashMap<TypeId, Any>, current crate allows to work any type, which may (or may not) result in more efficient and clean code.

Example

Basic example of how to define context, which will contain some tracing identifier, which can be used for things like distinguishing HTTP requests.

use tyco::{context, FutureExt, TypedContext};

mod trace_id {
    use super::*;

    #[derive(Clone, Default, Debug, PartialEq)]
    pub struct TraceId(String);

    tyco::context!(TraceId);
}

let t = trace_id::TraceId::default();

let spawned_fut = tokio::spawn(
    async { println!("{:?}", trace_id::TraceId::current()) }
    // 2 ways to pass context to spawned future
    .with_current::<trace_id::TraceId>()
    .with(t.clone()),
);

Also check more complex examples (on how to use it with HTTP server/client for example).

Commit count: 4

cargo fmt