Skip to main content

Buffer

Struct Buffer 

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

The single owner of document text.

Invariants, upheld inside every mutation so no observer sees them disagree:

  1. text is LF-only (no \r). The trailing terminator, if any, is an empty final line in text, not a flag. Line boundaries are the rope’s own bookkeeping (LF-only by feature selection) — there is no second index to drift.
  2. revision bumps once per committed transaction (via Buffer::bump_revision, called by the transaction engine).

Implementations§

Source§

impl Buffer

Source

pub fn new(input: &str) -> Result<Self, LoadError>

Load input as a new document.

Refuses only input past the u32 offset space (see LoadError — no policy limit); normalizes \r\n | \r → \n, remembering the detected EolFlavor; mints a fresh DocId at revision 0.

Source

pub fn len(&self) -> u32

Total byte length of the document.

Source

pub fn is_empty(&self) -> bool

Whether the document is empty (a single empty line — never truly zero).

Source

pub fn text(&self) -> Cow<'_, str>

The whole document text, LF-only, as one slice.

Cold-path read: Borrowed only while the document is a single rope chunk; an O(len) materialization otherwise. For whole-document scans (find seeding, select-all-occurrences), serialization, and tests — per-frame and per-keystroke code reads via Buffer::slice / Buffer::line / Buffer::char_at / Buffer::char_before.

Source

pub fn slice(&self, range: Range<u32>) -> Cow<'_, str>

The text in range (byte offsets; must lie on char boundaries, like str slicing). Clamps to the document end. The workhorse ranged read — Borrowed while the range sits in one rope chunk, Owned across chunks.

Source

pub fn line(&self, row: u32) -> Cow<'_, str>

The text of one row, excluding its trailing \n. Out-of-range rows return "".

Source

pub fn char_at(&self, offset: u32) -> Option<char>

The char whose first byte is at offsetNone at/past the end. (offset must be a char boundary.) The forward one-char read every boundary scan uses instead of slicing the whole text. O(log chunks).

Source

pub fn char_before(&self, offset: u32) -> Option<char>

The char ending at offsetNone at 0. (offset must be a char boundary.) The backward twin of Buffer::char_at.

Source

pub fn line_count(&self) -> u32

Number of lines. Always ≥ 1 (an empty document is one empty line); a trailing \n yields a final empty line, so line_count("a\nb\n") == 3.

Source

pub fn line_len(&self, row: u32) -> u32

Byte length of one row’s text (excludes \n). Out-of-range → 0. Allocation-free (range arithmetic, no line materialization).

Source

pub fn revision(&self) -> Revision

The current revision.

Source

pub fn doc_id(&self) -> DocId

This document’s identity.

Source

pub fn eol_flavor(&self) -> EolFlavor

The EOL flavor detected at load.

Source

pub fn offset_to_point(&self, offset: u32) -> Point

Convert a byte offset to a Point. Offsets past the end clamp to the end of the document. O(log chunks).

Source

pub fn point_to_offset(&self, point: Point) -> u32

Convert a Point to a byte offset, clamping the row to the last line and the column to that line’s length. O(log chunks).

Source

pub fn clip_offset(&self, offset: u32, bias: Bias) -> u32

Clamp a byte offset into [0, len] and snap it to a char boundary (direction from bias). Idempotent.

Source

pub fn clip_point(&self, point: Point, bias: Bias) -> Point

Clip a Point to a valid position: clamp the row/col, then snap the resulting offset to a char boundary.

Source

pub fn snapshot(&self) -> Snapshot

An immutable copy for background consumers. O(1) — the rope clone shares structure; nothing is materialized until the consumer reads.

Source

pub fn serialize(&self, flavor: EolFlavor) -> String

Serialize for saving: verbatim EOL-expansion of the stored LF text into flavor. The empty final line reproduces a trailing terminator by construction; a no-trailing-newline file round-trips without one. Cold-path O(len), like any save.

Trait Implementations§

Source§

impl Clone for Buffer

Source§

fn clone(&self) -> Buffer

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

impl Debug for Buffer

Source§

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

Formats the value using the given formatter. Read more

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