Make some Position functions const.

This commit is contained in:
Stephen Chung 2021-07-14 13:58:18 +08:00
parent 0e77c4f9a0
commit 925325820e

View File

@ -83,53 +83,75 @@ impl Position {
/// Create a new [`Position`]. /// Create a new [`Position`].
/// ///
/// `line` must not be zero. /// `line` must not be zero.
/// If [`Position`] is zero, then it is at the beginning of a line. ///
/// If `position` is zero, then it is at the beginning of a line.
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if `line` is zero. /// Panics if `line` is zero.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn new(line: u16, _position: u16) -> Self { pub fn new(line: u16, position: u16) -> Self {
assert!(line != 0, "line cannot be zero"); assert!(line != 0, "line cannot be zero");
let _pos = position;
Self { Self {
#[cfg(not(feature = "no_position"))] #[cfg(not(feature = "no_position"))]
line, line,
#[cfg(not(feature = "no_position"))] #[cfg(not(feature = "no_position"))]
pos: _position, pos: _pos,
} }
} }
/// Create a new [`Position`].
///
/// If `line` is zero, then [`None`] is returned.
///
/// If `position` is zero, then it is at the beginning of a line.
#[inline(always)]
#[must_use]
pub const fn new_const(line: u16, position: u16) -> Option<Self> {
if line == 0 {
return None;
}
let _pos = position;
Some(Self {
#[cfg(not(feature = "no_position"))]
line,
#[cfg(not(feature = "no_position"))]
pos: _pos,
})
}
/// Get the line number (1-based), or [`None`] if there is no position. /// Get the line number (1-based), or [`None`] if there is no position.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn line(self) -> Option<usize> { pub const fn line(self) -> Option<usize> {
if self.is_none() { #[cfg(not(feature = "no_position"))]
return if self.is_none() {
None None
} else { } else {
#[cfg(not(feature = "no_position"))] Some(self.line as usize)
return Some(self.line as usize); };
#[cfg(feature = "no_position")] #[cfg(feature = "no_position")]
unreachable!("there is no Position"); return None;
}
} }
/// Get the character position (1-based), or [`None`] if at beginning of a line. /// Get the character position (1-based), or [`None`] if at beginning of a line.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn position(self) -> Option<usize> { pub const fn position(self) -> Option<usize> {
if self.is_none() {
None
} else {
#[cfg(not(feature = "no_position"))] #[cfg(not(feature = "no_position"))]
return if self.pos == 0 { return if self.is_none() {
None
} else if self.pos == 0 {
None None
} else { } else {
Some(self.pos as usize) Some(self.pos as usize)
}; };
#[cfg(feature = "no_position")] #[cfg(feature = "no_position")]
unreachable!("there is no Position"); return None;
}
} }
/// Advance by one character position. /// Advance by one character position.
#[inline(always)] #[inline(always)]