Skip to content
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
46 changes: 46 additions & 0 deletions traversable/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,53 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[allow(unused_macros)]
macro_rules! blank_traverse_impl {
( $type:ty ) => {
impl Traversable for $type {
#[inline]
fn traverse<V: Visitor>(&self, _visitor: &mut V) -> ControlFlow<V::Break> {
ControlFlow::Continue(())
}
}

impl TraversableMut for $type {
#[inline]
fn traverse_mut<V: VisitorMut>(&mut self, _visitor: &mut V) -> ControlFlow<V::Break> {
ControlFlow::Continue(())
}
}
};
}

#[allow(unused_macros)]
macro_rules! trivial_traverse_impl {
( $type:ty ) => {
impl Traversable for $type {
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
visitor.enter(self)?;
visitor.leave(self)?;
ControlFlow::Continue(())
}
}

impl TraversableMut for $type {
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
visitor.enter_mut(self)?;
visitor.leave_mut(self)?;
ControlFlow::Continue(())
}
}
};
}

#[cfg(feature = "ordered-float-5")]
mod ordered_float_5;
#[cfg(feature = "stacksafe-1")]
mod stacksafe_1;
#[cfg(feature = "std")]
mod std_container;
#[cfg(feature = "std")]
mod std_primary;
mod trivial;
mod tuple;
202 changes: 202 additions & 0 deletions traversable/src/impls/std_container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright 2025 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use core::ops::ControlFlow;
use std::boxed::Box;
use std::cell::Cell;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;

use crate::Traversable;
use crate::TraversableMut;
use crate::Visitor;
use crate::VisitorMut;

trait DerefAndTraverse {
fn deref_and_traverse<V: Visitor>(self, visitor: &mut V) -> ControlFlow<V::Break>;
}

trait DerefAndTraverseMut {
fn deref_and_traverse_mut<V: VisitorMut>(self, visitor: &mut V) -> ControlFlow<V::Break>;
}

impl<T: Traversable> DerefAndTraverse for &T {
fn deref_and_traverse<V: Visitor>(self, visitor: &mut V) -> ControlFlow<V::Break> {
self.traverse(visitor)
}
}

impl<T: TraversableMut> DerefAndTraverseMut for &mut T {
fn deref_and_traverse_mut<V: VisitorMut>(self, visitor: &mut V) -> ControlFlow<V::Break> {
self.traverse_mut(visitor)
}
}

impl<TK: Traversable, TV: Traversable> DerefAndTraverse for (&TK, &TV) {
fn deref_and_traverse<V: Visitor>(self, visitor: &mut V) -> ControlFlow<V::Break> {
self.0.traverse(visitor)?;
self.1.traverse(visitor)?;
ControlFlow::Continue(())
}
}

impl<TK, TV: TraversableMut> DerefAndTraverseMut for (TK, &mut TV) {
fn deref_and_traverse_mut<V: VisitorMut>(self, visitor: &mut V) -> ControlFlow<V::Break> {
self.1.traverse_mut(visitor)
}
}

macro_rules! impl_drive_for_into_iterator {
( $type:ty ; $($generics:tt)+ ) => {
impl< $($generics)+ > Traversable for $type
where
$type: 'static,
for<'a> &'a $type: IntoIterator,
for<'a> <&'a $type as IntoIterator>::Item: DerefAndTraverse,
{
#[allow(for_loops_over_fallibles)]
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
for item in self {
item.deref_and_traverse(visitor)?;
}
ControlFlow::Continue(())
}
}

impl< $($generics)+ > TraversableMut for $type
where
$type: 'static,
for<'a> &'a mut $type: IntoIterator,
for<'a> <&'a mut $type as IntoIterator>::Item: DerefAndTraverseMut,
{
#[allow(for_loops_over_fallibles)]
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
for item in self {
item.deref_and_traverse_mut(visitor)?;
}
ControlFlow::Continue(())
}
}
};
}

