thread-share 0.1.6

A Rust library for safe and efficient data sharing between threads with zero-copy operations, change detection, and enhanced thread management.
Documentation
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::thread;
use std::time::Duration;
use thread_share::{share, ArcThreadShareLocked};

#[test]
fn test_arc_thread_share_locked_from_arc() {
    let original = share!(100);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    assert_eq!(locked_share.get(), 100);
}

#[test]
fn test_arc_thread_share_locked_set_get() {
    let original = share!(42);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    locked_share.set(100);
    assert_eq!(locked_share.get(), 100);

    locked_share.set(200);
    assert_eq!(locked_share.get(), 200);

    locked_share.set(300);
    assert_eq!(locked_share.get(), 300);
}

#[test]
fn test_arc_thread_share_locked_update() {
    let original = share!(vec![1, 2, 3]);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    locked_share.update(|v| v.push(4));
    assert_eq!(locked_share.get(), vec![1, 2, 3, 4]);

    locked_share.update(|v| {
        v[0] = 100;
        v.push(5);
    });
    assert_eq!(locked_share.get(), vec![100, 2, 3, 4, 5]);
}

#[test]
fn test_arc_thread_share_locked_read() {
    let original = share!(String::from("hello world"));
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    let length = locked_share.read(|s| s.len());
    assert_eq!(length, 11);

    let contains_hello = locked_share.read(|s| s.contains("hello"));
    assert!(contains_hello);

    let uppercase = locked_share.read(|s| s.to_uppercase());
    assert_eq!(uppercase, "HELLO WORLD");
}

#[test]
fn test_arc_thread_share_locked_write() {
    let original = share!(vec![1, 2, 3]);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    let doubled = locked_share.write(|v| {
        v.iter_mut().for_each(|x| *x *= 2);
        v.clone()
    });
    assert_eq!(doubled, vec![2, 4, 6]);
    assert_eq!(locked_share.get(), vec![2, 4, 6]);

    let sum = locked_share.write(|v| {
        let sum = v.iter().sum::<i32>();
        v.push(sum);
        sum
    });
    assert_eq!(sum, 12);
    assert_eq!(locked_share.get(), vec![2, 4, 6, 12]);
}

#[test]
fn test_arc_thread_share_locked_thread_safety() {
    let original = share!(0);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data.clone());
    let mut handles = vec![];

    // Spawn multiple threads that increment the counter
    for _ in 0..5 {
        let share_clone = ArcThreadShareLocked::from_arc(arc_data.clone());
        let handle = thread::spawn(move || {
            for _ in 0..100 {
                share_clone.update(|x| *x += 1);
                thread::sleep(Duration::from_millis(1));
            }
        });
        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    // Final value should be 500 (5 threads × 100 increments each)
    assert_eq!(locked_share.get(), 500);
}

#[test]
fn test_arc_thread_share_locked_concurrent_access() {
    let original = share!(vec![0; 100]);
    let arc_data = original.as_arc_locked();

    // Writer thread
    let writer = thread::spawn({
        let share_clone = ArcThreadShareLocked::from_arc(arc_data.clone());
        move || {
            for i in 0..100 {
                share_clone.update(|v| {
                    v[i] = i as i32;
                });
                thread::sleep(Duration::from_millis(1));
            }
        }
    });

    // Reader thread
    let reader = thread::spawn({
        let share_clone = ArcThreadShareLocked::from_arc(arc_data.clone());
        move || {
            let mut last_sum = 0;
            for _ in 0..100 {
                let current_sum = share_clone.read(|v| v.iter().sum::<i32>());
                if current_sum > last_sum {
                    last_sum = current_sum;
                }
                thread::sleep(Duration::from_millis(2));
            }
            last_sum
        }
    });

    let final_sum = reader.join().unwrap();
    writer.join().unwrap();

    // Verify final state
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);
    let final_vec = locked_share.get();
    let expected_sum: i32 = (0..100).sum();
    assert_eq!(final_sum, expected_sum);
    assert_eq!(final_vec.iter().sum::<i32>(), expected_sum);
}

#[test]
fn test_arc_thread_share_locked_custom_struct() {
    #[derive(Clone, Debug, PartialEq)]
    struct TestStruct {
        value: i32,
        text: String,
        flag: bool,
    }

    let original = share!(TestStruct {
        value: 10,
        text: "test".to_string(),
        flag: false,
    });

    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    // Test initial state
    let initial = locked_share.get();
    assert_eq!(initial.value, 10);
    assert_eq!(initial.text, "test");
    assert_eq!(initial.flag, false);

    // Test update
    locked_share.update(|data| {
        data.value = 20;
        data.text = "updated".to_string();
        data.flag = true;
    });

    // Test updated state
    let updated = locked_share.get();
    assert_eq!(updated.value, 20);
    assert_eq!(updated.text, "updated");
    assert_eq!(updated.flag, true);
}

#[test]
fn test_arc_thread_share_locked_string_operations() {
    let original = share!(String::from("hello"));
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    // Test string operations
    let length = locked_share.read(|s| s.len());
    assert_eq!(length, 5);

    locked_share.update(|s| s.push_str(" world"));
    assert_eq!(locked_share.get(), "hello world");

    let uppercase = locked_share.read(|s| s.to_uppercase());
    assert_eq!(uppercase, "HELLO WORLD");

    locked_share.update(|s| s.truncate(5));
    assert_eq!(locked_share.get(), "hello");
}

