pub unsafe trait ObjectRefAnyCompatible: ObjectRefCore {
// Provided methods
fn extra_check(obj: &Self::ContainerType) -> bool { ... }
fn convert_fallback(value: AnyView<'_>) -> Option<Self> { ... }
fn custom_type_str() -> String { ... }
fn custom_mismatch_type_info(value: AnyView<'_>) -> String { ... }
}Expand description
The AnyCompatible implementation of an object reference, with every
hook defaulted: every implementer is AnyCompatible.
#[derive(ObjectRef)] implements it with the defaults. A typed reference
that accepts only some objects of its type, or converts other values,
implements it by hand and overrides only what differs, such as
extra_check and
convert_fallback below. A type with another
representation, such as Array, implements
AnyCompatible instead.
§Safety
Self is exactly one ObjectArc<Self::ContainerType>. The
hooks do not modify an ffi.List or ffi.Dict that holds the value they
examine, whose cell a container read borrows while they run; otherwise
they cannot break the AnyCompatible contract.
§Examples
use tvm_ffi::derive::ObjectRef;
use tvm_ffi::{rs_object, Any, AnyView, Object, ObjectArc};
use tvm_ffi::object::ObjectRefAnyCompatible;
#[rs_object(type_key = "my_ext.MyPair", final)]
pub struct MyPairObj {
base_: Object,
pub first: i64,
pub second: i64,
}
#[derive(Clone, ObjectRef)]
pub struct MyPair {
data: ObjectArc<MyPairObj>,
}
/// A `MyPair` whose `first` is positive.
#[derive(Clone, ObjectRef)]
#[object_ref(skip_any_compatible)]
pub struct PositivePair {
data: ObjectArc<MyPairObj>,
}
unsafe impl ObjectRefAnyCompatible for PositivePair {
fn extra_check(pair: &MyPairObj) -> bool {
pair.first > 0
}
// A positive integer `n` converts to the pair `(n, n)`.
fn convert_fallback(value: AnyView<'_>) -> Option<Self> {
let n = i64::try_from(value).ok().filter(|n| *n > 0)?;
Some(Self::from_object(MyPairObj::new(Object::new(), n, n)))
}
}
let negative = Any::from(MyPair::from_object(MyPairObj::new(Object::new(), -1, 2)));
assert!(negative.downcast_ref::<PositivePair>().is_none());
assert!(PositivePair::try_from(negative).is_err());
assert_eq!(PositivePair::try_from(Any::from(3_i64))?.second, 3);Provided Methods§
Sourcefn extra_check(obj: &Self::ContainerType) -> bool
fn extra_check(obj: &Self::ContainerType) -> bool
Whether obj, an instance of ContainerType, is a value of Self.
Every strict and converting cast checks it, after the object type.
Sourcefn convert_fallback(value: AnyView<'_>) -> Option<Self>
fn convert_fallback(value: AnyView<'_>) -> Option<Self>
Convert a value that is not a Self object, such as an integer that
stands for a constant; None rejects it. Only converting casts (TryFrom<Any>,
typed function arguments) call it; downcast_ref, downcast and
match_any! never do.
Sourcefn custom_type_str() -> String
fn custom_type_str() -> String
The type name in conversion errors and type schemas; by default the
object’s type key. See AnyCompatible::type_str.
Sourcefn custom_mismatch_type_info(value: AnyView<'_>) -> String
fn custom_mismatch_type_info(value: AnyView<'_>) -> String
The name of the mismatched value in conversion errors; by default its
runtime type key. See AnyCompatible::get_mismatch_type_info.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".