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
//! High-performance I/O utilities for asynchronous file operations, compression, and caching.
//!
//! This module provides comprehensive async I/O operations using smol runtime,
//! compression algorithms (Brotli, Gzip), cryptographic hashing, and thread-safe
//! data structures for caching and concurrency. Designed for high-throughput
//! applications requiring efficient data processing and storage.
//!
//! ## Features
//!
//! - **Async File Operations**: Non-blocking file I/O with smol runtime
//! - **Compression Support**: Brotli and Gzip compression/decompression
//! - **Parallel Processing**: Concurrent file operations and batch processing
//! - **Streaming I/O**: Efficient stream processing and channel utilities
//! - **Advanced Writers**: Buffered and compressed file writing
//! - **Thread-Safe Caching**: LRU caches and atomic counters
//! - **Memory Efficient**: Optimized memory usage with string interning
//! - **Cryptographic Operations**: Hashing and integrity checking
//!
//! ## Architecture
//!
//! ### Core Components
//! - **`utils`**: Basic file operations, compression, counters, and caching
//! - **`parallelism`**: Parallel file processing using existing parallel module
//! - **`writers`**: Async file writers with buffering and compression
//! - **`streams`**: Streaming I/O operations and channel utilities
//!
//! ## Examples
//!
//! ### Basic Async File Operations
//! ```rust,no_run
//! use trash_utilities::io::utils::*;
//! use smol;
//!
//! # smol::block_on(async {
//! // Read and write files asynchronously
//! let content = read_file_async("input.txt").await.unwrap();
//! write_file_async("output.txt", &content).await.unwrap();
//!
//! // Directory operations
//! create_dir_async("backup").await.unwrap();
//! let entries = read_dir_async(".").await.unwrap();
//! println!("Found {} files/directories", entries.len());
//!
//! // File metadata
//! let size = get_file_size_async("input.txt").await.unwrap();
//! let modified = get_file_modified_async("input.txt").await.unwrap();
//! println!("File size: {} bytes, modified: {:?}", size, modified);
//! # });
//! ```
//!
//! ### Compression and Archiving
//! ```rust,no_run
//! use trash_utilities::io::utils::*;
//! use smol;
//!
//! # smol::block_on(async {
//! // Compress data
//! let data = b"This is some data to compress efficiently.";
//! let compressed = compress_data_brotli(data, 6).await.unwrap();
//!
//! // Write compressed data to file
//! write_file_async("data.compressed", &compressed).await.unwrap();
//!
//! // Read and decompress
//! let compressed_data = read_file_async("data.compressed").await.unwrap();
//! let decompressed = decompress_data_brotli(&compressed_data).await.unwrap();
//!
//! assert_eq!(data.to_vec(), decompressed);
//! println!("Compression ratio: {:.2}", compressed.len() as f64 / data.len() as f64);
//! # });
//! ```
//!
//! ### Parallel File Processing
//! ```rust,no_run
//! use trash_utilities::io::parallelism::*;
//! use smol;
//!
//! # smol::block_on(async {
//! // Process multiple files in parallel
//! let files = vec!["file1.txt", "file2.txt", "file3.txt"];
//!
//! // Read all files concurrently
//! let contents = read_files_parallel(&files).await.unwrap();
//!
//! // Process contents (e.g., count lines)
//! let line_counts = process_files_parallel(contents, |content| {
//! content.lines().count()
//! }).await;
//!
//! for (i, count) in line_counts.iter().enumerate() {
//! println!("File {}: {} lines", i + 1, count);
//! }
//!
//! // Write results in parallel
//! let results: Vec<String> = line_counts.iter()
//! .enumerate()
//! .map(|(i, &count)| format!("File {}: {} lines\n", i + 1, count))
//! .collect();
//!
//! write_files_parallel(&files, &results).await.unwrap();
//! # });
//! ```
//!
//! ### Streaming Data Processing
//! ```rust,no_run
//! use trash_utilities::io::streams::*;
//! use smol;
//!
//! # smol::block_on(async {
//! // Create a streaming processor
//! let mut processor = create_stream_processor();
//!
//! // Process data in chunks
//! let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
//! let chunks = data.chunks(3);
//!
//! for chunk in chunks {
//! processor.process_chunk(chunk).await.unwrap();
//! }
//!
//! // Get final result
//! let result = processor.finalize().await.unwrap();
//! println!("Processed {} elements", result);
//! # });
//! ```
//!
//! ### Advanced File Writing
//! ```rust,no_run
//! use trash_utilities::io::writers::*;
//! use smol;
//!
//! # smol::block_on(async {
//! // Create a buffered, compressed writer
//! let writer = create_compressed_writer("output.gz", 6).await.unwrap();
//!
//! // Write data efficiently
//! let data = b"Large amounts of data to write efficiently...";
//! writer.write_chunk(data).await.unwrap();
//! writer.write_chunk(data).await.unwrap();
//!
//! // Flush and close
//! writer.flush().await.unwrap();
//! writer.close().await.unwrap();
//!
//! // Verify file was created and compressed
//! let compressed_size = get_file_size_async("output.gz").await.unwrap();
//! println!("Compressed file size: {} bytes", compressed_size);
//! # });
//! ```
//!
//! ### Thread-Safe Caching and Counters
//! ```rust
//! use trash_utilities::io::utils::*;
//! use std::sync::Arc;
//! use std::thread;
//!
//! // Shared cache across threads
//! let cache = Arc::new(LruCache::new(1000));
//!
//! // Atomic counter for statistics
//! let counter = Arc::new(AtomicCounter::new());
//!
//! let mut handles = vec![];
//!
//! for i in 0..10 {
//! let cache_clone = Arc::clone(&cache);
//! let counter_clone = Arc::clone(&counter);
//!
//! let handle = thread::spawn(move || {
//! // Use cache and counter from multiple threads
//! cache_clone.insert(format!("key_{}", i), format!("value_{}", i));
//! counter_clone.increment();
//! });
//!
//! handles.push(handle);
//! }
//!
//! // Wait for all threads
//! for handle in handles {
//! handle.join().unwrap();
//! }
//!
//! assert_eq!(counter.get(), 10);
//! assert_eq!(cache.len(), 10);
//! ```
//!
//! ### String Interning for Memory Efficiency
//! ```rust
//! use trash_utilities::io::utils::*;
//!
//! // Create string interner for memory efficiency
//! let interner = StringInterner::new();
//!
//! // Intern frequently used strings
//! let user1 = interner.intern("frequently_used_username");
//! let user2 = interner.intern("frequently_used_username");
//!
//! // Both references point to the same memory location
//! assert_eq!(user1.as_ptr(), user2.as_ptr());
//! assert_eq!(interner.len(), 1); // Only one copy in memory
//!
//! // Useful for caching, configuration, etc.
//! let config_keys = vec![
//! "database.host",
//! "database.port",
//! "cache.size",
//! "cache.ttl",
//! ];
//!
//! let interned_keys: Vec<_> = config_keys.iter()
//! .map(|key| interner.intern(key))
//! .collect();
//!
//! println!("Interned {} unique strings", interner.len());
//! ```
// Submodule declarations
// Re-exports for backward compatibility and convenience
pub use *;
pub use *;
pub use *;
pub use *;