pub struct WithContext<T, Ctx> {
pub inner: T,
pub ctx: Ctx,
}Expand description
Pairs a value with a context that supplies trait implementations.
By wrapping a value in WithContext, you can provide custom
implementations of traits like Ord, Hash, or Display without
modifying the original type.
§Examples
use context_trait::{WithContext, OrdContext};
use std::cmp::Ordering;
let ctx = OrdContext { compare: |a: &i32, b: &i32| b.cmp(a) };
let a = WithContext { inner: 1, ctx };
let b = WithContext { inner: 2, ctx };
assert_eq!(a.cmp(&b), Ordering::Greater); // reversed!Fields§
§inner: TThe wrapped value.
ctx: CtxThe context providing trait implementations.
Trait Implementations§
Source§impl<T, Ctx> Clone for WithContext<T, Ctx>
impl<T, Ctx> Clone for WithContext<T, Ctx>
Source§fn clone(&self) -> WithContext<T, Ctx>
fn clone(&self) -> WithContext<T, Ctx>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T, Ctx> Debug for WithContext<T, Ctx>
impl<T, Ctx> Debug for WithContext<T, Ctx>
Source§impl<T> Display for WithContext<T, DisplayContext<T>>
impl<T> Display for WithContext<T, DisplayContext<T>>
Source§impl<T> Hash for WithContext<T, HashContext<T>>
impl<T> Hash for WithContext<T, HashContext<T>>
Source§impl<T> Ord for WithContext<T, OrdContext<T>>
impl<T> Ord for WithContext<T, OrdContext<T>>
Source§fn cmp(&self, other: &WithContext<T, OrdContext<T>>) -> Ordering
fn cmp(&self, other: &WithContext<T, OrdContext<T>>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T> PartialEq for WithContext<T, OrdContext<T>>
impl<T> PartialEq for WithContext<T, OrdContext<T>>
Source§fn eq(&self, other: &WithContext<T, OrdContext<T>>) -> bool
fn eq(&self, other: &WithContext<T, OrdContext<T>>) -> bool
Tests for
self and other values to be equal, and is used by ==.Source§impl<T> PartialOrd for WithContext<T, OrdContext<T>>
impl<T> PartialOrd for WithContext<T, OrdContext<T>>
impl<T, Ctx> Copy for WithContext<T, Ctx>
impl<T> Eq for WithContext<T, OrdContext<T>>
Auto Trait Implementations§
impl<T, Ctx> Freeze for WithContext<T, Ctx>
impl<T, Ctx> RefUnwindSafe for WithContext<T, Ctx>where
T: RefUnwindSafe,
Ctx: RefUnwindSafe,
impl<T, Ctx> Send for WithContext<T, Ctx>
impl<T, Ctx> Sync for WithContext<T, Ctx>
impl<T, Ctx> Unpin for WithContext<T, Ctx>
impl<T, Ctx> UnsafeUnpin for WithContext<T, Ctx>where
T: UnsafeUnpin,
Ctx: UnsafeUnpin,
impl<T, Ctx> UnwindSafe for WithContext<T, Ctx>where
T: UnwindSafe,
Ctx: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more