tvm_ffi/derive.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//! The derive macros of object types and object references.
20
21/// Bind an object type that another library, usually C++, registers: derive
22/// [`ObjectCore`](crate::ObjectCore) for a `#[repr(C)]` object struct whose
23/// first field is its parent, so that a pointer to the object is one to its
24/// parent. Define a new type in Rust with [`rs_object`](macro@crate::rs_object)
25/// instead.
26///
27/// Without `register` or `type_index`, `type_index()` takes the index the
28/// defining library registered for the key, and panics if that library is not
29/// loaded yet, so a binding never registers a key with the wrong child slots.
30/// Such a panic is final: `type_index()` of the type, and of every type under
31/// it, keeps panicking after the library loads, so call it only once the
32/// library is loaded. It also panics unless the registered key has the type's
33/// parent and depth and, when the type table records a size, a size no
34/// smaller than the type's.
35///
36/// A binding that declares only part of its object must not be constructed:
37/// keep its fields private.
38///
39/// # Options
40///
41/// `#[object(...)]` takes a comma-separated list, as in
42/// `#[object(type_key = "ffi.String", unsafe type_index = TypeIndex::kTVMFFIStr)]`:
43///
44/// * `type_key = "..."` - The type key; required.
45/// * `unsafe type_index = expr` - A static index instead of the registered
46/// one; the `unsafe` marks the caller's promise that the runtime gives that
47/// index to this type.
48/// * `final` - The type has no subtypes, as C++ `_type_final`.
49/// * `immutable` - Implement
50/// [`ImmutableObject`](crate::object::ImmutableObject), as for a C++ type
51/// not registered as `_type_mutable`; every subtype must be immutable too.
52/// * `rs_parent` - Implement `ShallowCopy` and
53/// [`UnsafeInit`](crate::reflection::UnsafeInit), so an `rs_object` can
54/// derive from the type. Each field other than the parent must be `Clone`
55/// and implement [`AnyCompatible`](crate::AnyCompatible), be an
56/// [`Any`](crate::Any) or have no drop glue.
57/// * `register` - The type is defined here: register its key under its
58/// parent when the library loads, as `rs_object` does.
59/// * `child_slots = N`, `child_slots_can_overflow = bool` - Type indices kept
60/// for subtypes, as C++ `_type_child_slots` and
61/// `_type_child_slots_can_overflow`; 0 and `true` by default. Needs
62/// `register`.
63/// * `s_eq_hash_kind = "TreeNode"` - The
64/// [`SEqHashKind`](crate::reflection::SEqHashKind) of structural equality;
65/// by default the parent's.
66/// * `reflection = path` - A `fn(&mut ObjectDef<Self>)` that registers the
67/// type's methods.
68///
69/// `s_eq_hash_kind`, `reflection` and field `#[reflect(...)]`, as
70/// [`rs_object`](macro@crate::rs_object) takes it, need `register` and no
71/// `type_index`.
72pub use tvm_ffi_macros::Object;
73
74/// Declare an object reference: the managed, reference-counted handle to an
75/// object type, as `Arc<FooObj>` is to `FooObj`.
76///
77/// The struct holds one field, `ObjectArc<FooObj>`. The derive implements
78/// [`ObjectRefCore`](crate::ObjectRefCore), the default
79/// [`ObjectRefAnyCompatible`](crate::object::ObjectRefAnyCompatible) (conversions to
80/// and from [`Any`](crate::Any), in-place downcasts, typed function
81/// arguments), the upcast to [`ObjectRef`](crate::object::ObjectRef),
82/// `Deref<Target = FooObj>` for field reads, `Debug`, `Display` (the runtime
83/// `repr`, as [`AnyView`](crate::AnyView) formats it), and a private
84/// `fn from_object(object: FooObj) -> Self` for the type's constructors.
85///
86/// # Options
87///
88/// `#[object_ref(...)]` takes:
89///
90/// * `parent = Ref` - The reference to the parent's object, or to a further
91/// ancestor's; `Self` converts into it. The object type must embed that
92/// object type through its chain of parents
93/// ([`ObjectDerivesFrom`](crate::object::ObjectDerivesFrom)), so a wrong parent does not
94/// compile. Any other ancestor takes
95/// [`impl_object_ref_upcast!`](crate::impl_object_ref_upcast).
96/// * `unsafe parent = Ref` - As `parent`, for a reference that promises more
97/// than its object type: a generic one (`Array<T>`, `Map<K, V>`,
98/// `Optional<T>`) or one with a custom `ObjectRefAnyCompatible`. The caller
99/// promises that every value of `Self` is a valid `Ref`; `parent` alone
100/// does not compile for such a reference.
101/// * `skip_deref` - No `Deref`, for a reference whose object is internal.
102/// * `skip_debug` - `Debug` is written by hand.
103/// * `skip_display` - `Display` is written by hand.
104/// * `skip_any_compatible` - `ObjectRefAnyCompatible` is written by hand,
105/// for a typed reference with a stricter check or fallback conversions.
106/// * `crate = path` - Where the generated code finds tvm-ffi, as for
107/// [`rs_object`](macro@crate::rs_object).
108///
109/// # Examples
110///
111/// ```standalone_crate
112/// use tvm_ffi::derive::ObjectRef;
113/// use tvm_ffi::{rs_object, Object, ObjectArc};
114///
115/// #[rs_object(type_key = "my_ext.MyPair")]
116/// pub struct MyPairObj {
117/// base_: Object,
118/// pub first: i64,
119/// pub second: i64,
120/// }
121///
122/// #[derive(Clone, ObjectRef)]
123/// pub struct MyPair {
124/// data: ObjectArc<MyPairObj>,
125/// }
126///
127/// #[rs_object(type_key = "my_ext.MyTriple", final)]
128/// pub struct MyTripleObj {
129/// base_: MyPairObj,
130/// pub third: i64,
131/// }
132///
133/// #[derive(Clone, ObjectRef)]
134/// #[object_ref(parent = MyPair)]
135/// pub struct MyTriple {
136/// data: ObjectArc<MyTripleObj>,
137/// }
138///
139/// impl MyTriple {
140/// pub fn new(first: i64, second: i64, third: i64) -> Self {
141/// let pair = MyPairObj::new(Object::new(), first, second);
142/// Self::from_object(MyTripleObj::new(pair, third))
143/// }
144/// }
145///
146/// let triple = MyTriple::new(1, 2, 3);
147/// assert_eq!(triple.first + triple.third, 4);
148/// let _pair: MyPair = triple.into();
149/// ```
150///
151/// A `MyPairObj` does not embed an `ArrayObj`, so it cannot claim `Array` as
152/// its parent:
153///
154/// ```compile_fail,E0277
155/// use tvm_ffi::derive::ObjectRef;
156/// use tvm_ffi::{rs_object, Array, Object, ObjectArc};
157///
158/// #[rs_object(type_key = "my_ext.MyPair")]
159/// pub struct MyPairObj {
160/// base_: Object,
161/// pub first: i64,
162/// }
163///
164/// #[derive(Clone, ObjectRef)]
165/// #[object_ref(unsafe parent = Array<i64>)]
166/// pub struct MyPair {
167/// data: ObjectArc<MyPairObj>,
168/// }
169/// ```
170pub use tvm_ffi_macros::ObjectRef;