impl_drive_for_into_iterator! { [T] ; T }
impl_drive_for_into_iterator! { [T; N] ; T, const N: usize }
impl_drive_for_into_iterator! { std::vec::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! { std::collections::BTreeMap<T, U> ; T, U }
impl_drive_for_into_iterator! { std::collections::HashMap<T, U> ; T, U }
impl_drive_for_into_iterator! { Option<T> ; T }
impl_drive_for_into_iterator! { Result<T, U> ; T, U }

impl<T: Traversable> Traversable for Box<T> {
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
(**self).traverse(visitor)
}
}

impl<T: TraversableMut> TraversableMut for Box<T> {
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
(**self).traverse_mut(visitor)
}
}

impl<T: Traversable> Traversable for Arc<T> {
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
(**self).traverse(visitor)
}
}

impl<T> Traversable for Mutex<T>
where
T: Traversable,
{
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
let lock = self.lock().unwrap();
lock.traverse(visitor)
}
}

impl<T> TraversableMut for Mutex<T>
where
T: TraversableMut,
{
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
let lock = self.get_mut().unwrap();
lock.traverse_mut(visitor)
}
}

impl<T> Traversable for RwLock<T>
where
T: Traversable,
{
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
let lock = self.read().unwrap();
lock.traverse(visitor)
}
}

impl<T> TraversableMut for RwLock<T>
where
T: TraversableMut,
{
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
let lock = self.get_mut().unwrap();
lock.traverse_mut(visitor)
}
}

impl<T> TraversableMut for Arc<Mutex<T>>
where
T: TraversableMut,
{
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
let mut lock = self.lock().unwrap();
lock.traverse_mut(visitor)
}
}

impl<T> TraversableMut for Arc<RwLock<T>>
where
T: TraversableMut,
{
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
let mut lock = self.write().unwrap();
lock.traverse_mut(visitor)
}
}

impl<T> Traversable for Cell<T>
where
T: Traversable + Copy,
{
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
self.get().traverse(visitor)
}
}

impl<T> TraversableMut for Cell<T>
where
T: TraversableMut,
{
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
self.get_mut().traverse_mut(visitor)
}
}
37 changes: 37 additions & 0 deletions traversable/src/impls/std_primary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2025 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use core::ops::ControlFlow;
use std::string::String;

use crate::Traversable;
use crate::TraversableMut;
use crate::Visitor;
use crate::VisitorMut;

#[cfg(not(feature = "traverse-std"))]
macro_rules! std_primary_impl {
( $type:ty ) => {
blank_traverse_impl!($type);
};
}

#[cfg(feature = "traverse-std")]
macro_rules! std_primary_impl {
( $type:ty ) => {
trivial_traverse_impl!($type);
};
}

std_primary_impl!(String);
56 changes: 56 additions & 0 deletions traversable/src/impls/trivial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2025 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use core::ops::ControlFlow;

use crate::Traversable;
use crate::TraversableMut;
use crate::Visitor;
use crate::VisitorMut;

#[cfg(not(feature = "traverse-trivial"))]
macro_rules! trivial_impl {
( $type:ty ) => {
blank_traverse_impl!($type);
};
}

#[cfg(feature = "traverse-trivial")]
macro_rules! trivial_impl {
( $type:ty ) => {
trivial_traverse_impl!($type);
};
}

trivial_impl!(());

trivial_impl!(u8);
trivial_impl!(u16);
trivial_impl!(u32);
trivial_impl!(u64);
trivial_impl!(u128);
trivial_impl!(usize);

trivial_impl!(i8);
trivial_impl!(i16);
trivial_impl!(i32);
trivial_impl!(i64);
trivial_impl!(i128);
trivial_impl!(isize);

trivial_impl!(f32);
trivial_impl!(f64);

trivial_impl!(char);
trivial_impl!(bool);
Loading