trash_parallelism 0.1.102

Azzybana Raccoon's comprehensive parallelism library.
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Async file writers with buffering and compression.
//!
//! This module provides ergonomic async writers for various output destinations,
//! with built-in buffering, compression, and serialization support.
//!
//! ## Features
//!
//! - **Buffered Writing**: Automatic buffering with configurable flush triggers
//! - **Compression**: Integrated Brotli/gzip compression for file writers
//! - **Serialization**: JSON/binary serialization using serde utilities
//! - **Async Operations**: Non-blocking writes using smol and futures-lite
//! - **Progress Tracking**: Optional progress callbacks and statistics
//!
//! ## Examples
//!
//! Basic buffered writing:
//! ```rust,no_run
//! use trash_utilities::io::writers::AsyncFileWriter;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create a compressed writer
//!     let mut writer = AsyncFileWriter::with_config("output.txt", 8192, true).await?;
//!     
//!     // Write data (buffered)
//!     writer.write(b"Hello, ").await?;
//!     writer.write(b"world!").await?;
//!     
//!     // Write JSON data
//!     writer.write_json(&serde_json::json!({"message": "done"})).await?;
//!     
//!     // Flush to disk
//!     writer.flush().await?;
//!     println!("Written {} bytes", writer.bytes_written());
//!     
//!     Ok(())
//! }
//! ```
//!
//! Streaming large data:
//! ```rust,no_run
//! use trash_utilities::io::writers::StreamingFileWriter;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let mut writer = StreamingFileWriter::new("large_output.bin", 65536, false).await?;
//!     
//!     for chunk in large_data_chunks {
//!         writer.write_chunk(&chunk).await?;
//!     }
//!     
//!     println!("Total bytes written: {}", writer.bytes_written());
//!     Ok(())
//! }
//! ```

// Standard library imports
// (none needed)

// External crate imports
use bytes::Bytes;
use futures_lite::{AsyncWriteExt, StreamExt};

/// Async buffered file writer with compression support
///
/// Provides buffered writing with automatic flushing, optional compression,
/// and progress tracking. Uses smol for async I/O operations.
///
/// # Examples
///
/// Basic usage:
/// ```rust,no_run
/// use trash_utilities::io::writers::AsyncFileWriter;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // Create writer with compression
///     let mut writer = AsyncFileWriter::with_config("output.txt", 8192, true).await?;
///     
///     // Write data
///     writer.write(b"Hello, world!").await?;
///     writer.write_json(&serde_json::json!({"status": "ok"})).await?;
///     
///     // Flush and check bytes written
///     writer.flush().await?;
///     println!("Bytes written: {}", writer.bytes_written());
///     
///     Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct AsyncFileWriter {
    file: smol::fs::File,
    buffer: Vec<u8>,
    buffer_size: usize,
    compressed: bool,
    bytes_written: u64,
}

impl AsyncFileWriter {
    /// Create a new async file writer with default settings
    ///
    /// # Parameters
    /// - `path`: Path to the file to write
    ///
    /// # Returns
    /// New `AsyncFileWriter` instance
    ///
    /// # Errors
    /// Returns error if file cannot be created
    pub async fn new(path: &str) -> Result<Self, std::io::Error> {
        Self::with_config(path, 8192, false).await
    }

    /// Create a new async file writer with custom configuration
    ///
    /// # Parameters
    /// - `path`: Path to the file to write
    /// - `buffer_size`: Size of the write buffer in bytes
    /// - `compressed`: Whether to compress data using Brotli
    ///
    /// # Returns
    /// Configured `AsyncFileWriter` instance
    ///
    /// # Errors
    /// Returns error if file cannot be created
    pub async fn with_config(
        path: &str,
        buffer_size: usize,
        compressed: bool,
    ) -> Result<Self, std::io::Error> {
        let file = smol::fs::File::create(path).await?;
        Ok(Self {
            file,
            buffer: Vec::with_capacity(buffer_size),
            buffer_size,
            compressed,
            bytes_written: 0,
        })
    }

    /// Write data to the buffer (async, non-blocking)
    ///
    /// Data is buffered until the buffer is full or `flush()` is called.
    /// If compression is enabled, data is compressed before buffering.
    ///
    /// # Parameters
    /// - `data`: Data to write
    ///
    /// # Returns
    /// Success or I/O error
    ///
    /// # Errors
    /// Returns error if compression fails or buffer write fails
    pub async fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> {
        let data_to_write = if self.compressed {
            // Compress data using our compression utilities
            crate::io::utils::compress_brotli(data, 6)?
        } else {
            data.to_vec()
        };

        self.buffer.extend_from_slice(&data_to_write);

        if self.buffer.len() >= self.buffer_size {
            self.flush().await?;
        }

        Ok(())
    }

