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§
Sourcefn measure(info: &N, len: usize) -> usize
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.
Sourcefn to_base_units(l: &N::L, in_measured_units: usize) -> usize
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 validx
Sourcefn from_base_units(l: &N::L, in_base_units: usize) -> usize
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 validx
Sourcefn is_boundary(l: &N::L, offset: usize) -> bool
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).
Sourcefn prev(l: &N::L, offset: usize) -> Option<usize>
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.
Sourcefn next(l: &N::L, offset: usize) -> Option<usize>
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.
Sourcefn can_fragment() -> bool
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§
impl Metric<BreaksInfo> for BreaksBaseMetric
impl Metric<BreaksInfo> for BreaksMetric
impl Metric<RopeInfo> for BaseMetric
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.