You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cache_diff/README.md
+61-4Lines changed: 61 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -26,11 +26,16 @@ When it returns an empty list, the two structs are identical.
26
26
27
27
You can manually implement the trait, or you can use the `#[derive(CacheDiff)]` macro to automatically generate the implementation.
28
28
29
-
Attributes are:
29
+
Top level struct configuration (Container attributes):
30
30
31
-
-`cache_diff(rename = "<new name>")` Specify custom name for the field
32
-
-`cache_diff(ignore)` Ignores the given field
33
-
-`cache_diff(display = <function>)` Specify a function to call to display the field
31
+
-`#[cache_diff(custom = <function>)]` Specify a function that receives references to both current and old values and returns a Vec of strings if there are any differences. This function is only called once. It can be in combination with `#[cache_diff(custom)]` on fields to combine multiple related fields into one diff (for example OS distribution and version) or to split apart a monolithic field into multiple differences (for example an "inventory" struct that contains a version and CPU architecture information).
32
+
33
+
Attributes for fields are:
34
+
35
+
-`#[cache_diff(rename = "<new name>")]` Specify custom name for the field
36
+
-`#[cache_diff(ignore)]` or `#[cache_diff(ignore = "<reason>")]` Ignores the given field with an optional comment string.
37
+
If the field is ignored because you're using a custom diff function (see container attributes) you can use
38
+
`cache_diff(ignore = "custom")` which will check that the container implements a custom function.
assert_eq!(diff.join(""), "version (`custom 3.3.0` to `custom 3.4.0`)");
190
195
```
191
196
197
+
### Customize one or more field differences
198
+
199
+
You can provide a custom implementation for a diffing a subset of fields without having to roll your own implementation.
200
+
201
+
#### Custom logic for one field example
202
+
203
+
Here's an example where someone wants to bust the cache after N cache calls. Everything else other than `cache_usage_count` can be derived. If you want to keep the existing derived difference checks, but add on a custom one you can do it like this:
Copy file name to clipboardExpand all lines: cache_diff/src/lib.rs
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -9,14 +9,14 @@
9
9
//!
10
10
//! Top level struct configuration (Container attributes):
11
11
//!
12
-
//! - `#[cache_diff(custom = <function>)]` Specify a function that receives references to both current and old values and returns a Vec of strings if there are any differences. This function is only called once. It can be in combination with `#[cache_diff(custom)]` on fields to combine multiple related fields into one diff (for example OS distribution and version) or to split apart a monolithic field into multiple differences (for example an "inventory" struct that contains a version and CPU architecture information).
12
+
//! - `#[cache_diff(custom = <function>)]` Specify a function that receives references to both current and old values and returns a Vec of strings if there are any differences. This function is only called once. It can be in combination with `#[cache_diff(custom)]` on fields to combine multiple related fields into one diff (for example OS distribution and version) or to split apart a monolithic field into multiple differences (for example an "inventory" struct that contains a version and CPU architecture information).
13
13
//!
14
14
//! Attributes for fields are:
15
15
//!
16
-
//! - `#[cache_diff(rename = "<new name>")]` Specify custom name for the field
17
-
//! - `#[cache_diff(ignore)]` or `#[cache_diff(ignore = "<reason>")]` Ignores the given field with an optional comment string.
18
-
//! If the field is ignored because you're using a custom diff function (see container attributes) you can use
19
-
//! `cache_diff(ignore = "custom")` which will check that the container implements a custom function.
16
+
//! - `#[cache_diff(rename = "<new name>")]` Specify custom name for the field
17
+
//! - `#[cache_diff(ignore)]` or `#[cache_diff(ignore = "<reason>")]` Ignores the given field with an optional comment string.
18
+
//! If the field is ignored because you're using a custom diff function (see container attributes) you can use
19
+
//! `cache_diff(ignore = "custom")` which will check that the container implements a custom function.
0 commit comments