Skip to content

Commit

Permalink
support any in fuzzing + bugfix ref issue
Browse files Browse the repository at this point in the history
  • Loading branch information
glendc committed Apr 15, 2024
1 parent a436217 commit be6d021
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
11 changes: 9 additions & 2 deletions fuzz/fuzz_targets/fuzz_employee_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use libfuzzer_sys::arbitrary::{self, Arbitrary};
use libfuzzer_sys::fuzz_target;
use venndb::VennDB;
use venndb::{Any, VennDB};

#[derive(Clone, Debug, Arbitrary, VennDB)]
pub struct Employee {
Expand All @@ -13,7 +13,7 @@ pub struct Employee {
alive: Option<bool>,
#[venndb(filter)]
faction: Faction,
#[venndb(filter)]
#[venndb(filter, any)]
planet: Option<Planet>,
}

Expand All @@ -25,10 +25,17 @@ pub enum Faction {

#[derive(Clone, Debug, Arbitrary, PartialEq, Eq, Hash)]
pub enum Planet {
Any,
Earth,
Mars,
}

impl Any for Planet {
fn is_any(&self) -> bool {
self == &Planet::Any
}
}

fuzz_target!(|rows: Vec<Employee>| {
let _ = EmployeeDB::from_rows(rows);
});
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ pub trait Any {
fn is_any(&self) -> bool;
}

impl<T: Any> Any for &T {
fn is_any(&self) -> bool {
T::is_any(*self)
}
}

impl<T: Any> Any for Option<T> {
fn is_any(&self) -> bool {
match self {
Some(value) => value.is_any(),
None => false,
}
}
}

impl<T: Any> Any for std::sync::Arc<T> {
fn is_any(&self) -> bool {
T::is_any(&**self)
}
}

impl<T: Any> Any for std::rc::Rc<T> {
fn is_any(&self) -> bool {
T::is_any(&**self)
}
}

impl<T: Any> Any for Box<T> {
fn is_any(&self) -> bool {
T::is_any(&**self)
}
}

#[doc(hidden)]
pub mod __internal {
//! Hidden thirdparty dependencies for venndb,
Expand Down
4 changes: 2 additions & 2 deletions venndb-macros/src/generate_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ fn generate_db_struct_method_append(
};
let is_any_value = if field.optional {
quote! {
data.#name.map(|v| ::venndb::Any::is_any(&v)).unwrap_or_default()
data.#name.as_ref().map(|v| ::venndb::Any::is_any(v)).unwrap_or_default()
}
} else {
quote! {
Expand Down Expand Up @@ -761,7 +761,7 @@ fn generate_query_struct_impl(
let value_filter = match field.filter_any_name() {
Some(name) => {
quote! {
if ::venndb::Any::is_any(value) {
if ::venndb::Any::is_any(&value) {
filter &= &self.db.#name;
} else {
#value_filter
Expand Down
21 changes: 21 additions & 0 deletions venndb-usage/tests/fails/any_filter_no_trait_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,25 @@ error[E0277]: the trait bound `Department: venndb::Any` is not satisfied
3 | #[derive(Debug, VennDB)]
| ^^^^^^ the trait `venndb::Any` is not implemented for `Department`
|
= help: the following other types implement trait `venndb::Any`:
Box<T>
Rc<T>
Arc<T>
Option<T>
&T
= note: this error originates in the derive macro `VennDB` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Department: venndb::Any` is not satisfied
--> tests/fails/any_filter_no_trait_impl.rs:3:17
|
3 | #[derive(Debug, VennDB)]
| ^^^^^^ the trait `venndb::Any` is not implemented for `Department`, which is required by `&Department: venndb::Any`
|
= help: the following other types implement trait `venndb::Any`:
Box<T>
Rc<T>
Arc<T>
Option<T>
&T
= note: required for `&Department` to implement `venndb::Any`
= note: this error originates in the derive macro `VennDB` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit be6d021

Please sign in to comment.