Add #[must_use]

This commit is contained in:
Stephen Chung
2021-06-12 22:47:43 +08:00
parent 68ea8c27fd
commit 8ca24059b1
28 changed files with 489 additions and 55 deletions

View File

@@ -67,6 +67,7 @@ impl FuncInfo {
/// Generate a signature of the function.
/// Exported under the `metadata` feature only.
#[cfg(feature = "metadata")]
#[must_use]
pub fn gen_signature(&self) -> String {
let mut sig = format!("{}(", self.name);
@@ -240,6 +241,7 @@ impl Module {
/// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);
/// ```
#[inline(always)]
#[must_use]
pub fn new() -> Self {
Self {
id: None,
@@ -269,6 +271,7 @@ impl Module {
/// assert_eq!(module.id(), Some("hello"));
/// ```
#[inline(always)]
#[must_use]
pub fn id(&self) -> Option<&str> {
self.id_raw().map(|s| s.as_str())
}
@@ -285,6 +288,7 @@ impl Module {
/// assert_eq!(module.id_raw().map(|s| s.as_str()), Some("hello"));
/// ```
#[inline(always)]
#[must_use]
pub fn id_raw(&self) -> Option<&Identifier> {
self.id.as_ref()
}
@@ -317,6 +321,7 @@ impl Module {
/// assert!(module.is_empty());
/// ```
#[inline(always)]
#[must_use]
pub fn is_empty(&self) -> bool {
self.functions.is_empty()
&& self.all_functions.is_empty()
@@ -349,6 +354,7 @@ impl Module {
/// # }
/// ```
#[inline(always)]
#[must_use]
pub fn is_indexed(&self) -> bool {
self.indexed
}
@@ -357,6 +363,7 @@ impl Module {
/// Exported under the `metadata` feature only.
#[cfg(feature = "metadata")]
#[inline(always)]
#[must_use]
pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
self.functions
.values()
@@ -379,6 +386,7 @@ impl Module {
/// assert!(module.contains_var("answer"));
/// ```
#[inline(always)]
#[must_use]
pub fn contains_var(&self, name: &str) -> bool {
self.variables.contains_key(name)
}
@@ -395,6 +403,7 @@ impl Module {
/// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);
/// ```
#[inline(always)]
#[must_use]
pub fn get_var_value<T: Variant + Clone>(&self, name: &str) -> Option<T> {
self.get_var(name).and_then(Dynamic::try_cast::<T>)
}
@@ -411,6 +420,7 @@ impl Module {
/// assert_eq!(module.get_var("answer").unwrap().cast::<i64>(), 42);
/// ```
#[inline(always)]
#[must_use]
pub fn get_var(&self, name: &str) -> Option<Dynamic> {
self.variables.get(name).cloned()
}
@@ -448,6 +458,7 @@ impl Module {
/// Get a reference to a namespace-qualified variable.
/// Name and Position in [`EvalAltResult`] are [`None`] and [`NONE`][Position::NONE] and must be set afterwards.
#[inline(always)]
#[must_use]
pub(crate) fn get_qualified_var(&self, hash_var: u64) -> Result<&Dynamic, Box<EvalAltResult>> {
self.all_variables.get(&hash_var).ok_or_else(|| {
EvalAltResult::ErrorVariableNotFound(String::new(), Position::NONE).into()
@@ -489,6 +500,7 @@ impl Module {
/// and number of parameters.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
#[must_use]
pub fn get_script_fn(
&self,
name: &str,
@@ -508,6 +520,7 @@ impl Module {
/// Thus the [`Module`] is automatically set to be non-indexed.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[must_use]
pub(crate) fn sub_modules_mut(&mut self) -> &mut BTreeMap<Identifier, Shared<Module>> {
// We must assume that the user has changed the sub-modules
// (otherwise why take a mutable reference?)
@@ -533,6 +546,7 @@ impl Module {
/// assert!(module.contains_sub_module("question"));
/// ```
#[inline(always)]
#[must_use]
pub fn contains_sub_module(&self, name: &str) -> bool {
self.modules.contains_key(name)
}
@@ -550,6 +564,7 @@ impl Module {
/// assert!(module.get_sub_module("question").is_some());
/// ```
#[inline(always)]
#[must_use]
pub fn get_sub_module(&self, name: &str) -> Option<&Module> {
self.modules.get(name).map(|m| m.as_ref())
}
@@ -594,6 +609,7 @@ impl Module {
/// assert!(module.contains_fn(hash));
/// ```
#[inline(always)]
#[must_use]
pub fn contains_fn(&self, hash_fn: u64) -> bool {
self.functions.contains_key(&hash_fn)
}
@@ -641,6 +657,7 @@ impl Module {
/// Remap type ID.
#[inline(always)]
#[must_use]
fn map_type(map: bool, type_id: TypeId) -> TypeId {
if !map {
return type_id;
@@ -1104,6 +1121,7 @@ impl Module {
///
/// The [`u64`] hash is returned by the [`set_native_fn`][Module::set_native_fn] call.
#[inline(always)]
#[must_use]
pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> {
self.functions.get(&hash_fn).map(|f| f.func.as_ref())
}
@@ -1112,6 +1130,7 @@ impl Module {
///
/// The [`u64`] hash is calculated by [`build_index`][Module::build_index].
#[inline(always)]
#[must_use]
pub fn contains_qualified_fn(&self, hash_fn: u64) -> bool {
self.all_functions.contains_key(&hash_fn)
}
@@ -1120,6 +1139,7 @@ impl Module {
///
/// The [`u64`] hash is calculated by [`build_index`][Module::build_index].
#[inline(always)]
#[must_use]
pub(crate) fn get_qualified_fn(&self, hash_qualified_fn: u64) -> Option<&CallableFunction> {
self.all_functions
.get(&hash_qualified_fn)
@@ -1266,6 +1286,7 @@ impl Module {
/// Get the number of variables, functions and type iterators in the [`Module`].
#[inline(always)]
#[must_use]
pub fn count(&self) -> (usize, usize, usize) {
(
self.variables.len(),
@@ -1276,12 +1297,14 @@ impl Module {
/// Get an iterator to the sub-modules in the [`Module`].
#[inline(always)]
#[must_use]
pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, Shared<Module>)> {
self.modules.iter().map(|(k, m)| (k.as_str(), m.clone()))
}
/// Get an iterator to the variables in the [`Module`].
#[inline(always)]
#[must_use]
pub fn iter_var(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
self.variables.iter().map(|(k, v)| (k.as_str(), v))
}
@@ -1289,6 +1312,7 @@ impl Module {
/// Get an iterator to the functions in the [`Module`].
#[inline(always)]
#[allow(dead_code)]
#[must_use]
pub(crate) fn iter_fn(&self) -> impl Iterator<Item = &FuncInfo> {
self.functions.values().map(Box::as_ref)
}
@@ -1303,6 +1327,7 @@ impl Module {
/// 5) Shared reference to function definition [`ScriptFnDef`][crate::ast::ScriptFnDef].
#[cfg(not(feature = "no_function"))]
#[inline(always)]
#[must_use]
pub(crate) fn iter_script_fn(
&self,
) -> impl Iterator<
@@ -1338,6 +1363,7 @@ impl Module {
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "internals"))]
#[inline(always)]
#[must_use]
pub fn iter_script_fn_info(
&self,
) -> impl Iterator<Item = (FnNamespace, FnAccess, &str, usize)> {
@@ -1359,6 +1385,7 @@ impl Module {
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "internals")]
#[inline(always)]
#[must_use]
pub fn iter_script_fn_info(
&self,
) -> impl Iterator<
@@ -1395,6 +1422,7 @@ impl Module {
/// # }
/// ```
#[cfg(not(feature = "no_module"))]
#[must_use]
pub fn eval_ast_as_new(
mut scope: crate::Scope,
ast: &crate::AST,
@@ -1463,6 +1491,7 @@ impl Module {
///
/// Panics if the [`Module`] is not yet indexed via [`build_index`][Module::build_index].
#[inline(always)]
#[must_use]
pub fn contains_indexed_global_functions(&self) -> bool {
self.contains_indexed_global_functions
}
@@ -1562,12 +1591,14 @@ impl Module {
/// Does a type iterator exist in the entire module tree?
#[inline(always)]
#[must_use]
pub fn contains_qualified_iter(&self, id: TypeId) -> bool {
self.all_type_iterators.contains_key(&id)
}
/// Does a type iterator exist in the module?
#[inline(always)]
#[must_use]
pub fn contains_iter(&self, id: TypeId) -> bool {
self.type_iterators.contains_key(&id)
}
@@ -1609,12 +1640,14 @@ impl Module {
/// Get the specified type iterator.
#[inline(always)]
#[must_use]
pub(crate) fn get_qualified_iter(&self, id: TypeId) -> Option<IteratorFn> {
self.all_type_iterators.get(&id).cloned()
}
/// Get the specified type iterator.
#[inline(always)]
#[must_use]
pub(crate) fn get_iter(&self, id: TypeId) -> Option<IteratorFn> {
self.type_iterators.get(&id).cloned()
}
@@ -1693,6 +1726,7 @@ impl From<StaticVec<Ident>> for NamespaceRef {
impl NamespaceRef {
/// Get the [`Scope`][crate::Scope] index offset.
#[inline(always)]
#[must_use]
pub(crate) fn index(&self) -> Option<NonZeroUsize> {
self.index
}

View File

@@ -41,6 +41,7 @@ impl ModuleResolversCollection {
/// engine.set_module_resolver(collection);
/// ```
#[inline(always)]
#[must_use]
pub fn new() -> Self {
Default::default()
}
@@ -76,11 +77,13 @@ impl ModuleResolversCollection {
}
/// Get an iterator of all the [module resolvers][ModuleResolver].
#[inline(always)]
#[must_use]
pub fn iter(&self) -> impl Iterator<Item = &dyn ModuleResolver> {
self.0.iter().map(|v| v.as_ref())
}
/// Get a mutable iterator of all the [module resolvers][ModuleResolver].
#[inline(always)]
#[must_use]
pub fn into_iter(self) -> impl Iterator<Item = Box<dyn ModuleResolver>> {
self.0.into_iter()
}
@@ -92,11 +95,13 @@ impl ModuleResolversCollection {
}
/// Is this [`ModuleResolversCollection`] empty?
#[inline(always)]
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Get the number of [module resolvers][ModuleResolver] in this [`ModuleResolversCollection`].
#[inline(always)]
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}

View File

@@ -77,6 +77,7 @@ impl FileModuleResolver {
/// engine.set_module_resolver(resolver);
/// ```
#[inline(always)]
#[must_use]
pub fn new() -> Self {
Self::new_with_extension(RHAI_SCRIPT_EXTENSION)
}
@@ -99,6 +100,7 @@ impl FileModuleResolver {
/// engine.set_module_resolver(resolver);
/// ```
#[inline(always)]
#[must_use]
pub fn new_with_path(path: impl Into<PathBuf>) -> Self {
Self::new_with_path_and_extension(path, RHAI_SCRIPT_EXTENSION)
}
@@ -118,6 +120,7 @@ impl FileModuleResolver {
/// engine.set_module_resolver(resolver);
/// ```
#[inline(always)]
#[must_use]
pub fn new_with_extension(extension: impl Into<Identifier>) -> Self {
Self {
base_path: None,
@@ -143,6 +146,7 @@ impl FileModuleResolver {
/// engine.set_module_resolver(resolver);
/// ```
#[inline(always)]
#[must_use]
pub fn new_with_path_and_extension(
path: impl Into<PathBuf>,
extension: impl Into<Identifier>,
@@ -157,6 +161,7 @@ impl FileModuleResolver {
/// Get the base path for script files.
#[inline(always)]
#[must_use]
pub fn base_path(&self) -> Option<&Path> {
self.base_path.as_ref().map(PathBuf::as_ref)
}
@@ -169,6 +174,7 @@ impl FileModuleResolver {
/// Get the script file extension.
#[inline(always)]
#[must_use]
pub fn extension(&self) -> &str {
&self.extension
}
@@ -188,12 +194,14 @@ impl FileModuleResolver {
}
/// Is the cache enabled?
#[inline(always)]
#[must_use]
pub fn is_cache_enabled(&self) -> bool {
self.cache_enabled
}
/// Is a particular path cached?
#[inline(always)]
#[must_use]
pub fn is_cached(&self, path: &str, source_path: Option<&str>) -> bool {
if !self.cache_enabled {
return false;
@@ -208,16 +216,19 @@ impl FileModuleResolver {
}
/// Empty the internal cache.
#[inline(always)]
pub fn clear_cache(&mut self) {
pub fn clear_cache(&mut self) -> &mut Self {
#[cfg(not(feature = "sync"))]
self.cache.borrow_mut().clear();
#[cfg(feature = "sync")]
self.cache.write().unwrap().clear();
self
}
/// Remove the specified path from internal cache.
///
/// The next time this path is resolved, the script file will be loaded once again.
#[inline(always)]
#[must_use]
pub fn clear_cache_for_path(
&mut self,
path: &str,
@@ -240,6 +251,8 @@ impl FileModuleResolver {
.map(|(_, v)| v);
}
/// Construct a full file path.
#[must_use]
#[must_use]
fn get_file_path(&self, path: &str, source_path: Option<&str>) -> PathBuf {
let path = Path::new(path);

View File

@@ -23,6 +23,7 @@ pub use stat::StaticModuleResolver;
/// Trait that encapsulates a module resolution service.
pub trait ModuleResolver: SendSync {
/// Resolve a module based on a path string.
#[must_use]
fn resolve(
&self,
engine: &Engine,
@@ -41,6 +42,7 @@ pub trait ModuleResolver: SendSync {
/// Override the default implementation of this method if the module resolver
/// serves modules based on compiled Rhai scripts.
#[allow(unused_variables)]
#[must_use]
fn resolve_ast(
&self,
engine: &Engine,

View File

@@ -41,6 +41,7 @@ impl StaticModuleResolver {
/// engine.set_module_resolver(resolver);
/// ```
#[inline(always)]
#[must_use]
pub fn new() -> Self {
Default::default()
}
@@ -57,38 +58,45 @@ impl StaticModuleResolver {
}
/// Does the path exist?
#[inline(always)]
#[must_use]
pub fn contains_path(&self, path: &str) -> bool {
self.0.contains_key(path)
}
/// Get an iterator of all the [modules][Module].
#[inline(always)]
#[must_use]
pub fn iter(&self) -> impl Iterator<Item = (&str, &Shared<Module>)> {
self.0.iter().map(|(k, v)| (k.as_str(), v))
}
/// Get a mutable iterator of all the [modules][Module].
#[inline(always)]
#[must_use]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Shared<Module>)> {
self.0.iter_mut().map(|(k, v)| (k.as_str(), v))
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
#[must_use]
pub fn into_iter(self) -> impl Iterator<Item = (Identifier, Shared<Module>)> {
self.0.into_iter()
}
/// Get an iterator of all the [module][Module] paths.
#[inline(always)]
#[must_use]
pub fn paths(&self) -> impl Iterator<Item = &str> {
self.0.keys().map(|s| s.as_str())
}
/// Get an iterator of all the [modules][Module].
#[inline(always)]
#[must_use]
pub fn values(&self) -> impl Iterator<Item = &Shared<Module>> {
self.0.values().map(|m| m)
}
/// Remove all [modules][Module].
#[inline(always)]
pub fn clear(&mut self) {
pub fn clear(&mut self) -> &mut Self {
self.0.clear();
self
}
/// Is this [`StaticModuleResolver`] empty?
#[inline(always)]
@@ -97,6 +105,7 @@ impl StaticModuleResolver {
}
/// Get the number of [modules][Module] in this [`StaticModuleResolver`].
#[inline(always)]
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
@@ -105,10 +114,11 @@ impl StaticModuleResolver {
///
/// Existing modules of the same path name are overwritten.
#[inline(always)]
pub fn merge(&mut self, other: Self) {
pub fn merge(&mut self, other: Self) -> &mut Self {
if !other.is_empty() {
self.0.extend(other.0.into_iter());
}
self
}
}