Skip to content

Commit cf0fe7f

Browse files
authored
feat: integrate with some third-party library (#6)
Signed-off-by: tison <wander4096@gmail.com>
1 parent db8aec9 commit cf0fe7f

File tree

8 files changed

+303
-0
lines changed

8 files changed

+303
-0
lines changed

Cargo.lock

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

visitor/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ rust-version.workspace = true
2626
[features]
2727
default = []
2828
derive = ["visitor-derive"]
29+
30+
# Implement Traversal for standard library types.
2931
std = []
3032

3133
# Visit all trivial types.
@@ -45,8 +47,16 @@ traverse-trivial = []
4547
# traversed via their contained types.
4648
traverse-std = []
4749

50+
# Implement Traversal for third-party library types.
51+
ordered-float-5 = ["dep:ordered-float-5"]
52+
stacksafe-01 = ["dep:stacksafe-01"]
53+
4854
[dependencies]
4955
visitor-derive = { workspace = true, optional = true }
5056

57+
# Optional dependencies for third-party library support
58+
ordered-float-5 = { version = "5.1", default-features = false, optional = true, package = "ordered-float" }
59+
stacksafe-01 = { version = "0.1", default-features = false, optional = true, package = "stacksafe" }
60+
5161
[lints]
5262
workspace = true
File renamed without changes.

visitor/src/impls/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#[cfg(feature = "ordered-float-5")]
16+
mod ordered_float_5;
17+
#[cfg(feature = "stacksafe-01")]
18+
mod stacksafe_01;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::ops::ControlFlow;
16+
17+
use ordered_float_5::OrderedFloat;
18+
19+
use crate::Traversable;
20+
use crate::TraversableMut;
21+
use crate::Visitor;
22+
use crate::VisitorMut;
23+
24+
impl<T: 'static> Traversable for OrderedFloat<T> {
25+
fn traverse<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
26+
visitor.enter(self)?;
27+
visitor.leave(self)?;
28+
ControlFlow::Continue(())
29+
}
30+
}
31+
32+
impl<T: 'static> TraversableMut for OrderedFloat<T> {
33+
fn traverse_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
34+
visitor.enter_mut(self)?;
35+
visitor.leave_mut(self)?;
36+
ControlFlow::Continue(())
37+
}
38+
}

visitor/src/impls/stacksafe_01.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2025 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::ops::ControlFlow;
16+
17+
use stacksafe_01::StackSafe;
18+
19+
use crate::Traversable;
20+
use crate::TraversableMut;
21+
22+
impl<T: Traversable> Traversable for StackSafe<T> {
23+
fn traverse<V: crate::Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
24+
(**self).traverse(visitor)
25+
}
26+
}
27+
28+
impl<T: TraversableMut> TraversableMut for StackSafe<T> {
29+
fn traverse_mut<V: crate::VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> {
30+
(**self).traverse_mut(visitor)
31+
}
32+
}

visitor/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub use visitor_derive::TraversableMut;
3232

3333
pub mod function;
3434

35+
/// Implementations for third-party library types.
36+
mod impls;
37+
3538
/// A visitor that can be used to traverse a data structure.
3639
pub trait Visitor {
3740
/// The type that can be used to break traversal early.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2025 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#![cfg(all(feature = "derive", feature = "ordered-float-5"))]
16+
17+
use std::any::Any;
18+
use std::ops::ControlFlow;
19+
20+
use ordered_float_5::OrderedFloat;
21+
use visitor::Traversable as _;
22+
use visitor::TraversableMut as _;
23+
use visitor::Visitor;
24+
use visitor::VisitorMut;
25+
use visitor_derive::Traversable;
26+
use visitor_derive::TraversableMut;
27+
28+
#[test]
29+
fn test_ordered_float() {
30+
#[derive(Debug, Clone, PartialEq, Eq, Traversable, TraversableMut)]
31+
pub enum Literal {
32+
Null,
33+
Float(OrderedFloat<f64>),
34+
}
35+
36+
struct FloatExpr(bool);
37+
38+
impl Visitor for FloatExpr {
39+
type Break = ();
40+
41+
fn enter(&mut self, this: &dyn Any) -> ControlFlow<Self::Break> {
42+
if let Some(Literal::Float(_)) = this.downcast_ref::<Literal>() {
43+
self.0 = true;
44+
}
45+
ControlFlow::Continue(())
46+
}
47+
}
48+
49+
impl VisitorMut for FloatExpr {
50+
type Break = ();
51+
52+
fn enter_mut(&mut self, this: &mut dyn Any) -> ControlFlow<Self::Break> {
53+
if let Some(Literal::Float(_)) = this.downcast_ref::<Literal>() {
54+
self.0 = true;
55+
}
56+
ControlFlow::Continue(())
57+
}
58+
}
59+
60+
assert!({
61+
let mut visitor = FloatExpr(false);
62+
let _ = Literal::Null.traverse(&mut visitor);
63+
!visitor.0
64+
});
65+
66+
assert!({
67+
let mut visitor = FloatExpr(false);
68+
let _ = Literal::Null.traverse_mut(&mut visitor);
69+
!visitor.0
70+
});
71+
72+
assert!({
73+
let mut visitor = FloatExpr(false);
74+
let _ = Literal::Float(OrderedFloat(0.0)).traverse(&mut visitor);
75+
visitor.0
76+
});
77+
78+
assert!({
79+
let mut visitor = FloatExpr(false);
80+
let _ = Literal::Float(OrderedFloat(0.0)).traverse_mut(&mut visitor);
81+
visitor.0
82+
});
83+
}

0 commit comments

Comments
 (0)