1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! # 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]);
//! });
//! ```
pub use WithContext;
pub use DisplayContext;
pub use HashContext;
pub use OrdContext;