context_trait/context.rs
1//! The core [`WithContext`] wrapper type.
2
3/// Pairs a value with a context that supplies trait implementations.
4///
5/// By wrapping a value in `WithContext`, you can provide custom
6/// implementations of traits like `Ord`, `Hash`, or `Display` without
7/// modifying the original type.
8///
9/// # Examples
10///
11/// ```
12/// use context_trait::{WithContext, OrdContext};
13/// use std::cmp::Ordering;
14///
15/// let ctx = OrdContext { compare: |a: &i32, b: &i32| b.cmp(a) };
16/// let a = WithContext { inner: 1, ctx };
17/// let b = WithContext { inner: 2, ctx };
18/// assert_eq!(a.cmp(&b), Ordering::Greater); // reversed!
19/// ```
20#[derive(Debug, Clone, Copy)]
21pub struct WithContext<T, Ctx> {
22 /// The wrapped value.
23 pub inner: T,
24 /// The context providing trait implementations.
25 pub ctx: Ctx,
26}