Skip to content

Commit

Permalink
Fine tune types.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Sep 8, 2023
1 parent b621640 commit 7b9e3a6
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/api/build_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl Engine {
///
/// To define a pretty-print name, call [`with_name`][`TypeBuilder::with_name`],
/// to use [`Engine::register_type_with_name`] instead.
#[must_use]
pub struct TypeBuilder<'a, T: Variant + Clone> {
engine: &'a mut Engine,
name: Option<&'static str>,
Expand Down
6 changes: 5 additions & 1 deletion src/api/call_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::{any::type_name, mem};
/// Options for calling a script-defined function via [`Engine::call_fn_with_options`].
#[derive(Debug, Hash)]
#[non_exhaustive]
#[must_use]
pub struct CallFnOptions<'t> {
/// A value for binding to the `this` pointer (if any). Default [`None`].
pub this_ptr: Option<&'t mut Dynamic>,
Expand All @@ -36,6 +35,7 @@ impl Default for CallFnOptions<'_> {
impl<'a> CallFnOptions<'a> {
/// Create a default [`CallFnOptions`].
#[inline(always)]
#[must_use]
pub fn new() -> Self {
Self {
this_ptr: None,
Expand All @@ -46,24 +46,28 @@ impl<'a> CallFnOptions<'a> {
}
/// Bind to the `this` pointer.
#[inline(always)]
#[must_use]
pub fn bind_this_ptr(mut self, value: &'a mut Dynamic) -> Self {
self.this_ptr = Some(value);
self
}
/// Set the custom state of this evaluation run (if any).
#[inline(always)]
#[must_use]
pub fn with_tag(mut self, value: impl Variant + Clone) -> Self {
self.tag = Some(Dynamic::from(value));
self
}
/// Set whether to evaluate the [`AST`] to load necessary modules before calling the function.
#[inline(always)]
#[must_use]
pub const fn eval_ast(mut self, value: bool) -> Self {
self.eval_ast = value;
self
}
/// Set whether to rewind the [`Scope`] after the function call.
#[inline(always)]
#[must_use]
pub const fn rewind_scope(mut self, value: bool) -> Self {
self.rewind_scope = value;
self
Expand Down
19 changes: 18 additions & 1 deletion src/ast/script_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,30 @@ impl fmt::Display for ScriptFnDef {
///
/// Created by [`AST::iter_functions`][super::AST::iter_functions].
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
#[non_exhaustive]
pub struct ScriptFnMetadata<'a> {
/// Function name.
pub name: &'a str,
/// Function parameters (if any).
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Vec::is_empty")
)]
pub params: Vec<&'a str>,
/// Function access mode.
pub access: FnAccess,
#[cfg(not(feature = "no_object"))]
/// Type of `this` pointer, if any.
/// Not available under `no_object`.
#[cfg(not(feature = "no_object"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub this_type: Option<&'a str>,
/// _(metadata)_ Function doc-comments (if any).
/// Exported under the `metadata` feature only.
Expand All @@ -102,6 +115,10 @@ pub struct ScriptFnMetadata<'a> {
///
/// Each line in non-block doc-comments starts with `///`.
#[cfg(feature = "metadata")]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Vec::is_empty")
)]
pub comments: Vec<&'a str>,
}

Expand Down
1 change: 1 addition & 0 deletions src/eval/chaining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static INDEXER_HASHES: OnceCell<(u64, u64)> = OnceCell::new();
#[must_use]
fn hash_idx() -> (u64, u64) {
*INDEXER_HASHES.get_or_init(|| {
#[allow(clippy::useless_conversion)]
(
calc_fn_hash(None, FN_IDX_GET, 2),
calc_fn_hash(None, FN_IDX_SET, 3),
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
//!
//! # Features
//!
#![cfg_attr(feature = "document-features", doc = document_features::document_features!(feature_label = "<span id=\"feature-{feature}\">**`{feature}`**</span>"))]
#![cfg_attr(feature = "document-features", doc = document_features::document_features!(
feature_label = "<span id=\"feature-{feature}\">**`{feature}`**</span>"
))]
//!
//! # On-Line Documentation
//!
Expand Down
14 changes: 7 additions & 7 deletions src/serde/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ enum FnType {
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct FnParam<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<&'a str>,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
#[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
pub typ: Option<Cow<'a, str>>,
}

Expand Down Expand Up @@ -164,14 +164,14 @@ impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ModuleMetadata<'a> {
#[serde(skip_serializing_if = "str::is_empty")]
pub doc: &'a str,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub modules: BTreeMap<&'a str, Self>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub custom_types: Vec<CustomTypeMetadata<'a>>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub functions: Vec<FnMetadata<'a>>,
#[serde(default, skip_serializing_if = "str::is_empty")]
pub doc: &'a str,
}

impl ModuleMetadata<'_> {
Expand Down
34 changes: 17 additions & 17 deletions src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ use num_traits::FromPrimitive;
/// Serializer for [`Dynamic`][crate::Dynamic].
pub struct DynamicSerializer {
/// Buffer to hold a temporary key.
_key: Identifier,
key: Identifier,

Check warning on line 18 in src/serde/ser.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_object,serde,metadata,internals,debugging, st...

field `key` is never read
/// Buffer to hold a temporary value.
_value: Dynamic,
value: Dynamic,
}

impl DynamicSerializer {
/// Create a [`DynamicSerializer`] from a [`Dynamic`][crate::Dynamic] value.
#[must_use]
pub const fn new(value: Dynamic) -> Self {
Self {
_key: Identifier::new_const(),
_value: value,
key: Identifier::new_const(),
value,
}
}
}
Expand Down Expand Up @@ -464,7 +464,7 @@ impl SerializeSeq for DynamicSerializer {
#[cfg(not(feature = "no_index"))]
{
let value = _value.serialize(&mut *self)?;
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
let arr = self.value.downcast_mut::<crate::Array>().unwrap();
arr.push(value);
Ok(())
}
Expand All @@ -481,7 +481,7 @@ impl SerializeSeq for DynamicSerializer {
#[inline]
fn end(self) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "no_index"))]
return Ok(self._value);
return Ok(self.value);
#[cfg(feature = "no_index")]
return Err(ERR::ErrorMismatchDataType(
"".into(),
Expand All @@ -500,7 +500,7 @@ impl SerializeTuple for DynamicSerializer {
#[cfg(not(feature = "no_index"))]
{
let value = _value.serialize(&mut *self)?;
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
let arr = self.value.downcast_mut::<crate::Array>().unwrap();
arr.push(value);
Ok(())
}
Expand All @@ -516,7 +516,7 @@ impl SerializeTuple for DynamicSerializer {
#[inline]
fn end(self) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "no_index"))]
return Ok(self._value);
return Ok(self.value);
#[cfg(feature = "no_index")]
return Err(ERR::ErrorMismatchDataType(
"".into(),
Expand All @@ -535,7 +535,7 @@ impl SerializeTupleStruct for DynamicSerializer {
#[cfg(not(feature = "no_index"))]
{
let value = _value.serialize(&mut *self)?;
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
let arr = self.value.downcast_mut::<crate::Array>().unwrap();
arr.push(value);
Ok(())
}
Expand All @@ -551,7 +551,7 @@ impl SerializeTupleStruct for DynamicSerializer {
#[inline]
fn end(self) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "no_index"))]
return Ok(self._value);
return Ok(self.value);
#[cfg(feature = "no_index")]
return Err(ERR::ErrorMismatchDataType(
"".into(),
Expand All @@ -570,7 +570,7 @@ impl SerializeMap for DynamicSerializer {
#[cfg(not(feature = "no_object"))]
{
let key = _key.serialize(&mut *self)?;
self._key = key
self.key = key
.into_immutable_string()
.map_err(|typ| {
ERR::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE)
Expand All @@ -590,9 +590,9 @@ impl SerializeMap for DynamicSerializer {
fn serialize_value<T: ?Sized + Serialize>(&mut self, _value: &T) -> RhaiResultOf<()> {
#[cfg(not(feature = "no_object"))]
{
let key = std::mem::take(&mut self._key);
let key = std::mem::take(&mut self.key);
let value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<crate::Map>().unwrap();
let map = self.value.downcast_mut::<crate::Map>().unwrap();
map.insert(key, value);
Ok(())
}
Expand All @@ -617,7 +617,7 @@ impl SerializeMap for DynamicSerializer {
ERR::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE)
})?;
let value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<crate::Map>().unwrap();
let map = self.value.downcast_mut::<crate::Map>().unwrap();
map.insert(key.into(), value);
Ok(())
}
Expand All @@ -633,7 +633,7 @@ impl SerializeMap for DynamicSerializer {
#[inline]
fn end(self) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "no_object"))]
return Ok(self._value);
return Ok(self.value);
#[cfg(feature = "no_object")]
return Err(ERR::ErrorMismatchDataType(
"".into(),
Expand All @@ -656,7 +656,7 @@ impl SerializeStruct for DynamicSerializer {
#[cfg(not(feature = "no_object"))]
{
let value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<crate::Map>().unwrap();
let map = self.value.downcast_mut::<crate::Map>().unwrap();
map.insert(_key.into(), value);
Ok(())
}
Expand All @@ -672,7 +672,7 @@ impl SerializeStruct for DynamicSerializer {
#[inline]
fn end(self) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "no_object"))]
return Ok(self._value);
return Ok(self.value);
#[cfg(feature = "no_object")]
return Err(ERR::ErrorMismatchDataType(
"".into(),
Expand Down
3 changes: 2 additions & 1 deletion src/types/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::{any::type_name, collections::BTreeMap};

/// _(internals)_ Information for a registered custom type.
/// Exported under the `internals` feature only.
#[derive(Debug, Eq, PartialEq, Clone, Hash, Default)]
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
#[non_exhaustive]
pub struct CustomTypeInfo {
/// Rust name of the custom type.
pub type_name: Identifier,
Expand Down
2 changes: 1 addition & 1 deletion src/types/var_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::prelude::v1::*;

/// Information on a variable definition.
#[derive(Debug, Hash)]
#[derive(Debug, Clone, Hash)]
pub struct VarDefInfo<'a> {
/// Name of the variable to be defined.
name: &'a str,
Expand Down

0 comments on commit 7b9e3a6

Please sign in to comment.