scuffle-ffmpeg 0.3.5

FFmpeg bindings for Rust.
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
use std::sync::{Arc, Mutex};

use bytes::{Buf, BytesMut};

/// A wrapper around a channel that implements `std::io::Read` and
/// `std::io::Write`. The wrapper allows for the channel to be used with the
/// `Input` and `Output` structs.
#[derive(Debug, Clone)]
pub struct ChannelCompat<T: Send> {
    /// I am unsure if the mutex is needed here. I do not think it is, but I am
    /// not sure. FFmpeg might require the IO to be synchronized, but I do not
    /// think it does.
    inner: Arc<Mutex<T>>,
    buffer: BytesMut,
}

impl<T: Send> ChannelCompat<T> {
    /// Creates a new `ChannelCompat` instance.
    pub fn new(inner: T) -> Self {
        Self {
            inner: Arc::new(Mutex::new(inner)),
            buffer: BytesMut::new(),
        }
    }
}

/// A trait that represents a channel that can be read from.
pub trait ChannelCompatRecv: Send {
    /// The type of data that the channel can receive.
    type Data: AsRef<[u8]>;

    /// Receives data from the channel.
    fn channel_recv(&mut self) -> Option<Self::Data>;

    /// Tries to receive data from the channel.
    fn try_channel_recv(&mut self) -> Option<Self::Data>;

    /// Converts the channel to a `ChannelCompat` instance.
    fn into_compat(self) -> ChannelCompat<Self>
    where
        Self: Sized,
    {
        ChannelCompat::new(self)
    }
}

/// A trait that represents a channel that can be written to.
pub trait ChannelCompatSend: Send {
    /// The type of data that the channel can send.
    type Data: From<Vec<u8>>;

    /// Sends data to the channel.
    fn channel_send(&mut self, data: Self::Data) -> bool;

    /// Converts the channel to a `ChannelCompat` instance.
    fn into_compat(self) -> ChannelCompat<Self>
    where
        Self: Sized,
    {
        ChannelCompat::new(self)
    }
}

#[cfg(feature = "tokio-channel")]
impl<D: AsRef<[u8]> + Send> ChannelCompatRecv for tokio::sync::mpsc::Receiver<D> {
    type Data = D;

    fn channel_recv(&mut self) -> Option<Self::Data> {
        self.blocking_recv()
    }

    fn try_channel_recv(&mut self) -> Option<Self::Data> {
        self.try_recv().ok()
    }
}

#[cfg(feature = "tokio-channel")]
impl<D: From<Vec<u8>> + Send> ChannelCompatSend for tokio::sync::mpsc::Sender<D> {
    type Data = D;

    fn channel_send(&mut self, data: Self::Data) -> bool {
        self.blocking_send(data).is_ok()
    }
}

#[cfg(feature = "tokio-channel")]
impl<D: AsRef<[u8]> + Send> ChannelCompatRecv for tokio::sync::mpsc::UnboundedReceiver<D> {
    type Data = D;

    fn channel_recv(&mut self) -> Option<Self::Data> {
        self.blocking_recv()
    }

    fn try_channel_recv(&mut self) -> Option<Self::Data> {
        self.try_recv().ok()
    }
}

#[cfg(feature = "tokio-channel")]
impl<D: From<Vec<u8>> + Send> ChannelCompatSend for tokio::sync::mpsc::UnboundedSender<D> {
    type Data = D;

    fn channel_send(&mut self, data: Self::Data) -> bool {
        self.send(data).is_ok()
    }
}

#[cfg(feature = "tokio-channel")]
impl<D: AsRef<[u8]> + Clone + Send> ChannelCompatRecv for tokio::sync::broadcast::Receiver<D> {
    type Data = D;

    fn channel_recv(&mut self) -> Option<Self::Data> {
        self.blocking_recv().ok()
    }

    fn try_channel_recv(&mut self) -> Option<Self::Data> {
        self.try_recv().ok()
    }
}

#[cfg(feature = "tokio-channel")]
impl<D: From<Vec<u8>> + Clone + Send> ChannelCompatSend for tokio::sync::broadcast::Sender<D> {
    type Data = D;

