Skip to content

Commit 9bcb7e6

Browse files
committed
Add serde_attr example and update README.md
1 parent e1b26ea commit 9bcb7e6

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,38 @@ fn foo() {}
196196
mod foo_fuzz {}
197197
```
198198

199+
#### Serde field attributes on function arguments
200+
201+
The `test_fuzz` macro allows [Serde field attributes] to be applied to function arguments. This provides another tool for dealing with difficult types.
202+
203+
The following is an example. Traits `serde::Serialize` and `serde::Deserialize` cannot be derived for `Context` because it contains a `Mutex`. However, `Context` implements `Default`. So applying `#[serde(skip)]` to the `Context` argument causes it to be skipped when serializing, and to take its default value when deserializing.
204+
205+
```rust
206+
use std::sync::Mutex;
207+
208+
// Traits `serde::Serialize` and `serde::Deserialize` cannot be derived for `Context` because it
209+
// contains a `Mutex`.
210+
#[derive(Default)]
211+
struct Context {
212+
lock: Mutex<()>,
213+
}
214+
215+
impl Clone for Context {
216+
fn clone(&self) -> Self {
217+
Self {
218+
lock: Mutex::new(()),
219+
}
220+
}
221+
}
222+
223+
#[test_fuzz::test_fuzz]
224+
fn target(#[serde(skip)] context: Context, x: i32) {
225+
assert!(x >= 0);
226+
}
227+
```
228+
229+
Note that when Serde field attributes are applied to an argument, the `test_fuzz` macro performs no other [conversions] on the argument.
230+
199231
### `test_fuzz_impl` macro
200232

201233
Whenever the [`test_fuzz`] macro is used in an `impl` block,
@@ -516,6 +548,7 @@ We reserve the right to change the format of corpora, crashes, hangs, and work q
516548
[Overview]: #overview
517549
[Postcard]: https://github.com/jamesmunns/postcard
518550
[Serde attributes]: https://serde.rs/attributes.html
551+
[Serde field attributes]: https://serde.rs/field-attrs.html
519552
[Substrate externalities]: https://substrate.dev/docs/en/knowledgebase/runtime/tests#mock-runtime-storage
520553
[The Cargo Book]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#choosing-features
521554
[Tips and tricks]: #tips-and-tricks
@@ -554,6 +587,7 @@ We reserve the right to change the format of corpora, crashes, hangs, and work q
554587
[associated_type.rs]: https://github.com/trailofbits/test-fuzz/blob/master/examples/tests/associated_type.rs#L26
555588
[auto-generate corpus files]: #auto-generated-corpus-files
556589
[conversion.rs]: https://github.com/trailofbits/test-fuzz/blob/master/examples/tests/conversion.rs#L5
590+
[conversions]: #serializable--deserializable-arguments
557591
[deriving them]: https://serde.rs/derive.html
558592
[is not enabled]: https://github.com/rust-lang/rust/issues/45599#issuecomment-460488107
559593
[patch]: https://doc.rust-lang.org/edition-guide/rust-2018/cargo-and-crates-io/replacing-dependencies-with-patch.html

examples/tests/serde_attr.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![cfg_attr(dylint_lib = "general", allow(crate_wide_allow))]
2+
#![allow(unused)]
3+
4+
use std::sync::Mutex;
5+
6+
// Traits `serde::Serialize` and `serde::Deserialize` cannot be derived for `Context` because it
7+
// contains a `Mutex`.
8+
#[derive(Default)]
9+
struct Context {
10+
lock: Mutex<()>,
11+
}
12+
13+
impl Clone for Context {
14+
fn clone(&self) -> Self {
15+
Self {
16+
lock: Mutex::new(()),
17+
}
18+
}
19+
}
20+
21+
#[test_fuzz::test_fuzz]
22+
fn target(#[serde(skip)] context: Context, x: i32) {
23+
assert!(x >= 0);
24+
}
25+
26+
#[test]
27+
fn test() {
28+
target(Context::default(), 0);
29+
}

0 commit comments

Comments
 (0)