Skip to content

Commit

Permalink
add trait bounds to loader factory constructors
Browse files Browse the repository at this point in the history
I don't like being able to create something called XLoaderFactory
that would not even implement LoaderFactory.

Also
- simplified phantom member type
- made new_cloning implemented on a simpler, non ambiguous, type.
  • Loading branch information
pchampin committed Dec 19, 2023
1 parent 436e0e8 commit 54a84e6
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions jsonld/src/loader_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ pub trait LoaderFactory {
/// A loader factory that yields default loaders.
#[derive(Debug, Clone, Default)]
pub struct DefaultLoaderFactory<L> {
_phantom: PhantomData<fn() -> L>,
_phantom: PhantomData<L>,
}

impl<L: Default> DefaultLoaderFactory<L> {
impl<L> DefaultLoaderFactory<L>
where
L: Loader<Iri<Arc<str>>, Location<Iri<Arc<str>>>, Output = Value<Location<Iri<Arc<str>>>>>
+ Default
+ Send
+ Sync,
L::Error: Display + Send,
{
/// Create a new [`DefaultLoaderFactory`].
#[inline]
pub fn new() -> Self {
Expand Down Expand Up @@ -70,7 +77,14 @@ pub struct ClosureLoaderFactory<L, F> {
_phantom: PhantomData<fn() -> L>,
}

impl<L, F> ClosureLoaderFactory<L, F> {
impl<L, F> ClosureLoaderFactory<L, F>
where
L: Loader<Iri<Arc<str>>, Location<Iri<Arc<str>>>, Output = Value<Location<Iri<Arc<str>>>>>
+ Send
+ Sync,
L::Error: Display + Send,
F: Fn() -> L,
{
/// Create a new [`ClosureLoaderFactory`] with given closure.
#[inline]
pub fn new(closure: F) -> Self {
Expand All @@ -79,7 +93,15 @@ impl<L, F> ClosureLoaderFactory<L, F> {
_phantom: PhantomData,
}
}
}

impl<L> ClosureLoaderFactory<L, fn() -> L>
where
L: Loader<Iri<Arc<str>>, Location<Iri<Arc<str>>>, Output = Value<Location<Iri<Arc<str>>>>>
+ Send
+ Sync,
L::Error: Display + Send,
{
/// Create a new [`ClosureLoaderFactory`] that yields loaders by cloning a template loader.
#[inline]
pub fn new_cloning(template_loader: L) -> ClosureLoaderFactory<L, impl Fn() -> L>
Expand Down

0 comments on commit 54a84e6

Please sign in to comment.