ino 0.0.3

I/O traits more general than std::io.
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
/*
 * Description: I/O traits with special support for Linux.
 *
 * Copyright (C) 2025 d@nny mc² <dmc2@amass.energy>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! I/O traits with special support for Linux.

use core::{mem, ops, ptr};
#[cfg(all(unix, feature = "std-io"))]
use std::{
  fs::File,
  io::{self, PipeReader, PipeWriter},
  os::fd::AsRawFd,
};

#[cfg(all(unix, feature = "std-io"))]
use libc;

use super::BufError;
#[cfg(all(unix, feature = "std-io"))]
use super::{BlockingPipe, NonBlockingPipe};

#[cfg(all(unix, feature = "std-io"))]
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct FilePos(libc::off64_t);
#[cfg(all(unix, feature = "std-io"))]
impl ops::Deref for FilePos {
  type Target = libc::off64_t;
  #[inline(always)]
  fn deref(&self) -> &Self::Target { &self.0 }
}
#[cfg(all(unix, feature = "std-io"))]
impl ops::DerefMut for FilePos {
  #[inline(always)]
  fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
}
#[cfg(all(unix, feature = "std-io"))]
impl From<u64> for FilePos {
  #[inline(always)]
  fn from(x: u64) -> Self {
    if x > libc::off64_t::MAX as u64 {
      unreachable!("could not convert {x} to file offset! (too large)");
    }
    Self(x as libc::off64_t)
  }
}
#[cfg(all(unix, feature = "std-io"))]
impl ops::AddAssign<usize> for FilePos {
  #[inline]
  fn add_assign(&mut self, rhs: usize) {
    if rhs > libc::off64_t::MAX as usize {
      unreachable!("could not convert {rhs} to file offset in +=! (too large)");
    }
    self.0 = match self.0.checked_add(rhs as libc::off64_t) {
      None => unreachable!("could not add {rhs} to file offset in +=! (would become too large)"),
      Some(n) => n,
    };
  }
}

pub trait PReadBuf<T> {
  type Pos: ops::AddAssign<usize>+Copy;
  type E: BufError;
  fn pread_buf<'buf>(
    &self,
    pos: Self::Pos,
    buf: &'buf mut [mem::MaybeUninit<T>],
  ) -> (usize, Option<Self::E>);
  #[inline]
  fn pread_buf_exact<'buf>(
    &self,
    mut pos: Self::Pos,
    mut buf: &'buf mut [mem::MaybeUninit<T>],
  ) -> Result<(), (usize, Self::E)> {
    let mut all_pread: usize = 0;
    while !buf.is_empty() {
      let (cur_pread, err) = self.pread_buf(pos, buf);
      all_pread += cur_pread;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("pread_buf() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_pread, err));
        }
      }
      buf = &mut buf[cur_pread..];
      pos += cur_pread;
    }
    Ok(())
  }
}

#[cfg(all(unix, feature = "std-io"))]
impl PReadBuf<u8> for File {
  type Pos = FilePos;
  type E = io::Error;
  #[inline]
  fn pread_buf<'buf>(
    &self,
    pos: Self::Pos,
    buf: &'buf mut [mem::MaybeUninit<u8>],
  ) -> (usize, Option<Self::E>) {
    let fd = self.as_raw_fd();
    let FilePos(offset) = pos;
    let count = buf.len();
    let buf: *mut libc::c_void = buf.as_mut_ptr().cast();
    match unsafe { libc::pread64(fd, buf, count, offset) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait PWriteData<T> {
  type Pos: ops::AddAssign<usize>+Copy;
  type E: BufError;
  fn pwrite_data<'data>(&mut self, pos: Self::Pos, data: &'data [T]) -> (usize, Option<Self::E>);
  #[inline]
  fn pwrite_data_exact<'data>(
    &mut self,
    mut pos: Self::Pos,
    mut data: &'data [T],
  ) -> Result<(), (usize, Self::E)> {
    let mut all_pwritten: usize = 0;
    while !data.is_empty() {
      let (cur_pwritten, err) = self.pwrite_data(pos, data);
      all_pwritten += cur_pwritten;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("pwrite_data() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_pwritten, err));
        }
      }
      data = &data[cur_pwritten..];
      pos += cur_pwritten;
    }
    Ok(())
  }
}

#[cfg(all(unix, feature = "std-io"))]
impl PWriteData<u8> for File {
  type Pos = FilePos;
  type E = io::Error;
  #[inline]
  fn pwrite_data<'data>(&mut self, pos: Self::Pos, data: &'data [u8]) -> (usize, Option<Self::E>) {
    let fd = self.as_raw_fd();
    let FilePos(offset) = pos;
    let count = data.len();
    let buf: *const libc::c_void = data.as_ptr().cast();
    match unsafe { libc::pwrite64(fd, buf, count, offset) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait Splice<P> {
  type E: BufError;
  fn splice(&mut self, to: &mut P, n: usize) -> (usize, Option<Self::E>);
  #[inline]
  fn splice_exact(&mut self, to: &mut P, mut n: usize) -> Result<(), (usize, Self::E)> {
    let mut all_spliced: usize = 0;
    while n > 0 {
      let (cur_spliced, err) = self.splice(to, n);
      all_spliced += cur_spliced;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("splice() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_spliced, err));
        }
      }
      n -= cur_spliced;
    }
    Ok(())
  }
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl Splice<BlockingPipe<PipeWriter>> for BlockingPipe<PipeReader> {
  type E = io::Error;
  #[inline]
  fn splice(&mut self, to: &mut BlockingPipe<PipeWriter>, n: usize) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    match unsafe { libc::splice(in_fd, ptr::null_mut(), out_fd, ptr::null_mut(), n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl Splice<BlockingPipe<PipeWriter>> for File {
  type E = io::Error;
  #[inline]
  fn splice(&mut self, to: &mut BlockingPipe<PipeWriter>, n: usize) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    match unsafe { libc::splice(in_fd, ptr::null_mut(), out_fd, ptr::null_mut(), n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl Splice<File> for BlockingPipe<PipeReader> {
  type E = io::Error;
  #[inline]
  fn splice(&mut self, to: &mut File, n: usize) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    match unsafe { libc::splice(in_fd, ptr::null_mut(), out_fd, ptr::null_mut(), n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait SpliceNonBlocking<P> {
  type E: BufError;
  fn splice_nonblocking(&mut self, to: &mut P, n: usize) -> (usize, Option<Self::E>);
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl SpliceNonBlocking<NonBlockingPipe<PipeWriter>> for NonBlockingPipe<PipeReader> {
  type E = io::Error;
  #[inline]
  fn splice_nonblocking(
    &mut self,
    to: &mut NonBlockingPipe<PipeWriter>,
    n: usize,
  ) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    match unsafe {
      libc::splice(
        in_fd,
        ptr::null_mut(),
        out_fd,
        ptr::null_mut(),
        n,
        libc::SPLICE_F_NONBLOCK,
      )
    } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait SpliceAt<P> {
  type E: BufError;
  type Pos: ops::AddAssign<usize>+Copy;
  fn splice_at(&self, start: &mut Self::Pos, n: usize, to: &mut P) -> (usize, Option<Self::E>);
  #[inline]
  fn splice_at_exact(
    &self,
    start: &mut Self::Pos,
    mut n: usize,
    to: &mut P,
  ) -> Result<(), (usize, Self::E)> {
    let mut all_spliced: usize = 0;
    while n > 0 {
      let (cur_spliced, err) = self.splice_at(start, n, to);
      all_spliced += cur_spliced;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("splice() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_spliced, err));
        }
      }
      n -= cur_spliced;
    }
    Ok(())
  }
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl SpliceAt<BlockingPipe<PipeWriter>> for File {
  type E = io::Error;
  type Pos = FilePos;
  #[inline]
  fn splice_at(
    &self,
    start: &mut Self::Pos,
    n: usize,
    to: &mut BlockingPipe<PipeWriter>,
  ) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    let &mut FilePos(mut in_off) = start;
    let in_off: *mut libc::off64_t = ptr::from_mut(&mut in_off);
    match unsafe { libc::splice(in_fd, in_off, out_fd, ptr::null_mut(), n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait SpliceToAt<P> {
  type E: BufError;
  type Pos: ops::AddAssign<usize>+Copy;
  fn splice_to_at(&mut self, n: usize, to: &mut P, at: &mut Self::Pos) -> (usize, Option<Self::E>);
  #[inline]
  fn splice_to_at_exact(
    &mut self,
    mut n: usize,
    to: &mut P,
    at: &mut Self::Pos,
  ) -> Result<(), (usize, Self::E)> {
    let mut all_spliced: usize = 0;
    while n > 0 {
      let (cur_spliced, err) = self.splice_to_at(n, to, at);
      all_spliced += cur_spliced;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("splice() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_spliced, err));
        }
      }
      n -= cur_spliced;
    }
    Ok(())
  }
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl SpliceToAt<File> for BlockingPipe<PipeReader> {
  type E = io::Error;
  type Pos = FilePos;
  #[inline]
  fn splice_to_at(
    &mut self,
    n: usize,
    to: &mut File,
    at: &mut Self::Pos,
  ) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    let &mut FilePos(mut out_off) = at;
    let out_off: *mut libc::off64_t = ptr::from_mut(&mut out_off);
    match unsafe { libc::splice(in_fd, ptr::null_mut(), out_fd, out_off, n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait Tee<P> {
  type E: BufError;
  fn tee(&self, to: &mut P, n: usize) -> (usize, Option<Self::E>);
  #[inline]
  fn tee_exact(&self, to: &mut P, mut n: usize) -> Result<(), (usize, Self::E)> {
    let mut all_teed: usize = 0;
    while n > 0 {
      let (cur_teed, err) = self.tee(to, n);
      all_teed += cur_teed;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("tee() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_teed, err));
        }
      }
      n -= cur_teed;
    }
    Ok(())
  }
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl Tee<BlockingPipe<PipeWriter>> for BlockingPipe<PipeReader> {
  type E = io::Error;
  #[inline]
  fn tee(&self, to: &mut BlockingPipe<PipeWriter>, n: usize) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    match unsafe { libc::tee(in_fd, out_fd, n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait TeeNonBlocking<P> {
  type E: BufError;
  fn tee_nonblocking(&self, to: &mut P, n: usize) -> (usize, Option<Self::E>);
}
#[cfg(all(target_os = "linux", feature = "std-io"))]
impl TeeNonBlocking<NonBlockingPipe<PipeWriter>> for NonBlockingPipe<PipeReader> {
  type E = io::Error;
  #[inline]
  fn tee_nonblocking(
    &self,
    to: &mut NonBlockingPipe<PipeWriter>,
    n: usize,
  ) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    match unsafe { libc::tee(in_fd, out_fd, n, libc::SPLICE_F_NONBLOCK) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}

pub trait CopyRange<P> {
  type Pos: ops::AddAssign<usize>+Copy;
  type E: BufError;
  fn copy_range(
    &self,
    start: &mut Self::Pos,
    n: usize,
    to: &mut P,
    pos: Option<&mut Self::Pos>,
  ) -> (usize, Option<Self::E>);
  #[inline]
  fn copy_range_exact(
    &self,
    start: &mut Self::Pos,
    mut n: usize,
    to: &mut P,
    pos: Option<&mut Self::Pos>,
  ) -> Result<(), (usize, Self::E)> {
    let mut all_copied: usize = 0;
    let pos_ptr: Option<ptr::NonNull<Self::Pos>> = pos.map(ptr::NonNull::from_mut);
    while n > 0 {
      let (cur_copied, err) =
        self.copy_range(start, n, to, pos_ptr.map(|mut pos| unsafe { pos.as_mut() }));
      all_copied += cur_copied;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("copy_range() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_copied, err));
        }
      }
      n -= cur_copied;
    }
    Ok(())
  }
  fn copy_range_mut(
    &mut self,
    n: usize,
    to: &mut P,
    pos: Option<&mut Self::Pos>,
  ) -> (usize, Option<Self::E>);
  #[inline]
  fn copy_range_exact_mut(
    &mut self,
    mut n: usize,
    to: &mut P,
    pos: Option<&mut Self::Pos>,
  ) -> Result<(), (usize, Self::E)> {
    let mut all_copied: usize = 0;
    let pos_ptr: Option<ptr::NonNull<Self::Pos>> = pos.map(ptr::NonNull::from_mut);
    while n > 0 {
      let (cur_copied, err) =
        self.copy_range_mut(n, to, pos_ptr.map(|mut pos| unsafe { pos.as_mut() }));
      all_copied += cur_copied;
      if let Some(err) = err {
        if err.would_block() {
          unreachable!("copy_range() is allowed to block!");
        }
        if !err.was_interrupted() {
          return Err((all_copied, err));
        }
      }
      n -= cur_copied;
    }
    Ok(())
  }
}

#[cfg(all(target_os = "linux", feature = "std-io"))]
impl CopyRange<File> for File {
  type Pos = FilePos;
  type E = io::Error;
  #[inline]
  fn copy_range(
    &self,
    start: &mut Self::Pos,
    n: usize,
    to: &mut File,
    mut pos: Option<&mut Self::Pos>,
  ) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    let &mut FilePos(mut in_off) = start;
    let in_off: *mut libc::off64_t = ptr::from_mut(&mut in_off);
    let out_off: *mut libc::off64_t = match pos.as_mut() {
      None => ptr::null_mut(),
      Some(off) => ptr::from_mut(off),
    };
    match unsafe { libc::copy_file_range(in_fd, in_off, out_fd, out_off, n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
  #[inline]
  fn copy_range_mut(
    &mut self,
    n: usize,
    to: &mut File,
    mut pos: Option<&mut Self::Pos>,
  ) -> (usize, Option<Self::E>) {
    let in_fd = self.as_raw_fd();
    let out_fd = to.as_raw_fd();
    let out_off: *mut libc::off64_t = match pos.as_mut() {
      None => ptr::null_mut(),
      Some(off) => ptr::from_mut(off),
    };
    match unsafe { libc::copy_file_range(in_fd, ptr::null_mut(), out_fd, out_off, n, 0) } {
      -1 => (0, Some(io::Error::last_os_error())),
      n => (n as usize, None),
    }
  }
}