Skip to content

Commit 2df62d4

Browse files
committed
Add a basic serilization/deserialization chapter to the tutorials book
1 parent cf5720e commit 2df62d4

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

src/core/array.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1218,26 +1218,30 @@ mod tests {
12181218

12191219
#[test]
12201220
fn array_serde_json() {
1221+
// ANCHOR: array_json_serde_snippet
12211222
let input = randu!(u8; 2, 2);
12221223
let serd = match serde_json::to_string(&input) {
12231224
Ok(serialized_str) => serialized_str,
12241225
Err(e) => e.to_string(),
12251226
};
12261227

12271228
let deserd: Array<u8> = serde_json::from_str(&serd).unwrap();
1229+
// ANCHOR_END: array_json_serde_snippet
12281230

12291231
assert_eq!(sum_all(&(input - deserd)), (0u32, 0u32));
12301232
}
12311233

12321234
#[test]
12331235
fn array_serde_bincode() {
1236+
// ANCHOR: array_bincode_serde_snippet
12341237
let input = randu!(u8; 2, 2);
12351238
let encoded = match bincode::serialize(&input) {
12361239
Ok(encoded) => encoded,
12371240
Err(_) => vec![],
12381241
};
12391242

12401243
let decoded: Array<u8> = bincode::deserialize(&encoded).unwrap();
1244+
// ANCHOR_END: array_bincode_serde_snippet
12411245

12421246
assert_eq!(sum_all(&(input - decoded)), (0u32, 0u32));
12431247
}

src/core/dim4.rs

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ mod tests {
151151

152152
#[test]
153153
fn dim4_serde() {
154+
// ANCHOR: dim4_json_serde_snippet
154155
let dims = dim4!(4, 4);
155156
let serd = match serde_json::to_string(&dims) {
156157
Ok(serialized_str) => serialized_str,
@@ -160,6 +161,7 @@ mod tests {
160161

161162
let deserd: Dim4 = serde_json::from_str(&serd).unwrap();
162163
assert_eq!(deserd, dims);
164+
// ANCHOR_END: dim4_json_serde_snippet
163165
}
164166
}
165167
}

src/core/random.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,19 @@ mod tests {
355355
#[test]
356356
#[cfg(feature = "afserde")]
357357
fn random_engine_serde_bincode() {
358-
let input = RandomEngine::new(RandomEngineType::THREEFRY_2X32_16, Some(2047));
358+
// ANCHOR: rng_bincode_serde_snippet
359+
use RandomEngineType::THREEFRY_2X32_16;
360+
361+
let input = RandomEngine::new(THREEFRY_2X32_16, Some(2047));
359362
let encoded = match bincode::serialize(&input) {
360363
Ok(encoded) => encoded,
361364
Err(_) => vec![],
362365
};
366+
// Save to disk or anything else required
363367

368+
// Load different object if required
364369
let decoded: RandomEngine = bincode::deserialize(&encoded).unwrap();
370+
// ANCHOR_END: rng_bincode_serde_snippet
365371

366372
assert_eq!(input.get_seed(), decoded.get_seed());
367373
assert_eq!(input.get_type(), decoded.get_type());

src/core/seq.rs

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ mod tests {
7878
use super::Seq;
7979
use crate::seq;
8080

81+
// ANCHOR: seq_json_serde_snippet
8182
let original = seq!(1:2:1);
8283
let serd = match serde_json::to_string(&original) {
8384
Ok(serialized_str) => serialized_str,
@@ -86,5 +87,6 @@ mod tests {
8687

8788
let deserd: Seq<i32> = serde_json::from_str(&serd).unwrap();
8889
assert_eq!(deserd, original);
90+
// ANCHOR_END: seq_json_serde_snippet
8991
}
9092
}

tutorials-book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- [Interoperability with CUDA](./cuda-interop.md)
99
- [Interoperability with OpenCL](./opencl-interop.md)
1010
- [Multhi-Threading](./multi-threading.md)
11+
- [Serialization & Deserialization](./serde.md)

tutorials-book/src/serde.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Serialization & Deserialization of ArrayFire Objects
2+
3+
To save [Array][1] contents, shape in JSON format, it just takes couple of lines of code as shown below:
4+
```rust,noplaypen
5+
{{#include ../../src/core/array.rs:array_json_serde_snippet}}
6+
```
7+
Saving [Array][1] in different formats is as simple as changing the object qualifier of methods `serialize` and `deserialize`. For example, if user wants to store [Array][1] in `bincode` format instead of JSON, the above code only needs to be change in couple of lines.
8+
```rust,noplaypen
9+
{{#include ../../src/core/array.rs:array_bincode_serde_snippet}}
10+
```
11+
12+
In similar fashion, we can serialize and deserialize [Dim4][2], [RandomEngine][3], [Seq][4] and other Enums. Examples of [Dim4][2], [RandomEngine][3] and [Seq][4] are given below.
13+
14+
```rust,noplaypen
15+
{{#include ../../src/core/dim4.rs:dim4_json_serde_snippet}}
16+
```
17+
18+
```rust,noplaypen
19+
{{#include ../../src/core/random.rs:rng_bincode_serde_snippet}}
20+
```
21+
22+
```rust,noplaypen
23+
{{#include ../../src/core/seq.rs:seq_json_serde_snippet}}
24+
```
25+
26+
[1]: http://arrayfire.org/arrayfire-rust/arrayfire/struct.Array.html
27+
[2]: http://arrayfire.org/arrayfire-rust/arrayfire/struct.Dim4.html
28+
[3]: http://arrayfire.org/arrayfire-rust/arrayfire/struct.RandomEngine.html
29+
[4]: http://arrayfire.org/arrayfire-rust/arrayfire/struct.Seq.html

0 commit comments

Comments
 (0)