|
1 | 1 | use super::{repeat, Cursor, SeekFrom}; |
2 | 2 | use crate::cmp::{self, min}; |
3 | | -use crate::io::prelude::*; |
4 | 3 | use crate::io::{self, IoSlice, IoSliceMut}; |
| 4 | +use crate::io::{BufRead, BufReader, BufWriter, Read, Result, Seek, Write}; |
5 | 5 | use crate::ops::Deref; |
6 | 6 |
|
7 | 7 | #[test] |
@@ -492,3 +492,54 @@ fn test_write_all_vectored() { |
492 | 492 | } |
493 | 493 | } |
494 | 494 | } |
| 495 | + |
| 496 | +#[test] |
| 497 | +#[cfg(unix)] |
| 498 | +fn copy_specialization() -> Result<()> { |
| 499 | + let path = crate::env::temp_dir(); |
| 500 | + let source_path = path.join("copy-spec.source"); |
| 501 | + let sink_path = path.join("copy-spec.sink"); |
| 502 | + |
| 503 | + let result: Result<()> = try { |
| 504 | + let mut source = crate::fs::OpenOptions::new() |
| 505 | + .read(true) |
| 506 | + .write(true) |
| 507 | + .create(true) |
| 508 | + .truncate(true) |
| 509 | + .open(&source_path)?; |
| 510 | + source.write_all(b"abcdefghiklmnopqr")?; |
| 511 | + source.seek(SeekFrom::Start(8))?; |
| 512 | + let mut source = BufReader::with_capacity(8, source.take(5)); |
| 513 | + source.fill_buf()?; |
| 514 | + assert_eq!(source.buffer(), b"iklmn"); |
| 515 | + source.get_mut().set_limit(6); |
| 516 | + source.get_mut().get_mut().seek(SeekFrom::Start(1))?; // "bcdefg" |
| 517 | + let mut source = source.take(10); // "iklmnbcdef" |
| 518 | + |
| 519 | + let mut sink = crate::fs::OpenOptions::new() |
| 520 | + .read(true) |
| 521 | + .write(true) |
| 522 | + .create(true) |
| 523 | + .truncate(true) |
| 524 | + .open(&sink_path)?; |
| 525 | + sink.write_all(b"000000")?; |
| 526 | + let mut sink = BufWriter::with_capacity(5, sink); |
| 527 | + sink.write_all(b"wxyz")?; |
| 528 | + assert_eq!(sink.buffer(), b"wxyz"); |
| 529 | + |
| 530 | + let copied = crate::io::copy(&mut source, &mut sink)?; |
| 531 | + assert_eq!(copied, 10); |
| 532 | + assert_eq!(sink.buffer().len(), 0); |
| 533 | + |
| 534 | + let mut sink = sink.into_inner()?; |
| 535 | + sink.seek(SeekFrom::Start(0))?; |
| 536 | + let mut copied = Vec::new(); |
| 537 | + sink.read_to_end(&mut copied)?; |
| 538 | + assert_eq!(&copied, b"000000wxyziklmnbcdef"); |
| 539 | + }; |
| 540 | + |
| 541 | + let rm1 = crate::fs::remove_file(source_path); |
| 542 | + let rm2 = crate::fs::remove_file(sink_path); |
| 543 | + |
| 544 | + result.and(rm1).and(rm2) |
| 545 | +} |
0 commit comments