From 2a8a0601afcb82d90d0766db5a954b70b10f856d Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 15 Nov 2021 10:29:52 +0100 Subject: publish --- tests/gats.rs | 17 +++++++ tests/tests.rs | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 tests/gats.rs create mode 100644 tests/tests.rs (limited to 'tests') diff --git a/tests/gats.rs b/tests/gats.rs new file mode 100644 index 0000000..92e483c --- /dev/null +++ b/tests/gats.rs @@ -0,0 +1,17 @@ +//! This test can be run with `cargo +nightly test --features=nightly` +#![cfg_attr(feature = "nightly", feature(generic_associated_types))] + +#[cfg(feature = "nightly")] +mod test_gats { + #[dynamize::dynamize] + pub trait MyTrait { + type A<'a>: Into<&'a str>; + + fn test1<'b>(&self) -> Self::A<'b>; + } + + fn test(mut some: T) { + let dyn_trait: &dyn DynMyTrait = &some; + let _: &str = dyn_trait.test1(); + } +} diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..6bccc04 --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,152 @@ +#![allow(dead_code)] + +#[test] +fn it_works() { + use dynamize::dynamize; + + mod some { + pub mod module { + pub type Result = std::io::Result; + } + } + + #[dynamize] + /// This is a great trait! + pub trait MyTrait { + type A: Into; + // if there are multiple Into bounds the first one is used + type B: Into + Into; + + fn test1(&self) -> Self::A; + fn test2(&self) -> Self::B; + fn test3(&self) -> Option; + fn test4(&self) -> Result<(), Self::A>; + fn test5(&self) -> Result; + /// some method documentation + fn test6(&self) -> Result; + + #[allow(clippy::type_complexity)] + fn test7(&self) -> Result>, Option>>; + + // also support Result type aliases with a fixed error type + fn test8(&self) -> some::module::Result; + + // fn test9(&self) -> &dyn Iterator; + + fn mut1(&mut self) -> Self::A; + + fn safe1(&self); + fn safe2(&self, num: i32) -> i32; + fn safe3<'a>(&self, text: &'a str) -> &'a str; + fn safe4(&self) -> Option; + + // non-dispatchable functions are skipped + fn non_dispatch1(); + fn non_dispatch2(num: i32); + fn non_dispatch3(self) -> Self::A; + fn non_dispatch4(&self) + where + Self: Sized; + } + + fn test(mut some: T) { + let dyn_trait: &dyn DynMyTrait = &some; + let _: String = dyn_trait.test1(); + let _: i32 = dyn_trait.test2(); + let _: Option = dyn_trait.test3(); + let _: Result<(), String> = dyn_trait.test4(); + let _: Result = dyn_trait.test5(); + let _: Result = dyn_trait.test6(); + let _: Result>, Option>> = dyn_trait.test7(); + + let dyn_trait: &mut dyn DynMyTrait = &mut some; + dyn_trait.mut1(); + + let _: () = dyn_trait.safe1(); + let _: i32 = dyn_trait.safe2(0); + let _: &str = dyn_trait.safe3("test"); + let _: Option = dyn_trait.safe4(); + } +} + +#[dynamize::dynamize] +trait Foo { + type A: Into; + + fn foobar(&self, x: X) -> Self::A; +} + +#[dynamize::dynamize] +trait Bar { + fn foobar(&self, x: X) -> A; +} + +fn test, X, A>(some: T) { + let _dyn_trait: &dyn DynBar = &some; +} + +#[dynamize::dynamize] +trait Bar1 { + fn foobar(&self, x: X) -> A; + fn foobar1(&self, x: X) -> B; + fn foobar2(&self, x: X) -> C; +} + +fn test1, X, A, B, C>(some: T) { + let _dyn_trait: &dyn DynBar1 = &some; +} + +#[dynamize::dynamize] +trait Buz { + type C: Into; + + fn foobar(&self, x: X) -> Result; +} + +fn test2, X, A>(some: T, x: X) -> Result { + let dyn_trait: &dyn DynBuz = &some; + dyn_trait.foobar(x) +} + +#[dynamize::dynamize] +trait Gen { + fn foobar(&self, a: A) -> A; +} + +use async_trait::async_trait; + +#[dynamize::dynamize] +#[dyn_trait_attr(async_trait)] +#[blanket_impl_attr(async_trait)] +#[async_trait] +trait SomeTraitWithAsync: Sync { + type A: Into; + async fn test1(&self) -> Self::A; +} + +async fn async_test(some: T) { + let dyn_trait: &dyn DynSomeTraitWithAsync = &some; + let _: String = dyn_trait.test1().await; +} + +#[dynamize::dynamize] +trait TraitWithCallback { + type A: Into; + fn fun_with_callback(&self, a: F); + + fn fun_with_callback1)>(&self, a: X); + + fn fun_with_callback2, String) -> bool>(&self, a: Y); + + fn fun_with_callback3(&self, a: Z); +} + +#[dynamize::dynamize] +#[dyn_trait_attr(async_trait)] +#[blanket_impl_attr(async_trait)] +#[async_trait] +trait AsyncWithCallback: Sync { + type A: Into; + async fn test1(&self) -> Self::A; + async fn fun_with_callback(&self, a: F); +} -- cgit v1.2.3