Skip to content

Commit d23b18f

Browse files
dj8yfodj8yf0μl
and
dj8yf0μl
authored
doc: make doc examples testable in ci (#326)
* doc: transfer doc for `BorshSerialize` * doc: replace references from Serialize, not reachable outside of derive context * doc: replace `ignore` -> `rust` whereever possible * doc: transfer doc for `BorshDeserialize` * doc: replace references from Deserialize, not reachable outside of derive context * doc: replace `ignore` -> `rust` whereever possible (deserialize) * doc: move top level crate doc to an .md file; add Shortcuts header * doc: transfer doc for `BorshSchema` * doc: replace references from Schema, not reachable outside of derive context * doc: replace `ignore` -> `rust` whereever possible (schema) * fmt: fix * doc: add separator between reexport docs and moved msg on item docs * doc: shortcuts in README.md * chore: remove most of hidden comments to not mess view on gh --------- Co-authored-by: dj8yf0μl <noreply@nowhere.org>
1 parent 73cb83b commit d23b18f

File tree

7 files changed

+1103
-1014
lines changed

7 files changed

+1103
-1014
lines changed

README.md

+6-57
Original file line numberDiff line numberDiff line change
@@ -39,65 +39,14 @@ fn test_simple_struct() {
3939
}
4040
```
4141

42-
## Features
42+
## Docs shortcuts
4343

44-
Opting out from Serde allows borsh to have some features that currently are not available for serde-compatible serializers.
45-
Currently we support two features: `borsh(init=<your initialization method name>` and `borsh(skip)` (the former one not available in Serde).
44+
Following pages are highlighted here just to give reader a chance at learning that
45+
they exist.
4646

47-
`borsh(init=...)` allows to automatically run an initialization function right after deserialization. This adds a lot of convenience for objects that are architectured to be used as strictly immutable. Usage example:
48-
49-
```rust
50-
#[derive(BorshSerialize, BorshDeserialize)]
51-
#[borsh(init=init)]
52-
struct Message {
53-
message: String,
54-
timestamp: u64,
55-
public_key: CryptoKey,
56-
signature: CryptoSignature
57-
hash: CryptoHash
58-
}
59-
60-
impl Message {
61-
pub fn init(&mut self) {
62-
self.hash = CryptoHash::new().write_string(self.message).write_u64(self.timestamp);
63-
self.signature.verify(self.hash, self.public_key);
64-
}
65-
}
66-
```
67-
68-
`borsh(skip)` allows to skip serializing/deserializing fields, assuming they implement `Default` trait, similarly to `#[serde(skip)]`.
69-
70-
```rust
71-
#[derive(BorshSerialize, BorshDeserialize)]
72-
struct A {
73-
x: u64,
74-
#[borsh(skip)]
75-
y: f32,
76-
}
77-
```
78-
79-
### Enum with explicit discriminant
80-
81-
`#[borsh(use_discriminant=false|true])` is required if you have an enum with explicit discriminant. This setting affects `BorshSerialize` and `BorshDeserialize` behaviour at the same time.
82-
83-
In the future, borsh will drop the requirement to explicitly use `#[borsh(use_discriminant=false|true)]`, and will default to `true`, but to make sure that the transition from the older versions of borsh (before 0.11 release) does not cause silent breaking changes in de-/serialization, borsh 1.0 will require to specify if the explicit enum discriminant should be used as a de-/serialization tag value.
84-
85-
If you don't specify `use_discriminant` option for enum with explicit discriminant, you will get an error:
86-
87-
```bash
88-
error: You have to specify `#[borsh(use_discriminant=true)]` or `#[borsh(use_discriminant=false)]` for all enums with explicit discriminant
89-
```
90-
91-
In order to preserve the behaviour of borsh versions before 0.11, which did not respect explicit enum discriminants for de-/serialization, use `#[borsh(use_discriminant=false)]`, otherwise, use `true`:
92-
93-
```rust
94-
#[derive(BorshDeserialize, BorshSerialize)]
95-
#[borsh(use_discriminant=false)]
96-
enum A {
97-
X,
98-
Y = 10,
99-
}
100-
```
47+
- [Derive Macro `BorshSerialize`](./docs/rustdoc_include/borsh_serialize.md)
48+
- [Derive Macro `BorshDeserialize`](./docs/rustdoc_include/borsh_deserialize.md)
49+
- [Derive Macro `BorshSchema`](./docs/rustdoc_include/borsh_schema.md)
10150

10251
## Advanced examples
10352

0 commit comments

Comments
 (0)