specta 2.0.0-rc.21

Easily export your Rust types to other languages
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use crate::{datatype::reference::Reference, datatype::*, r#type::macros::*, *};

use std::borrow::Cow;

impl_primitives!(
    i8 i16 i32 i64 i128 isize
    u8 u16 u32 u64 u128 usize
    f32 f64
    bool char
    String
);

impl_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13); // Technically we only support 12-tuples but the `T13` is required due to how the macro works

const _: () = {
    use std::{cell::*, rc::Rc, sync::*};
    impl_containers!(Box Rc Arc Cell RefCell Mutex RwLock);
};

#[cfg(feature = "tokio")]
const _: () = {
    use tokio::sync::{Mutex, RwLock};
    impl_containers!(Mutex RwLock);
};

impl<'a> Type for &'a str {
    impl_passthrough!(String);
}

impl Type for Box<str> {
    impl_passthrough!(String);
}

impl<'a, T: Type + 'static> Type for &'a T {
    impl_passthrough!(T);
}

impl<T: Type> Type for [T] {
    impl_passthrough!(Vec<T>);
}

impl<'a, T: ?Sized + ToOwned + Type + 'static> Type for std::borrow::Cow<'a, T> {
    impl_passthrough!(T);
}

use std::ffi::*;
impl_as!(
    str as String
    CString as String
    CStr as String
    OsString as String
    OsStr as String
);

use std::path::*;
impl_as!(
    Path as String
    PathBuf as String
);

use std::net::*;
impl_as!(
    IpAddr as String
    Ipv4Addr as String
    Ipv6Addr as String

    SocketAddr as String
    SocketAddrV4 as String
    SocketAddrV6 as String
);

use std::sync::atomic::*;
impl_as!(
    AtomicBool as bool
    AtomicI8 as i8
    AtomicI16 as i16
    AtomicI32 as i32
    AtomicIsize as isize
    AtomicU8 as u8
    AtomicU16 as u16
    AtomicU32 as u32
    AtomicUsize as usize
    AtomicI64 as i64
    AtomicU64 as u64
);

use std::num::*;
impl_as!(
    NonZeroU8 as u8
    NonZeroU16 as u16
    NonZeroU32 as u32
    NonZeroU64 as u64
    NonZeroUsize as usize
    NonZeroI8 as i8
    NonZeroI16 as i16
    NonZeroI32 as i32
    NonZeroI64 as i64
    NonZeroIsize as isize
    NonZeroU128 as u128
    NonZeroI128 as i128
);

use std::collections::*;
impl_for_list!(
    false; Vec<T> as "Vec"
    false; VecDeque<T> as "VecDeque"
    false; BinaryHeap<T> as "BinaryHeap"
    false; LinkedList<T> as "LinkedList"
    true; HashSet<T> as "HashSet"
    true; BTreeSet<T> as "BTreeSet"
);

impl<'a, T: Type> Type for &'a [T] {
    impl_passthrough!(Vec<T>);
}

impl<const N: usize, T: Type> Type for [T; N] {
    fn inline(type_map: &mut TypeCollection, generics: Generics) -> DataType {
        DataType::List(List {
            ty: Box::new(
                // TODO: This is cursed. Fix it properly!!!
                match Vec::<T>::inline(type_map, generics) {
                    DataType::List(List { ty, .. }) => *ty,
                    _ => unreachable!(),
                },
            ),
            length: Some(N),
            unique: false,
        })
    }

    fn reference(type_map: &mut TypeCollection, generics: &[DataType]) -> Reference {
        Reference {
            inner: DataType::List(List {
                ty: Box::new(
                    // TODO: This is cursed. Fix it properly!!!
                    match Vec::<T>::reference(type_map, generics).inner {
                        DataType::List(List { ty, .. }) => *ty,
                        _ => unreachable!(),
                    },
                ),
                length: Some(N),
                unique: false,
            }),
        }
    }
}

impl<T: Type> Type for Option<T> {
    fn inline(type_map: &mut TypeCollection, generics: Generics) -> DataType {
        let mut ty = None;
        if let Generics::Provided(generics) = &generics {
            ty = generics.get(0).cloned()
        }

        DataType::Nullable(Box::new(match ty {
            Some(ty) => ty,
            None => T::inline(type_map, generics),
        }))
    }

