Skip to content

Commit 912aaf3

Browse files
committed
make MapSlice more general
1 parent 726c40c commit 912aaf3

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

libsql-wal/src/io/buf.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// from tokio uring
22

3-
use std::mem::{size_of, MaybeUninit};
3+
use std::{
4+
borrow::Borrow,
5+
marker::PhantomData,
6+
mem::{size_of, MaybeUninit},
7+
};
48

5-
use bytes::BytesMut;
9+
use bytes::{Bytes, BytesMut};
610
use zerocopy::{AsBytes, FromBytes};
711

812
pub unsafe trait IoBuf: Unpin + 'static {
@@ -90,6 +94,20 @@ unsafe impl IoBuf for BytesMut {
9094
}
9195
}
9296

97+
unsafe impl IoBuf for Bytes {
98+
fn stable_ptr(&self) -> *const u8 {
99+
self.as_ptr()
100+
}
101+
102+
fn bytes_init(&self) -> usize {
103+
self.len()
104+
}
105+
106+
fn bytes_total(&self) -> usize {
107+
self.len()
108+
}
109+
}
110+
93111
unsafe impl IoBufMut for BytesMut {
94112
fn stable_mut_ptr(&mut self) -> *mut u8 {
95113
self.as_mut_ptr()
@@ -185,11 +203,11 @@ impl<T> ZeroCopyBuf<T> {
185203
}
186204
}
187205

188-
pub fn map_slice<F>(self, f: F) -> MapSlice<T, F>
206+
pub fn map_slice<F>(self, f: F) -> MapSlice<Self, F, T>
189207
where
190208
for<'a> F: Fn(&'a Self) -> &'a [u8] + Unpin + 'static,
191209
{
192-
MapSlice { inner: self, f }
210+
MapSlice::new(self, f)
193211
}
194212

195213
#[inline]
@@ -220,32 +238,42 @@ impl<T> ZeroCopyBuf<T> {
220238
}
221239
}
222240

223-
pub struct MapSlice<T, F> {
224-
inner: ZeroCopyBuf<T>,
241+
pub struct MapSlice<T, F, U> {
242+
inner: T,
225243
f: F,
244+
_p: PhantomData<U>,
226245
}
227246

228-
impl<T, F> MapSlice<T, F> {
229-
pub(crate) fn into_inner(self) -> ZeroCopyBuf<T> {
247+
impl<T, F, U> MapSlice<T, F, U> {
248+
pub(crate) fn into_inner(self) -> T {
230249
self.inner
231250
}
251+
252+
pub(crate) fn new(inner: T, f: F) -> Self {
253+
Self {
254+
inner,
255+
f,
256+
_p: PhantomData,
257+
}
258+
}
232259
}
233260

234-
unsafe impl<T, F> IoBuf for MapSlice<T, F>
261+
unsafe impl<T, F, U> IoBuf for MapSlice<T, F, U>
235262
where
236-
for<'a> F: Fn(&'a ZeroCopyBuf<T>) -> &'a [u8] + Unpin + 'static,
237-
T: Unpin + 'static + AsBytes,
263+
for<'a> F: Fn(&'a ZeroCopyBuf<U>) -> &'a [u8] + Unpin + 'static,
264+
T: Borrow<ZeroCopyBuf<U>> + Unpin + 'static,
265+
U: AsBytes + Unpin + 'static,
238266
{
239267
fn stable_ptr(&self) -> *const u8 {
240-
(self.f)(&self.inner).as_ptr()
268+
(self.f)(&self.inner.borrow()).as_ptr()
241269
}
242270

243271
fn bytes_init(&self) -> usize {
244-
(self.f)(&self.inner).len()
272+
(self.f)(&self.inner.borrow()).len()
245273
}
246274

247275
fn bytes_total(&self) -> usize {
248-
(self.f)(&self.inner).len()
276+
(self.f)(&self.inner.borrow()).len()
249277
}
250278
}
251279

0 commit comments

Comments
 (0)