1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Adapter pattern for foreign types.
//!
//! Use [`Adapter`] when you want to encode/decode a type from another crate
//! but cannot implement [`crate::Encode`] / [`crate::Decode`] for it directly
//! (the orphan rule prevents that). An `Adapter` declares an intermediate
//! representation (`Repr`) that zerec already knows how to handle, and two
//! conversion functions.
//!
//! You rarely call these methods yourself — the `#[zerec(via = "...")]`
//! attribute on a derived field does it for you.
//!
//! # Example
//!
//! ```rust,ignore
//! use zerec::Adapter;
//!
//! // A foreign physics body we cannot touch.
//! struct RigidBody { mass: f32, vel: [f32; 3] }
//!
//! struct RigidBodyAdapter;
//!
//! impl Adapter<RigidBody> for RigidBodyAdapter {
//! type Repr = (f32, [f32; 3]);
//! fn to_repr(v: &RigidBody) -> Self::Repr { (v.mass, v.vel) }
//! fn from_repr(r: Self::Repr) -> RigidBody { RigidBody { mass: r.0, vel: r.1 } }
//! }
//!
//! // In your own struct:
//! #[derive(Encode, Decode)]
//! struct Scene {
//! #[zerec(via = "RigidBodyAdapter")]
//! body: RigidBody,
//! }
//! ```
use crate::;
/// A bridge between a foreign type `T` and zerec's codec.