1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// This file demonstrates compile-time assertion for Send + 'static bounds
//
// The test cases below show:
// 1. Valid: Using Send + 'static types (passes compilation)
// 2. Invalid: Using non-Send types (fails at compile time with clear error)
//
// To see these in action, comment out valid functions and uncomment invalid ones
use Arc;
use com_thread;
// ✅ VALID: Using Send + 'static types
async
async
// ❌ INVALID EXAMPLES (COMMENTED OUT - uncomment to see compile error)
//
// These would FAIL to compile with clear error messages:
//
// USE CASE 1: Non-Send type (Rc - reference counted, not atomic)
// #[com_thread]
// fn invalid_with_rc(rc: std::rc::Rc<i32>) -> i32 {
// *rc
// }
// ERROR: `Rc<i32>` cannot be sent between threads safely
// --> the trait `Send` is not implemented for `Rc<i32>`
// --> SOLUTION: Use Arc<i32> instead
//
// USE CASE 2: Borrowed reference (can't cross thread boundary)
// #[com_thread]
// fn invalid_with_ref(s: &str) -> usize {
// s.len()
// }
// ERROR: `&str` cannot be sent between threads safely
// --> lifetime references can't live in another thread
// --> SOLUTION: Use String instead of &str
//
// USE CASE 3: Custom type without Send
// struct NotSend {
// rc: std::rc::Rc<i32>,
// }
//
// #[com_thread]
// fn invalid_with_custom(obj: NotSend) -> i32 {
// 42
// }
// ERROR: `NotSend` cannot be sent between threads safely
// --> the trait `Send` is not implemented for `NotSend`
// --> SOLUTION: Either:
// --> - Add `#[derive(Send)]` to NotSend (if all fields are Send)
// --> - Change non-Send fields to Send equivalents
// --> - Use `unsafe impl Send for NotSend { }`
//
// USE CASE 4: Non-'static type (lifetime reference in struct)
// struct NotStatic<'a> {
// reference: &'a str,
// }
//
// #[com_thread]
// fn invalid_with_lifetime(obj: NotStatic) -> String {
// obj.reference.to_string()
// }
// ERROR: `NotStatic` cannot be sent between threads safely
// --> the trait `Send` is not implemented for `NotStatic<'_>`
// --> SOLUTION: Remove lifetime references, use owned data (String not &str)