Skip to main content

freya_query/
captured.rs

1use std::{
2    hash::Hash,
3    ops::{
4        Deref,
5        DerefMut,
6    },
7};
8
9/// Capture values to use later inside Queries or Mutations, but with a catch, if the capture value changes the mutation will not recapture it because
10/// the [PartialEq] implementation always returns `true`.
11///
12/// So in other words `Capture(1) == Capture(5)` will be `true`.
13///
14/// **This is intended to use for value types that are not mean to be diffed and that are expected to maintain their value across time.
15/// Like "Clients" of external resources such as API Clients.**
16///
17/// Just so its clear, you might or not need this, use carefully.
18#[derive(Clone)]
19pub struct Captured<T: Clone>(pub T);
20
21impl<T: Clone> Hash for Captured<T> {
22    fn hash<H: std::hash::Hasher>(&self, _state: &mut H) {}
23}
24
25impl<T: Clone> PartialEq for Captured<T> {
26    fn eq(&self, _other: &Self) -> bool {
27        true
28    }
29}
30
31impl<T: Clone> Eq for Captured<T> {}
32
33impl<T: Clone> Deref for Captured<T> {
34    type Target = T;
35
36    fn deref(&self) -> &Self::Target {
37        &self.0
38    }
39}
40
41impl<T: Clone> DerefMut for Captured<T> {
42    fn deref_mut(&mut self) -> &mut Self::Target {
43        &mut self.0
44    }
45}