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
119 changes: 119 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions visitor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ rust-version.workspace = true
[features]
default = []
derive = ["visitor-derive"]

# Implement Traversal for standard library types.
std = []

# Visit all trivial types.
Expand All @@ -45,8 +47,16 @@ traverse-trivial = []
# traversed via their contained types.
traverse-std = []

# Implement Traversal for third-party library types.
ordered-float-5 = ["dep:ordered-float-5"]
stacksafe-01 = ["dep:stacksafe-01"]

[dependencies]
visitor-derive = { workspace = true, optional = true }

# Optional dependencies for third-party library support
ordered-float-5 = { version = "5.1", default-features = false, optional = true, package = "ordered-float" }
stacksafe-01 = { version = "0.1", default-features = false, optional = true, package = "stacksafe" }

[lints]
workspace = true
File renamed without changes.
18 changes: 18 additions & 0 deletions visitor/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.

#[cfg(feature = "ordered-float-5")]
mod ordered_float_5;
#[cfg(feature = "stacksafe-01")]
mod stacksafe_01;
38 changes: 38 additions & 0 deletions visitor/src/impls/ordered_float_5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 std::ops::ControlFlow;

use ordered_float_5::OrderedFloat;

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

impl<T: 'static> Traversable for OrderedFloat<T> {
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
visitor.enter(self)?;
visitor.leave(self)?;
ControlFlow::Continue(())
}
}

impl<T: 'static> TraversableMut for OrderedFloat<T> {
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
visitor.enter_mut(self)?;
visitor.leave_mut(self)?;
ControlFlow::Continue(())
}
}
32 changes: 32 additions & 0 deletions visitor/src/impls/stacksafe_01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 std::ops::ControlFlow;

use stacksafe_01::StackSafe;

use crate::Traversable;
use crate::TraversableMut;

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

impl<T: TraversableMut> TraversableMut for StackSafe<T> {
fn traverse_mut<V: crate::VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
(**self).traverse_mut(visitor)
}
}
3 changes: 3 additions & 0 deletions visitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub use visitor_derive::TraversableMut;

pub mod function;

/// Implementations for third-party library types.
mod impls;

/// A visitor that can be used to traverse a data structure.
pub trait Visitor {
/// The type that can be used to break traversal early.
Expand Down
83 changes: 83 additions & 0 deletions visitor/tests/test_ordered_float_5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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.

#![cfg(all(feature = "derive", feature = "ordered-float-5"))]

use std::any::Any;
use std::ops::ControlFlow;

use ordered_float_5::OrderedFloat;
use visitor::Traversable as _;
use visitor::TraversableMut as _;
use visitor::Visitor;
use visitor::VisitorMut;
use visitor_derive::Traversable;
use visitor_derive::TraversableMut;

#[test]
fn test_ordered_float() {
#[derive(Debug, Clone, PartialEq, Eq, Traversable, TraversableMut)]
pub enum Literal {
Null,
Float(OrderedFloat<f64>),
}

struct FloatExpr(bool);

impl Visitor for FloatExpr {
type Break = ();

fn enter(&mut self, this: &dyn Any) -> ControlFlow<Self::Break> {
if let Some(Literal::Float(_)) = this.downcast_ref::<Literal>() {
self.0 = true;
}
ControlFlow::Continue(())
}
}

impl VisitorMut for FloatExpr {
type Break = ();

fn enter_mut(&mut self, this: &mut dyn Any) -> ControlFlow<Self::Break> {
if let Some(Literal::Float(_)) = this.downcast_ref::<Literal>() {
self.0 = true;
}
ControlFlow::Continue(())
}
}

assert!({
let mut visitor = FloatExpr(false);
let _ = Literal::Null.traverse(&mut visitor);
!visitor.0
});

assert!({
let mut visitor = FloatExpr(false);
let _ = Literal::Null.traverse_mut(&mut visitor);
!visitor.0
});

assert!({
let mut visitor = FloatExpr(false);
let _ = Literal::Float(OrderedFloat(0.0)).traverse(&mut visitor);
visitor.0
});

assert!({
let mut visitor = FloatExpr(false);
let _ = Literal::Float(OrderedFloat(0.0)).traverse_mut(&mut visitor);
visitor.0
});
}
Loading