Skip to main content

context_trait/
lib.rs

1#![deny(unsafe_code)]
2
3//! # context-trait
4//!
5//! Runtime-synthesized trait instances using function pointer tables,
6//! scoped to a callback.
7//!
8//! This crate provides [`WithContext`], a wrapper that pairs a value with
9//! a context supplying trait implementations. Built-in contexts include
10//! [`OrdContext`], [`HashContext`], and [`DisplayContext`].
11//!
12//! Declarative macros [`with_ord!`], [`with_hash!`], and [`with_display!`]
13//! make it easy to use non-default trait implementations in a scoped block.
14//!
15//! The [`impl_context_trait!`] macro lets users define new context types
16//! for arbitrary traits.
17//!
18//! # Examples
19//!
20//! ```
21//! use context_trait::{with_ord, OrdContext, WithContext};
22//!
23//! let items = vec![3i32, 1, 4, 1, 5];
24//! with_ord!(items, |a: &i32, b: &i32| b.cmp(a), |wrapped: &[WithContext<i32, OrdContext<i32>>]| {
25//!     let mut sorted = wrapped.to_vec();
26//!     sorted.sort();
27//!     let values: Vec<i32> = sorted.into_iter().map(|w| w.inner).collect();
28//!     assert_eq!(values, vec![5, 4, 3, 1, 1]);
29//! });
30//! ```
31
32mod context;
33mod display_ctx;
34mod hash_ctx;
35mod macros;
36mod ord_ctx;
37
38pub use context::WithContext;
39pub use display_ctx::DisplayContext;
40pub use hash_ctx::HashContext;
41pub use ord_ctx::OrdContext;