1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* 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.
*/
//! Runtime type dispatch, structural traversals ([`structural_walk`],
//! [`structural_visit`], [`structural_mutate`], [`structural_map`]) and
//! modules.
//!
//! [`structural_walk`]: crate::structural_walk
//! [`structural_visit`]: crate::structural_visit
//! [`structural_mutate`]: crate::structural_mutate
//! [`structural_map`]: crate::structural_map
/// Borrow guarantees of the structural views, checked as `compile_fail` doctests.
///
/// `StructuralVisitorView::base` cannot outlive its view:
/// ```compile_fail
/// use tvm_ffi::extra::structural_visit::{StructuralVisitorObj, StructuralVisitorView};
/// fn retain<D>(view: &StructuralVisitorView<'_, D>) -> &'static StructuralVisitorObj {
/// view.base()
/// }
/// ```
///
/// The visitor prefix cannot be converted into an owning FFI value:
///
/// ```compile_fail,E0277
/// use tvm_ffi::Any;
/// use tvm_ffi::StructuralVisitorView;
/// fn retain<D>(view: &StructuralVisitorView<'_, D>) -> Any {
/// Any::from(view.base())
/// }
/// ```
///
/// A node borrowed through a copied `MutationView` cannot survive consumption of the view:
/// ```compile_fail,E0505
/// use tvm_ffi::MutationView;
/// use tvm_ffi::StructuralMutatorView;
/// fn invalid<D>(value: MutationView<'_>, dispatch: &mut D, mutator: &mut StructuralMutatorView<'_, D>) {
/// let view = value.as_any_view();
/// let copy = view;
/// let node = copy.downcast_ref::<tvm_ffi::collections::array::ArrayObj>().unwrap();
/// mutator.default_mutate(dispatch, value).unwrap();
/// println!("{}", node.size);
/// }
/// ```
///
/// Moving the handle into a nested callback cannot bypass that borrow:
/// ```compile_fail,E0505
/// use tvm_ffi::{dispatch, structural_mutate, Any, MutationView, Result, UnchangedOr};
/// use tvm_ffi::StructuralMutatorView;
/// struct Inner<'a>(Option<MutationView<'a>>);
/// #[dispatch(mutate, (MutationView<'_>, &mut StructuralMutatorView<'_, Self>), Result<UnchangedOr<Any>>)]
/// impl<'a> Inner<'a> {
/// fn mutate_bool(&mut self, _: bool, mutator: &mut StructuralMutatorView<'_, Self>) -> Result<UnchangedOr<Any>> {
/// let pending = self.0.take().unwrap();
/// mutator.default_mutate(self, pending)
/// }
/// }
/// fn invalid(value: MutationView<'_>) {
/// let view = value.as_any_view();
/// let node = view.downcast_ref::<tvm_ffi::collections::array::ArrayObj>().unwrap();
/// structural_mutate(true, Inner(Some(value))).unwrap();
/// println!("{}", node.size);
/// }
/// ```
///
/// A copied inspection view also cannot survive low-level default mutation:
/// ```compile_fail,E0505
/// use tvm_ffi::{dispatch, Any, MutationView, Result, Unchanged, UnchangedOr};
/// use tvm_ffi::StructuralMutatorView;
/// struct Pass;
/// #[dispatch(mutate, (MutationView<'_>, &mut StructuralMutatorView<'_, Self>), Result<UnchangedOr<Any>>)]
/// impl Pass {
/// fn mutate_any(&mut self, value: MutationView<'_>, mutator: &mut StructuralMutatorView<'_, Self>) -> Result<UnchangedOr<Any>> {
/// let view = value.as_any_view();
/// let copy = view;
/// mutator.default_mutate(self, value).unwrap();
/// println!("{}", copy.type_index());
/// Ok(Unchanged.into())
/// }
/// }
/// ```
;