From 2f5ce2fe5b1910f5cdc42954097518373f3961c0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 24 Feb 2022 09:08:10 +0800 Subject: [PATCH] Deprecate Position::new_const. --- src/api/deprecated.rs | 26 +++++++++++++++++++++++++- src/tokenizer.rs | 22 +--------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/api/deprecated.rs b/src/api/deprecated.rs index dd8ebd7a..62d7b52c 100644 --- a/src/api/deprecated.rs +++ b/src/api/deprecated.rs @@ -2,7 +2,7 @@ use crate::{ Dynamic, Engine, EvalAltResult, Expression, FnPtr, ImmutableString, NativeCallContext, - RhaiResult, RhaiResultOf, Scope, AST, + Position, RhaiResult, RhaiResultOf, Scope, AST, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -314,3 +314,27 @@ impl Expression<'_> { self.get_string_value() } } + +impl Position { + /// 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. + /// + /// # Deprecated + /// + /// This function is deprecated. Use [`new`][Position::new] (which panics when `line` is zero) instead. + /// + /// This method will be removed in the next major version. + #[deprecated(since = "1.6.0", note = "use `new` instead")] + #[inline(always)] + #[must_use] + pub const fn new_const(line: u16, position: u16) -> Option { + if line == 0 { + None + } else { + Some(Self::new(line, position)) + } + } +} diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 8d3c70b2..bf8326ac 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -94,7 +94,7 @@ impl Position { /// Panics if `line` is zero. #[inline(always)] #[must_use] - pub fn new(line: u16, position: u16) -> Self { + pub const fn new(line: u16, position: u16) -> Self { assert!(line != 0, "line cannot be zero"); let _pos = position; @@ -106,26 +106,6 @@ impl 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] - #[must_use] - pub const fn new_const(line: u16, position: u16) -> Option { - 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. #[inline] #[must_use]