Skip to main content

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};
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
57    fn read_some_into<T: MutBits>(&mut self, buf: &mut T) -> Result<usize, Error> {
58        let mut b = [0u8; 4096];
59        let read_stream = self.peek(&mut b)?;
60        let (stream, _) = b.split_at_mut(read_stream);
61        let consumed = buf.write_some_bytes(stream);
62        let (stream, _) = stream.split_at_mut(consumed);
63        std::io::Read::read_exact(self, stream)?;
64        Ok(consumed)
65    }
66}
67impl Bits for &mut std::net::TcpStream {
68    fn next_u8(&mut self) -> Result<Option<u8>, Error> {
69        BitsWrapper::Borrowed(self).next_u8()
70    }
71
72    fn read_some_into<T: MutBits>(&mut self, buf: &mut T) -> Result<usize, Error> {
73        let mut b = [0u8; 4096];
74        let read_stream = self.peek(&mut b)?;
75        let (stream, _) = b.split_at_mut(read_stream);
76        let consumed = buf.write_some_bytes(stream);
77        let (stream, _) = stream.split_at_mut(consumed);
78        std::io::Read::read_exact(self, stream)?;
79        Ok(consumed)
80    }
81}
82impl MutBits for std::net::TcpStream {
83    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
84        BitsWrapper::Borrowed(self).write_u8(val)
85    }
86    fn flush(&mut self) -> Result<(), Error> {
87        std::io::Write::flush(self)?;
88        Ok(())
89    }
90}
91impl MutBits for &mut std::net::TcpStream {
92    fn write_u8(&mut self, val: u8) -> Result<(), Error> {
93        BitsWrapper::Borrowed(self).write_u8(val)
94    }
95    fn flush(&mut self) -> Result<(), Error> {
96        std::io::Write::flush(self)?;
97        Ok(())
98    }
99}
100
101#[cfg(windows)]
102impl crate::SeekRead for std::fs::File {
103    fn seek_read(&mut self, out: &mut [u8], offset: u64) -> Result<usize, Error> {
104        Ok(std::os::windows::fs::FileExt::seek_read(self, out, offset)?)
105    }
106}
107#[cfg(windows)]
108impl crate::SeekWrite for std::fs::File {
109    fn seek_write(&mut self, input: &[u8], offset: u64) -> Result<usize, Error> {
110        Ok(std::os::windows::fs::FileExt::seek_write(
111            self, input, offset,
112        )?)
113    }
114}
115
116#[cfg(unix)]
117impl crate::SeekRead for std::fs::File {
118    fn seek_read(&mut self, out: &mut [u8], offset: u64) -> Result<usize, Error> {
119        Ok(std::os::unix::fs::FileExt::read_at(self, out, offset)?)
120    }
121}
122#[cfg(unix)]
123impl crate::SeekWrite for std::fs::File {
124    fn seek_write(&mut self, input: &[u8], offset: u64) -> Result<usize, Error> {
125        Ok(std::os::unix::fs::FileExt::write_at(self, input, offset)?)
126    }
127}