Skip to main content

ts_typegen/
marker.rs

1//! The `strict`-mode marker trait.
2//!
3//! `TsType` has no methods and no runtime cost. Its only purpose is turning an
4//! unmapped field type into a `rustc` error instead of a silent `unknown` in the
5//! generated TypeScript.
6
7use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
8
9/// Marks a type as having a known TypeScript binding. Implemented for every
10/// type the lowering pipeline understands; a field whose type has no impl
11/// fails to compile instead of silently emitting `unknown`.
12pub trait TsType {}
13
14macro_rules! impl_ts_type {
15    ($($t:ty),* $(,)?) => { $(impl TsType for $t {})* };
16}
17
18impl_ts_type!(
19    bool,
20    char,
21    str,
22    String,
23    i8,
24    i16,
25    i32,
26    i64,
27    i128,
28    isize,
29    u8,
30    u16,
31    u32,
32    u64,
33    u128,
34    usize,
35    f32,
36    f64,
37    std::path::PathBuf,
38    (),
39);
40
41impl<T: TsType + ?Sized> TsType for &T {}
42impl<T: TsType + ?Sized> TsType for Box<T> {}
43impl<T: TsType + ?Sized> TsType for std::rc::Rc<T> {}
44impl<T: TsType + ?Sized> TsType for std::sync::Arc<T> {}
45impl<T: TsType> TsType for Option<T> {}
46impl<T: TsType> TsType for Vec<T> {}
47impl<T: TsType> TsType for VecDeque<T> {}
48impl<T: TsType> TsType for [T] {}
49impl<T: TsType, const N: usize> TsType for [T; N] {}
50impl<T: TsType> TsType for HashSet<T> {}
51impl<T: TsType> TsType for BTreeSet<T> {}
52impl<K: TsType, V: TsType> TsType for HashMap<K, V> {}
53impl<K: TsType, V: TsType> TsType for BTreeMap<K, V> {}
54impl<T: TsType, E: TsType> TsType for Result<T, E> {}
55impl<A: TsType, B: TsType> TsType for (A, B) {}
56impl<A: TsType, B: TsType, C: TsType> TsType for (A, B, C) {}