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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Serializing references to constant objects.
//!
//! Sometimes it's useful to serialize objects that *you know* are already present in each process,
//! typically because they are stored in a global static/const variable. For example, you might have
//! several unit structs implementing a trait, and you wish to serialize the `dyn Trait` without
//! wrapping it in a box. Or you might have a limited list of "actions" you wish to ask a subprocess
//! to perform, and these actions are stored in a constant array or several constant variables.
//!
//! [`StaticRef`] is a type similar to `&'static T` that implements [`Object`] and stores a plain
//! reference to the underlying value instead of serializing it as an object:
//!
//! ```rust
//! use crossmist::{StaticRef, static_ref};
//!
//! struct Configuration {
//! meows: bool,
//! woofs: bool,
//! }
//!
//! const CAT: Configuration = Configuration { meows: true, woofs: false };
//! const DOG: Configuration = Configuration { meows: false, woofs: true };
//!
//! #[crossmist::main]
//! fn main() {
//! test.run(static_ref!(Configuration, CAT));
//! }
//!
//! #[crossmist::func]
//! fn test(conf: StaticRef<Configuration>) {
//! assert_eq!(conf.meows, true);
//! assert_eq!(conf.woofs, false);
//! }
//! ```
//!
//! Here's a more complicated example featuring `StaticRef<&'static dyn Trait>` (similar to
//! `&'static &'static dyn Trait`). The double indirection is required because `dyn Trait` is
//! unsized, and thus `&dyn Trait` is more than just a pointer, which `StaticRef` does not support
//! directly.
//!
//! ```rust
//! use crossmist::{StaticRef, static_ref};
//!
//! trait Speak {
//! fn speak(&self) -> String;
//! }
//!
//! struct Cat;
//! impl Speak for Cat {
//! fn speak(&self) -> String {
//! "Meow!".to_string()
//! }
//! }
//!
//! struct Dog;
//! impl Speak for Dog {
//! fn speak(&self) -> String {
//! "Woof!".to_string()
//! }
//! }
//!
//! #[crossmist::main]
//! fn main() {
//! test.run(static_ref!(&'static dyn Speak, &Cat));
//! }
//!
//! #[crossmist::func]
//! fn test(animal: StaticRef<&'static dyn Speak>) {
//! assert_eq!(animal.speak(), "Meow!");
//! }
//! ```
use crate::;
use fmt;
use Deref;
/// A `&'static T` implementing [`Object`].
///
/// See the documentation for [`mod@crossmist::static_ref`] for a tutorial-grade explanation.
///
/// This type can be created via one of the following two ways:
///
/// - Safely, via [`static_ref!`]
/// - Unsafely, via [`StaticRef::new_unchecked`]
///
/// # Example
///
/// ```rust
/// use crossmist::{StaticRef, static_ref};
///
/// let num = static_ref!(i32, 123);
/// assert_eq!(*num, 123);
/// ```
// Implement Clone/Copy even for T: !Clone/Copy
/// Create a [`StaticRef`] safely.
///
/// This macro takes `T, value` and returns [`StaticRef<T>`] referencing the given value. The value
/// must be a compile-time constant: this is the only way to ensure soundness. If you need to
/// reference a `static`, non-compile-time constant, use [`StaticRef::new_unchecked`].
///
/// # Example
///
/// ```rust
/// use crossmist::{StaticRef, static_ref};
///
/// const NUM: i32 = 123;
/// let num = static_ref!(i32, NUM);
/// assert_eq!(*num, 123);
/// ```
pub use static_ref;