cessen / ropey

A utf8 text rope for manipulating and editing large texts.
MIT License
1.04k stars 46 forks source link

Async IO support for `Rope`? #62

Closed Nughm3 closed 2 years ago

Nughm3 commented 2 years ago

I've been trying to read from/write to files from a Rope. The documentation suggests this:

let mut text = Rope::from_reader(
    BufReader::new(File::open("my_great_book.txt")?)
)?;

This works well for synchronous programs, but I was unable to find any asynchronous counterpart to Rope::from_reader. I tried to use tokio::io::BufReader, but since that doesn't implement std::io::Read (instead tokio::io::AsyncRead) it isn't compatible with from_reader.

I'm wondering if async support is a possibility, for example something like Rope::from_reader_async behind a feature flag.

Thanks!

cessen commented 2 years ago

As mentioned in the documentation, Rope::from_reader() and Rope::write_to() are just convenience functions for basic/toy use cases, not the main way to do input/output when you need specific IO behavior. In general, IO is orthogonal to Ropey, and you're expected to bridge Ropey with whatever IO you need to do.

To that end, the more serious/general counterparts to those convenience functions are RopeBuilder (for reading into a Rope) and the Chunks iterator (for writing out from a Rope). They don't do or manage IO themselves, but that's the point: they can work with any IO system.

That should allow you load and save Ropes in an async context. You just need to handle the actual IO part yourself.

Hope that helps!