Seven-Streams commented on code in PR #794:
URL: https://github.com/apache/tvm-ffi/pull/794#discussion_r4019750131


##########
rust/tvm-ffi/tests/test_structural_visit.rs:
##########
@@ -23,13 +23,133 @@ use tvm_ffi::{
     dispatch, get_type_attr, structural_visit, structural_walk, Any, Array, 
DLDataType,
     DLDataTypeCode, DefRegionKind, Error, FieldGetter, Function, Map, Object, 
ObjectRefCore,
     Result, String as FfiString, StructuralVisitor, TypeIndex, VisitCallbacks, 
VisitContext,
-    VisitInterrupt, VisitValue, WalkOrder, WalkResult, RUNTIME_ERROR,
+    VisitInterrupt, VisitLayer, VisitValue, WalkDispatch, WalkOrder, 
WalkResult, WalkWithLayer,
+    RUNTIME_ERROR,
 };
 
 fn runtime_error(message: &str) -> Error {
     Error::new(RUNTIME_ERROR, message, "")
 }
 
+#[test]

Review Comment:
   add a dispatch test



##########
rust/tvm-ffi/src/extra/structural_visit/layer.rs:
##########
@@ -0,0 +1,320 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+//! Reusable customization of default structural descent.
+
+use super::*;
+
+/// A reusable default-recursion policy sharing state with traversal callbacks.
+///
+/// `visit_children()` on this layer's context continues with the next layer
+/// (or the built-in hooks and reflected fields). `visit()` re-enters the full
+/// callback engine for a child. A tuple `(outer, inner)` composes two layers;
+/// tuples may nest. Layers are shared during recursive calls, so mutable data
+/// belongs in the context's state.
+///
+/// This layer tracks array nesting for both visit callbacks and a walk
+/// dispatcher. The current array's walk callback runs outside its own scope;
+/// callbacks for its children see the updated depth.
+///
+/// ```
+/// use tvm_ffi::{
+///     structural_visit, Any, Array, DefRegionKind, Result, VisitCallbacks,
+///     VisitContext, VisitInterrupt, VisitLayer, VisitValue, WalkOrder,
+///     WalkDispatch, WalkResult, WalkWithLayer,
+/// };
+///
+/// #[derive(Default)]
+/// struct Stats { depth: usize, depths: Vec<usize> }
+///
+/// struct ArrayScope;
+/// impl VisitLayer<Stats> for ArrayScope {
+///     fn default_visit(
+///         &self, value: &VisitValue, visitor: &mut VisitContext<'_, Stats>,
+///     ) -> Result<Option<VisitInterrupt>> {
+///         let old_depth = visitor.state().depth;
+///         if value.cast::<Array<Any>>().is_some() {
+///             visitor.state_mut().depth += 1;
+///         }
+///         let result = visitor.visit_children();
+///         visitor.state_mut().depth = old_depth;
+///         result // Restore state before forwarding an interrupt or error.
+///     }
+/// }
+///
+/// let root = Array::new(vec![Any::from(Array::new(vec![1_i64])), 
Any::from(2_i64)]);
+/// let mut visitor = VisitCallbacks::new(
+///     Stats::default(),
+///     |_: i64, visitor: &mut VisitContext<'_, Stats>| {
+///         let state = visitor.state_mut();
+///         state.depths.push(state.depth);
+///     },
+/// ).with_layer(ArrayScope);
+/// structural_visit(&root, &mut visitor)?;
+/// assert_eq!(visitor.state().depths, vec![2, 1]);
+/// assert_eq!(visitor.state().depth, 0);
+///
+/// impl WalkDispatch for Stats {
+///     fn dispatch_walk(
+///         &mut self, value: &VisitValue, _: DefRegionKind,
+///     ) -> Option<Result<WalkResult>> {
+///         value.cast::<i64>()?;
+///         self.depths.push(self.depth);
+///         Some(Ok(WalkResult::Advance))
+///     }
+/// }
+/// let mut walker = WalkWithLayer::new(Stats::default(), ArrayScope);
+/// walker.walk(&root, WalkOrder::PreOrder)?;
+/// assert_eq!(walker.state().depths, vec![2, 1]);
+/// assert_eq!(walker.state().depth, 0);
+/// # Ok::<(), tvm_ffi::Error>(())
+/// ```

Review Comment:
   too many docs



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to