Skip to content

Commit

Permalink
Use generic-array 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bluk committed Sep 18, 2023
1 parent e32bbb0 commit f40a2a1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ version = "0.0.2"

[dependencies]
either = { version ="1.9.0", default-features = false }
generic-array = "0.14.7"
generic-array = "1.0.0"
paste = "1.0.14"
typenum = { version = "1.16.0", default-features = false }

Expand Down
2 changes: 1 addition & 1 deletion src/facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ where
}

/// Pushes a new tuple value for a predicate.
pub(crate) fn push<N: ArrayLength<ConstantId>>(
pub(crate) fn push<N: ArrayLength>(
mut facts: SectionMut<'_>,
pred: PredicateId,
constants: GenericArray<ConstantId, N>,
Expand Down
33 changes: 17 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use alloc::vec::Vec;
use core::iter;
#[cfg(feature = "std")]
use std::vec::Vec;
use utils::{ExpectedConstantTy, TryFromConstantArray, VoidResult};
use utils::{TryFromConstantArray, VoidResult};

use either::Either;
use generic_array::{functional::FunctionalSequence, ArrayLength, GenericArray};
Expand Down Expand Up @@ -325,8 +325,7 @@ impl<'s> Passdata<'s> {
pub fn add_fact<'a, T>(&mut self, predicate: &str, constants: T) -> Result<bool>
where
T: IntoArray<Constant<'a>>,
<T as IntoArray<Constant<'a>>>::Length: ArrayLength<ConstantTy>,
<T as IntoArray<Constant<'a>>>::Length: ArrayLength<ConstantId>,
<T as IntoArray<Constant<'a>>>::Length: ArrayLength,
{
let constants = constants.into_array();

Expand Down Expand Up @@ -370,7 +369,7 @@ impl<'s> Passdata<'s> {
where
T: QueryArgs + 'a,
R: TryFromConstantArray<'a, Length = T::Length<'a>> + 'a,
T::Length<'a>: ArrayLength<ExpectedConstantTy>,
T::Length<'a>: ArrayLength,
{
self.schema.validate_conversions(predicate, &args.tys())?;
self.schema
Expand Down Expand Up @@ -412,7 +411,7 @@ impl<'s> Passdata<'s> {
pub fn contains_edb<'a, T>(&'a self, pred: &str, args: T) -> Result<bool>
where
T: QueryArgs + 'a,
T::Length<'a>: ArrayLength<ExpectedConstantTy>,
T::Length<'a>: ArrayLength,
{
self.query_edb::<_, VoidResult<T::Length<'a>>>(pred, args)
.map(|mut values| values.next().is_some())
Expand All @@ -429,7 +428,7 @@ impl<'s> Passdata<'s> {
where
T: QueryArgs + 'a,
R: TryFromConstantArray<'a, Length = T::Length<'a>> + 'a,
T::Length<'a>: ArrayLength<ExpectedConstantTy>,
T::Length<'a>: ArrayLength,
{
let mut iter = self.query_edb(pred, args)?;
let Some(first) = iter.next() else {
Expand Down Expand Up @@ -654,9 +653,11 @@ mod tests {
);
assert_eq!(
data.query_exclusive_edb("b", ("xyz", 1234, AnyBool)),
Ok(Some(
arr![Constant<'_>; Constant::from("xyz"), Constant::from(1234), Constant::from(false)]
))
Ok(Some(arr![
Constant::from("xyz"),
Constant::from(1234),
Constant::from(false)
]))
);
assert_eq!(
data.query_exclusive_edb::<_, (&str, i64, bool)>("b", ("xyz", AnyNum, AnyBool)),
Expand Down Expand Up @@ -685,7 +686,7 @@ mod tests {
);
assert_eq!(
data.query_exclusive_edb("d", AnyBytes),
Ok(Some(arr![&[u8]; "xyz".as_bytes()]))
Ok(Some(arr!["xyz".as_bytes()]))
);
assert_eq!(data.query_exclusive_edb::<_, &[u8]>("d", "abc"), Ok(None));
assert_eq!(data.query_exclusive_edb("d", "xyz"), Ok(Some("xyz")));
Expand Down Expand Up @@ -827,13 +828,13 @@ mod tests {

prop_assert!(!pd.contains_edb(&predicate, n)?);
prop_assert!(!pd.contains_edb(&predicate, (n,))?);
prop_assert!(!pd.contains_edb(&predicate, arr![i64; n])?);
prop_assert!(!pd.contains_edb(&predicate, arr![n])?);

pd.add_fact(&predicate, n)?;

prop_assert!(pd.contains_edb(&predicate, n)?);
prop_assert!(pd.contains_edb(&predicate, (n,))?);
prop_assert!(pd.contains_edb(&predicate, arr![i64; n])?);
prop_assert!(pd.contains_edb(&predicate, arr![n])?);
}

#[allow(clippy::ignored_unit_patterns)]
Expand All @@ -847,14 +848,14 @@ mod tests {
prop_assert!(!pd.contains_edb(&predicate, s.as_str())?);
prop_assert!(!pd.contains_edb(&predicate, s.clone())?);
prop_assert!(!pd.contains_edb(&predicate, (s.clone(),))?);
prop_assert!(!pd.contains_edb(&predicate, arr![&str; &s])?);
prop_assert!(!pd.contains_edb(&predicate, arr![&s])?);

pd.add_fact(&predicate, s.as_str())?;

prop_assert!(pd.contains_edb(&predicate, s.as_str())?);
prop_assert!(pd.contains_edb(&predicate, s.clone())?);
prop_assert!(pd.contains_edb(&predicate, (s.clone(),))?);
prop_assert!(pd.contains_edb(&predicate, arr![&str; &s])?);
prop_assert!(pd.contains_edb(&predicate, arr![&s])?);
}


Expand All @@ -869,14 +870,14 @@ mod tests {
prop_assert!(!pd.contains_edb(&predicate, b.as_slice())?);
prop_assert!(!pd.contains_edb(&predicate, b.clone())?);
prop_assert!(!pd.contains_edb(&predicate, (b.clone(),))?);
prop_assert!(!pd.contains_edb(&predicate, arr![&[u8]; &b])?);
prop_assert!(!pd.contains_edb(&predicate, arr![&b])?);

pd.add_fact(&predicate, b.as_slice())?;

prop_assert!(pd.contains_edb(&predicate, b.as_slice())?);
prop_assert!(pd.contains_edb(&predicate, b.clone())?);
prop_assert!(pd.contains_edb(&predicate, (b.clone(),))?);
prop_assert!(pd.contains_edb(&predicate, arr![&[u8]; &b])?);
prop_assert!(pd.contains_edb(&predicate, arr![&b])?);
}

#[allow(clippy::ignored_unit_patterns)]
Expand Down
74 changes: 55 additions & 19 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ pub enum ExpectedConstantTy {
/// Converts data into an array.
pub trait IntoArray<T>: Sized {
/// Length of the generic array.
type Length: ArrayLength<T>;
type Length: ArrayLength;

/// Converts `self` into an array.
fn into_array(self) -> GenericArray<T, Self::Length>;
}

impl<T, L> IntoArray<T> for GenericArray<T, L>
where
L: ArrayLength<T>,
L: ArrayLength,
{
type Length = L;

Expand Down Expand Up @@ -237,6 +237,19 @@ impl QueryArg for String {
}
}

impl<'a> QueryArg for &'a String {
fn ty(&self) -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}

fn is_match(&self, other: values::Constant<'_>) -> bool {
match other {
crate::Constant::Bytes(other) => self.as_bytes() == other,
crate::Constant::Bool(_) | crate::Constant::Num(_) => false,
}
}
}

impl<'b> QueryArg for Cow<'b, str> {
fn ty(&self) -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
Expand Down Expand Up @@ -276,6 +289,19 @@ impl QueryArg for Vec<u8> {
}
}

impl<'a> QueryArg for &'a Vec<u8> {
fn ty(&self) -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}

fn is_match(&self, other: values::Constant<'_>) -> bool {
match other {
crate::Constant::Bytes(other) => *self == other,
crate::Constant::Bool(_) | crate::Constant::Num(_) => false,
}
}
}

impl<'b> QueryArg for Cow<'b, [u8]> {
fn ty(&self) -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
Expand Down Expand Up @@ -393,7 +419,7 @@ where
Self: Sized,
{
/// Number of arguments in the fact.
type Length<'a>: ArrayLength<ExpectedConstantTy> + ArrayLength<values::Constant<'a>>;
type Length<'a>: ArrayLength;

fn tys<'a>(&self) -> GenericArray<ExpectedConstantTy, Self::Length<'a>>;

Expand All @@ -408,28 +434,26 @@ where
type Length<'a> = typenum::U1;

fn tys<'a>(&self) -> GenericArray<ExpectedConstantTy, Self::Length<'a>> {
generic_array::arr![ExpectedConstantTy; self.ty()]
generic_array::arr![self.ty()]
}

fn is_match<'a>(&self, other: &GenericArray<values::Constant<'a>, Self::Length<'a>>) -> bool {
self.is_match(other[0])
}
}

impl<'b, T, L> QueryArgs for GenericArray<T, L>
impl<T, L> QueryArgs for GenericArray<T, L>
where
T: QueryArg,
L: ArrayLength<T> + ArrayLength<ExpectedConstantTy> + ArrayLength<values::Constant<'b>>,
L: ArrayLength,
{
type Length<'a> = typenum::U1;

fn tys<'a>(&self) -> GenericArray<ExpectedConstantTy, Self::Length<'a>> {
use typenum::Unsigned;

GenericArray::from_exact_iter(
core::iter::repeat(self[0].ty()).take(Self::Length::to_usize()),
)
.unwrap()
GenericArray::try_from_iter(core::iter::repeat(self[0].ty()).take(Self::Length::to_usize()))
.unwrap()
}

fn is_match<'a>(&self, other: &GenericArray<values::Constant<'a>, Self::Length<'a>>) -> bool {
Expand Down Expand Up @@ -461,7 +485,7 @@ macro_rules! impl_query_result {
#[allow(non_snake_case)]
let ($([<a_ $I>],)+) = self;

generic_array::arr![ExpectedConstantTy; $([<a_ $I>].ty()),+]
generic_array::arr![$([<a_ $I>].ty()),+]
}

fn is_match<'a>(&self, other: &GenericArray<values::Constant<'a>, Self::Length<'a>>) -> bool {
Expand Down Expand Up @@ -521,6 +545,12 @@ impl Value for Vec<u8> {
}
}

impl<'a> Value for &'a Vec<u8> {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}
}

impl<'a> Value for Cow<'a, [u8]> {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
Expand All @@ -539,6 +569,12 @@ impl Value for String {
}
}

impl<'a> Value for &'a String {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
}
}

impl<'a> Value for Cow<'a, str> {
fn supported_ty() -> ExpectedConstantTy {
ExpectedConstantTy::Bytes
Expand All @@ -552,7 +588,7 @@ impl<'a> Value for values::Constant<'a> {
}

pub trait TryFromConstantArray<'a>: Sized {
type Length: ArrayLength<ExpectedConstantTy> + ArrayLength<values::Constant<'a>>;
type Length: ArrayLength;

type Error;

Expand All @@ -570,14 +606,14 @@ pub(crate) struct VoidResult<L> {

impl<'a, L> TryFromConstantArray<'a> for VoidResult<L>
where
L: ArrayLength<ExpectedConstantTy> + ArrayLength<values::Constant<'a>>,
L: ArrayLength,
{
type Length = L;

type Error = Infallible;

fn required_tys() -> GenericArray<ExpectedConstantTy, Self::Length> {
GenericArray::from_exact_iter(
GenericArray::try_from_iter(
core::iter::repeat(ExpectedConstantTy::Any).take(Self::Length::to_usize()),
)
.unwrap()
Expand All @@ -600,7 +636,7 @@ where
type Error = QueryResultError;

fn required_tys() -> GenericArray<ExpectedConstantTy, Self::Length> {
arr![ExpectedConstantTy; T::supported_ty()]
arr![T::supported_ty()]
}

fn try_from_constants(
Expand All @@ -622,14 +658,14 @@ impl<'a, T, E, L> TryFromConstantArray<'a> for GenericArray<T, L>
where
T: TryFrom<values::Constant<'a>, Error = E> + Value,
QueryResultError: From<E>,
L: ArrayLength<ExpectedConstantTy> + ArrayLength<values::Constant<'a>> + ArrayLength<T>,
L: ArrayLength,
{
type Length = L;

type Error = QueryResultError;

fn required_tys() -> GenericArray<ExpectedConstantTy, Self::Length> {
GenericArray::from_exact_iter(
GenericArray::try_from_iter(
core::iter::repeat(T::supported_ty()).take(Self::Length::to_usize()),
)
.unwrap()
Expand Down Expand Up @@ -683,7 +719,7 @@ where
ty: PhantomData,
};

if let Some(arr) = GenericArray::from_exact_iter(iter) {
if let Ok(arr) = GenericArray::try_from_iter(iter) {
return Ok(arr);
}

Expand Down Expand Up @@ -712,7 +748,7 @@ macro_rules! impl_try_from_constant_array_tuple {
type Error = QueryResultError;

fn required_tys() -> GenericArray<ExpectedConstantTy, Self::Length> {
generic_array::arr![ExpectedConstantTy; $($I::supported_ty()),+]
generic_array::arr![$($I::supported_ty()),+]
}

fn try_from_constants(
Expand Down

0 comments on commit f40a2a1

Please sign in to comment.