    fn reference(type_map: &mut TypeCollection, generics: &[DataType]) -> Reference {
        Reference {
            inner: DataType::Nullable(Box::new(
                generics
                    .get(0)
                    .cloned()
                    .unwrap_or_else(|| T::reference(type_map, generics).inner),
            )),
        }
    }
}

impl<T> Type for std::marker::PhantomData<T> {
    fn inline(_: &mut TypeCollection, _: Generics) -> DataType {
        DataType::Literal(LiteralType::None)
    }
}

// Serde does no support `Infallible` as it can't be constructed as a `&self` method is uncallable on it.
const _: () = {
    const IMPL_LOCATION: ImplLocation =
        internal::construct::impl_location("specta/src/type/impls.rs:234:10");

    impl Type for std::convert::Infallible {
        fn inline(_: &mut TypeCollection, _: Generics) -> DataType {
            DataType::Enum(internal::construct::r#enum(
                "Infallible".into(),
                internal::construct::sid("Infallible", "::todo:4:10"),
                EnumRepr::External,
                false,
                vec![],
                vec![],
            ))
        }
        fn reference(type_map: &mut TypeCollection, _: &[DataType]) -> reference::Reference {
            let generics = vec![];
            reference::reference::<Self>(
                type_map,
                internal::construct::data_type_reference(
                    "Infallible".into(),
                    internal::construct::sid("Infallible", "::todo:4:10"),
                    generics,
                ),
            )
        }
    }

    impl NamedType for std::convert::Infallible {
        fn sid() -> SpectaID {
            internal::construct::sid("Infallible", "::todo:234:10")
        }

        fn named_data_type(type_map: &mut TypeCollection, generics: &[DataType]) -> NamedDataType {
            internal::construct::named_data_type(
                "Infallible".into(),
                "".into(),
                None,
                Self::sid(),
                IMPL_LOCATION,
                <Self as Type>::inline(type_map, Generics::Provided(generics)),
            )
        }
        fn definition_named_data_type(type_map: &mut TypeCollection) -> NamedDataType {
            internal::construct::named_data_type(
                "Infallible".into(),
                "".into(),
                None,
                Self::sid(),
                IMPL_LOCATION,
                <Self as Type>::inline(type_map, Generics::Definition),
            )
        }
    }
};

impl<T: Type> Type for std::ops::Range<T> {
    fn inline(type_map: &mut TypeCollection, _generics: Generics) -> DataType {
        let ty = Some(T::inline(type_map, Generics::Definition));
        DataType::Struct(StructType {
            name: "Range".into(),
            sid: None,
            generics: vec![],
            fields: StructFields::Named(NamedFields {
                fields: vec![
                    (
                        "start".into(),
                        Field {
                            optional: false,
                            flatten: false,
                            deprecated: None,
                            docs: Cow::Borrowed(""),
                            ty: ty.clone(),
                        },
                    ),
                    (
                        "end".into(),
                        Field {
                            optional: false,
                            flatten: false,
                            deprecated: None,
                            docs: Cow::Borrowed(""),
                            ty,
                        },
                    ),
                ],
                tag: None,
            }),
        })
    }
}

impl<T: Type> Flatten for std::ops::Range<T> {}

impl<T: Type> Type for std::ops::RangeInclusive<T> {
    impl_passthrough!(std::ops::Range<T>); // Yeah Serde are cringe
}

impl<T: Type> Flatten for std::ops::RangeInclusive<T> {}

impl_for_map!(HashMap<K, V> as "HashMap");
impl_for_map!(BTreeMap<K, V> as "BTreeMap");
impl<K: Type, V: Type> Flatten for std::collections::HashMap<K, V> {}
impl<K: Type, V: Type> Flatten for std::collections::BTreeMap<K, V> {}

