Generic string buffer read/write traits


#1

Has anyone come up with generic interfaces for non-fallible sequential reading and writing of buffered UTF-8 data, similar to what bytes::Buf and bytes::BufMut do for various byte buffers? On the writing side the most obvious recipient would be String, but it should be possible to substitute a more efficient rope-like container for large texts, or a fixed-length string slice.

Basic requirements:

Reading

With the reading trait, it should be possible to:

  • Borrow the next contiguous str slice from the container.
  • Advance the position to a valid index in the remaining UTF-8 sequence.

Writing

With the writing trait, it should be possible to:

  • Append an str slice after the current writing position.
  • Unsafely borrow the next available buffer, preferably with capacity enough to hold any UTF-8 sequence, as a mutable byte slice.
  • Unsafely commit a specified number of bytes in the buffer as the written UTF-8 sequence without checking for its validity.

The methods would not return Result, but panic on any parameter errors like string indexing does. The writing container does not have to be expandable and the implementation may panic when it’s full.

Please let me know if there is a crate or a work in progress implementing something like this.


#2

This old thread might be of interest.


#3

FWIW, I still am looking for something akin to CharSequence for Rust :frowning:

The relevant issue is https://github.com/rust-lang/regex/issues/386