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
use std::io::{self, Read, Write, Seek, SeekFrom};
use std::fs::File;
use std::fmt;
use std::path::Path;
use std::env;
use std;
use super::imp;
pub struct TempFile(File);
impl fmt::Debug for TempFile {
#[cfg(unix)]
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::os::unix::io::AsRawFd;
write!(f, "TempFile({})", self.0.as_raw_fd())
}
#[cfg(windows)]
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::os::windows::io::AsRawHandle;
write!(f, "TempFile({})", self.0.as_raw_handle() as usize)
}
}
impl TempFile {
#[inline]
pub fn new() -> io::Result<TempFile> {
Self::new_in(&env::temp_dir())
}
#[inline]
pub fn new_in<P: AsRef<Path>>(dir: P) -> io::Result<TempFile> {
imp::create(dir.as_ref()).map(|f| TempFile(f))
}
#[inline]
pub fn shared(count: usize) -> io::Result<Vec<TempFile>> {
Self::shared_in(&env::temp_dir(), count)
}
#[inline]
pub fn shared_in<P: AsRef<Path>>(dir: P, count: usize) -> io::Result<Vec<TempFile>> {
imp::create_shared(dir.as_ref(), count).map(|files| {
files.into_iter().map(|f|TempFile(f)).collect()
})
}
#[inline]
pub fn len(&self) -> io::Result<u64> {
self.0.metadata().map(|m| m.len())
}
#[inline(always)]
pub fn set_len(&self, size: u64) -> io::Result<()> {
self.0.set_len(size)
}
}
impl Read for TempFile {
#[inline(always)]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
impl Write for TempFile {
#[inline(always)]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
#[inline(always)]
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
impl Seek for TempFile {
#[inline(always)]
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.0.seek(pos)
}
}
#[cfg(unix)]
impl std::os::unix::io::AsRawFd for TempFile {
#[inline(always)]
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
self.0.as_raw_fd()
}
}
#[cfg(windows)]
impl std::os::windows::io::AsRawHandle for TempFile {
#[inline(always)]
fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
self.0.as_raw_handle()
}
}