A better Rust ATProto crate
1use crate::io::{BufRead, Read, Seek, SeekFrom};
2
3/// Adapter to `std::io` traits.
4#[derive(Clone)]
5pub struct ToStd<T: ?Sized> {
6 inner: T,
7}
8
9impl<T> ToStd<T> {
10 /// Create a new adapter.
11 pub fn new(inner: T) -> Self {
12 Self { inner }
13 }
14
15 /// Consume the adapter, returning the inner object.
16 pub fn into_inner(self) -> T {
17 self.inner
18 }
19}
20
21impl<T: ?Sized> ToStd<T> {
22 /// Borrow the inner object.
23 pub fn inner(&self) -> &T {
24 &self.inner
25 }
26
27 /// Mutably borrow the inner object.
28 pub fn inner_mut(&mut self) -> &mut T {
29 &mut self.inner
30 }
31}
32
33impl<T: Read + ?Sized> std::io::Read for ToStd<T> {
34 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
35 Read::read(self.inner_mut(), buf).map_err(std::io::Error::from)
36 }
37
38 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std::io::Result<usize> {
39 Read::read_to_end(self.inner_mut(), buf).map_err(std::io::Error::from)
40 }
41
42 fn read_to_string(&mut self, buf: &mut String) -> std::io::Result<usize> {
43 Read::read_to_string(self.inner_mut(), buf).map_err(std::io::Error::from)
44 }
45
46 fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
47 Read::read_exact(self.inner_mut(), buf).map_err(std::io::Error::from)
48 }
49}
50
51impl From<SeekFrom> for std::io::SeekFrom {
52 fn from(from: SeekFrom) -> Self {
53 match from {
54 SeekFrom::Start(offset) => std::io::SeekFrom::Start(offset),
55 SeekFrom::Current(offset) => std::io::SeekFrom::Current(offset),
56 SeekFrom::End(offset) => std::io::SeekFrom::End(offset),
57 }
58 }
59}
60
61impl From<std::io::SeekFrom> for SeekFrom {
62 fn from(from: std::io::SeekFrom) -> Self {
63 match from {
64 std::io::SeekFrom::Start(offset) => SeekFrom::Start(offset),
65 std::io::SeekFrom::Current(offset) => SeekFrom::Current(offset),
66 std::io::SeekFrom::End(offset) => SeekFrom::End(offset),
67 }
68 }
69}
70
71impl<T: Seek + ?Sized> std::io::Seek for ToStd<T> {
72 fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
73 Seek::seek(self.inner_mut(), pos.into()).map_err(std::io::Error::from)
74 }
75
76 fn rewind(&mut self) -> std::io::Result<()> {
77 self.seek(std::io::SeekFrom::Start(0))?;
78 Ok(())
79 }
80
81 fn stream_position(&mut self) -> std::io::Result<u64> {
82 Seek::stream_position(self.inner_mut()).map_err(std::io::Error::from)
83 }
84
85 fn seek_relative(&mut self, offset: i64) -> std::io::Result<()> {
86 self.seek(std::io::SeekFrom::Current(offset))?;
87 Ok(())
88 }
89}
90
91impl<T: BufRead + ?Sized> std::io::BufRead for ToStd<T> {
92 fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
93 BufRead::fill_buf(self.inner_mut()).map_err(std::io::Error::from)
94 }
95
96 fn consume(&mut self, amount: usize) {
97 BufRead::consume(self.inner_mut(), amount);
98 }
99
100 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> std::io::Result<usize> {
101 BufRead::read_until(self.inner_mut(), byte, buf).map_err(std::io::Error::from)
102 }
103
104 fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
105 BufRead::read_line(self.inner_mut(), buf).map_err(std::io::Error::from)
106 }
107}