    fn channel_send(&mut self, data: Self::Data) -> bool {
        self.send(data).is_ok()
    }
}

#[cfg(feature = "crossbeam-channel")]
impl<D: AsRef<[u8]> + Send> ChannelCompatRecv for crossbeam_channel::Receiver<D> {
    type Data = D;

    fn channel_recv(&mut self) -> Option<Self::Data> {
        self.recv().ok()
    }

    fn try_channel_recv(&mut self) -> Option<Self::Data> {
        self.try_recv().ok()
    }
}

#[cfg(feature = "crossbeam-channel")]
impl<D: From<Vec<u8>> + Send> ChannelCompatSend for crossbeam_channel::Sender<D> {
    type Data = D;

    fn channel_send(&mut self, data: Self::Data) -> bool {
        self.send(data).is_ok()
    }
}

impl<D: AsRef<[u8]> + Send> ChannelCompatRecv for std::sync::mpsc::Receiver<D> {
    type Data = D;

    fn channel_recv(&mut self) -> Option<Self::Data> {
        self.recv().ok()
    }

    fn try_channel_recv(&mut self) -> Option<Self::Data> {
        self.try_recv().ok()
    }
}

impl<D: From<Vec<u8>> + Send> ChannelCompatSend for std::sync::mpsc::Sender<D> {
    type Data = D;

    fn channel_send(&mut self, data: Self::Data) -> bool {
        self.send(data).is_ok()
    }
}

impl<D: From<Vec<u8>> + Send> ChannelCompatSend for std::sync::mpsc::SyncSender<D> {
    type Data = D;

    fn channel_send(&mut self, data: Self::Data) -> bool {
        self.send(data).is_ok()
    }
}

impl<T: ChannelCompatRecv> std::io::Read for ChannelCompat<T> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.buffer.len() >= buf.len() {
            buf.copy_from_slice(&self.buffer[..buf.len()]);
            self.buffer.advance(buf.len());
            return Ok(buf.len());
        }

        let mut inner = self.inner.lock().unwrap();

        let mut total_read = 0;
        if self.buffer.is_empty() {
            let Some(data) = inner.channel_recv() else {
                return Ok(0);
            };

            let data = data.as_ref();
            let min = data.len().min(buf.len());

            buf.copy_from_slice(&data[..min]);
            self.buffer.extend_from_slice(&data[min..]);
            total_read += min;
        } else {
            buf[..self.buffer.len()].copy_from_slice(&self.buffer);
            total_read += self.buffer.len();
            self.buffer.clear();
        }

        while let Some(Some(data)) = (total_read < buf.len()).then(|| inner.try_channel_recv()) {
            let data = data.as_ref();
            let min = data.len().min(buf.len() - total_read);
            buf[total_read..total_read + min].copy_from_slice(&data[..min]);
            self.buffer.extend_from_slice(&data[min..]);
            total_read += min;
        }

        Ok(total_read)
    }
}

impl<T: ChannelCompatSend> std::io::Write for ChannelCompat<T> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if !self.inner.lock().unwrap().channel_send(buf.to_vec().into()) {
            return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "Unexpected EOF"));
        }

        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[cfg(test)]
#[cfg_attr(all(test, coverage_nightly), coverage(off))]
mod tests {
    use std::io::{Read, Write};

    use rand::Rng;
    use rand::distr::StandardUniform;

    use crate::io::channel::{ChannelCompat, ChannelCompatRecv, ChannelCompatSend};

