Skip to content

Commit dc0269e

Browse files
authored
Merge pull request #161 from tisonkun/derive-visitor
feat: integrate with derive-visitor
2 parents 6c47902 + 0e7da65 commit dc0269e

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rust-version = "1.60"
1818
arbitrary = { version = "1.0.0", optional = true }
1919
borsh = { version = "1.2.0", optional = true, default-features = false }
2020
bytemuck = { version = "1.12.2", optional = true, default-features = false }
21+
derive-visitor = { version = "0.4.0", optional = true }
2122
num-cmp = { version = "0.1.0", optional = true }
2223
num-traits = { version = "0.2.1", default-features = false }
2324
proptest = { version = "1.0.0", optional = true }

src/lib.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,71 @@ fn canonicalize_signed_zero<T: FloatCore>(x: T) -> T {
105105
#[repr(transparent)]
106106
pub struct OrderedFloat<T>(pub T);
107107

108+
#[cfg(feature = "derive-visitor")]
109+
mod impl_derive_visitor {
110+
use crate::OrderedFloat;
111+
use derive_visitor::{Drive, DriveMut, Event, Visitor, VisitorMut};
112+
113+
impl<T: 'static> Drive for OrderedFloat<T> {
114+
fn drive<V: Visitor>(&self, visitor: &mut V) {
115+
visitor.visit(self, Event::Enter);
116+
visitor.visit(self, Event::Exit);
117+
}
118+
}
119+
120+
impl<T: 'static> DriveMut for OrderedFloat<T> {
121+
fn drive_mut<V: VisitorMut>(&mut self, visitor: &mut V) {
122+
visitor.visit(self, Event::Enter);
123+
visitor.visit(self, Event::Exit);
124+
}
125+
}
126+
127+
#[test]
128+
pub fn test_derive_visitor() {
129+
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
130+
pub enum Literal {
131+
Null,
132+
Float(OrderedFloat<f64>),
133+
}
134+
135+
#[derive(Visitor, VisitorMut)]
136+
#[visitor(Literal(enter))]
137+
struct FloatExpr(bool);
138+
139+
impl FloatExpr {
140+
fn enter_literal(&mut self, lit: &Literal) {
141+
if let Literal::Float(_) = lit {
142+
self.0 = true;
143+
}
144+
}
145+
}
146+
147+
assert!({
148+
let mut visitor = FloatExpr(false);
149+
Literal::Null.drive(&mut visitor);
150+
!visitor.0
151+
});
152+
153+
assert!({
154+
let mut visitor = FloatExpr(false);
155+
Literal::Null.drive_mut(&mut visitor);
156+
!visitor.0
157+
});
158+
159+
assert!({
160+
let mut visitor = FloatExpr(false);
161+
Literal::Float(OrderedFloat(0.0)).drive(&mut visitor);
162+
visitor.0
163+
});
164+
165+
assert!({
166+
let mut visitor = FloatExpr(false);
167+
Literal::Float(OrderedFloat(0.0)).drive_mut(&mut visitor);
168+
visitor.0
169+
});
170+
}
171+
}
172+
108173
#[cfg(feature = "num-cmp")]
109174
mod impl_num_cmp {
110175
use super::OrderedFloat;

0 commit comments

Comments
 (0)