From def7e64734397f81fea141cdcb0bddc0730f9363 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 29 Feb 2024 14:35:14 -0300 Subject: [PATCH 1/4] Remove `TS::trasparent` --- macros/src/deps.rs | 1 - macros/src/lib.rs | 5 ----- ts-rs/src/chrono.rs | 7 ------ ts-rs/src/lib.rs | 54 +++++++++++++-------------------------------- 4 files changed, 15 insertions(+), 52 deletions(-) diff --git a/macros/src/deps.rs b/macros/src/deps.rs index 4d096c091..da02a80fd 100644 --- a/macros/src/deps.rs +++ b/macros/src/deps.rs @@ -13,7 +13,6 @@ impl Dependencies { } /// Adds the given type. - /// If the type is transparent, then we'll get resolve the child dependencies during runtime. pub fn push(&mut self, ty: &Type) { self.0.push(quote![.push::<#ty>()]); self.0.push(quote![ diff --git a/macros/src/lib.rs b/macros/src/lib.rs index d5d4dbf81..afb24c210 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -87,10 +87,6 @@ impl DerivedTS { { #dependencies } - - fn transparent() -> bool { - false - } } #export @@ -145,7 +141,6 @@ impl DerivedTS { impl TS for #generics { type WithoutGenerics = #generics; fn name() -> String { stringify!(#generics).to_owned() } - fn transparent() -> bool { false } } )* } diff --git a/ts-rs/src/chrono.rs b/ts-rs/src/chrono.rs index e5acfd749..514d9d4c8 100644 --- a/ts-rs/src/chrono.rs +++ b/ts-rs/src/chrono.rs @@ -14,7 +14,6 @@ macro_rules! impl_dummy { type WithoutGenerics = $t; fn name() -> String { String::new() } fn inline() -> String { String::new() } - fn transparent() -> bool { false } } )*}; } @@ -33,9 +32,6 @@ impl TS for DateTime { fn inline() -> String { "string".to_owned() } - fn transparent() -> bool { - false - } } impl TS for Date { @@ -49,7 +45,4 @@ impl TS for Date { fn inline() -> String { "string".to_owned() } - fn transparent() -> bool { - false - } } diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 2c2e0404d..5d4eba702 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -367,12 +367,7 @@ pub trait TS { struct Visit<'a>(&'a mut Vec); impl<'a> TypeVisitor for Visit<'a> { fn visit(&mut self) { - if T::transparent() { - // the dependency `T` is "transparent", meaning that our original type depends - // on the dependencies of `T` as well. - T::dependency_types().for_each(self); - } else if let Some(dep) = Dependency::from_ty::() { - // the dependency `T` is not transparent, so we just add it to the output + if let Some(dep) = Dependency::from_ty::() { self.0.push(dep); } } @@ -382,10 +377,6 @@ pub trait TS { deps } - /// `true` if this is a transparent type, e.g tuples or a list. - /// This is used for resolving imports when using the `export!` macro. - fn transparent() -> bool; - /// Manually export this type to a file. /// The output file can be specified by annotating the type with `#[ts(export_to = ".."]`. /// By default, the filename will be derived from the types name. @@ -453,7 +444,6 @@ macro_rules! impl_primitives { type WithoutGenerics = Self; fn name() -> String { $l.to_owned() } fn inline() -> String { ::name() } - fn transparent() -> bool { false } } )*)* }; } @@ -474,7 +464,6 @@ macro_rules! impl_tuples { { ()$(.push::<$i>())* } - fn transparent() -> bool { true } } }; ( $i2:ident $(, $i:ident)* ) => { @@ -504,7 +493,6 @@ macro_rules! impl_wrapper { { ((std::marker::PhantomData::,), T::generics()) } - fn transparent() -> bool { T::transparent() } } }; } @@ -529,7 +517,6 @@ macro_rules! impl_shadow { { <$s>::generics() } - fn transparent() -> bool { <$s>::transparent() } } }; } @@ -554,9 +541,6 @@ impl TS for Option { { ((std::marker::PhantomData::,), T::generics()) } - fn transparent() -> bool { - T::transparent() - } } impl TS for Result { @@ -583,9 +567,6 @@ impl TS for Result { ((PhantomData::,), E::generics()), ) } - fn transparent() -> bool { - false - } } impl TS for Vec { @@ -611,9 +592,6 @@ impl TS for Vec { { ((std::marker::PhantomData::,), T::generics()) } - fn transparent() -> bool { - false - } } // Arrays longer than this limit will be emitted as Array @@ -655,10 +633,6 @@ impl TS for [T; N] { { ((std::marker::PhantomData::,), T::generics()) } - - fn transparent() -> bool { - false - } } impl TS for HashMap { @@ -690,9 +664,6 @@ impl TS for HashMap { ((PhantomData::,), V::generics()), ) } - fn transparent() -> bool { - false - } } impl TS for Range { @@ -705,11 +676,15 @@ impl TS for Range { where Self: 'static, { - ().push::() + I::dependency_types() } - fn transparent() -> bool { - true + fn generics() -> impl TypeList + where + Self: 'static, + { + use std::marker::PhantomData; + ((PhantomData::,), I::generics()) } } @@ -723,11 +698,15 @@ impl TS for RangeInclusive { where Self: 'static, { - ().push::() + I::dependency_types() } - fn transparent() -> bool { - true + fn generics() -> impl TypeList + where + Self: 'static, + { + use std::marker::PhantomData; + ((PhantomData::,), I::generics()) } } @@ -817,7 +796,4 @@ impl TS for Dummy { fn name() -> String { "Dummy".to_owned() } - fn transparent() -> bool { - false - } } From 59f7062b77daf6cf65081d913bb5f57666f54924 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 29 Feb 2024 14:50:22 -0300 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e3844a41..73a0d13cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Added `TS::generics()` ([#241](https://github.com/Aleph-Alpha/ts-rs/pull/241)) - Added `TS::WithoutGenerics` ([#241](https://github.com/Aleph-Alpha/ts-rs/pull/241)) - `Result`, `Option`, `HashMap` and `Vec` had their implementations of `TS` changed ([#241](https://github.com/Aleph-Alpha/ts-rs/pull/241)) +- Removed `TS::transparent()` ([#243](https://github.com/Aleph-Alpha/ts-rs/pull/243)) ### Features From 38ec2f72f5a9228b8f4950af26eac89d3900af14 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 29 Feb 2024 15:04:39 -0300 Subject: [PATCH 3/4] Use TypeList methods + impl_shadow --- ts-rs/src/lib.rs | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 5d4eba702..11abd64c9 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -683,33 +683,11 @@ impl TS for Range { where Self: 'static, { - use std::marker::PhantomData; - ((PhantomData::,), I::generics()) - } -} - -impl TS for RangeInclusive { - type WithoutGenerics = RangeInclusive; - fn name() -> String { - format!("{{ start: {}, end: {}, }}", I::name(), I::name()) - } - - fn dependency_types() -> impl TypeList - where - Self: 'static, - { - I::dependency_types() - } - - fn generics() -> impl TypeList - where - Self: 'static, - { - use std::marker::PhantomData; - ((PhantomData::,), I::generics()) + I::generics().push::() } } +impl_shadow!(as Range: impl TS for RangeInclusive); impl_shadow!(as Vec: impl TS for HashSet); impl_shadow!(as Vec: impl TS for BTreeSet); impl_shadow!(as HashMap: impl TS for BTreeMap); From 5ebe368f1f7f7b588da89d575b33bb6e3774174f Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 29 Feb 2024 15:06:00 -0300 Subject: [PATCH 4/4] Add missing method to impl_shadow --- ts-rs/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 11abd64c9..f60c64bd4 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -502,6 +502,7 @@ macro_rules! impl_shadow { (as $s:ty: $($impl:tt)*) => { $($impl)* { type WithoutGenerics = <$s as TS>::WithoutGenerics; + fn ident() -> String { <$s>::ident() } fn name() -> String { <$s>::name() } fn inline() -> String { <$s>::inline() } fn inline_flattened() -> String { <$s>::inline_flattened() }