Crate rapt [] [src]

Rapt

Runtime application instrumentation toolkit

Rapt provides a standard interface for providing runtime introspection capabilities for Rust-based libraries and applications.

There are a few key components to this library:

Instrument

Instrument is a thread-safe wrapper for a Serde-serializable value. It is parametrized over Listener

Instruments are cloneable and the wrapped value can be safely updated using Instrument#update.

Instrument board

Instrument board is a concept of aggregating a number of instruments into a single structure and implementing or deriving Instruments for it. Please note that if derivation is used (using rapt_derive crate), the last type parameter must be bound to Listener:

extern crate rapt;
extern crate serde;
#[macro_use]
extern crate rapt_derive;

use serde::Serialize;
use rapt::{Listener, Instrument};

#[allow(dead_code)]
#[derive(Instruments)]
struct AppInstruments<T : Serialize, L: Listener> {
    value: Instrument<T, L>,
}

pub fn main() {}

In the above example, L must always remain the last type parameter.

It is parametrized over Listener.

Listener

Listener is a trait that allows instruments to notify interested parties about updates

Example

extern crate rapt;
extern crate serde;

#[macro_use]
extern crate rapt_derive;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate assert_matches;

use serde::Serialize;
use rapt::{Listener, Instrument};

#[derive(Debug, Clone, Copy, Serialize)]
enum Status { Stopped, Started }

#[derive(Clone, Serialize)]
struct Service {
    status: Status,
}

#[derive(Instruments)]
struct AppInstruments<L: Listener> {
    http_server: Instrument<Service, L>,
}

use std::thread;
use std::time::Duration;

fn main() {
    let app_instruments = AppInstruments::<()> {
        http_server: Instrument::new(Service { status: Status::Stopped }),
    };
    let http_server_svc = app_instruments.http_server.clone();
    let thread_handle = thread::spawn(move || {
        thread::sleep(Duration::from_millis(100));
        let _ = http_server_svc.update(|v| v.status = Status::Started).unwrap();
    });
    thread::sleep(Duration::from_millis(200));
    assert_matches!(app_instruments.http_server.read().and_then(|v| Ok(v.status)), Ok(Status::Started));
    let _ = thread_handle.join().unwrap();
}

Modules

ser

Serialization utilities

Structs

Instrument

A thread-safe wrapper for a Serde-serializable value

Enums

ReadError

An error that might occur during Instrument#read

UpdateError

An error that might occur during Instrument#update

Traits

Instruments

Instrument board trait

Listener

Trait that allows instruments to notify interested parties about updates