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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! Macros for defining newtype wrappers around variable-length types.
//!
//! # Examples
//!
//! As an example, we'll create a wrapper around `Str` that also tracks the number of UTF8 code
//! points in the string. To ensure these stay in sync, we allow the users of our library to
//! access the codepoint count in immutable form (read access) but not mutable form (write access).
//! The newtype wrapper helps us ensure this visibility as desired:
//!
//! ```
//! use varlen::prelude::*;
//!
//! define_varlen_newtype! {
//! #[repr(transparent)]
//! /// A string which counts UTF8 code points.
//! pub struct CountedStr(
//! Tup2<
//! // Count of UTF8 code points in the str
//! FixedLen<usize>,
//! // Paylod
//! Str
//! >);
//!
//! with init: struct CountedStrInitImpl<_>(_);
//! with inner_ref: fn inner(&self) -> &_;
//! with inner_mut: fn inner_mut(self: _) -> _;
//! }
//!
//! impl CountedStr {
//! pub fn from_str<'a>(src: &'a str) -> impl 'a + Initializer<Self> {
//! let count = src.chars().count();
//! CountedStrInitImpl(
//! tup2::Init(
//! FixedLen(count),
//! Str::copy(src),
//! )
//! )
//! }
//!
//! pub fn char_count(&self) -> usize {
//! self.inner().refs().0.0
//! }
//!
//! pub fn str(&self) -> &str {
//! &self.inner().refs().1[..]
//! }
//! }
//!
//! let s = VBox::new(CountedStr::from_str("hellö wörld"));
//! assert_eq!(11, s.char_count());
//! assert_eq!(13, s.str().len());
//! ```
/// Defines a newtype wrapper around a varlen type.
///
/// # Examples
///
/// As an example, we'll create a wrapper around `Str` that also tracks the number of UTF8 code
/// points in the string. To ensure these stay in sync, we allow the users of our library to
/// access the codepoint count in immutable form (read access) but not mutable form (write access).
/// The newtype wrapper helps us ensure this visibility as desired:
///
/// ```
/// use varlen::prelude::*;
///
/// define_varlen_newtype! {
/// #[repr(transparent)]
/// /// A string which counts UTF8 code points.
/// pub struct CountedStr(
/// Tup2<
/// // Count of UTF8 code points in the str
/// FixedLen<usize>,
/// // Paylod
/// Str
/// >);
///
/// with init: struct CountedStrInitImpl<_>(_);
/// with inner_ref: fn inner(&self) -> &_;
/// with inner_mut: fn inner_mut(self: _) -> _;
/// }
///
/// impl CountedStr {
/// pub fn from_str<'a>(src: &'a str) -> impl 'a + Initializer<Self> {
/// let count = src.chars().count();
/// CountedStrInitImpl(
/// tup2::Init(
/// FixedLen(count),
/// Str::copy(src),
/// )
/// )
/// }
///
/// pub fn char_count(&self) -> usize {
/// self.inner().refs().0.0
/// }
///
/// pub fn str(&self) -> &str {
/// &self.inner().refs().1[..]
/// }
/// }
///
/// let s = VBox::new(CountedStr::from_str("hellö wörld"));
/// assert_eq!(11, s.char_count());
/// assert_eq!(13, s.str().len());
/// ```
///
/// # Generated items
///
/// The macro generates the following items:
///
/// * The struct, exactly as you specified. This is `CountedStr` in the example above.
/// * An initializer struct, with signature as specified. The type argument is expected to be an
/// initializer for the inner type.
/// * Two member functions, with signatures as above, for accessing the inner type immutably
/// (`inner_ref`) and mutably (`inner_mut`).
/// * Implementations of traits [`crate::VarLen`] and [`crate::Initializer`] for your newtype.
///
/// For all of these except the trait implementations, you must provide a signature in the
/// macro invocation site. The signature can specify the name, visibility, and documentation
/// for this item.
///
/// # Using generics
///
/// Your type may use generics. However, the syntax diverges a little from standard Rust
/// syntax, because of limitations of Rust's `macro_rules`. The requirements are:
///
/// * Every generic must be surrounded by parentheses in the type's definition.
///
/// * You must provide an explicit `with signature` clause to define the `impl`'s signatures.
///
/// The following example shows this in action:
///
/// ```
/// use varlen::prelude::*;
///
/// define_varlen_newtype! {
/// #[repr(transparent)]
/// /// A string which counts UTF8 code points.
/// pub struct TwoArrays<(T: Copy), (U: Clone = u16)>(
/// pub Tup2<
/// Array<T>,
/// Array<U>,
/// >);
///
/// with signature: impl<(T: Copy), (U: Clone)> TwoArrays <(T), (U)> { _ }
/// with init: pub struct TwoArraysInit<_>(_);
/// with inner_ref: pub fn inner(&self) -> &_;
/// with inner_mut: pub fn inner_mut(self: _) -> _;
/// }
///
/// let t: VBox<TwoArrays<u16, u8>> = VBox::new(TwoArraysInit(
/// tup2::Init(
/// Array::copy(&[1, 2, 3]),
/// Array::copy(&[4, 5, 6, 7]),
/// )
/// ));
/// assert_eq!(&t.inner().refs().0[..], &[1, 2, 3]);
/// assert_eq!(&t.inner().refs().1[..], &[4, 5, 6, 7]);
/// ```
)?
with init:
$*
$initvis:vis $init:;
with inner_ref:
$*
$refvis:vis fn $ref:ident ;
with inner_mut:
$*
$mutvis:vis fn $mut:ident ;
) =>
}
pub use define_varlen_newtype;
/// Lifts a [`crate::Initializer<T>`] implementation to a newtype.
///
/// # Examples
///
/// ```
/// use varlen::prelude::*;
///
/// /// Custom initializer for initializing from arrays of size 3.
/// pub struct Init3Array(SizedInit<MoveFrom<u16, 3>>);
///
/// impl Init3Array {
/// pub fn new(arr: [u16; 3]) -> Self {
/// Init3Array(SizedInit(3, MoveFrom(arr)))
/// }
/// }
///
/// impl_initializer_as_newtype! {
/// impl Initializer<Array<u16>> for Init3Array { _ }
/// }
///
/// let v: VBox<Array<u16>> = VBox::new(Init3Array::new([4, 5, 6]));
/// assert_eq!(&v[..], &[4, 5, 6]);
/// ```
///
/// # Generics
///
/// Your type may use generics. However, the syntax diverges a little from standard Rust
/// syntax, because of limitations of Rust's `macro_rules`. We require that the first set
/// of generics in the `impl` statement uses parens `(...)` around each generic argument.
///
/// The following example shows this in action:
///
/// ```
/// use varlen::prelude::*;
///
/// /// Custom initializer for cloning from arrays of size 3.
/// pub struct Init3Array<'a, T>(SizedInit<CloneFrom<'a, T>>);
///
/// impl<'a, T: Clone> Init3Array<'a, T> {
/// pub fn new(arr: &'a [T; 3]) -> Self {
/// Init3Array(SizedInit(3, CloneFrom(arr)))
/// }
/// }
///
/// impl_initializer_as_newtype! {
/// impl<('a), (T: Clone)> Initializer<Array<T>> for Init3Array<'a, T> { _ }
/// }
///
/// let v: VBox<Array<u64>> = VBox::new(Init3Array::new(&[4, 5, 6]));
/// assert_eq!(&v[..], &[4, 5, 6]);
/// ```
) =>
}
pub use impl_initializer_as_newtype;