arc feature to enable using Engine and other data structures from multiple threads (#142)

* `arc` feature to make engine usable from multiple threads.

`arc` is turned on by default. When enabled, std::sync::Arc
will be used instead of std::rc::Rc. The former makes regorus
types like Engine, Value, ast nodes etc Send, allowing for
usability from multiple threads.
Arc would add a performance overhead though since the reference
counting will now become atomic.

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* Make engine and related types Debug

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

* Input, Data as json. Evaluate bool queries.

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>

---------

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2024-02-13 10:23:19 -08:00
committed by GitHub
parent 13eb06e4be
commit 53b990f97d
14 changed files with 96 additions and 24 deletions
+15 -3
View File
@@ -20,6 +20,12 @@ mod value;
pub use engine::Engine;
pub use value::Value;
#[cfg(feature = "arc")]
use std::sync::Arc as Rc;
#[cfg(not(feature = "arc"))]
use std::rc::Rc;
/// Location of an [`Expression`] in a Rego query.
///
/// ```
@@ -68,7 +74,7 @@ pub struct Expression {
pub value: Value,
/// The Rego expression.
pub text: std::rc::Rc<str>,
pub text: Rc<str>,
/// Location of the expression in the query string.
pub location: Location,
@@ -263,7 +269,7 @@ pub struct QueryResults {
/// A user defined builtin function implementation.
///
/// It is not necessary to implement this trait directly.
pub trait Extension: FnMut(Vec<Value>) -> anyhow::Result<Value> {
pub trait Extension: FnMut(Vec<Value>) -> anyhow::Result<Value> + Send + Sync {
/// Fn, FnMut etc are not sized and cannot be cloned in their boxed form.
/// clone_box exists to overcome that.
fn clone_box<'a>(&self) -> Box<dyn 'a + Extension>
@@ -274,7 +280,7 @@ pub trait Extension: FnMut(Vec<Value>) -> anyhow::Result<Value> {
/// Automatically make matching closures a valid [`Extension`].
impl<F> Extension for F
where
F: FnMut(Vec<Value>) -> anyhow::Result<Value> + Clone,
F: FnMut(Vec<Value>) -> anyhow::Result<Value> + Clone + Send + Sync,
{
fn clone_box<'a>(&self) -> Box<dyn 'a + Extension>
where
@@ -291,6 +297,12 @@ impl<'a> Clone for Box<dyn 'a + Extension> {
}
}
impl std::fmt::Debug for dyn Extension {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
f.write_fmt(format_args!("<extension>"))
}
}
/// Items in `unstable` are likely to change.
#[doc(hidden)]
pub mod unstable {