1use std::sync::Arc;
2
3pub use structify_derive::structify;
4
5pub struct Dep<T> {
6 value: Arc<T>,
7}
8
9impl<T> Dep<T> {
10 pub fn new(value: T) -> Self {
11 Self {
12 value: Arc::new(value),
13 }
14 }
15
16 pub fn get(&self) -> Arc<T> {
17 self.value.clone()
18 }
19
20 pub fn inner(&self) -> &T {
21 &self.value
22 }
23}
24
25impl<T> From<Arc<T>> for Dep<T> {
26 fn from(value: Arc<T>) -> Self {
27 Self { value }
28 }
29}
30
31impl<T> From<T> for Dep<T> {
32 fn from(value: T) -> Self {
33 Self::new(value)
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::{structify, Dep};
40
41 #[structify]
42 fn unit() {}
43
44 #[test]
45 fn test_unit() {
46 Unit::new().execute();
47 }
48
49 #[structify]
50 fn unit_with_return() -> i32 {
51 42
52 }
53
54 #[test]
55 fn test_unit_with_return() {
56 assert_eq!(UnitWithReturn::new().execute(), 42);
57 }
58
59 #[structify]
60 fn args_with_return(a: i32, b: i32) -> i32 {
61 a + b
62 }
63
64 #[test]
65 fn test_args_with_return() {
66 assert_eq!(ArgsWithReturn::new(1, 2).execute(), 3);
67 }
68
69 #[structify]
70 fn arg_and_dep_with_return(i: i32, dep: Dep<i32>) -> i32 {
71 *dep.inner() + i
72 }
73
74 #[test]
75 fn test_arg_and_dep_with_return() {
76 let s = ArgAndDepWithReturn::new(1).execute(41);
77 assert_eq!(s, 42);
78 }
79
80 #[structify]
81 fn mixed_args_and_deps_with_return(dep: Dep<i32>, a: i32, dep2: Dep<i32>, b: i32) -> i32 {
82 *dep.inner() + *dep2.inner() + a + b
83 }
84
85 #[test]
86 fn test_mixed_args_and_deps_with_return() {
87 let s = MixedArgsAndDepsWithReturn::new(1, 2).execute(3, 4);
88 assert_eq!(s, 10);
89 }
90
91 #[structify(NameIsB)]
92 fn name_is_a() {}
93
94 #[test]
95 fn test_name_is_not_a() {
96 NameIsB::new().execute();
97 }
98
99 #[structify]
100 async fn async_unit_with_return() -> i32 {
101 42
102 }
103
104 #[tokio::test]
105 async fn test_async_unit_with_return() {
106 assert_eq!(AsyncUnitWithReturn::new().execute().await, 42);
107 }
108
109 #[structify]
110 async fn async_args_with_return(a: i32, b: i32) -> i32 {
111 a + b
112 }
113
114 #[tokio::test]
115 async fn test_async_args_with_return() {
116 assert_eq!(AsyncArgsWithReturn::new(1, 2).execute().await, 3);
117 }
118
119 #[structify]
120 async fn async_arg_and_dep_with_return(i: i32, dep: Dep<i32>) -> i32 {
121 *dep.inner() + i
122 }
123
124 #[tokio::test]
125 async fn test_async_arg_and_dep_with_return() {
126 let s = AsyncArgAndDepWithReturn::new(1).execute(41).await;
127 assert_eq!(s, 42);
128 }
129}