    /// Write serialized data (async, non-blocking)
    ///
    /// Serializes the data to JSON and writes it with optional compression.
    ///
    /// # Type Parameters
    /// - `T`: Type that implements serde Serialize
    ///
    /// # Parameters
    /// - `data`: Data to serialize and write
    ///
    /// # Returns
    /// Success or error
    ///
    /// # Errors
    /// Returns error if serialization, compression, or writing fails
    pub async fn write_json<T: serde::Serialize>(
        &mut self,
        data: &T,
    ) -> Result<(), std::io::Error> {
        let json = serde_json::to_string(data)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
        self.write(json.as_bytes()).await
    }

    /// Flush buffer to file (async, non-blocking)
    ///
    /// Forces all buffered data to be written to the file.
    ///
    /// # Returns
    /// Success or I/O error
    ///
    /// # Errors
    /// Returns error if file write fails
    pub async fn flush(&mut self) -> Result<(), std::io::Error> {
        if !self.buffer.is_empty() {
            self.file.write_all(&self.buffer).await?;
            self.bytes_written += self.buffer.len() as u64;
            self.buffer.clear();
        }
        self.file.flush().await?;
        Ok(())
    }

    /// Get total bytes written to the file
    #[must_use]
    pub fn bytes_written(&self) -> u64 {
        self.bytes_written
    }

    /// Check if compression is enabled
    #[must_use]
    pub fn is_compressed(&self) -> bool {
        self.compressed
    }
}

impl Drop for AsyncFileWriter {
    fn drop(&mut self) {
        // Note: This is sync, in practice you'd want an async drop
        futures_lite::future::block_on(async {
            let _ = self.flush().await;
        });
    }
}

/// Streaming file writer for large data sets
///
/// Optimized for streaming large amounts of data with minimal memory usage.
/// Uses chunked writing and can integrate with async streams.
pub struct StreamingFileWriter {
    file: smol::fs::File,
    chunk_size: usize,
    compressed: bool,
    bytes_written: u64,
}

impl StreamingFileWriter {
    /// Create a new streaming file writer
    ///
    /// # Parameters
    /// - `path`: Path to the file to write
    /// - `chunk_size`: Size of chunks to write at once
    /// - `compressed`: Whether to compress chunks
    ///
    /// # Returns
    /// New `StreamingFileWriter` instance
    ///
    /// # Errors
    /// Returns error if file cannot be created
    pub async fn new(
        path: &str,
        chunk_size: usize,
        compressed: bool,
    ) -> Result<Self, std::io::Error> {
        let file = smol::fs::File::create(path).await?;
        Ok(Self {
            file,
            chunk_size,
            compressed,
            bytes_written: 0,
        })
    }

    /// Write a chunk of data (async, non-blocking)
    ///
    /// Writes data immediately without buffering. Suitable for streaming.
    ///
    /// # Parameters
    /// - `data`: Data chunk to write
    ///
    /// # Returns
    /// Success or I/O error
    ///
    /// # Errors
    /// Returns error if compression or writing fails
    pub async fn write_chunk(&mut self, data: &[u8]) -> Result<(), std::io::Error> {
        let data_to_write = if self.compressed {
            crate::io::utils::compress_brotli(data, 6)?
        } else {
            data.to_vec()
        };

        // Write the chunk immediately and flush to ensure data is visible
        // to subsequent reads before the file is closed.
        self.file.write_all(&data_to_write).await?;
        self.file.flush().await?;
        self.bytes_written += data_to_write.len() as u64;
        Ok(())
    }

    /// Write from an async stream (async, non-blocking)
    ///
    /// Consumes an async stream and writes all chunks to the file.
    ///
    /// # Type Parameters
    /// - `S`: Async stream type yielding byte chunks
    ///
    /// # Parameters
    /// - `stream`: Async stream of byte data
    ///
    /// # Returns
    /// Success or error
    ///
    /// # Errors
    /// Returns error if reading from stream or writing fails
    pub async fn write_from_stream<S>(&mut self, mut stream: S) -> Result<(), std::io::Error>
    where
        S: futures_lite::Stream<Item = Result<Bytes, std::io::Error>> + Unpin,
    {
        while let Some(chunk_result) = stream.next().await {
            let chunk = chunk_result?;
            self.write_chunk(&chunk).await?;
        }
        self.file.flush().await?;
        Ok(())
    }

