pub struct Rank(/* private fields */);Expand description
Comparable ranks ordered lexicographically.
Generated ranks are always in normal form: a non-empty alphanumeric string with no trailing
0. In that form, lexicographic comparison matches the intended rank order; see
Rank::parse for the validation rules.
Implementations§
Source§impl Rank
impl Rank
Sourcepub fn parse(s: &str) -> Result<Rank>
pub fn parse(s: &str) -> Result<Rank>
Parse a rank from its string representation.
Conditions for a valid rank (normal form):
- Non-empty and all base-36 alphanumeric characters (
0-9a-z). - Does not end with
0. A trailing zero does not change a base-36 fraction ("1"and"10"both represent 1/36), which would permit multiple strings for the same rank. Disallowing it makes the mapping between rank strings and values one-to-one, so lexicographic comparison always matches numeric order. Leading and middle zeroes remain valid: they contribute to the value ("01"≠"1"). - This rule also excludes zero (all digits
0), which is reserved as the lower endpoint of the open rank interval.
If the condition is not met, Error::InvalidRank.
§Examples
use pinto::rank::Rank;
let lower = Rank::parse("i").expect("canonical rank");
let upper = Rank::after(Some(&lower));
assert!(lower < upper);Sourcepub fn between(lo: Option<&Rank>, hi: Option<&Rank>) -> Result<Rank>
pub fn between(lo: Option<&Rank>, hi: Option<&Rank>) -> Result<Rank>
Return a rank between lo and hi. None represents the open lower or upper endpoint.
Precondition: lo < hi (when both are Some). The return value mid satisfies lo < mid < hi.
Returns Error::InvalidRank when the bounds are not in ascending order.
Sourcepub fn after(prev: Option<&Rank>) -> Rank
pub fn after(prev: Option<&Rank>) -> Rank
Return the rank immediately following (greater than) prev. If prev is None, return
the first rank.
Uses tail-add-only logic and does not go through between(prev, None) (midpoint to 1.0).
The midpoint method halves the difference from 1.0 on each append, which makes repeated
appends produce increasingly long strings. Instead, compute the shortest rank greater than
prev directly:
- Increase the digits less than
z(BASE-1) by one when looking from the right edge, and truncate the rest. It lexicographically exceedsprevby that digit, and the number of digits does not increase (often decreases). - Only when all digits are
z(prev ≒ 1.0), add an intermediate digit (BASE/2) to the end to extend it.
The incremented digits and added intermediate digits are always non-zero, so the result is always in normal form (no trailing zeros).
Sourcepub fn before(next: Option<&Rank>) -> Rank
pub fn before(next: Option<&Rank>) -> Rank
Return the rank immediately preceding (less than) next.
This is a convenience wrapper around between(None, Some(next)), matching Rank::after
so callers do not need to spell out the open lower endpoint.
before(Some(n))always returns a rank less thann.before(None)returns the median rank (base-36"i"= 0.5), matchingRank::afterwithNone.
Sourcepub fn rebalance(count: usize) -> Vec<Rank>
pub fn rebalance(count: usize) -> Vec<Rank>
Generate a short, evenly spaced rank sequence while maintaining the order.
The smallest fixed width that can hold count canonical ranks is chosen. Every rank has that width and a non-zero last digit, so it remains in normal form. The returned sequence is strictly monotonically increasing and leaves roughly equal gaps between adjacent values.
If count is zero, the returned vector is empty.
Trait Implementations§
impl Eq for Rank
Source§impl Ord for Rank
impl Ord for Rank
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Rank
impl PartialOrd for Rank
impl StructuralPartialEq for Rank
Auto Trait Implementations§
impl Freeze for Rank
impl RefUnwindSafe for Rank
impl Send for Rank
impl Sync for Rank
impl Unpin for Rank
impl UnsafeUnpin for Rank
impl UnwindSafe for Rank
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more