Skip to main content

tvm_ffi/reflection/
access_path.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
20//! Access paths, which locate a value nested in another.
21
22use crate::Any;
23use crate::derive::{Object, ObjectRef};
24use crate::object::{Object, ObjectArc, ObjectRef};
25
26/// What an [`AccessStep`] accesses: an attribute, an array item or a map
27/// item, or one of those that is missing.
28///
29/// The values are part of the ABI.
30#[repr(i32)]
31#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
32#[non_exhaustive]
33pub enum AccessKind {
34    /// An object attribute; the key is its name.
35    Attr = 0,
36    /// An array item; the key is its index.
37    ArrayItem = 1,
38    /// A map item; the key is the map key.
39    MapItem = 2,
40    /// An attribute that does not exist, in an error report.
41    AttrMissing = 3,
42    /// An array index out of range, in an error report.
43    ArrayItemMissing = 4,
44    /// A map key that is absent, in an error report.
45    MapItemMissing = 5,
46}
47
48/// The object behind an [`AccessStep`], laid out as the C++ `AccessStepObj`.
49#[repr(C)]
50#[derive(Object)]
51#[object(type_key = "ffi.reflection.AccessStep", final)]
52pub struct AccessStepObj {
53    object: Object,
54    /// What the step accesses.
55    pub kind: AccessKind,
56    /// The attribute name, array index or map key.
57    pub key: Any,
58}
59
60/// One step of an [`AccessPath`]: an attribute, array index or map key.
61#[repr(C)]
62#[derive(ObjectRef, Clone)]
63pub struct AccessStep {
64    data: ObjectArc<AccessStepObj>,
65}
66
67/// The object behind an [`AccessPath`], laid out as the C++ `AccessPathObj`.
68#[repr(C)]
69#[derive(Object)]
70#[object(type_key = "ffi.reflection.AccessPath", final)]
71pub struct AccessPathObj {
72    object: Object,
73    /// The path without its last step; `None` at the root.
74    pub parent: Option<ObjectRef>,
75    /// The last step; `None` at the root.
76    pub step: Option<AccessStep>,
77    /// The number of steps; 0 at the root.
78    pub depth: i32,
79}
80
81/// A path from a root value to a nested one, such as `root.field[0]`, as a
82/// chain of parents; its methods are reached with `reflection::get_method`.
83#[repr(C)]
84#[derive(ObjectRef, Clone)]
85pub struct AccessPath {
86    data: ObjectArc<AccessPathObj>,
87}