    macro_rules! make_test {
        (
            $(
                $(
                    #[variant($name:ident, $channel:expr$(, cfg($($cfg_meta:meta)*))?)]
                )*
                |$tx:ident, $rx:ident| $body:block
            )*
        ) => {
            $(
                $(
                    #[test]
                    $(#[cfg($($cfg_meta)*)])?
                    fn $name() {
                        let ($tx, $rx) = $channel;
                        $body
                    }
                )*
            )*
        };
    }

    // test 1000 byte read
    make_test! {
        #[variant(
            test_read_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_read_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_read_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, rx| {
            let mut reader = rx.into_compat();

            // generate 1000 bytes of random data
            let mut rng = rand::rng();
            let data: Vec<u8> = (0..1000).map(|_| rng.sample::<u8, _>(StandardUniform)).collect();

            let mut tx = tx;
            let write_result = tx.channel_send(data.clone());
            assert!(write_result);

            // read 1000 bytes
            let mut buffer = vec![0u8; 1000];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok());
            assert_eq!(read_result.unwrap(), data.len());

            // data read must match data written
            assert_eq!(buffer, data);
        }
    }

    // test 1000 byte write
    make_test! {
        #[variant(
            test_write_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_write_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_write_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_write_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_write_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_write_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, rx| {
            let mut writer = tx.into_compat();

        // generate 1000 bytes of random data
        let mut rng = rand::rng();
        let data: Vec<u8> = (0..1000).map(|_| rng.sample::<u8, _>(StandardUniform)).collect();

        let write_result = writer.write(&data);
        assert!(write_result.is_ok(), "Failed to write data to the channel");
        assert_eq!(write_result.unwrap(), data.len(), "Written byte count mismatch");

        // read 1000 bytes
        let mut rx = rx;
        let read_result = rx.channel_recv();
        assert!(read_result.is_some(), "No data received from the channel");

        let received_data = read_result.unwrap();
        assert_eq!(received_data.len(), data.len(), "Received byte count mismatch");

        // data read must match data written
        assert_eq!(
            received_data, data,
            "Mismatch between written data and received data"
        );
        }
    }

