Expand description
Swap out a type’s Ord, Hash, Display (or any user-defined trait)
for one block of code, without newtype boilerplate.
Sometimes you want to sort a Vec<i32> descending, hash a String
case-insensitively, or render a number with a custom prefix, but only
for one operation. The orthodox Rust answer is to wrap the value in a
newtype that implements the trait differently. That works, but it
requires a new struct, manual impls, and you lose access to all the
existing impls on the inner type.
This crate offers a lighter alternative. WithContext<T, Ctx> is a
wrapper that pairs a value with a context, a small Copy struct of
function pointers that supplies the relevant trait implementation.
Standard library APIs that take Ord / Hash / Display work
unchanged; the context decides the behavior.
Three built-in contexts cover the common cases:
OrdContext<T>supplies a custom comparator.HashContext<T>supplies a custom hasher.DisplayContext<T>supplies a custom formatter.
And three corresponding macros wrap up the lift / call / project dance:
For traits beyond these three, impl_context_trait! generates a new
context type for an arbitrary trait of yours.
§Why function pointers?
Contexts hold fn pointers, not Box<dyn Fn>. This makes the wrapper
Copy regardless of T, which is what lets BTreeSet,
slice::sort, and HashMap accept it without complaint. It also
means the comparator must be stateless. If you need captured state,
reach for a newtype.
See docs/phase3-context-trait.md for the full design
rationale.
§Examples
Sort a slice descending without a newtype:
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(); // uses the descending comparator
let values: Vec<i32> = sorted.into_iter().map(|w| w.inner).collect();
assert_eq!(values, vec![5, 4, 3, 1, 1]);
});Macros§
- impl_
context_ trait - Define a new context type and its trait implementation for
WithContext. - with_
display - Wraps items in
WithContext<T, DisplayContext<T>>with a custom display function, runs a callback, and provides the wrapped slice. - with_
hash - Wraps items in
WithContext<T, HashContext<T>>with a custom hash function, runs a callback, and provides the wrapped slice. - with_
ord - Wraps items in
WithContext<T, OrdContext<T>>with a custom comparator, runs a callback, and provides the wrapped slice.
Structs§
- Display
Context - A context providing a custom display function.
- Hash
Context - A context providing a custom hash function.
- OrdContext
- A context providing a custom comparison function.
- With
Context - Pairs a value with a context that supplies trait implementations.