Skip to main content

RopeSlice

Struct RopeSlice 

pub struct RopeSlice<'a>(/* private fields */);
Expand description

An immutable view into part of a Rope.

RopeSlice is to Rope what &str is to String: RopeSlices only know about the text within their range, all indexing is relative to their range, all iterators are truncated their range, etc. Nothing should be too surprising here.

§A Warning About From<&str>

RopeSlice implements From<&str>, allowing you to create RopeSlices directly from string slices without an intermediate Rope. However, such RopeSlices are not normal RopeSlices. They are just a thin wrapper over the &str they were created from, and unlike normal RopeSlices have no underlying rope data structure to rely on.

The most important implication of this is that most operations that are documented as running in O(log N) actually run in O(N) for such slices. For short strings (this feature’s intended use case) this doesn’t matter. However, for long strings this can have significant negative performance impacts.

Implementations§

§

impl<'a> RopeSlice<'a>

pub fn slice<R>(&self, byte_range: R) -> RopeSlice<'a>
where R: RangeBounds<usize>,

Gets an immutable slice of the RopeSlice.

Uses range syntax, e.g. 2..7, 2.., etc.

Runs in O(log N) time.

§Panics
  • If the start of the range is greater than the end.
  • If the end of the range is out of bounds (i.e. end > len()).
  • If the range ends are not on char boundaries.

pub fn len(&self) -> usize

Length of the text in bytes.

Runs in O(1) time.

pub fn len_lines(&self, line_type: LineType) -> usize

Total number of lines in the text, according to the passed line type.

Runs in best case O(1) time, worst-case O(log N).

pub fn is_char_boundary(&self, byte_idx: usize) -> bool

Returns whether byte_idx is a char boundary.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn floor_char_boundary(&self, byte_idx: usize) -> usize

Returns the byte index of the closest char boundary less than or equal to byte_idx.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn ceil_char_boundary(&self, byte_idx: usize) -> usize

Returns the byte index of the closest char boundary greater than or equal to byte_idx.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn trailing_line_break_idx(&self, line_type: LineType) -> Option<usize>

If the text ends with a line break, returns its byte index. Otherwise returns None.

Note: a CRLF pair is always treated as a single unit, and thus this function will return the index of the CR in such cases, even with LineType::LF where CR is not on its own recognized as a line break.

Runs in O(log N) time.

pub fn byte(&self, byte_idx: usize) -> u8

Returns the byte at byte_idx.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx >= len()).

pub fn char(&self, byte_idx: usize) -> char

Returns the char at byte_idx.

Note that this takes a byte index, not a char index.

Runs in O(log N) time.

§Panics
  • If byte_idx is out of bounds (i.e. byte_idx >= len()).
  • If byte_idx is not a char boundary.

pub fn line(&self, line_idx: usize, line_type: LineType) -> RopeSlice<'a>

Returns the line at line_idx, according to the given line type.

Note: lines are zero-indexed.

Runs in O(log N) time.

§Panics

Panics if line_idx is out of bounds (i.e. line_idx >= len_lines()).

