diff --git a/macros/src/deps.rs b/macros/src/deps.rs index f3958bd..366ad82 100644 --- a/macros/src/deps.rs +++ b/macros/src/deps.rs @@ -2,75 +2,32 @@ use proc_macro2::TokenStream; use quote::{quote, ToTokens}; use syn::Type; -pub struct Dependencies { - v0: Vec, - v1: Vec -} - -impl Default for Dependencies { - fn default() -> Self { - Dependencies { - v0: Vec::new(), - v1: vec![quote![()]] - } - } -} +#[derive(Default)] +pub struct Dependencies(Vec); impl Dependencies { /// Adds all dependencies from the given type pub fn append_from(&mut self, ty: &Type) { - self.v1.push(quote![.extend(<#ty as ts_rs::TS>::dependency_types())]); - self.v0 - .push(quote!(dependencies.append(&mut <#ty as ts_rs::TS>::dependencies());)); + self.0.push(quote![.extend(<#ty as ts_rs::TS>::dependency_types())]); } /// Adds the given type if it's *not* transparent. /// If it is, all it's child dependencies are added instead. pub fn push_or_append_from(&mut self, ty: &Type) { - self.v1.push(quote![.push::<#ty>()]); - self.v0.push(quote! { - if <#ty as ts_rs::TS>::transparent() { - dependencies.append(&mut <#ty as ts_rs::TS>::dependencies()); - } else { - if let Some(dep) = ts_rs::Dependency::from_ty::<#ty>() { - dependencies.push(dep); - } - } - }); + self.0.push(quote![.push::<#ty>()]); } pub fn append(&mut self, other: Dependencies) { - let other_v2 = other.to_tokens_v2(); - self.v1.push(quote![.extend(#other_v2)]); - self.v0.push(quote! { - dependencies.append(&mut #other); - }) - } - - pub fn to_tokens_v2(&self) -> impl ToTokens + '_ { - struct ToTokensV2<'a>(&'a Dependencies); - impl<'a> ToTokens for ToTokensV2<'a> { - fn to_tokens(&self, tokens: &mut TokenStream) { - let lines = &self.0.v1; - tokens.extend(quote![{ - use ts_rs::typelist::TypeList; - #(#lines)* - }]) - } - } - ToTokensV2(self) + self.0.push(quote![.extend(#other)]); } } impl ToTokens for Dependencies { fn to_tokens(&self, tokens: &mut TokenStream) { - let dependencies = &self.v0; - tokens.extend(quote! { - { - let mut dependencies = Vec::new(); - #( #dependencies )* - dependencies - } - }) + let lines = &self.0; + tokens.extend(quote![{ + use ts_rs::typelist::TypeList; + ()#(#lines)* + }]) } } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index e318ca8..452d992 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -81,7 +81,6 @@ impl DerivedTS { .unwrap_or_else(TokenStream::new); let impl_start = generate_impl(&rust_ty, &generics); - let dependencies_v2 = dependencies.to_tokens_v2(); quote! { #impl_start { const EXPORT_TO: Option<&'static str> = Some(#export_to); @@ -97,18 +96,12 @@ impl DerivedTS { } #inline_flattened - fn dependencies() -> Vec - where - Self: 'static, - { - #dependencies - } - + #[allow(clippy::unused_unit)] fn dependency_types() -> impl ts_rs::typelist::TypeList where Self: 'static, { - #dependencies_v2 + #dependencies } fn transparent() -> bool { diff --git a/ts-rs/src/chrono.rs b/ts-rs/src/chrono.rs index 092626e..7aee2a9 100644 --- a/ts-rs/src/chrono.rs +++ b/ts-rs/src/chrono.rs @@ -7,14 +7,12 @@ use chrono::{ }; use super::{impl_primitives, TS}; -use crate::Dependency; macro_rules! impl_dummy { ($($t:ty),*) => {$( impl TS for $t { fn name() -> String { String::new() } fn inline() -> String { String::new() } - fn dependencies() -> Vec { vec![] } fn transparent() -> bool { false } } )*}; @@ -33,9 +31,6 @@ impl TS for DateTime { fn inline() -> String { "string".to_owned() } - fn dependencies() -> Vec { - vec![] - } fn transparent() -> bool { false } @@ -51,9 +46,6 @@ impl TS for Date { fn inline() -> String { "string".to_owned() } - fn dependencies() -> Vec { - vec![] - } fn transparent() -> bool { false } diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index bc5edb9..98b2be8 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -165,6 +165,8 @@ use std::{ path::{Path, PathBuf}, }; +use crate::typelist::TypeList; + pub use ts_rs_macros::TS; pub use crate::export::ExportError; @@ -292,34 +294,26 @@ pub trait TS { fn dependency_types() -> impl TypeList where - Self: 'static { () } + Self: 'static {} - fn dependencies() -> Vec - where - Self: 'static; + fn dependencies() -> Vec where Self: 'static { + use crate::typelist::TypeVisitor; - //fn dependencies() -> Vec where Self: 'static { - // use crate::typelist::{TypeList, TypeVisitor}; - // - // let mut deps: Vec = vec![]; - // struct Visit<'a>(&'a mut Vec); - // impl<'a> TypeVisitor for Visit<'a> { - // fn visit(&mut self) { - // if let Some(dep) = Dependency::from_ty::() { - // self.0.push(dep); - // } - // } - // } - // Self::dependency_types().for_each(&mut Visit(&mut deps)); - // - // deps - //} + let mut deps: Vec = vec![]; + struct Visit<'a>(&'a mut Vec); + impl<'a> TypeVisitor for Visit<'a> { + fn visit(&mut self) { + if let Some(dep) = Dependency::from_ty::() { + self.0.push(dep); + } + } + } + Self::dependency_types().for_each(&mut Visit(&mut deps)); - fn _push_to(l: impl TypeList) -> impl TypeList where Self: 'static { - l.push::() + deps } - /// `true` if this is a transparent type, e.g tuples or a list. + /// `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; @@ -393,7 +387,6 @@ macro_rules! impl_primitives { $l.to_owned() } fn inline() -> String { $l.to_owned() } - fn dependencies() -> Vec { vec![] } fn transparent() -> bool { false } } )*)* }; @@ -414,15 +407,6 @@ macro_rules! impl_tuples { { ()$(.push::<$i>())* } - fn dependencies() -> Vec - where - Self: 'static - { - [$( Dependency::from_ty::<$i>() ),*] - .into_iter() - .flatten() - .collect() - } fn transparent() -> bool { true } } }; @@ -450,12 +434,6 @@ macro_rules! impl_wrapper { { T::dependency_types() } - fn dependencies() -> Vec - where - Self: 'static - { - T::dependencies() - } fn transparent() -> bool { T::transparent() } } }; @@ -475,12 +453,6 @@ macro_rules! impl_shadow { { <$s>::dependency_types() } - fn dependencies() -> Vec<$crate::Dependency> - where - Self: 'static - { - <$s>::dependencies() - } fn transparent() -> bool { <$s>::transparent() } } }; @@ -511,12 +483,6 @@ impl TS for Option { { ().push::() } - fn dependencies() -> Vec - where - Self: 'static - { - [Dependency::from_ty::()].into_iter().flatten().collect() - } fn transparent() -> bool { true @@ -536,15 +502,6 @@ impl TS for Result { { ().push::().push::() } - fn dependencies() -> Vec - where - Self: 'static - { - [Dependency::from_ty::(), Dependency::from_ty::()] - .into_iter() - .flatten() - .collect() - } fn transparent() -> bool { true } @@ -565,12 +522,6 @@ impl TS for Vec { { ().push::() } - fn dependencies() -> Vec - where - Self: 'static - { - [Dependency::from_ty::()].into_iter().flatten().collect() - } fn transparent() -> bool { true } @@ -622,12 +573,6 @@ impl TS for [T; N] { { ().push::() } - fn dependencies() -> Vec - where - Self: 'static - { - [Dependency::from_ty::()].into_iter().flatten().collect() - } fn transparent() -> bool { true @@ -659,15 +604,6 @@ impl TS for HashMap { { ().push::().push::() } - fn dependencies() -> Vec - where - Self: 'static - { - [Dependency::from_ty::(), Dependency::from_ty::()] - .into_iter() - .flatten() - .collect() - } fn transparent() -> bool { true @@ -695,12 +631,6 @@ impl TS for Range { { ().push::() } - fn dependencies() -> Vec - where - Self: 'static - { - [Dependency::from_ty::()].into_iter().flatten().collect() - } fn transparent() -> bool { true @@ -728,12 +658,6 @@ impl TS for RangeInclusive { { ().push::() } - fn dependencies() -> Vec - where - Self: 'static - { - [Dependency::from_ty::()].into_iter().flatten().collect() - } fn transparent() -> bool { true @@ -809,5 +733,4 @@ impl_primitives! { () => "null" } #[rustfmt::skip] -pub(crate) use impl_primitives; -use crate::typelist::TypeList; +pub(crate) use impl_primitives; \ No newline at end of file diff --git a/ts-rs/tests/generics.rs b/ts-rs/tests/generics.rs index 6141005..578d93b 100644 --- a/ts-rs/tests/generics.rs +++ b/ts-rs/tests/generics.rs @@ -171,6 +171,7 @@ fn inline() { #[test] #[ignore = "We haven't figured out how to inline generics with bounds yet"] +#[allow(unreachable_code)] fn inline_with_bounds() { todo!("FIX ME: https://github.com/Aleph-Alpha/ts-rs/issues/214"); diff --git a/ts-rs/tests/ranges.rs b/ts-rs/tests/ranges.rs index 10cdcd6..be491ca 100644 --- a/ts-rs/tests/ranges.rs +++ b/ts-rs/tests/ranges.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeSet; use std::ops::{Range, RangeInclusive}; use ts_rs::{Dependency, TS}; @@ -22,10 +23,9 @@ fn range() { "type RangeTest = { a: { start: number, end: number, }, b: { start: string, end: string, }, c: { start: { start: number, end: number, }, end: { start: number, end: number, }, }, d: { start: number, end: number, }, e: { start: Inner, end: Inner, }, }" ); assert_eq!( - RangeTest::dependencies(), + RangeTest::dependencies().into_iter().collect::>().into_iter().collect::>(), vec![ Dependency::from_ty::().unwrap(), - Dependency::from_ty::().unwrap() ] ); } diff --git a/ts-rs/tests/test.rs b/ts-rs/tests/test.rs deleted file mode 100644 index f860ce7..0000000 --- a/ts-rs/tests/test.rs +++ /dev/null @@ -1,42 +0,0 @@ -use std::collections::{BTreeSet, HashMap}; -use ts_rs::{Dependency, TS}; -use ts_rs::typelist::TypeList; -use ts_rs::typelist::TypeVisitor; - -fn get_deps() -> BTreeSet { - use ts_rs::typelist::{TypeList, TypeVisitor}; - - let mut deps = BTreeSet::new(); - struct Visit<'a>(&'a mut BTreeSet); - impl<'a> TypeVisitor for Visit<'a> { - fn visit(&mut self) { - if let Some(dep) = Dependency::from_ty::() { - self.0.insert(dep); - } - } - } - T::dependency_types().for_each(&mut Visit(&mut deps)); - - deps -} - -#[allow(dead_code)] -#[test] -fn test() { - #[derive(TS)] - struct A {} - - #[derive(TS)] - struct B {} - - #[derive(TS)] - struct X { - x: i32, - a: A, - a2: A, - map: HashMap, - } - - println!("{:?}", X::dependencies().into_iter().collect::>()); - println!("{:?}", get_deps::()); -}