irox_bits/
stdimpls.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2025 IROX Contributors
3//
4
5use crate::mutbits::MutBits;
6use crate::{Bits, BitsWrapper, Error, Seek, SeekFrom, SeekRead, SeekWrite};
7use std::io::{Read, Write};
8
9impl Bits for std::fs::File {
10    fn next_u8(&mut self) -> Result<Option<u8>, Error> {
11        use std::io::Read;
12        let mut buf: [u8; 1] = [0];
13        let read = self.read(&mut buf)?;
14        if read == 1 {
15            return Ok(Some(buf[0]));
16        }
17        Ok(None)
18    }
19}
20
21impl MutBits for std::fs::File {
22    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
23        Ok(self.write_all(&[val])?)
24    }
25
26    fn flush(&mut self) -> Result<(), Error> {
27        Write::flush(self)?;
28        Ok(())
29    }
30}
31impl Seek for std::fs::File {
32    fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error> {
33        std::io::Seek::seek(self, pos.into())?;
34        Ok(std::io::Seek::stream_position(self)?)
35    }
36}
37impl<T: Read> Bits for std::io::BufReader<T> {
38    fn next_u8(&mut self) -> Result<Option<u8>, Error> {
39        BitsWrapper::Borrowed(self).next_u8()
40    }
41}
42impl<T: Write> MutBits for std::io::BufWriter<T> {
43    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
44        BitsWrapper::Borrowed(self).write_u8(val)
45    }
46    fn flush(&mut self) -> Result<(), Error> {
47        Write::flush(self)?;
48        Ok(())
49    }
50}
51
52impl Bits for std::net::TcpStream {
53    fn next_u8(&mut self) -> Result<Option<u8>, Error> {
54        BitsWrapper::Borrowed(self).next_u8()
55    }
56}
57impl Bits for &mut std::net::TcpStream {
58    fn next_u8(&mut self) -> Result<Option<u8>, Error> {
59        BitsWrapper::Borrowed(self).next_u8()
60    }
61}
62impl MutBits for std::net::TcpStream {
63    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
64        BitsWrapper::Borrowed(self).write_u8(val)
65    }
66    fn flush(&mut self) -> Result<(), Error> {
67        std::io::Write::flush(self)?;
68        Ok(())
69    }
70}
71impl MutBits for &mut std::net::TcpStream {
72    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
73        BitsWrapper::Borrowed(self).write_u8(val)
74    }
75    fn flush(&mut self) -> Result<(), Error> {
76        std::io::Write::flush(self)?;
77        Ok(())
78    }
79}
80
81#[cfg(windows)]
82impl SeekRead for std::fs::File {
83    fn seek_read(&mut self, out: &mut [u8], offset: u64) -> Result<usize, Error> {
84        Ok(std::os::windows::fs::FileExt::seek_read(self, out, offset)?)
85    }
86}
87#[cfg(windows)]
88impl SeekWrite for std::fs::File {
89    fn seek_write(&mut self, input: &[u8], offset: u64) -> Result<usize, Error> {
90        Ok(std::os::windows::fs::FileExt::seek_write(
91            self, input, offset,
92        )?)
93    }
94}
95
96#[cfg(unix)]
97impl SeekRead for std::fs::File {
98    fn seek_read(&mut self, out: &mut [u8], offset: u64) -> Result<usize, Error> {
99        Ok(std::os::unix::fs::FileExt::read_at(self, out, offset)?)
100    }
101}
102#[cfg(unix)]
103impl SeekWrite for std::fs::File {
104    fn seek_write(&mut self, input: &[u8], offset: u64) -> Result<usize, Error> {
105        Ok(std::os::unix::fs::FileExt::write_at(self, input, offset)?)
106    }
107}