    // test read with smaller buffer than data
    make_test! {
        #[variant(
            test_read_smaller_buffer_than_data_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_read_smaller_buffer_than_data_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_read_smaller_buffer_than_data_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_smaller_buffer_than_data_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_smaller_buffer_than_data_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_smaller_buffer_than_data_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, rx| {
            let mut reader = ChannelCompat::new(rx);
            let data = b"PartialReadTest".to_vec();
            let mut tx = tx;
            let send_result = tx.channel_send(data);
            assert!(send_result);

            let mut buffer = vec![0u8; 7]; // buffer.len() < data.len()
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok());
            assert_eq!(read_result.unwrap(), buffer.len());
            assert_eq!(&buffer, b"Partial");

            // Read the remaining part of the data
            let mut buffer = vec![0u8; 8];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok());
            assert_eq!(read_result.unwrap(), buffer.len());
            assert_eq!(&buffer, b"ReadTest");
        }
    }

    // test read with no data
    make_test! {
        #[variant(
            test_read_no_data_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_read_no_data_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_read_no_data_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_no_data_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_no_data_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_no_data_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, rx| {
            let mut reader = ChannelCompat::new(rx);

            // no data is sent to the channel + drop tx to prevent it from blocking
            drop(tx);
            let mut buffer = vec![0u8; 10];
            let read_result = reader.read(&mut buffer);

            assert!(read_result.is_ok());
            assert_eq!(read_result.unwrap(), 0);
        }
    }

    // test read non-empty buffer after initial read to catch else
    make_test! {
        #[variant(
            test_read_else_case_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_read_else_case_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_read_else_case_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_else_case_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_else_case_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_else_case_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, rx| {
            let mut reader = ChannelCompat::new(rx);
            let mut tx = tx;

            let data1 = b"FirstChunk".to_vec();
            let write_result1 = tx.channel_send(data1);
            assert!(write_result1, "Failed to send data1");

            // read the first part of the data ("First")
            let mut buffer = vec![0u8; 5];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok(), "Failed to read the first chunk");
            let bytes_read = read_result.unwrap();
            assert_eq!(bytes_read, buffer.len(), "Mismatch in first chunk read size");
            assert_eq!(&buffer, b"First", "Buffer content mismatch for first part of FirstChunk");

            // read the remaining part of data1 ("Chunk") and part of data2 which hasn't been written yet ("Secon")
            let mut buffer = vec![0u8; 10];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok(), "Failed to read the next 10 bytes");
            let bytes_read = read_result.unwrap();

            // validate that the buffer contains "Chunk" at this point
            assert_eq!(bytes_read, 5, "Unexpected read size for the next part");
            assert_eq!(&buffer[..bytes_read], b"Chunk", "Buffer content mismatch for combined reads");

            // Write second chunk of data ("SecondChunk")
            let data2 = b"SecondChunk".to_vec();
            let write_result2 = tx.channel_send(data2);
            assert!(write_result2, "Failed to send data2");

            // verify that there's leftover data from data2
            let mut buffer = vec![0u8; 5];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok(), "Failed to read leftover data from data2");
            let bytes_read = read_result.unwrap();
            assert!(bytes_read > 0, "No leftover data from data2 was available");
        }
    }

    // test read to hit the while loop
    make_test! {
        #[variant(
            test_read_while_case_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_read_while_case_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_read_while_case_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_while_case_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_while_case_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_read_while_case_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, rx| {
            let mut reader = ChannelCompat::new(rx);
            let mut tx = tx;

            let data1 = b"FirstChunk".to_vec();
            let write_result1 = tx.channel_send(data1);
            assert!(write_result1, "Failed to send data1");

            // read "First"
            let mut buffer = vec![0u8; 5];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok(), "Failed to read the first chunk");
            let bytes_read = read_result.unwrap();
            assert_eq!(bytes_read, buffer.len(), "Mismatch in first chunk read size");
            assert_eq!(&buffer, b"First", "Buffer content mismatch for first part of FirstChunk");

            // write "SecondChunk"
            let data2 = b"SecondChunk".to_vec();
            let write_result2 = tx.channel_send(data2);
            assert!(write_result2, "Failed to send data2");

            // read "ChunkSecon"
            let mut buffer = vec![0u8; 10];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok(), "Failed to read the next chunk of data");
            let bytes_read = read_result.unwrap();
            assert!(bytes_read > 0, "No data was read");
            assert_eq!(&buffer[..bytes_read], b"ChunkSecon", "Buffer content mismatch");

            // continue reading to enter the while loop
            let mut buffer = vec![0u8; 6];
            let read_result = reader.read(&mut buffer);
            assert!(read_result.is_ok(), "Failed to read remaining data");
            let bytes_read = read_result.unwrap();
            assert!(bytes_read > 0, "No additional data was read");
            assert_eq!(&buffer[..bytes_read], b"dChunk", "Buffer content mismatch for remaining data");
        }
    }

    // test write return ErrorKind::UnexpectedEof
    make_test! {
        #[variant(
            test_write_eof_error_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_write_eof_error_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_write_eof_error_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_write_eof_error_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_write_eof_error_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_write_eof_error_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, rx| {
            let mut writer = ChannelCompat::new(tx);

            // simulate sending failure by dropping the receiver
            drop(rx);

            let data = vec![42u8; 100];
            let write_result = writer.write(&data);
            assert!(write_result.is_err());
            assert_eq!(write_result.unwrap_err().kind(), std::io::ErrorKind::UnexpectedEof);
        }
    }

    // test write flush
    make_test! {
        #[variant(
            test_flush_std_mpsc,
            std::sync::mpsc::channel::<Vec<u8>>()
        )]
        #[variant(
            test_flush_std_sync_mpsc,
            std::sync::mpsc::sync_channel::<Vec<u8>>(1)
        )]
        #[variant(
            test_flush_tokio_mpsc,
            tokio::sync::mpsc::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_flush_tokio_unbounded,
            tokio::sync::mpsc::unbounded_channel::<Vec<u8>>(),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_flush_tokio_broadcast,
            tokio::sync::broadcast::channel::<Vec<u8>>(1),
            cfg(feature = "tokio-channel")
        )]
        #[variant(
            test_flush_crossbeam_unbounded,
            crossbeam_channel::unbounded::<Vec<u8>>(),
            cfg(feature = "crossbeam-channel")
        )]
        |tx, _rx| {
            let mut writer = ChannelCompat::new(tx);

            let flush_result = writer.flush();
            assert!(flush_result.is_ok());
        }
    }
}