Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to acquire BufReader #50

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

DrSensor
Copy link
Contributor

@DrSensor DrSensor commented Jan 10, 2024

Choose one: is this a 馃悰 bug fix, a 馃檵 feature, or a 馃敠 documentation change?
馃檵 feature

Checklist

  • tests pass

Context

I have a case where I store RandomAccessDisk in struct like this

struct LSPBackend {
  files: HashMap<Url, RandomAccessDisk>, // I store the file instead of path to make it efficient
  // ...other stuff
}

It's quite common to do buffered read so I expose reader() method.
Accessing the underlying fs::File is possible by calling reader().get_ref() or reader().get_mut().

The reader() method open up the possibility for this pattern

let line = 10;
let column = 10;
let length = 5;

let mut file = RandomAccessDisk::open("text.db").await.unwrap();

let offset = column + (
    file.reader()
        .lines()
        .take(line - 1)
        .fold(column, |acc, line| acc + line.unwrap().len() as u64)
        .await
);

let _ = file.del(offset, length).await;

Semver Changes

3.1.0

@DrSensor DrSensor changed the title Feature/buffered reader Method to acquire BufReader Jan 10, 2024
@DrSensor DrSensor changed the title Method to acquire BufReader Add method to acquire BufReader Jan 10, 2024
@DrSensor DrSensor marked this pull request as draft January 10, 2024 04:36
@DrSensor DrSensor marked this pull request as ready for review January 10, 2024 06:37
@ttiurani
Copy link
Member

ttiurani commented Jan 10, 2024

Thanks for the PR! This is possible, if not a bit unconventional, because you can of course already read with read() but still ok.

However, I think this needs tests. Especially this should have a test where you first create a sparse file (with a large enough deleted section that a hole is actually created) and then take out this BufReader and read over the hole. It most likely does work and gives zeroes, but should IMO still be verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants