Skip to content

Commit

Permalink
Add Send + Sync to common traits (#172)
Browse files Browse the repository at this point in the history
also fix some clippy warnings
  • Loading branch information
mkatychev authored Jul 30, 2024
1 parent b4b827a commit d5be533
Show file tree
Hide file tree
Showing 27 changed files with 97 additions and 69 deletions.
9 changes: 6 additions & 3 deletions api/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
//! for different kinds of datasets,
//! as well as a few implementations for them.
use std::error::Error;

use crate::graph::adapter::{DatasetGraph, PartialUnionGraph, UnionGraph};
use crate::quad::{iter_spog, Quad};
use crate::source::{IntoSource, QuadSource, StreamResult};
use crate::term::matcher::{GraphNameMatcher, TermMatcher};
use crate::term::{GraphName, SimpleTerm, Term};

use resiter::{filter::*, filter_map::*, flat_map::*, map::*};
use std::error::Error;

mod _foreign_impl;
pub mod adapter;
Expand Down Expand Up @@ -56,7 +58,7 @@ pub trait Dataset {
where
Self: 'x;
/// The error type that this dataset may raise.
type Error: Error + 'static;
type Error: Error + Send + Sync + 'static;

/// An iterator visiting all quads of this dataset in arbitrary order.
///
Expand Down Expand Up @@ -346,7 +348,7 @@ pub type MdResult<D, T> = std::result::Result<T, <D as MutableDataset>::Mutation
/// see also [`SetDataset`].
pub trait MutableDataset: Dataset {
/// The error type that this dataset may raise during mutations.
type MutationError: Error + 'static;
type MutationError: Error + Send + Sync + 'static;

/// Insert the given quad in this dataset.
///
Expand Down Expand Up @@ -609,6 +611,7 @@ mod check_implementability {
/// - a list of terms (either atoms or index of quad)
/// - a list of triples (SPO indexes)
/// - a list of named graphs associated the triple indexes contained in the graph
///
/// This avoids the need to store arbitrarily nested triples.
/// NB: unasserted triples are not used in any quoted graph.
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions api/src/dataset/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! I define adapters for the [`dataset`](super) related traits.
use std::error::Error;

use super::*;
use crate::graph::{GTerm, Graph, MutableGraph};
use crate::quad::Spog;
Expand Down
9 changes: 6 additions & 3 deletions api/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
//! for different kinds of graph,
//! as well as a few implementations for them.
use std::error::Error;

use crate::dataset::adapter::GraphAsDataset;
use crate::source::{IntoSource, StreamResult, TripleSource};
use crate::term::{matcher::TermMatcher, SimpleTerm, Term};
use crate::triple::Triple;

use resiter::{filter::*, flat_map::*, map::*};
use std::error::Error;

mod _foreign_impl;
pub mod adapter;
Expand Down Expand Up @@ -53,7 +55,7 @@ pub trait Graph {
where
Self: 'x;
/// The error type that this graph may raise.
type Error: Error + 'static;
type Error: Error + Send + Sync + 'static;

/// An iterator visiting all triples of this graph in arbitrary order.
///
Expand Down Expand Up @@ -305,7 +307,7 @@ pub type MgResult<G, T> = std::result::Result<T, <G as MutableGraph>::MutationEr
/// see also [`SetGraph`].
pub trait MutableGraph: Graph {
/// The error type that this graph may raise during mutations.
type MutationError: Error + 'static;
type MutationError: Error + Send + Sync + 'static;

/// Insert in this graph a triple made of the the given terms.
///
Expand Down Expand Up @@ -530,6 +532,7 @@ mod check_implementability {
/// where the graph maintains
/// - a list of terms (either atoms or index of triple)
/// - a list of triples (SPO indexes, plus an 'asserted' flag)
///
/// This avoids the need to store arbitrarily nested triples.
use super::*;
use crate::term::SimpleTerm;
Expand Down
4 changes: 2 additions & 2 deletions api/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::source::*;
/// A triple serializer writes triples according to a given format.
pub trait TripleSerializer {
/// The error type that may be raised during serialization.
type Error: 'static + std::error::Error;
type Error: std::error::Error + Send + Sync + 'static;

/// Serialize all triples from the given [`TripleSource`].
fn serialize_triples<TS>(
Expand Down Expand Up @@ -47,7 +47,7 @@ pub trait TripleSerializer {
/// A quad serializer writes quads according to a given format.
pub trait QuadSerializer {
/// The error type that may be raised during serialization.
type Error: 'static + std::error::Error;
type Error: std::error::Error + Send + Sync + 'static;

/// Serialize all quads from the given [`QuadSource`].
fn serialize_quads<QS>(
Expand Down
14 changes: 6 additions & 8 deletions api/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
//! [`for_each_quad`]: QuadSource::for_each_quad
//! [higher-rank trait bounds]: https://doc.rust-lang.org/nomicon/hrtb.html
use std::error::Error;

pub mod convert;
pub mod filter;
pub mod filter_map;
Expand All @@ -70,7 +68,7 @@ pub use _stream_error::*;
mod _triple;
pub use _triple::*;

use std::convert::Infallible;
use std::{convert::Infallible, error::Error};

/// A source produces [items](Source::Item), and may also fail in the process.
///
Expand All @@ -87,7 +85,7 @@ pub trait Source {
/// The type of items this source yields.
type Item<'x>;
/// The type of errors produced by this source.
type Error: Error + 'static;
type Error: Error + Send + Sync + 'static;

/// Call f for some item(s) (possibly zero) from this source, if any.
///
Expand All @@ -96,7 +94,7 @@ pub trait Source {
/// Return an error if either the source or `f` errs.
fn try_for_some_item<E, F>(&mut self, f: F) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E>;

/// Call f for all items from this source.
Expand All @@ -106,7 +104,7 @@ pub trait Source {
fn try_for_each_item<F, E>(&mut self, mut f: F) -> StreamResult<(), Self::Error, E>
where
F: FnMut(Self::Item<'_>) -> Result<(), E>,
E: Error,
E: Error + Send + Sync + 'static,
{
while self.try_for_some_item(&mut f)? {}
Ok(())
Expand Down Expand Up @@ -200,14 +198,14 @@ pub trait Source {
impl<'a, I, T, E> Source for I
where
I: Iterator<Item = Result<T, E>> + 'a,
E: Error + 'static,
E: Error + Send + Sync + 'static,
{
type Item<'x> = T;
type Error = E;

fn try_for_some_item<E2, F>(&mut self, mut f: F) -> StreamResult<bool, Self::Error, E2>
where
E2: Error,
E2: Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E2>,
{
match self.next() {
Expand Down
6 changes: 4 additions & 2 deletions api/src/source/_quad.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error;

use super::*;
use crate::dataset::{CollectibleDataset, Dataset, MutableDataset};
use crate::quad::Quad;
Expand All @@ -20,7 +22,7 @@ pub trait QuadSource: Source + IsQuadSource {
#[inline]
fn try_for_some_quad<E, F>(&mut self, mut f: F) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: Error + Send + Sync + 'static,
F: FnMut(Self::Quad<'_>) -> Result<(), E>,
{
self.try_for_some_item(|i| f(Self::i2q(i)))
Expand All @@ -33,7 +35,7 @@ pub trait QuadSource: Source + IsQuadSource {
fn try_for_each_quad<F, E>(&mut self, mut f: F) -> StreamResult<(), Self::Error, E>
where
F: FnMut(Self::Quad<'_>) -> Result<(), E>,
E: Error,
E: Error + Send + Sync + 'static,
{
self.try_for_each_item(|i| f(Self::i2q(i)))
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/source/_stream_error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::error::Error;

/// A error that is raised by functions that move fallible `Source`s into
/// fallible `Sinks`.
///
Expand All @@ -24,6 +22,8 @@ where
#[error("Sink failed: {0}")]
SinkError(#[source] SinkErr),
}
use std::error::Error;

pub use StreamError::*;

impl<SourceErr, SinkErr> StreamError<SourceErr, SinkErr>
Expand Down
6 changes: 4 additions & 2 deletions api/src/source/_triple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error;

use super::*;
use crate::graph::{CollectibleGraph, Graph, MutableGraph};
use crate::triple::Triple;
Expand All @@ -20,7 +22,7 @@ pub trait TripleSource: Source + IsTripleSource {
#[inline]
fn try_for_some_triple<E, F>(&mut self, mut f: F) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: Error + Send + Sync + 'static,
F: FnMut(TSTriple<Self>) -> Result<(), E>,
{
self.try_for_some_item(|i| f(Self::i2t(i)))
Expand All @@ -33,7 +35,7 @@ pub trait TripleSource: Source + IsTripleSource {
fn try_for_each_triple<F, E>(&mut self, mut f: F) -> StreamResult<(), Self::Error, E>
where
F: FnMut(TSTriple<Self>) -> Result<(), E>,
E: Error,
E: Error + Send + Sync + 'static,
{
self.try_for_each_item(|i| f(Self::i2t(i)))
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/source/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<TS: TripleSource> Source for ToQuads<TS> {

fn try_for_some_item<E, F>(&mut self, mut f: F) -> super::StreamResult<bool, Self::Error, E>
where
E: std::error::Error,
E: std::error::Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E>,
{
self.0.try_for_some_triple(|t| {
Expand All @@ -40,7 +40,7 @@ impl<QS: QuadSource> Source for ToTriples<QS> {

fn try_for_some_item<E, F>(&mut self, mut f: F) -> super::StreamResult<bool, Self::Error, E>
where
E: std::error::Error,
E: std::error::Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E>,
{
self.0.try_for_some_quad(|q| {
Expand Down
10 changes: 5 additions & 5 deletions api/src/source/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where

fn try_for_some_item<E, F>(&mut self, mut f: F) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: std::error::Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E>,
{
let p = &mut self.predicate;
Expand Down Expand Up @@ -55,7 +55,7 @@ mod _triple {

fn try_for_some_item<E, F>(&mut self, mut f: F) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: std::error::Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E>,
{
self.0.try_for_some_item(|i| f(S::i2t(i)))
Expand Down Expand Up @@ -84,7 +84,7 @@ mod _quad {

fn try_for_some_item<E, F>(&mut self, mut f: F) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: std::error::Error + Send + Sync + 'static,
F: FnMut(Self::Item<'_>) -> Result<(), E>,
{
self.0.try_for_some_item(|i| f(S::i2q(i)))
Expand Down Expand Up @@ -130,7 +130,7 @@ mod test {
];
let mut h: Vec<[SimpleTerm; 3]> = vec![];
g.triples()
.filter_triples(|t| !Term::eq(t.p(), &ez_term(":e")))
.filter_triples(|t| !Term::eq(t.p(), ez_term(":e")))
.for_each_triple(|t| {
h.insert_triple(t).unwrap();
})
Expand All @@ -153,7 +153,7 @@ mod test {
];
let mut h: Vec<Spog<SimpleTerm>> = vec![];
d.quads()
.filter_quads(|q| !Term::eq(q.p(), &ez_term(":e")))
.filter_quads(|q| !Term::eq(q.p(), ez_term(":e")))
.for_each_quad(|q| {
h.insert_quad(q).unwrap();
})
Expand Down
4 changes: 2 additions & 2 deletions api/src/source/filter_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! I define [`FilterMapSource`] the result type of [`Source::filter_map_items`].
use super::*;
use std::collections::VecDeque;
use std::{collections::VecDeque, error::Error};

/// The result of [`Source::filter_map_items`].
pub struct FilterMapSource<S, F> {
Expand All @@ -18,7 +18,7 @@ where

fn try_for_some_item<E, F2>(&mut self, mut f: F2) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: Error + Send + Sync + 'static,
F2: FnMut(Self::Item<'_>) -> Result<(), E>,
{
let filter_map = &mut self.filter_map;
Expand Down
4 changes: 2 additions & 2 deletions api/src/source/map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! I define [`MapSource`], the result type of [`Source::map_items`].
use super::*;
use std::collections::VecDeque;
use std::{collections::VecDeque, error::Error};

/// The result of [`Source::map_items`].
pub struct MapSource<S, F> {
Expand All @@ -18,7 +18,7 @@ where

fn try_for_some_item<E, F2>(&mut self, mut f: F2) -> StreamResult<bool, Self::Error, E>
where
E: Error,
E: Error + Send + Sync + 'static,
F2: FnMut(Self::Item<'_>) -> Result<(), E>,
{
let map = &mut self.map;
Expand Down
5 changes: 3 additions & 2 deletions api/src/sparql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use crate::source::TripleSource;
use crate::term::Term;

use std::borrow::Borrow;
use std::error::Error;

Expand All @@ -41,7 +42,7 @@ pub trait SparqlDataset {
/// The type of triples that GRAPH and DESCRIBE queries will return.
type TriplesResult: TripleSource;
/// The type of errors that processing SPARQL queries may raise.
type SparqlError: Error + 'static;
type SparqlError: Error + Send + Sync + 'static;
/// The type representing pre-processed queries.
///
/// See [`prepare_query`](#tymethod.prepare_query) for more detail.
Expand Down Expand Up @@ -81,7 +82,7 @@ pub trait SparqlDataset {
/// [`prepare_query`](SparqlDataset::prepare_query) method.
pub trait Query: Sized {
/// The error type that might be raised when parsing a query.
type Error: Error + 'static;
type Error: Error + Send + Sync + 'static;
/// Parse the given text into a [`Query`].
fn parse(query_source: &str) -> Result<Self, Self::Error>;
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ pub trait FromTerm: Sized {
/// See also [`FromTerm`]
pub trait TryFromTerm: Sized {
/// The error type produced when failing to copy a given term
type Error: 'static + std::error::Error;
type Error: std::error::Error + Send + Sync + 'static;
/// Try to copy `term` into an instance of this type.
fn try_from_term<T: Term>(term: T) -> Result<Self, Self::Error>;
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/term/_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn ensure_owned(m: MownStr) -> MownStr<'static> {
let m = m.clone();
// Safety: the transmute bellow is safe, because if m.is_owned() is true,
// then it's data is not restricted to lifetime 'a.
unsafe { std::mem::transmute(m) }
unsafe { std::mem::transmute::<mownstr::MownStr<'_>, mownstr::MownStr<'_>>(m) }
} else {
m.to_string().into()
}
Expand Down
2 changes: 1 addition & 1 deletion c14n/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use thiserror::Error;

/// Canonicalization error.
#[derive(Debug, Error)]
pub enum C14nError<E: std::error::Error> {
pub enum C14nError<E: std::error::Error + Send + Sync + 'static> {
/// The dataset raised an error during canonicalization
#[error("Error from dataset: {0}")]
Dataset(#[from] E),
Expand Down
Loading

0 comments on commit d5be533

Please sign in to comment.