    /// Get total bytes written
    #[must_use]
    pub fn bytes_written(&self) -> u64 {
        self.bytes_written
    }

    /// Get the chunk size
    #[must_use]
    pub fn chunk_size(&self) -> usize {
        self.chunk_size
    }
}

/// Buffered writer with automatic compression and serialization
///
/// Advanced writer that combines buffering, compression, and serialization
/// with progress tracking and error recovery.
pub struct AdvancedFileWriter {
    inner: AsyncFileWriter,
    progress_callback: Option<Box<dyn Fn(u64) + Send + Sync>>,
    error_recovery: bool,
}

impl AdvancedFileWriter {
    /// Create a new advanced file writer
    ///
    /// # Parameters
    /// - `path`: Path to the file to write
    /// - `buffer_size`: Size of the write buffer
    /// - `compressed`: Whether to compress data
    /// - `progress_callback`: Optional callback for progress updates
    /// - `error_recovery`: Whether to attempt error recovery
    ///
    /// # Returns
    /// New `AdvancedFileWriter` instance
    ///
    /// # Errors
    /// Returns error if file cannot be created
    pub async fn new(
        path: &str,
        buffer_size: usize,
        compressed: bool,
        progress_callback: Option<Box<dyn Fn(u64) + Send + Sync>>,
        error_recovery: bool,
    ) -> Result<Self, std::io::Error> {
        let inner = AsyncFileWriter::with_config(path, buffer_size, compressed).await?;
        Ok(Self {
            inner,
            progress_callback,
            error_recovery,
        })
    }

    /// Write data with progress tracking and error recovery
    ///
    /// # Parameters
    /// - `data`: Data to write
    ///
    /// # Returns
    /// Success or error
    ///
    /// # Errors
    /// Returns error if writing fails (with optional recovery attempts)
    pub async fn write_with_progress(&mut self, data: &[u8]) -> Result<(), std::io::Error> {
        let result = self.inner.write(data).await;

        if let Err(ref e) = result
            && self.error_recovery
        {
            // Attempt recovery (could implement retry logic, backup writing, etc.)
            eprintln!("Write error, attempting recovery: {e}");
            // For now, just log - could implement more sophisticated recovery
        }

        // Update progress
        if let Some(ref callback) = self.progress_callback {
            callback(self.inner.bytes_written());
        }

        result
    }

    /// Write serialized data with progress tracking
    ///
    /// # Type Parameters
    /// - `T`: Type that implements serde Serialize
    ///
    /// # Parameters
    /// - `data`: Data to serialize and write
    ///
    /// # Returns
    /// Success or error
    ///
    /// # Errors
    /// Returns error if serialization or writing fails
    pub async fn write_json_with_progress<T: serde::Serialize>(
        &mut self,
        data: &T,
    ) -> Result<(), std::io::Error> {
        let json = serde_json::to_string(data)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
        self.write_with_progress(json.as_bytes()).await
    }

    /// Flush all buffered data
    ///
    /// # Returns
    /// Success or I/O error
    ///
    /// # Errors
    /// Returns an I/O error if flushing the buffer fails.
    pub async fn flush(&mut self) -> Result<(), std::io::Error> {
        self.inner.flush().await
    }

    /// Get total bytes written
    #[must_use]
    pub fn bytes_written(&self) -> u64 {
        self.inner.bytes_written()
    }
}

/// Utility function for writing to stdout asynchronously
///
/// # Parameters
/// - `data`: Data to write to stdout
///
/// # Returns
/// Success or I/O error
///
/// # Errors
/// Returns error if stdout write fails
pub fn write_stdout_async(data: &[u8]) -> Result<(), std::io::Error> {
    use std::io::Write;
    let mut stdout = std::io::stdout().lock();
    stdout.write_all(data)?;
    stdout.flush()?;
    Ok(())
}

/// Utility function for writing to stderr asynchronously
///
/// # Parameters
/// - `data`: Data to write to stderr
///
/// # Returns
/// Success or I/O error
///
/// # Errors
/// Returns error if stderr write fails
pub fn write_stderr_async(data: &[u8]) -> Result<(), std::io::Error> {
    use std::io::Write;
    let mut stderr = std::io::stderr().lock();
    stderr.write_all(data)?;
    stderr.flush()?;
    Ok(())
}