#[test]
fn test_arc_thread_share_locked_numeric_operations() {
    let original = share!(0u64);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    // Test numeric operations
    locked_share.update(|n| *n += 100);
    assert_eq!(locked_share.get(), 100);

    locked_share.update(|n| *n *= 2);
    assert_eq!(locked_share.get(), 200);

    locked_share.update(|n| *n -= 50);
    assert_eq!(locked_share.get(), 150);

    let is_even = locked_share.read(|n| n % 2 == 0);
    assert!(is_even);
}

#[test]
fn test_arc_thread_share_locked_vector_operations() {
    let original = share!(Vec::<i32>::new());
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    // Test empty vector
    let length = locked_share.read(|v| v.len());
    assert_eq!(length, 0);

    // Test push operations
    locked_share.update(|v| v.push(1));
    assert_eq!(locked_share.get(), vec![1]);

    locked_share.update(|v| v.push(2));
    assert_eq!(locked_share.get(), vec![1, 2]);

    // Test clear operation
    locked_share.update(|v| v.clear());
    assert_eq!(locked_share.get(), vec![] as Vec<i32>);
}

#[test]
fn test_arc_thread_share_locked_multiple_clones() {
    let original = share!(42);
    let arc_data = original.as_arc_locked();

    // Create multiple clones
    let clone1 = ArcThreadShareLocked::from_arc(arc_data.clone());
    let clone2 = ArcThreadShareLocked::from_arc(arc_data.clone());

    // Change through one clone
    clone1.set(100);

    // All should see the change
    assert_eq!(clone1.get(), 100);
    assert_eq!(clone2.get(), 100);

    // Change through another clone
    clone2.update(|x| *x += 50);

    // All should see the change
    assert_eq!(clone1.get(), 150);
    assert_eq!(clone2.get(), 150);
}

#[test]
fn test_arc_thread_share_locked_large_data() {
    let large_vec: Vec<i32> = (0..1000).collect();
    let original = share!(large_vec.clone());
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    // Verify initial data
    assert_eq!(locked_share.get(), large_vec);

    // Test update with large data
    locked_share.update(|v| {
        v.iter_mut().for_each(|x| *x *= 2);
    });

    let doubled: Vec<i32> = large_vec.iter().map(|x| x * 2).collect();
    assert_eq!(locked_share.get(), doubled);
}

#[test]
fn test_arc_thread_share_locked_memory_management() {
    let original = share!(String::from("test"));
    let arc_data = original.as_arc_locked();

    // Create many clones to test memory management
    let mut clones = Vec::new();
    for _ in 0..100 {
        clones.push(ArcThreadShareLocked::from_arc(arc_data.clone()));
    }

    // Update through one clone
    clones[0].set(String::from("updated"));

    // Verify all clones see the change
    for clone in &clones {
        assert_eq!(clone.get(), "updated");
    }

    // Drop clones to test cleanup
    drop(clones);

    // Original should still work
    assert_eq!(original.get(), "updated");
}

#[test]
fn test_arc_thread_share_locked_edge_cases() {
    // Test with zero-sized type
    let original = share!(());
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    locked_share.set(());
    assert_eq!(locked_share.get(), ());

    // Test with unit type
    locked_share.update(|_| {});
    assert_eq!(locked_share.get(), ());
}

#[test]
fn test_arc_thread_share_locked_performance_pattern() {
    let original = share!(0);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data.clone());
    let share_clone = ArcThreadShareLocked::from_arc(arc_data);

    // Simulate high-frequency updates
    let handle = thread::spawn(move || {
        for _ in 0..1000 {
            share_clone.update(|x| *x += 1);
        }
    });

    // Simulate high-frequency reads
    let mut total = 0;
    for _ in 0..1000 {
        total += locked_share.read(|x| *x);
        thread::sleep(Duration::from_micros(1));
    }

    handle.join().unwrap();

    // Verify final state
    let final_value = locked_share.get();
    assert_eq!(final_value, 1000);
    assert!(total > 0); // Should have read some values
}

#[test]
fn test_arc_thread_share_locked_sync_with_original() {
    let original = share!(42);
    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    // Change through original
    original.set(100);

    // Locked share should see the change
    assert_eq!(locked_share.get(), 100);

    // Change through locked share
    locked_share.set(200);

    // Original should see the change
    assert_eq!(original.get(), 200);

    // Both should be in sync
    assert_eq!(original.get(), locked_share.get());
}

#[test]
fn test_arc_thread_share_locked_complex_nested_operations() {
    #[derive(Clone, Debug, PartialEq)]
    struct ComplexStruct {
        numbers: Vec<i32>,
        metadata: String,
        flags: Vec<bool>,
    }

    let original = share!(ComplexStruct {
        numbers: vec![1, 2, 3],
        metadata: "initial".to_string(),
        flags: vec![false, false, false],
    });

    let arc_data = original.as_arc_locked();
    let locked_share = ArcThreadShareLocked::from_arc(arc_data);

    // Complex nested update
    locked_share.update(|data| {
        // Update numbers
        data.numbers.iter_mut().for_each(|n| *n *= 2);
        data.numbers.push(8);

        // Update metadata
        data.metadata.push_str("_updated");

        // Update flags
        data.flags.iter_mut().for_each(|f| *f = true);
        data.flags.push(false);
    });

    // Verify complex update
    let result = locked_share.get();
    assert_eq!(result.numbers, vec![2, 4, 6, 8]);
    assert_eq!(result.metadata, "initial_updated");
    assert_eq!(result.flags, vec![true, true, true, false]);
}