-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
042aa7b
commit 155b5fa
Showing
3 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,102 @@ | ||
use std::iter::FromIterator; | ||
use std::{iter::FromIterator, marker::PhantomData}; | ||
|
||
pub trait TraversalRef { | ||
type Source; | ||
type Target; | ||
type Container: FromIterator<Self::Target> + IntoIterator<Item = Self::Target>; | ||
type Container; | ||
|
||
fn get_all_ref(&self, source: Self::Source) -> Self::Container; | ||
fn set_all_ref(&self, source: Self::Source, target: Self::Target) -> Self::Container; | ||
fn set_all_ref(&self, source: Self::Source, target: Self::Target) -> Self::Source; | ||
} | ||
|
||
pub trait TraversalMut: TraversalRef { | ||
fn get_all_mut(&self, source: Self::Source) -> Self::Container; | ||
fn set_all_mut(&self, source: Self::Source, target: Self::Target) -> Self::Container; | ||
fn get_all_mut(&mut self, source: Self::Source) -> Self::Container; | ||
fn set_all_mut(&mut self, source: Self::Source, target: Self::Target) -> Self::Source; | ||
} | ||
|
||
pub trait Traversal: TraversalMut { | ||
fn get_all(&self, source: Self::Source) -> Self::Container; | ||
fn set_all(&self, source: Self::Source, target: Self::Target) -> Self::Container; | ||
fn get_all(self, source: Self::Source) -> Self::Container; | ||
fn set_all(self, source: Self::Source, target: Self::Target) -> Self::Source; | ||
} | ||
|
||
pub struct TraversalId<I> { | ||
phantom: PhantomData<I>, | ||
} | ||
|
||
impl<I> TraversalRef for TraversalId<I> | ||
where | ||
I: FromIterator<I::Item> + IntoIterator, | ||
I::Item: Clone, | ||
{ | ||
type Source = I; | ||
type Target = I::Item; | ||
type Container = I; | ||
|
||
fn get_all_ref(&self, source: Self::Source) -> Self::Container { | ||
source | ||
} | ||
|
||
fn set_all_ref(&self, source: Self::Source, target: Self::Target) -> Self::Source { | ||
source.into_iter().map(|_| target.clone()).collect() | ||
} | ||
} | ||
|
||
impl<I> TraversalMut for TraversalId<I> | ||
where | ||
I: FromIterator<I::Item> + IntoIterator, | ||
I::Item: Clone, | ||
{ | ||
fn get_all_mut(&mut self, source: Self::Source) -> Self::Container { | ||
source | ||
} | ||
|
||
fn set_all_mut(&mut self, source: Self::Source, target: Self::Target) -> Self::Source { | ||
source.into_iter().map(|_| target.clone()).collect() | ||
} | ||
} | ||
|
||
impl<I> Traversal for TraversalId<I> | ||
where | ||
I: FromIterator<I::Item> + IntoIterator, | ||
I::Item: Clone, | ||
{ | ||
fn get_all(self, source: Self::Source) -> Self::Container { | ||
source | ||
} | ||
|
||
fn set_all(self, source: Self::Source, target: Self::Target) -> Self::Source { | ||
source.into_iter().map(|_| target.clone()).collect() | ||
} | ||
} | ||
|
||
pub struct TraversalInvariantMap<T, F, G> { | ||
traversal: T, | ||
covariant: F, | ||
contravariant: G, | ||
} | ||
|
||
impl<T, F, G, B> TraversalRef for TraversalInvariantMap<T, F, G> | ||
where | ||
T: TraversalRef, | ||
T::Container: IntoIterator<Item = T::Target> + FromIterator<B>, | ||
T::Target: Clone, | ||
F: Fn(T::Target) -> B, | ||
G: Fn(B) -> T::Target, | ||
{ | ||
type Source = T::Source; | ||
type Target = B; | ||
type Container = T::Container; | ||
|
||
fn get_all_ref(&self, source: Self::Source) -> Self::Container { | ||
self.traversal | ||
.get_all_ref(source) | ||
.into_iter() | ||
.map(|target| (self.covariant)(target)) | ||
.collect() | ||
} | ||
|
||
fn set_all_ref(&self, source: Self::Source, target: Self::Target) -> Self::Source { | ||
self.traversal | ||
.set_all_ref(source, (self.contravariant)(target)) | ||
} | ||
} |