[][src]Trait xi_rope::tree::Metric

pub trait Metric<N: NodeInfo> {
    fn measure(info: &N, len: usize) -> usize;
fn to_base_units(l: &N::L, in_measured_units: usize) -> usize;
fn from_base_units(l: &N::L, in_base_units: usize) -> usize;
fn is_boundary(l: &N::L, offset: usize) -> bool;
fn prev(l: &N::L, offset: usize) -> Option<usize>;
fn next(l: &N::L, offset: usize) -> Option<usize>;
fn can_fragment() -> bool; }

A trait for quickly processing attributes of a NodeInfo.

For the conceptual background see the blog post, Rope science, part 2: metrics.

Required methods

fn measure(info: &N, len: usize) -> usize

Return the size of the NodeInfo::L, as measured by this metric.

The usize argument is the total size/length of the node, in base units.

Examples

For the LinesMetric, this gives the number of lines in string contained in the leaf. For the BaseMetric, this gives the size of the string in uft8 code units, that is, bytes.

fn to_base_units(l: &N::L, in_measured_units: usize) -> usize

Returns the smallest offset, in base units, for an offset in measured units.

Invariants:

  • from_base_units(to_base_units(x)) == x is True for valid x

fn from_base_units(l: &N::L, in_base_units: usize) -> usize

Returns the smallest offset in measured units corresponding to an offset in base units.

Invariants:

  • from_base_units(to_base_units(x)) == x is True for valid x

fn is_boundary(l: &N::L, offset: usize) -> bool

Return whether the offset in base units is a boundary of this metric. If a boundary is at end of a leaf then this method must return true. However, a boundary at the beginning of a leaf is optional (the previous leaf will be queried).

fn prev(l: &N::L, offset: usize) -> Option<usize>

Returns the index of the boundary directly preceding offset, or None if no such boundary exists. Input and result are in base units.

fn next(l: &N::L, offset: usize) -> Option<usize>

Returns the index of the first boundary for which index > offset, or None if no such boundary exists. Input and result are in base units.

fn can_fragment() -> bool

Returns true if the measured units in this metric can span multiple leaves. As an example, in a metric that measures lines in a rope, a line may start in one leaf and end in another; however in a metric measuring bytes, storage of a single byte cannot extend across leaves.

Loading content...

Implementors

impl Metric<BreaksInfo> for BreaksBaseMetric[src]

impl Metric<BreaksInfo> for BreaksMetric[src]

impl Metric<RopeInfo> for BaseMetric[src]

impl Metric<RopeInfo> for LinesMetric[src]

Measured unit is newline amount. Base unit is utf8 code unit. Boundary is trailing and determined by a newline char.

impl Metric<RopeInfo> for Utf16CodeUnitsMetric[src]

Loading content...