[go: up one dir, main page]

Trait Metric

Source
pub trait Metric<N: NodeInfo> {
    // Required methods
    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;
}
Expand description

A trait for quickly processing attributes of a NodeInfo.

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

Required Methods§

Source

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.

Source

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
Source

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
Source

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

Source

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.

Source

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.

Source

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Metric<BreaksInfo> for BreaksBaseMetric

Source§

impl Metric<BreaksInfo> for BreaksMetric

Source§

impl Metric<RopeInfo> for BaseMetric

Source§

impl Metric<RopeInfo> for LinesMetric

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

Source§

impl Metric<RopeInfo> for Utf16CodeUnitsMetric