context-trait 0.1.0

Runtime-synthesized trait instances scoped to callbacks
Documentation
  • Coverage
  • 100%
    14 out of 14 items documented9 out of 9 items with examples
  • Size
  • Source code size: 21.11 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.91 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 42s Average build duration of successful builds.
  • all releases: 1m 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • joshburgess/reify-reflect
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • joshburgess

context-trait

Runtime-synthesized trait instances using function pointer tables, scoped to a callback.

This crate provides [WithContext], a wrapper that pairs a value with a context supplying trait implementations. Built-in contexts include [OrdContext], [HashContext], and [DisplayContext].

Declarative macros [with_ord!], [with_hash!], and [with_display!] make it easy to use non-default trait implementations in a scoped block.

The [impl_context_trait!] macro lets users define new context types for arbitrary traits.

Examples

use context_trait::{with_ord, OrdContext, WithContext};

let items = vec![3i32, 1, 4, 1, 5];
with_ord!(items, |a: &i32, b: &i32| b.cmp(a), |wrapped: &[WithContext<i32, OrdContext<i32>>]| {
    let mut sorted = wrapped.to_vec();
    sorted.sort();
    let values: Vec<i32> = sorted.into_iter().map(|w| w.inner).collect();
    assert_eq!(values, vec![5, 4, 3, 1, 1]);
});