use std::{fmt::Display, pin::Pin}; #[autogen::register] struct Struct { t: T, y: T, } #[autogen::apply] impl Struct { fn result(&self, _e: E) -> Result { todo!() } } trait Def where Self: Sized, { type Item; const DEF: Option; fn get_def(self) -> Option { Self::DEF } } trait S { type Struct; fn get(&self) -> &Self::Struct; } #[test] fn trait_generic_arg_as_input() { #[autogen::apply] impl From for String { fn from(val: Struct) -> Self { format!("{} as string", val.t) } } let s = Struct { t: "abc", y: "y" }; let string: String = s.into(); assert_eq!(string, "abc as string"); } #[test] fn trait_generic_arg_as_output() { #[autogen::apply] impl From for Struct { fn from(val: T) -> Struct { let a: Struct = Struct { t: val.clone(), y: val, }; a } } let string: String = "abc".to_string(); let s: Struct<_> = string.into(); assert_eq!(s.t, "abc"); } #[test] fn associated_type_and_const() { #[autogen::apply] impl Def for Struct { type Item = Struct; const DEF: Option = None; } #[autogen::apply(Struct2 = Struct)] impl S for Struct2 { type Struct = Struct2; fn get(&self) -> &Self::Struct { self } } let s = Struct { t: "t", y: "y" }; assert_eq!(s.get().t, s.t); assert!(s.get_def().is_none()); } #[test] fn inner_fn() { #[autogen::apply] impl Def for T { type Item = Struct; const DEF: Option = None; fn get_def(self) -> Option { #[autogen::apply] fn inner(t: T) -> Option { let s: Struct = Struct { y: t.clone(), t }; Some(s) } inner(self) } } let string = "abc".to_string(); assert_eq!(string.get_def().unwrap().t, "abc"); } #[test] fn expressions() { #[autogen::apply] impl Iterator for Struct { type Item = Struct; fn next(&mut self) -> Option { None } } #[autogen::apply] impl From>> for Struct { fn from(value: Box>) -> Self { let from_str = Struct::from("abc".to_string()); let _aa: String = test3(from_str); // MethodCall let _ = any::().result::(any::()); let vec: Vec = value.collect::>().into_iter().collect(); // Index, Reference let s: &Struct = &vec[calc_index::<&Struct>(&vec.iter())]; // Binary let mut x = calc_index::<&Struct>(&vec.iter()) + calc_index::<&Struct>(&vec.iter()); test(s); // Loop let _ = loop { // If if x > 1 && s.t == any::().t { // Break break Some(test2::(s)); } else { // Field let _ = test2::(s).t; // Assign x = calc_index::<&Struct>(&vec.iter()) + calc_index::<&Struct>(&vec.iter()); } }; // Cast let _ = s as *const Struct; // Closure let _: Vec<_> = vec .iter() .map(|s: &Struct| -> &Struct { if s.t == any::<&Struct>().y { s } else { any::<&Struct>() } }) .collect(); // Unsafe unsafe { let a = &any::(); Pin::new_unchecked(a); } // Const let _: Struct = const { any::() }; // Let if let Some(a) = any::>() { let _: Struct = a; } // ForLoop for i in any::>() { let _: Struct = i; } // Match let _ = match any::>() { Some(some) => some, // Return None => return any::(), }; // Paren (any::()); // Range for _ in to_any::()..to_any::() { let x = to_any::(); println!("{x}"); } // Repeat let _ = [const { any::() }; 2]; // Struct let _ = Struct { t: any::().t, ..any::() }; // Tuple let _ = (any::(), any::>()); // Unary let _ = !to_any::(); // While while to_any::() < 3 { // Call let _ = any::(); } try_test::(); let _a = a1(s); // Array let array = [test2::(s)]; array.into_iter().next().unwrap() } } #[autogen::apply] fn try_test() -> Option { // Try let val = any::>()?; Some(val) } #[autogen::apply] fn test(_value: &impl Iterator) -> Struct { todo!() } fn test2(_value: &impl Iterator) -> T { todo!() } fn calc_index(_value: &impl Iterator) -> usize { todo!() } const fn any() -> A { todo!() } fn to_any() -> B { any::(); todo!() } #[autogen::apply] fn test3>(value: Struct) -> S { value.into() } #[autogen::apply] async fn a1(value: &impl Iterator) { // Async let a = async { any::() }; a.await; // Await a2::(test2::(value)).await; } #[autogen::apply] async fn a2>(value: Struct) -> S { value.into() } }