Skip to content

Commit 348c4c9

Browse files
committed
fix buf and file impl
1 parent aa011b4 commit 348c4c9

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

libsql-wal/src/io/file.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,17 @@ impl FileExt for File {
193193
let (buffer, ret) = tokio::task::spawn_blocking(move || {
194194
// let mut read = 0;
195195

196+
let len = buf.bytes_total();
197+
let init = buf.bytes_init();
196198
let chunk = unsafe {
197-
let len = buf.bytes_total();
198-
let ptr = buf.stable_mut_ptr();
199-
std::slice::from_raw_parts_mut(ptr, len)
199+
let ptr = buf.stable_mut_ptr().offset(init as _);
200+
std::slice::from_raw_parts_mut(ptr, len - init)
200201
};
201202

202203
let ret = file.read_exact_at(chunk, offset);
203204
if ret.is_ok() {
204205
unsafe {
205-
buf.set_init(buf.bytes_total());
206+
buf.set_init(init + chunk.len());
206207
}
207208
}
208209
(buf, ret)
@@ -222,16 +223,17 @@ impl FileExt for File {
222223
let (buffer, ret) = tokio::task::spawn_blocking(move || {
223224
// let mut read = 0;
224225

226+
let len = buf.bytes_total();
227+
let init = buf.bytes_init();
225228
let chunk = unsafe {
226-
let len = buf.bytes_total();
227-
let ptr = buf.stable_mut_ptr();
228-
std::slice::from_raw_parts_mut(ptr, len)
229+
let ptr = buf.stable_mut_ptr().offset(init as _);
230+
std::slice::from_raw_parts_mut(ptr, len - init)
229231
};
230232

231233
let ret = file.read_at(chunk, offset);
232234
if let Ok(n) = ret {
233235
unsafe {
234-
buf.set_init(n);
236+
buf.set_init(init + n);
235237
}
236238
}
237239
(buf, ret)
@@ -358,13 +360,13 @@ mod test {
358360
file.write_all(&[1; 12345]).unwrap();
359361
file.write_all(&[2; 50]).unwrap();
360362

361-
let buf = vec![0u8; 12345];
363+
let buf = Vec::with_capacity(12345);
362364
let (buf, ret) = file.read_exact_at_async(buf, 0).await;
363365
ret.unwrap();
364366
assert_eq!(buf.len(), 12345);
365367
assert!(buf.iter().all(|x| *x == 1));
366368

367-
let buf = vec![2u8; 50];
369+
let buf = Vec::with_capacity(50);
368370
let (buf, ret) = file.read_exact_at_async(buf, 12345).await;
369371
ret.unwrap();
370372
assert_eq!(buf.len(), 50);

0 commit comments

Comments
 (0)