Skip to content

Implement ReadAt and Size for Arc<RandomAccessFile> #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/raf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::{Seek, SeekFrom};
use std::os::unix::fs::FileExt;
#[cfg(windows)]
use std::os::windows::fs::FileExt;
use std::{fs::File, io, io::Write, path::Path};
use std::{fs::File, io, io::Write, path::Path, sync::Arc};

use super::{ReadAt, Size, WriteAt};

Expand Down Expand Up @@ -151,3 +151,17 @@ impl Size for RandomAccessFile {
self.file.size()
}
}

impl ReadAt for Arc<RandomAccessFile> {
#[inline]
fn read_at(&self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
(**self).read_at(pos, buf)
}
}

impl Size for Arc<RandomAccessFile> {
#[inline]
fn size(&self) -> io::Result<Option<u64>> {
(**self).size()
}
}
17 changes: 17 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
fs::File,
io::{Error, ErrorKind, Read, Result, Seek, SeekFrom},
str,
sync::Arc,
};

#[cfg(feature = "byteorder")]
Expand Down Expand Up @@ -118,6 +119,22 @@ fn test_size_cursor() {
assert!(s.contains("\n"));
}

#[test]
fn test_clone_size_cursor() {
let file = Arc::new(RandomAccessFile::open("tests/pi.txt").unwrap());
let mut curs = SizeCursor::new(file);
let mut curs_copy = curs.clone();

let mut buf = [0; 4];
assert_eq!(4, curs.read(&mut buf).unwrap());
let s = str::from_utf8(buf.as_ref()).unwrap();
assert_eq!(s, "3.14");

assert_eq!(4, curs_copy.read(&mut buf).unwrap());
let s = str::from_utf8(buf.as_ref()).unwrap();
assert_eq!(s, "3.14");
}

#[test]
#[cfg(feature = "byteorder")]
fn test_byteio() {
Expand Down