Skip to main content

GapBuffer

Struct GapBuffer 

Source
pub struct GapBuffer { /* private fields */ }
Expand description

A gap buffer structure.

Useful for small and frequent insertions and deletions. O(1) insertions and deletions on average. O(n) gap repositioning on edit location change.

Example use case: Shell

Trait Implementations§

Source§

impl TextBuffer for GapBuffer

Source§

fn create_buffer(init_size: usize) -> Self
where Self: Sized,

Creates a gap buffer according to gap size.

Start is at first insertable location. End is at first character after last insertable location.

Buffer is initialized to designated gap size.

Source§

fn insert(&mut self, pos: usize, char: char)

Inserts into a gap buffer.

  1. Moves gap
  2. Checks if gap is empty (if so, resize by doubling existing buffer length)
  3. Insert at gap_start and increment gap_start by 1 (slot filled)
§Panics

Panics if pos is out of bounds.

Source§

fn delete(&mut self, start: usize, len: usize)

Deletes from a gap buffer.

  1. Moves gap
  2. Extends gap by len by adding to gap_end
§Panics

Panics if gap_end + len is out of bounds.

Source§

fn read( &self, start: usize, end: usize, ) -> impl DoubleEndedIterator<Item = &char>

Reads from a gap buffer.

Reads from the left and right of the buffer, skipping over the gap.

§Panics

Panics if start is out of bounds. Panics if end is out of bounds. Panics if start is greater than end.

Source§

fn len(&self) -> usize

Calculates the length of a gap buffer.

Length = Length of buffer - size of gap

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.