[][src]Struct char_circle::CharCircle

pub struct CharCircle { /* fields omitted */ }

A circular buffer of characters.

The reallocation policy of std::collections::VecDeque makes it a poor choice as a circular buffer for modifying strings in place. It reallocates early (!) and when it does, it places its contents in such a way that requires shuffling to convert it back into a string in the common case. This circular buffer provides a better alternative.

This circular buffer will not allocate early and is optimized for the case when you read exactly the contents that were in the initial buffer. In that case, the read head is guaranteed to be at index 0 of the underlying buffer, making the conversion into a string trivial.

Additionally, this circular buffer provides a char oriented interface, but uses UTF-8 internally, allowing it to operate directly on Strings.

Methods

impl CharCircle[src]

Important traits for CharCircle
pub fn new(s: String) -> CharCircle[src]

Construct a new CharCircle using a string as the initial buffer.

Important traits for CharCircle
pub fn empty() -> CharCircle[src]

Construct a new, empty CharCircle.

pub fn len(&self) -> usize[src]

The number of UTF-8 bytes in the buffer.

pub fn n_chars(&self) -> usize[src]

The number of characters in the buffer.

pub fn capacity(&self) -> usize[src]

The number of bytes the buffer can hold before reallocating.

This refers to the length of the backing vector. That vector may have additional capacity allocated to it that is not reported by this method.

pub fn read_char(&self) -> Option<char>[src]

Read the next character in the buffer.

pub fn read(&self, buf: &mut [u8]) -> Result<usize>[src]

Read bytes from this circle into a buffer.

This method will only ever read complete UTF-8 characters. It returns the number of bytes read; it never returns an error.

This is the implementation of std::io::Read for CharCircle.

pub fn read_str<'a>(&self, buf: &'a mut [u8]) -> &'a str[src]

Read bytes from this circle into a buffer.

This method is equivalent to RawCircle::read except the return value is the buffer cast to a &str.

pub fn write_char(&self, ch: char)[src]

Write a character into the buffer.

pub fn write(&mut self, buf: &[u8]) -> Result<usize>[src]

Read bytes from a string into this buffer;

This method will only ever write complete UTF-8 characters. It returns the number of bytes written. This method returns an error if the input is not valid UTF-8.

This is the implementation of std::io::Write for CharCircle.

pub fn write_str(&self, buf: &str) -> usize[src]

Read bytes from a string into this buffer;

This method is equivalent to CharCircle::write except that it cannot return an error because the input is valid UTF-8.

pub fn into_vec(self) -> Vec<u8>[src]

Unpack this circular buffer into a byte vector.

pub fn into_string(self) -> String[src]

Unpack this circular buffer into a string.

Important traits for Chars<'a>
pub fn take_chars(&self, n: usize) -> Chars[src]

Read characters from the buffer with an iterator.

The returned iterator will read at most n characters and ensures that it has been exhausted upon drop.

Calling next on the iterator is equivalent to calling read_char on this buffer.

Important traits for Chars<'a>
pub fn take_current_chars(&self) -> Chars[src]

Read the current characters from the buffer with an iterator.

The returned iterator will read at most n characters, where n is the number of characters currently in the buffer, and ensures that it has been exhausted upon drop.

Calling next on the iterator is equivalent to calling read_char on this buffer.

This is equivalent to calling CharCircle::take_chars with the current number of characters in the buffer. In particular, interleaving calls to read_char and write_char on the buffer with calls to next on the iterator may cause the iterator to consume characters that were not in the buffer at the time it was created.

Trait Implementations

impl Clone for CharCircle[src]

default fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl Default for CharCircle[src]

impl From<String> for CharCircle[src]

impl From<CharCircle> for String[src]

impl From<CharCircle> for Vec<u8>[src]

impl Debug for CharCircle[src]

impl Read for CharCircle[src]

default fn read_vectored(
    &mut self,
    bufs: &mut [IoVecMut]
) -> Result<usize, Error>
[src]

🔬 This is a nightly-only experimental API. (iovec)

Like read, except that it reads into a slice of buffers. Read more

unsafe default fn initializer(&self) -> Initializer[src]

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader can work with buffers of uninitialized memory. Read more

default fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0
[src]

Read all bytes until EOF in this source, placing them into buf. Read more

default fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0
[src]

Read all bytes until EOF in this source, appending them to buf. Read more

default fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0
[src]

Read the exact number of bytes required to fill buf. Read more

default fn by_ref(&mut self) -> &mut Self
1.0.0
[src]

Creates a "by reference" adaptor for this instance of Read. Read more

default fn bytes(self) -> Bytes<Self>
1.0.0
[src]

Transforms this Read instance to an [Iterator] over its bytes. Read more

default fn chain<R>(self, next: R) -> Chain<Self, R> where
    R: Read
1.0.0
[src]

Creates an adaptor which will chain this stream with another. Read more

default fn take(self, limit: u64) -> Take<Self>
1.0.0
[src]

Creates an adaptor which will read at most limit bytes from it. Read more

impl Write for CharCircle[src]

default fn write_vectored(&mut self, bufs: &[IoVec]) -> Result<usize, Error>[src]

🔬 This is a nightly-only experimental API. (iovec)

Like write, except that it writes from a slice of buffers. Read more

default fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0
[src]

Attempts to write an entire buffer into this writer. Read more

default fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
[src]

Writes a formatted string into this writer, returning any error encountered. Read more

default fn by_ref(&mut self) -> &mut Self
1.0.0
[src]

Creates a "by reference" adaptor for this instance of Write. Read more

Auto Trait Implementations

impl Send for CharCircle

impl !Sync for CharCircle

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]