Refactor.

This commit is contained in:
Stephen Chung 2022-09-25 12:24:03 +08:00
parent def1a683ef
commit b56a9c22f3
20 changed files with 55 additions and 16 deletions

View File

@ -147,6 +147,7 @@ impl Expression<'_> {
impl AsRef<Expr> for Expression<'_> { impl AsRef<Expr> for Expression<'_> {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &Expr { fn as_ref(&self) -> &Expr {
self.0 self.0
} }
@ -156,6 +157,7 @@ impl Deref for Expression<'_> {
type Target = Expr; type Target = Expr;
#[inline(always)] #[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
self.0 self.0
} }

View File

@ -75,6 +75,7 @@ impl Engine {
/// Not available under `no_module`. /// Not available under `no_module`.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
#[must_use]
pub fn module_resolver(&self) -> &dyn crate::ModuleResolver { pub fn module_resolver(&self) -> &dyn crate::ModuleResolver {
&*self.module_resolver &*self.module_resolver
} }

View File

@ -16,12 +16,14 @@ impl Engine {
/// Get the global namespace module (which is the fist module in `global_modules`). /// Get the global namespace module (which is the fist module in `global_modules`).
#[inline(always)] #[inline(always)]
#[allow(dead_code)] #[allow(dead_code)]
#[must_use]
pub(crate) fn global_namespace(&self) -> &Module { pub(crate) fn global_namespace(&self) -> &Module {
self.global_modules.first().unwrap() self.global_modules.first().unwrap()
} }
/// Get a mutable reference to the global namespace module /// Get a mutable reference to the global namespace module
/// (which is the first module in `global_modules`). /// (which is the first module in `global_modules`).
#[inline(always)] #[inline(always)]
#[must_use]
pub(crate) fn global_namespace_mut(&mut self) -> &mut Module { pub(crate) fn global_namespace_mut(&mut self) -> &mut Module {
let module = self.global_modules.first_mut().unwrap(); let module = self.global_modules.first_mut().unwrap();
Shared::get_mut(module).expect("not shared") Shared::get_mut(module).expect("not shared")

View File

@ -919,6 +919,7 @@ impl<A: Into<Self>> AddAssign<A> for AST {
impl AsRef<[Stmt]> for AST { impl AsRef<[Stmt]> for AST {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &[Stmt] { fn as_ref(&self) -> &[Stmt] {
self.statements() self.statements()
} }
@ -927,6 +928,7 @@ impl AsRef<[Stmt]> for AST {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
impl AsRef<crate::Module> for AST { impl AsRef<crate::Module> for AST {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &crate::Module { fn as_ref(&self) -> &crate::Module {
self.shared_lib().as_ref() self.shared_lib().as_ref()
} }
@ -935,6 +937,7 @@ impl AsRef<crate::Module> for AST {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
impl AsRef<crate::Shared<crate::Module>> for AST { impl AsRef<crate::Shared<crate::Module>> for AST {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &crate::Shared<crate::Module> { fn as_ref(&self) -> &crate::Shared<crate::Module> {
self.shared_lib() self.shared_lib()
} }

View File

@ -257,6 +257,7 @@ impl Hash for FloatWrapper<crate::FLOAT> {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
impl<F: Float> AsRef<F> for FloatWrapper<F> { impl<F: Float> AsRef<F> for FloatWrapper<F> {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &F { fn as_ref(&self) -> &F {
&self.0 &self.0
} }
@ -265,6 +266,7 @@ impl<F: Float> AsRef<F> for FloatWrapper<F> {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
impl<F: Float> AsMut<F> for FloatWrapper<F> { impl<F: Float> AsMut<F> for FloatWrapper<F> {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_mut(&mut self) -> &mut F { fn as_mut(&mut self) -> &mut F {
&mut self.0 &mut self.0
} }
@ -275,6 +277,7 @@ impl<F: Float> Deref for FloatWrapper<F> {
type Target = F; type Target = F;
#[inline(always)] #[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
@ -283,6 +286,7 @@ impl<F: Float> Deref for FloatWrapper<F> {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
impl<F: Float> DerefMut for FloatWrapper<F> { impl<F: Float> DerefMut for FloatWrapper<F> {
#[inline(always)] #[inline(always)]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0 &mut self.0
} }

View File

@ -28,6 +28,7 @@ impl fmt::Debug for Ident {
impl AsRef<str> for Ident { impl AsRef<str> for Ident {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &str { fn as_ref(&self) -> &str {
self.name.as_ref() self.name.as_ref()
} }
@ -37,6 +38,7 @@ impl Deref for Ident {
type Target = ImmutableString; type Target = ImmutableString;
#[inline(always)] #[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.name &self.name
} }
@ -44,6 +46,7 @@ impl Deref for Ident {
impl DerefMut for Ident { impl DerefMut for Ident {
#[inline(always)] #[inline(always)]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.name &mut self.name
} }

View File

@ -70,6 +70,7 @@ impl Deref for Namespace {
type Target = StaticVec<Ident>; type Target = StaticVec<Ident>;
#[inline(always)] #[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.path &self.path
} }
@ -77,6 +78,7 @@ impl Deref for Namespace {
impl DerefMut for Namespace { impl DerefMut for Namespace {
#[inline(always)] #[inline(always)]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.path &mut self.path
} }

View File

@ -424,6 +424,7 @@ impl Deref for StmtBlock {
type Target = StmtBlockContainer; type Target = StmtBlockContainer;
#[inline(always)] #[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.block &self.block
} }
@ -431,6 +432,7 @@ impl Deref for StmtBlock {
impl DerefMut for StmtBlock { impl DerefMut for StmtBlock {
#[inline(always)] #[inline(always)]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.block &mut self.block
} }
@ -438,6 +440,7 @@ impl DerefMut for StmtBlock {
impl AsRef<[Stmt]> for StmtBlock { impl AsRef<[Stmt]> for StmtBlock {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &[Stmt] { fn as_ref(&self) -> &[Stmt] {
&self.block &self.block
} }
@ -445,6 +448,7 @@ impl AsRef<[Stmt]> for StmtBlock {
impl AsMut<[Stmt]> for StmtBlock { impl AsMut<[Stmt]> for StmtBlock {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_mut(&mut self) -> &mut [Stmt] { fn as_mut(&mut self) -> &mut [Stmt] {
&mut self.block &mut self.block
} }

View File

@ -240,12 +240,12 @@ impl Engine {
source.map_or_else( source.map_or_else(
|| { || {
if pos.is_none() { if pos.is_none() {
println!("{}", s); println!("{s}");
} else { } else {
println!("{:?} | {}", pos, s); println!("{pos:?} | {s}");
} }
}, },
|source| println!("{} @ {:?} | {}", source, pos, s), |source| println!("{source} @ {pos:?} | {s}"),
) )
}); });
} }

View File

@ -399,6 +399,7 @@ impl Deref for Target<'_> {
type Target = Dynamic; type Target = Dynamic;
#[inline] #[inline]
#[must_use]
fn deref(&self) -> &Dynamic { fn deref(&self) -> &Dynamic {
match self { match self {
Self::RefMut(r) => r, Self::RefMut(r) => r,
@ -416,6 +417,7 @@ impl Deref for Target<'_> {
impl AsRef<Dynamic> for Target<'_> { impl AsRef<Dynamic> for Target<'_> {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &Dynamic { fn as_ref(&self) -> &Dynamic {
self self
} }
@ -423,6 +425,7 @@ impl AsRef<Dynamic> for Target<'_> {
impl DerefMut for Target<'_> { impl DerefMut for Target<'_> {
#[inline] #[inline]
#[must_use]
fn deref_mut(&mut self) -> &mut Dynamic { fn deref_mut(&mut self) -> &mut Dynamic {
match self { match self {
Self::RefMut(r) => r, Self::RefMut(r) => r,
@ -440,6 +443,7 @@ impl DerefMut for Target<'_> {
impl AsMut<Dynamic> for Target<'_> { impl AsMut<Dynamic> for Target<'_> {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_mut(&mut self) -> &mut Dynamic { fn as_mut(&mut self) -> &mut Dynamic {
self self
} }

View File

@ -194,8 +194,8 @@ impl FileModuleResolver {
/// Get a reference to the file module resolver's [scope][Scope]. /// Get a reference to the file module resolver's [scope][Scope].
/// ///
/// The [scope][Scope] is used for compiling module scripts. /// The [scope][Scope] is used for compiling module scripts.
#[must_use]
#[inline(always)] #[inline(always)]
#[must_use]
pub const fn scope(&self) -> &Scope { pub const fn scope(&self) -> &Scope {
&self.scope &self.scope
} }
@ -211,8 +211,8 @@ impl FileModuleResolver {
/// Get a mutable reference to the file module resolver's [scope][Scope]. /// Get a mutable reference to the file module resolver's [scope][Scope].
/// ///
/// The [scope][Scope] is used for compiling module scripts. /// The [scope][Scope] is used for compiling module scripts.
#[must_use]
#[inline(always)] #[inline(always)]
#[must_use]
pub fn scope_mut(&mut self) -> &mut Scope<'static> { pub fn scope_mut(&mut self) -> &mut Scope<'static> {
&mut self.scope &mut self.scope
} }

View File

@ -252,6 +252,7 @@ impl<'d, T: Any + Clone> Deref for DynamicWriteLock<'d, T> {
type Target = T; type Target = T;
#[inline] #[inline]
#[must_use]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
match self.0 { match self.0 {
DynamicWriteLockInner::Reference(ref reference) => *reference, DynamicWriteLockInner::Reference(ref reference) => *reference,
@ -263,6 +264,7 @@ impl<'d, T: Any + Clone> Deref for DynamicWriteLock<'d, T> {
impl<'d, T: Any + Clone> DerefMut for DynamicWriteLock<'d, T> { impl<'d, T: Any + Clone> DerefMut for DynamicWriteLock<'d, T> {
#[inline] #[inline]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
match self.0 { match self.0 {
DynamicWriteLockInner::Reference(ref mut reference) => *reference, DynamicWriteLockInner::Reference(ref mut reference) => *reference,

View File

@ -23,6 +23,7 @@ use std::prelude::v1::*;
/// Turn on the `sync` feature to make it [`Send`] `+` [`Sync`]. /// Turn on the `sync` feature to make it [`Send`] `+` [`Sync`].
#[derive(Debug)] #[derive(Debug)]
#[non_exhaustive] #[non_exhaustive]
#[must_use]
pub enum EvalAltResult { pub enum EvalAltResult {
/// System error. Wrapped values are the error message and the internal error. /// System error. Wrapped values are the error message and the internal error.
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
@ -494,6 +495,7 @@ impl EvalAltResult {
/// The [position][Position] of this error is set to [`NONE`][Position::NONE] afterwards. /// The [position][Position] of this error is set to [`NONE`][Position::NONE] afterwards.
#[cold] #[cold]
#[inline(never)] #[inline(never)]
#[must_use]
pub fn take_position(&mut self) -> Position { pub fn take_position(&mut self) -> Position {
let pos = self.position(); let pos = self.position();
self.set_position(Position::NONE); self.set_position(Position::NONE);

View File

@ -53,6 +53,7 @@ impl Deref for ImmutableString {
type Target = SmartString; type Target = SmartString;
#[inline(always)] #[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
@ -60,6 +61,7 @@ impl Deref for ImmutableString {
impl AsRef<SmartString> for ImmutableString { impl AsRef<SmartString> for ImmutableString {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &SmartString { fn as_ref(&self) -> &SmartString {
&self.0 &self.0
} }
@ -67,6 +69,7 @@ impl AsRef<SmartString> for ImmutableString {
impl AsRef<str> for ImmutableString { impl AsRef<str> for ImmutableString {
#[inline(always)] #[inline(always)]
#[must_use]
fn as_ref(&self) -> &str { fn as_ref(&self) -> &str {
&self.0 &self.0
} }
@ -74,6 +77,7 @@ impl AsRef<str> for ImmutableString {
impl Borrow<SmartString> for ImmutableString { impl Borrow<SmartString> for ImmutableString {
#[inline(always)] #[inline(always)]
#[must_use]
fn borrow(&self) -> &SmartString { fn borrow(&self) -> &SmartString {
&self.0 &self.0
} }
@ -81,6 +85,7 @@ impl Borrow<SmartString> for ImmutableString {
impl Borrow<str> for ImmutableString { impl Borrow<str> for ImmutableString {
#[inline(always)] #[inline(always)]
#[must_use]
fn borrow(&self) -> &str { fn borrow(&self) -> &str {
self.as_str() self.as_str()
} }
@ -623,7 +628,7 @@ impl ImmutableString {
#[inline] #[inline]
#[must_use] #[must_use]
pub fn into_owned(mut self) -> String { pub fn into_owned(mut self) -> String {
self.make_mut(); // Make sure it is unique reference let _ = self.make_mut(); // Make sure it is unique reference
shared_take(self.0).into() // Should succeed shared_take(self.0).into() // Should succeed
} }
/// Make sure that the [`ImmutableString`] is unique (i.e. no other outstanding references). /// Make sure that the [`ImmutableString`] is unique (i.e. no other outstanding references).
@ -631,6 +636,7 @@ impl ImmutableString {
/// ///
/// If there are other references to the same string, a cloned copy is used. /// If there are other references to the same string, a cloned copy is used.
#[inline(always)] #[inline(always)]
#[must_use]
pub(crate) fn make_mut(&mut self) -> &mut SmartString { pub(crate) fn make_mut(&mut self) -> &mut SmartString {
shared_make_mut(&mut self.0) shared_make_mut(&mut self.0)
} }

View File

@ -13,6 +13,7 @@ use std::prelude::v1::*;
/// Error encountered when tokenizing the script text. /// Error encountered when tokenizing the script text.
#[derive(Debug, Eq, PartialEq, Clone, Hash)] #[derive(Debug, Eq, PartialEq, Clone, Hash)]
#[non_exhaustive] #[non_exhaustive]
#[must_use]
pub enum LexError { pub enum LexError {
/// An unexpected symbol is encountered when tokenizing the script text. /// An unexpected symbol is encountered when tokenizing the script text.
UnexpectedInput(String), UnexpectedInput(String),
@ -58,8 +59,8 @@ impl fmt::Display for LexError {
impl LexError { impl LexError {
/// Convert a [`LexError`] into a [`ParseError`]. /// Convert a [`LexError`] into a [`ParseError`].
#[inline(always)] #[cold]
#[must_use] #[inline(never)]
pub fn into_err(self, pos: Position) -> ParseError { pub fn into_err(self, pos: Position) -> ParseError {
ParseError(Box::new(self.into()), pos) ParseError(Box::new(self.into()), pos)
} }
@ -72,6 +73,7 @@ impl LexError {
/// massive code changes to remove/add back enum variants in match statements. /// massive code changes to remove/add back enum variants in match statements.
#[derive(Debug, Eq, PartialEq, Clone, Hash)] #[derive(Debug, Eq, PartialEq, Clone, Hash)]
#[non_exhaustive] #[non_exhaustive]
#[must_use]
pub enum ParseErrorType { pub enum ParseErrorType {
/// The script ends prematurely. /// The script ends prematurely.
UnexpectedEOF, UnexpectedEOF,
@ -171,7 +173,8 @@ pub enum ParseErrorType {
impl ParseErrorType { impl ParseErrorType {
/// Make a [`ParseError`] using the current type and position. /// Make a [`ParseError`] using the current type and position.
#[inline(always)] #[cold]
#[inline(never)]
#[must_use] #[must_use]
pub(crate) fn into_err(self, pos: Position) -> ParseError { pub(crate) fn into_err(self, pos: Position) -> ParseError {
ParseError(self.into(), pos) ParseError(self.into(), pos)
@ -277,6 +280,7 @@ impl From<LexError> for ParseErrorType {
/// Error when parsing a script. /// Error when parsing a script.
#[derive(Debug, Eq, PartialEq, Clone, Hash)] #[derive(Debug, Eq, PartialEq, Clone, Hash)]
#[must_use]
pub struct ParseError( pub struct ParseError(
/// Parse error type. /// Parse error type.
pub Box<ParseErrorType>, pub Box<ParseErrorType>,

View File

@ -10,7 +10,7 @@ fn test_max_operations() -> Result<(), Box<EvalAltResult>> {
engine.on_progress(|count| { engine.on_progress(|count| {
if count % 100 == 0 { if count % 100 == 0 {
println!("{}", count); println!("{count}");
} }
None None
}); });
@ -68,7 +68,7 @@ fn test_max_operations_functions() -> Result<(), Box<EvalAltResult>> {
engine.on_progress(|count| { engine.on_progress(|count| {
if count % 100 == 0 { if count % 100 == 0 {
println!("{}", count); println!("{count}");
} }
None None
}); });
@ -124,7 +124,7 @@ fn test_max_operations_eval() -> Result<(), Box<EvalAltResult>> {
engine.on_progress(|count| { engine.on_progress(|count| {
if count % 100 == 0 { if count % 100 == 0 {
println!("{}", count); println!("{count}");
} }
None None
}); });

View File

@ -42,7 +42,7 @@ mod test {
#[rhai_fn(name = "no_effect", set = "no_effect", pure)] #[rhai_fn(name = "no_effect", set = "no_effect", pure)]
pub fn no_effect(array: &mut Array, value: INT) { pub fn no_effect(array: &mut Array, value: INT) {
// array is not modified // array is not modified
println!("Array = {:?}, Value = {}", array, value); println!("Array = {array:?}, Value = {value}");
} }
} }
} }

View File

@ -66,7 +66,7 @@ fn test_print_debug() -> Result<(), Box<EvalAltResult>> {
); );
for entry in logbook.read().unwrap().iter() { for entry in logbook.read().unwrap().iter() {
println!("{}", entry); println!("{entry}");
} }
Ok(()) Ok(())

View File

@ -820,6 +820,6 @@ fn test_serde_blob() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
fn test_serde_json_borrowed_string() { fn test_serde_json_borrowed_string() {
let value = json!({ "a": "b" }); let value = json!({ "a": "b" });
println!("value: {:?}", value); println!("value: {value:?}");
let _: Dynamic = serde_json::from_value(value).unwrap(); let _: Dynamic = serde_json::from_value(value).unwrap();
} }

View File

@ -140,7 +140,7 @@ fn test_scope_eval() -> Result<(), Box<EvalAltResult>> {
// Second invocation using the same state // Second invocation using the same state
let result = engine.eval_with_scope::<INT>(&mut scope, "x")?; let result = engine.eval_with_scope::<INT>(&mut scope, "x")?;
println!("result: {}", result); // should print 966 println!("result: {result}"); // should print 966
// Variable y is changed in the script // Variable y is changed in the script
assert_eq!( assert_eq!(