Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Drive for concrete container types #17

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion derive-visitor-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "derive-visitor-macros"
description = "Macros for derive-visitor package"
categories = ["development-tools"]
version = "0.3.0"
version = "0.4.0"
authors = ["Kit Isaev <[email protected]>"]
license = "MIT"
documentation = "https://docs.rs/derive-visitor"
Expand Down
4 changes: 2 additions & 2 deletions derive-visitor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "derive-visitor"
description = "Derive visitor pattern for arbitrary data structures"
categories = ["development-tools"]
version = "0.3.0"
version = "0.4.0"
authors = ["Kit Isaev <[email protected]>"]
license = "MIT"
edition = "2018"
Expand All @@ -13,7 +13,7 @@ keywords = ["visitor", "derive", "macro"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive-visitor-macros = { version = "0.3.0", path = "../derive-visitor-macros" }
derive-visitor-macros = { version = "0.4.0", path = "../derive-visitor-macros" }

[features]
std-types-drive = []
68 changes: 40 additions & 28 deletions derive-visitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub enum Event {
Exit,
}

/// A data structure that can drive a [visitor](Visitor) through iself.
/// A data structure that can drive a [visitor](Visitor) through itself.
///
/// Derive or implement this trait for any type that you want to be able to
/// traverse with a visitor.
Expand Down Expand Up @@ -531,38 +531,50 @@ impl<TK, TV: DriveMut> DerefAndDriveMut for (TK, &mut TV) {
}
}

// The following should be able to handle almost all container types:
// array, BTreeMap, BTreeSet, BinaryHeap, slice, HashMap, HashSet, LinkedList, Vec, VecDeque
//
// It even handles Option, since it has a trivial IntoIterator impl.
impl<T> Drive for T
where
T: 'static,
// HRTB, because this needs to be true for late-bound lifetimes,
// as both &self and &visitor have anonymous lifetimes.
for<'a> &'a T: IntoIterator,
for<'a> <&'a T as IntoIterator>::Item: DerefAndDrive,
{
fn drive<V: Visitor>(&self, visitor: &mut V) {
for item in self {
item.deref_and_drive(visitor);
// Implement Drive and DriveMut for container types in standard library.
macro_rules! impl_drive_for_into_iterator {
( $type:ty ; $($generics:tt)+ ) => {
impl< $($generics)+ > Drive for $type
where
$type: 'static,
for<'a> &'a $type: IntoIterator,
for<'a> <&'a $type as IntoIterator>::Item: DerefAndDrive,
{
fn drive<V: Visitor>(&self, visitor: &mut V) {
for item in self {
item.deref_and_drive(visitor);
}
}
}
}
}

impl<T> DriveMut for T
where
T: 'static,
for<'a> &'a mut T: IntoIterator,
for<'a> <&'a mut T as IntoIterator>::Item: DerefAndDriveMut,
{
fn drive_mut<V: VisitorMut>(&mut self, visitor: &mut V) {
for item in self {
item.deref_and_drive_mut(visitor);
impl< $($generics)+ > DriveMut for $type
where
$type: 'static,
for<'a> &'a mut $type: IntoIterator,
for<'a> <&'a mut $type as IntoIterator>::Item: DerefAndDriveMut,
{
fn drive_mut<V: VisitorMut>(&mut self, visitor: &mut V) {
for item in self {
item.deref_and_drive_mut(visitor);
}
}
}
}
};
}

impl_drive_for_into_iterator! { [T] ; T }
impl_drive_for_into_iterator! { Vec<T> ; T }
impl_drive_for_into_iterator! { std::collections::BTreeSet<T> ; T }
impl_drive_for_into_iterator! { std::collections::BinaryHeap<T> ; T }
impl_drive_for_into_iterator! { std::collections::HashSet<T> ; T }
impl_drive_for_into_iterator! { std::collections::LinkedList<T> ; T }
impl_drive_for_into_iterator! { std::collections::VecDeque<T> ; T }
impl_drive_for_into_iterator! { Option<T> ; T }
impl_drive_for_into_iterator! { Result<T, U> ; T, U }
impl_drive_for_into_iterator! { std::collections::BTreeMap<T, U> ; T, U }
impl_drive_for_into_iterator! { std::collections::HashMap<T, U> ; T, U }
impl_drive_for_into_iterator! { [T; N] ; T, const N: usize }

impl<T> Drive for Box<T>
where
T: Drive,
Expand Down
Loading