pub fn chunk(&self, byte_idx: usize) -> (&'a str, usize)

Returns the chunk containing the byte at byte_idx.

Also returns the byte index of the beginning of the chunk.

Note: for convenience, a one-past-the-end byte_idx returns the last chunk of the RopeSlice.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn byte_to_line_idx(&self, byte_idx: usize, line_type: LineType) -> usize

Returns the line index of the line that the given byte belongs to.

Notes:

  • Counts lines according to the passed line type.
  • Lines are zero-indexed. Therefore this is functionally equivalent to counting the line breaks before the specified byte.
  • byte_idx can be one-past-the-end, which will return the last line index.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn line_to_byte_idx(&self, line_idx: usize, line_type: LineType) -> usize

Returns the byte index of the start of the given line.

Notes:

  • Counts lines according to the passed line type.
  • Lines are zero-indexed.
  • line_idx can be one-past-the-end, which will return one-past-the-end byte index.

Runs in O(log N) time.

§Panics

Panics if line_idx is out of bounds (i.e. line_idx > len_lines()).

pub fn bytes(&self) -> Bytes<'a> ⓘ

Creates an iterator over the bytes of the Rope.

Runs in O(log N) time.

pub fn bytes_at(&self, byte_idx: usize) -> Bytes<'a> ⓘ

Creates an iterator over the bytes of the Rope, starting at byte byte_idx.

If byte_idx == len() then an iterator at the end of the Rope is created (i.e. next() will return None).

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn chars(&self) -> Chars<'a> ⓘ

Creates an iterator over the chars of the Rope.

Runs in O(log N) time.

pub fn chars_at(&self, byte_idx: usize) -> Chars<'a> ⓘ

Creates an iterator over the chars of the Rope, starting at byte_idx.

Note that this takes a byte index, not a char index.

If byte_idx == len() then an iterator at the end of the Rope is created (i.e. next() will return None).

Runs in O(log N) time.

§Panics
  • If byte_idx is out of bounds (i.e. byte_idx > len()).
  • If byte_idx is not a char boundary.

pub fn char_indices(&self) -> CharIndices<'a> ⓘ

Creates an iterator over the chars of the Rope and their byte positions.

On each call to next or prev this iterator returns the byte index where the returned character starts.

Runs in O(log N) time.

pub fn char_indices_at(&self, byte_idx: usize) -> CharIndices<'a> ⓘ

Creates an iterator over the chars of the Rope and their byte positions, starting at byte_idx.

Note that this takes a byte index, not a char index.

If byte_idx == len() then an iterator at the end of the Rope is created (i.e. next() will return None).

On each call to next or prev this iterator returns the byte index where the returned character starts.

Runs in O(log N) time.

§Panics
  • If byte_idx is out of bounds (i.e. byte_idx > len()).
  • If byte_idx is not a char boundary.

pub fn lines(&self, line_type: LineType) -> Lines<'a> ⓘ

Creates an iterator over the lines of the Rope.

Note: the iterator will iterate over lines according to the passed line type.

Runs in O(log N) time.

pub fn lines_at(&self, line_idx: usize, line_type: LineType) -> Lines<'a> ⓘ

Creates an iterator over the lines of the Rope, starting at line line_idx.

Notes:

  • The iterator will iterate over lines according to the passed line type.
  • If line_idx == len_lines() then an iterator at the end of the Rope is created (i.e. next() will return None).

Runs in O(log N) time.

§Panics

Panics if line_idx is out of bounds (i.e. line_idx > len_lines()).

pub fn chunks(&self) -> Chunks<'a> ⓘ

Creates an iterator over the chunks of the Rope.

Runs in O(log N) time.

pub fn chunks_at(&self, byte_idx: usize) -> (Chunks<'a>, usize)

Creates an iterator over the chunks of the Rope, with the iterator starting at the chunk containing byte_idx.

Also returns the byte index of the beginning of the chunk to be yielded by next().

If byte_idx == len() an iterator at the end of the Rope (yielding None on a call to next()) is created, and the returned byte index is the end of the text.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn chunk_cursor(&self) -> ChunkCursor<'a>

Creates a cursor for navigating the chunks of the text, starting on the first chunk.

Runs in O(log N) time.

pub fn chunk_cursor_at(&self, byte_idx: usize) -> ChunkCursor<'a>

Creates a cursor for navigating the chunks of the text, with the cursor starting at the chunk containing byte_idx.

For convenience, byte_idx == len() is accepted, and puts the cursor on the last chunk. Note that for non-zero-length texts this is redundant with byte_idx == len() - 1.

Runs in O(log N) time.

§Panics

Panics if byte_idx is out of bounds (i.e. byte_idx > len()).

pub fn as_str(&self) -> Option<&'a str>

Returns the text as a string slice if it’s contiguous in memory.

§

impl<'a> RopeSlice<'a>

Non-panicking versions of some of RopeSlice’s methods.

pub fn try_slice<R>(&self, byte_range: R) -> Result<RopeSlice<'a>, Error>
where R: RangeBounds<usize>,

Non-panicking version of slice().

On failure this returns the cause of the failure.

pub fn get_byte(&self, byte_idx: usize) -> Option<u8>

Non-panicking version of byte().

If byte_idx is out of bounds, returns None.

pub fn get_char(&self, byte_idx: usize) -> Result<char, Error>

Non-panicking version of char().

Will fail if byte_idx is either:

  • Not a char boundary.
  • Out of bounds.

On failure returns the cause of failure.

pub fn get_line( &self, line_idx: usize, line_type: LineType, ) -> Option<RopeSlice<'a>>

Non-panicking version of line().

If line_idx is out of bounds, returns None.

pub fn get_chunk(&self, byte_idx: usize) -> Option<(&'a str, usize)>

Non-panicking version of chunk().

If byte_idx is out of bounds, returns None.

Trait Implementations§

§

impl<'a> Clone for RopeSlice<'a>

§

fn clone(&self) -> RopeSlice<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<'a> Copy for RopeSlice<'a>

§

impl Debug for RopeSlice<'_>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Display for RopeSlice<'_>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl DisplayWidth for RopeSlice<'_>

§

fn display_width(&self) -> usize

§

impl Eq for RopeSlice<'_>

§

impl<'a> From<&'a Rope> for RopeSlice<'a>

§

fn from(r: &Rope) -> RopeSlice<'_>

Converts to this type from the input type.
§

impl<'a> From<&'a str> for RopeSlice<'a>

Creates a RopeSlice directly from a string slice.

Warning: RopeSlices created this way aren’t normal RopeSlices:

Runs in O(1) time.

§

fn from(text: &'a str) -> RopeSlice<'a>

Converts to this type from the input type.
§

impl From<RopeSlice<'_>> for Rope

§

fn from(rs: RopeSlice<'_>) -> Rope

Converts to this type from the input type.
Source§

impl<'a> From<RopeSlice<'a>> for RegexCursorInput<'a>

Source§

fn from(slice: RopeSlice<'a>) -> Self

Converts to this type from the input type.
§

impl Hash for RopeSlice<'_>

§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl Ord for RopeSlice<'_>

§

fn cmp(&self, other: &RopeSlice<'_>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
§

impl PartialEq<&str> for RopeSlice<'_>

§

fn eq(&self, other: &&str) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl PartialEq<Cow<'_, str>> for RopeSlice<'_>

§

fn eq(&self, other: &Cow<'_, str>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl PartialEq<Rope> for RopeSlice<'_>

§

fn eq(&self, other: &Rope) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl PartialEq<RopeSlice<'_>> for Rope

§

fn eq(&self, other: &RopeSlice<'_>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl PartialEq<RopeSlice<'_>> for RopeSlice<'_>

§

fn eq(&self, other: &RopeSlice<'_>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl PartialEq<String> for RopeSlice<'_>

§

fn eq(&self, other: &String) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl PartialEq<str> for RopeSlice<'_>

§

fn eq(&self, other: &str) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl PartialOrd<RopeSlice<'_>> for RopeSlice<'_>

§

fn partial_cmp(&self, other: &RopeSlice<'_>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl RopeExt for RopeSlice<'_>

Source§

fn as_slice(&self) -> RopeSlice<'_>

Source§

fn len_lines_unix(&self) -> usize

Source§

fn find_next_byte<R>(&self, byte_range: R, needles: &[u8]) -> Option<usize>
where R: RangeBounds<usize> + Clone,

Source§

fn find_prev_byte<R>(&self, byte_range: R, needles: &[u8]) -> Option<usize>
where R: RangeBounds<usize> + Clone,

Source§

fn get_grapheme(&self, byte_index: usize) -> Option<RopeSlice<'_>>

Source§

fn grapheme(&self, byte_index: usize) -> RopeSlice<'_>

Source§

fn graphemes(&self) -> Graphemes<'_> ⓘ

Source§

fn is_grapheme_boundary(&self, byte_offset: usize) -> bool

Source§

fn is_grapheme_start(&self, byte_index: usize) -> bool

Source§

fn snap_to_grapheme_start(&self, byte_index: usize) -> Option<usize>

Source§

fn last_grapheme_start(&self) -> Option<usize>

Source§

fn prev_grapheme_boundary(&self, byte_offset: usize) -> Option<usize>

Source§

fn next_grapheme_boundary(&self, byte_offset: usize) -> Option<usize>

Source§

fn floor_grapheme_boundary(&self, byte_offset: usize) -> usize

Source§

fn ceil_grapheme_boundary(&self, byte_offset: usize) -> usize

Source§

fn display_column(&self, byte_index: usize) -> usize

Auto Trait Implementations§

§

impl<'a> Freeze for RopeSlice<'a>

§

impl<'a> RefUnwindSafe for RopeSlice<'a>

§

impl<'a> Send for RopeSlice<'a>

§

impl<'a> Sync for RopeSlice<'a>

§

impl<'a> Unpin for RopeSlice<'a>

§

impl<'a> UnsafeUnpin for RopeSlice<'a>

§

impl<'a> UnwindSafe for RopeSlice<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<I> FromItem<I> for I

Source§

fn from_item(item: I) -> I

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more