Skip to content

Commit 0292e1e

Browse files
committed
implement FileExt for Either
1 parent b922257 commit 0292e1e

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

libsql-wal/src/io/file.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::fs::File;
22
use std::future::Future;
33
use std::io::{self, ErrorKind, IoSlice, Result, Write};
44

5+
use libsql_sys::wal::either::Either;
6+
57
use super::buf::{IoBuf, IoBufMut};
68

79
pub trait FileExt: Send + Sync + 'static {
@@ -73,6 +75,93 @@ pub trait FileExt: Send + Sync + 'static {
7375
) -> impl Future<Output = (B, Result<()>)> + Send;
7476
}
7577

78+
impl<U, V> FileExt for Either<U, V>
79+
where
80+
V: FileExt,
81+
U: FileExt,
82+
{
83+
fn len(&self) -> io::Result<u64> {
84+
match self {
85+
Either::A(x) => x.len(),
86+
Either::B(x) => x.len(),
87+
}
88+
}
89+
90+
fn write_at_vectored(&self, bufs: &[IoSlice], offset: u64) -> Result<usize> {
91+
match self {
92+
Either::A(x) => x.write_at_vectored(bufs, offset),
93+
Either::B(x) => x.write_at_vectored(bufs, offset),
94+
}
95+
}
96+
97+
fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize> {
98+
match self {
99+
Either::A(x) => x.write_at(buf, offset),
100+
Either::B(x) => x.write_at(buf, offset),
101+
}
102+
}
103+
104+
fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize> {
105+
match self {
106+
Either::A(x) => x.read_at(buf, offset),
107+
Either::B(x) => x.read_at(buf, offset),
108+
}
109+
}
110+
111+
fn sync_all(&self) -> Result<()> {
112+
match self {
113+
Either::A(x) => x.sync_all(),
114+
Either::B(x) => x.sync_all(),
115+
}
116+
}
117+
118+
fn set_len(&self, len: u64) -> Result<()> {
119+
match self {
120+
Either::A(x) => x.set_len(len),
121+
Either::B(x) => x.set_len(len),
122+
}
123+
}
124+
125+
fn read_exact_at_async<B: IoBufMut + Send + 'static>(
126+
&self,
127+
buf: B,
128+
offset: u64,
129+
) -> impl Future<Output = (B, Result<()>)> + Send {
130+
async move {
131+
match self {
132+
Either::A(x) => x.read_exact_at_async(buf, offset).await,
133+
Either::B(x) => x.read_exact_at_async(buf, offset).await,
134+
}
135+
}
136+
}
137+
138+
fn read_at_async<B: IoBufMut + Send + 'static>(
139+
&self,
140+
buf: B,
141+
offset: u64,
142+
) -> impl Future<Output = (B, Result<usize>)> + Send {
143+
async move {
144+
match self {
145+
Either::A(x) => x.read_at_async(buf, offset).await,
146+
Either::B(x) => x.read_at_async(buf, offset).await,
147+
}
148+
}
149+
}
150+
151+
fn write_all_at_async<B: IoBuf + Send + 'static>(
152+
&self,
153+
buf: B,
154+
offset: u64,
155+
) -> impl Future<Output = (B, Result<()>)> + Send {
156+
async move {
157+
match self {
158+
Either::A(x) => x.write_all_at_async(buf, offset).await,
159+
Either::B(x) => x.write_all_at_async(buf, offset).await,
160+
}
161+
}
162+
}
163+
}
164+
76165
impl FileExt for File {
77166
fn write_at_vectored(&self, bufs: &[IoSlice], offset: u64) -> Result<usize> {
78167
Ok(nix::sys::uio::pwritev(self, bufs, offset as _)?)

0 commit comments

Comments
 (0)