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

View File

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

View File

@ -16,12 +16,14 @@ impl Engine {
/// Get the global namespace module (which is the fist module in `global_modules`).
#[inline(always)]
#[allow(dead_code)]
#[must_use]
pub(crate) fn global_namespace(&self) -> &Module {
self.global_modules.first().unwrap()
}
/// Get a mutable reference to the global namespace module
/// (which is the first module in `global_modules`).
#[inline(always)]
#[must_use]
pub(crate) fn global_namespace_mut(&mut self) -> &mut Module {
let module = self.global_modules.first_mut().unwrap();
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 {
#[inline(always)]
#[must_use]
fn as_ref(&self) -> &[Stmt] {
self.statements()
}
@ -927,6 +928,7 @@ impl AsRef<[Stmt]> for AST {
#[cfg(not(feature = "no_function"))]
impl AsRef<crate::Module> for AST {
#[inline(always)]
#[must_use]
fn as_ref(&self) -> &crate::Module {
self.shared_lib().as_ref()
}
@ -935,6 +937,7 @@ impl AsRef<crate::Module> for AST {
#[cfg(not(feature = "no_function"))]
impl AsRef<crate::Shared<crate::Module>> for AST {
#[inline(always)]
#[must_use]
fn as_ref(&self) -> &crate::Shared<crate::Module> {
self.shared_lib()
}

View File

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

View File

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

View File

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

View File

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

View File

@ -240,12 +240,12 @@ impl Engine {
source.map_or_else(
|| {
if pos.is_none() {
println!("{}", s);
println!("{s}");
} 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;
#[inline]
#[must_use]
fn deref(&self) -> &Dynamic {
match self {
Self::RefMut(r) => r,
@ -416,6 +417,7 @@ impl Deref for Target<'_> {
impl AsRef<Dynamic> for Target<'_> {
#[inline(always)]
#[must_use]
fn as_ref(&self) -> &Dynamic {
self
}
@ -423,6 +425,7 @@ impl AsRef<Dynamic> for Target<'_> {
impl DerefMut for Target<'_> {
#[inline]
#[must_use]
fn deref_mut(&mut self) -> &mut Dynamic {
match self {
Self::RefMut(r) => r,
@ -440,6 +443,7 @@ impl DerefMut for Target<'_> {
impl AsMut<Dynamic> for Target<'_> {
#[inline(always)]
#[must_use]
fn as_mut(&mut self) -> &mut Dynamic {
self
}

View File

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

View File

@ -252,6 +252,7 @@ impl<'d, T: Any + Clone> Deref for DynamicWriteLock<'d, T> {
type Target = T;
#[inline]
#[must_use]
fn deref(&self) -> &Self::Target {
match self.0 {
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> {
#[inline]
#[must_use]
fn deref_mut(&mut self) -> &mut Self::Target {
match self.0 {
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`].
#[derive(Debug)]
#[non_exhaustive]
#[must_use]
pub enum EvalAltResult {
/// System error. Wrapped values are the error message and the internal error.
#[cfg(not(feature = "sync"))]
@ -494,6 +495,7 @@ impl EvalAltResult {
/// The [position][Position] of this error is set to [`NONE`][Position::NONE] afterwards.
#[cold]
#[inline(never)]
#[must_use]
pub fn take_position(&mut self) -> Position {
let pos = self.position();
self.set_position(Position::NONE);

View File

@ -53,6 +53,7 @@ impl Deref for ImmutableString {
type Target = SmartString;
#[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target {
&self.0
}
@ -60,6 +61,7 @@ impl Deref for ImmutableString {
impl AsRef<SmartString> for ImmutableString {
#[inline(always)]
#[must_use]
fn as_ref(&self) -> &SmartString {
&self.0
}
@ -67,6 +69,7 @@ impl AsRef<SmartString> for ImmutableString {
impl AsRef<str> for ImmutableString {
#[inline(always)]
#[must_use]
fn as_ref(&self) -> &str {
&self.0
}
@ -74,6 +77,7 @@ impl AsRef<str> for ImmutableString {
impl Borrow<SmartString> for ImmutableString {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &SmartString {
&self.0
}
@ -81,6 +85,7 @@ impl Borrow<SmartString> for ImmutableString {
impl Borrow<str> for ImmutableString {
#[inline(always)]
#[must_use]
fn borrow(&self) -> &str {
self.as_str()
}
@ -623,7 +628,7 @@ impl ImmutableString {
#[inline]
#[must_use]
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
}
/// 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.
#[inline(always)]
#[must_use]
pub(crate) fn make_mut(&mut self) -> &mut SmartString {
shared_make_mut(&mut self.0)
}

View File

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

View File

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

View File

@ -42,7 +42,7 @@ mod test {
#[rhai_fn(name = "no_effect", set = "no_effect", pure)]
pub fn no_effect(array: &mut Array, value: INT) {
// 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() {
println!("{}", entry);
println!("{entry}");
}
Ok(())

View File

@ -820,6 +820,6 @@ fn test_serde_blob() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_object"))]
fn test_serde_json_borrowed_string() {
let value = json!({ "a": "b" });
println!("value: {:?}", value);
println!("value: {value:?}");
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
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
assert_eq!(