const _: () = {
    const SID: SpectaID = internal::construct::sid("SystemTime", "::type::impls:305:10");
    const IMPL_LOCATION: ImplLocation =
        internal::construct::impl_location("specta/src/type/impls.rs:302:10");

    impl Type for std::time::SystemTime {
        fn inline(type_map: &mut TypeCollection, _: Generics) -> DataType {
            DataType::Struct(internal::construct::r#struct(
                "SystemTime".into(),
                Some(internal::construct::sid("SystemTime", "::todo:3:10")),
                vec![],
                internal::construct::struct_named(
                    vec![
                        (
                            "duration_since_epoch".into(),
                            internal::construct::field(
                                false,
                                false,
                                None,
                                "".into(),
                                Some({
                                    let ty = <i64 as Type>::reference(type_map, &[]).inner;
                                    ty
                                }),
                            ),
                        ),
                        (
                            "duration_since_unix_epoch".into(),
                            internal::construct::field(
                                false,
                                false,
                                None,
                                "".into(),
                                Some({
                                    let ty = <u32 as Type>::reference(type_map, &[]).inner;
                                    ty
                                }),
                            ),
                        ),
                    ],
                    None,
                ),
            ))
        }

        fn reference(type_map: &mut TypeCollection, _: &[DataType]) -> reference::Reference {
            reference::reference::<Self>(
                type_map,
                internal::construct::data_type_reference("SystemTime".into(), SID, vec![]),
            )
        }
    }

    impl NamedType for std::time::SystemTime {
        fn sid() -> SpectaID {
            SID
        }
        fn named_data_type(type_map: &mut TypeCollection, generics: &[DataType]) -> NamedDataType {
            internal::construct::named_data_type(
                "SystemTime".into(),
                "".into(),
                None,
                Self::sid(),
                IMPL_LOCATION,
                <Self as Type>::inline(type_map, Generics::Provided(generics)),
            )
        }
        fn definition_named_data_type(type_map: &mut TypeCollection) -> NamedDataType {
            internal::construct::named_data_type(
                "SystemTime".into(),
                "".into(),
                None,
                Self::sid(),
                IMPL_LOCATION,
                <Self as Type>::inline(type_map, Generics::Definition),
            )
        }
    }
    #[automatically_derived]
    impl Flatten for std::time::SystemTime {}
};

const _: () = {
    const SID: SpectaID = internal::construct::sid("Duration", "::type::impls:401:10");
    const IMPL_LOCATION: ImplLocation =
        internal::construct::impl_location("specta/src/type/impls.rs:401:10");

    impl Type for std::time::Duration {
        fn inline(type_map: &mut TypeCollection, _: Generics) -> DataType {
            DataType::Struct(internal::construct::r#struct(
                "Duration".into(),
                Some(SID),
                vec![],
                internal::construct::struct_named(
                    vec![
                        (
                            "secs".into(),
                            internal::construct::field(
                                false,
                                false,
                                None,
                                "".into(),
                                Some({
                                    let ty = <u64 as Type>::reference(type_map, &[]).inner;
                                    ty
                                }),
                            ),
                        ),
                        (
                            "nanos".into(),
                            internal::construct::field(
                                false,
                                false,
                                None,
                                "".into(),
                                Some({
                                    let ty = <u32 as Type>::reference(type_map, &[]).inner;
                                    ty
                                }),
                            ),
                        ),
                    ],
                    None,
                ),
            ))
        }
        fn reference(type_map: &mut TypeCollection, _: &[DataType]) -> reference::Reference {
            reference::reference::<Self>(
                type_map,
                internal::construct::data_type_reference("Duration".into(), Self::sid(), vec![]),
            )
        }
    }

    impl NamedType for std::time::Duration {
        fn sid() -> SpectaID {
            SID
        }
        fn named_data_type(type_map: &mut TypeCollection, generics: &[DataType]) -> NamedDataType {
            internal::construct::named_data_type(
                "Duration".into(),
                "".into(),
                None,
                Self::sid(),
                IMPL_LOCATION,
                <Self as Type>::inline(type_map, Generics::Provided(generics)),
            )
        }
        fn definition_named_data_type(type_map: &mut TypeCollection) -> NamedDataType {
            internal::construct::named_data_type(
                "Duration".into(),
                "".into(),
                None,
                Self::sid(),
                IMPL_LOCATION,
                <Self as Type>::inline(type_map, Generics::Definition),
            )
        }
    }

    impl Flatten for std::time::Duration {}
};