pub struct OneIndexed(/* private fields */);Expand description
Type-safe wrapper for a value whose logical range starts at 1, for
instance the line or column numbers in a file
Internally this is represented as a NonZeroUsize, this enables some
memory optimizations
Implementations§
Source§impl OneIndexed
impl OneIndexed
pub const ONE: NonZeroUsize
Sourcepub const fn new(value: usize) -> Option<Self>
pub const fn new(value: usize) -> Option<Self>
Creates a non-zero if the given value is not zero.
Sourcepub const fn from_zero_indexed(value: usize) -> Self
pub const fn from_zero_indexed(value: usize) -> Self
Construct a new OneIndexed from a zero-indexed value
Sourcepub const fn to_zero_indexed(self) -> usize
pub const fn to_zero_indexed(self) -> usize
Return the zero-indexed primitive value for this OneIndexed
Sourcepub const fn saturating_add(self, rhs: usize) -> Self
pub const fn saturating_add(self, rhs: usize) -> Self
Saturating integer addition. Computes self + rhs, saturating at
the numeric bounds instead of overflowing.
Sourcepub const fn saturating_sub(self, rhs: usize) -> Self
pub const fn saturating_sub(self, rhs: usize) -> Self
Saturating integer subtraction. Computes self - rhs, saturating
at the numeric bounds instead of overflowing.
Sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Returns None if overflow occurred.
Sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Returns None if overflow occurred.
Sourcepub const fn digits(self) -> NonZeroUsize
pub const fn digits(self) -> NonZeroUsize
Calculate the number of digits in self.
This is primarily intended for computing the length of the string representation for formatted printing.
§Examples
use ruff_source_file::OneIndexed;
let one = OneIndexed::new(1).unwrap();
assert_eq!(one.digits().get(), 1);
let hundred = OneIndexed::new(100).unwrap();
assert_eq!(hundred.digits().get(), 3);
let thousand = OneIndexed::new(1000).unwrap();
assert_eq!(thousand.digits().get(), 4);Trait Implementations§
Source§impl Clone for OneIndexed
impl Clone for OneIndexed
Source§fn clone(&self) -> OneIndexed
fn clone(&self) -> OneIndexed
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more