tvm_ffi/extra/mod.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19//! Runtime type dispatch, structural traversals ([`structural_walk`],
20//! [`structural_visit`], [`structural_mutate`], [`structural_map`]) and
21//! modules.
22//!
23//! [`structural_walk`]: crate::structural_walk
24//! [`structural_visit`]: crate::structural_visit
25//! [`structural_mutate`]: crate::structural_mutate
26//! [`structural_map`]: crate::structural_map
27
28pub mod cold_init_lazy_lock;
29pub mod dispatch;
30pub mod module;
31mod structural_common;
32pub mod structural_eq_hash;
33pub mod structural_map;
34pub mod structural_mutate;
35pub mod structural_visit;
36pub mod structural_walk;
37pub mod unchanged;
38pub mod visit_error_context;
39
40/// Borrow guarantees of the structural views, checked as `compile_fail` doctests.
41///
42/// `StructuralVisitorView::base` cannot outlive its view:
43/// ```compile_fail
44/// use tvm_ffi::extra::structural_visit::{StructuralVisitorObj, StructuralVisitorView};
45/// fn retain<D>(view: &StructuralVisitorView<'_, D>) -> &'static StructuralVisitorObj {
46/// view.base()
47/// }
48/// ```
49///
50/// The visitor prefix cannot be converted into an owning FFI value:
51///
52/// ```compile_fail,E0277
53/// use tvm_ffi::Any;
54/// use tvm_ffi::StructuralVisitorView;
55/// fn retain<D>(view: &StructuralVisitorView<'_, D>) -> Any {
56/// Any::from(view.base())
57/// }
58/// ```
59///
60/// A node borrowed through a copied `MutationView` cannot survive consumption of the view:
61/// ```compile_fail,E0505
62/// use tvm_ffi::MutationView;
63/// use tvm_ffi::StructuralMutatorView;
64/// fn invalid<D>(value: MutationView<'_>, dispatch: &mut D, mutator: &mut StructuralMutatorView<'_, D>) {
65/// let view = value.as_any_view();
66/// let copy = view;
67/// let node = copy.downcast_ref::<tvm_ffi::collections::array::ArrayObj>().unwrap();
68/// mutator.default_mutate(dispatch, value).unwrap();
69/// println!("{}", node.size);
70/// }
71/// ```
72///
73/// Moving the handle into a nested callback cannot bypass that borrow:
74/// ```compile_fail,E0505
75/// use tvm_ffi::{dispatch, structural_mutate, Any, MutationView, Result, UnchangedOr};
76/// use tvm_ffi::StructuralMutatorView;
77/// struct Inner<'a>(Option<MutationView<'a>>);
78/// #[dispatch(mutate, (MutationView<'_>, &mut StructuralMutatorView<'_, Self>), Result<UnchangedOr<Any>>)]
79/// impl<'a> Inner<'a> {
80/// fn mutate_bool(&mut self, _: bool, mutator: &mut StructuralMutatorView<'_, Self>) -> Result<UnchangedOr<Any>> {
81/// let pending = self.0.take().unwrap();
82/// mutator.default_mutate(self, pending)
83/// }
84/// }
85/// fn invalid(value: MutationView<'_>) {
86/// let view = value.as_any_view();
87/// let node = view.downcast_ref::<tvm_ffi::collections::array::ArrayObj>().unwrap();
88/// structural_mutate(true, Inner(Some(value))).unwrap();
89/// println!("{}", node.size);
90/// }
91/// ```
92///
93/// A copied inspection view also cannot survive low-level default mutation:
94/// ```compile_fail,E0505
95/// use tvm_ffi::{dispatch, Any, MutationView, Result, Unchanged, UnchangedOr};
96/// use tvm_ffi::StructuralMutatorView;
97/// struct Pass;
98/// #[dispatch(mutate, (MutationView<'_>, &mut StructuralMutatorView<'_, Self>), Result<UnchangedOr<Any>>)]
99/// impl Pass {
100/// fn mutate_any(&mut self, value: MutationView<'_>, mutator: &mut StructuralMutatorView<'_, Self>) -> Result<UnchangedOr<Any>> {
101/// let view = value.as_any_view();
102/// let copy = view;
103/// mutator.default_mutate(self, value).unwrap();
104/// println!("{}", copy.type_index());
105/// Ok(Unchanged.into())
106/// }
107/// }
108/// ```
109#[cfg(doctest)]
110pub struct StructuralViewBorrowChecks;