Trackable

Trait Trackable 

Source
pub trait Trackable {
    type Event: From<Location>;

    // Required methods
    fn history(&self) -> Option<&History<Self::Event>>;
    fn history_mut(&mut self) -> Option<&mut History<Self::Event>>;

    // Provided methods
    fn track<F>(&mut self, f: F)
       where F: FnOnce() -> Self::Event { ... }
    fn in_tracking(&self) -> bool { ... }
}
Expand description

This trait allows to track an instance of an implementation type.

A trackable instance can have a tracking history that manages own backtrace-like (but more general) history for tracking.

You can add entries to the history by calling tracking macros(e.g., track!).

See TrackableError as a typical implementaion of this trait.

§Examples

Defines a trackable type.

#[macro_use]
extern crate trackable;

use trackable::{Trackable, History, Location};

#[derive(Default)]
struct TrackableObject {
    history: History<Location>,
}
impl Trackable for TrackableObject {
    type Event = Location;
    fn history(&self) -> Option<&History<Self::Event>> {
        Some(&self.history)
    }
    fn history_mut(&mut self) -> Option<&mut History<Self::Event>> {
        Some(&mut self.history)
    }
}

fn main() {
    let o = TrackableObject::default();
    let o = track!(o);
    let o = track!(o, "Hello");
    let o = track!(o, "Hello {}", "World!");

    assert_eq!(format!("\n{}", o.history).replace('\\', "/"), r#"
HISTORY:
  [0] at src/lib.rs:23
  [1] at src/lib.rs:24 -- Hello
  [2] at src/lib.rs:25 -- Hello World!
"#);
}

Required Associated Types§

Source

type Event: From<Location>

Event type which a history of an instance of this type can have.

Required Methods§

Source

fn history(&self) -> Option<&History<Self::Event>>

Returns the reference of the tracking history of this instance.

If it is not being tracked, this will return `None.

Source

fn history_mut(&mut self) -> Option<&mut History<Self::Event>>

Returns the mutable reference of the tracking history of this instance.

If it is not being tracked, this will return `None.

Provided Methods§

Source

fn track<F>(&mut self, f: F)
where F: FnOnce() -> Self::Event,

Add an event into the tail of the history of this instance.

Typically, this is called via track! macro.

Source

fn in_tracking(&self) -> bool

Returns true if it is being tracked, otherwise false.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, E: Trackable> Trackable for Result<T, E>

Source§

type Event = <E as Trackable>::Event

Source§

fn history(&self) -> Option<&History<Self::Event>>

Source§

fn history_mut(&mut self) -> Option<&mut History<Self::Event>>

Source§

impl<T: Trackable> Trackable for Option<T>

Source§

type Event = <T as Trackable>::Event

Source§

fn history(&self) -> Option<&History<Self::Event>>

Source§

fn history_mut(&mut self) -> Option<&mut History<Self::Event>>

Source§

impl<T: Trackable> Trackable for Poll<T>

Source§

type Event = <T as Trackable>::Event

Source§

fn history(&self) -> Option<&History<Self::Event>>

Source§

fn history_mut(&mut self) -> Option<&mut History<Self::Event>>

Implementors§