tvm_ffi/object.rs
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
/*
* 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.
*/
use std::ops::Deref;
use std::sync::atomic::AtomicU64;
use crate::derive::ObjectRef;
use crate::type_traits::AnyCompatible;
pub use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
/// Object related ABI handling
use tvm_ffi_sys::{TVMFFIAny, TVMFFIGetTypeInfo, TVMFFIObject, COMBINED_REF_COUNT_BOTH_ONE};
/// Held while `#[derive(Object)]` with `#[type_register]` registers a type, so
/// that registrations from Rust do not race each other: the runtime does not
/// lock its type table and leaves that to callers.
#[doc(hidden)]
pub static TYPE_REGISTRATION: std::sync::Mutex<()> = std::sync::Mutex::new(());
/// Object type is by default the TVMFFIObject
#[repr(C)]
pub struct Object {
/// example implementation of the object
header: TVMFFIObject,
}
/// Arc-like wrapper for Object that allows shared ownership
///
/// \tparam T The type of the object to be wrapped
#[repr(C)]
pub struct ObjectArc<T: ObjectCore> {
ptr: std::ptr::NonNull<T>,
_phantom: std::marker::PhantomData<T>,
}
unsafe impl<T: Send + Sync + ObjectCore> Send for ObjectArc<T> {}
unsafe impl<T: Send + Sync + ObjectCore> Sync for ObjectArc<T> {}
/// The native representation of an object-system type.
///
/// # Safety
/// `Self` must have a compatible C layout with a valid `TVMFFIObject` prefix
/// at offset zero. Its registered type, inheritance and payload must describe
/// that layout correctly, including for foreign declarations.
///
/// A live managed object and its header must remain at their original allocation
/// address until destruction. Safe APIs must not move, replace or expose mutable
/// access to that header while managed; payload mutation must preserve its type
/// invariants. Preallocation builders and values moved out during destruction
/// are not live managed objects and cannot be borrowed as `AnyView`.
pub unsafe trait ObjectCore: Sized + 'static {
/// the type key of the object
const TYPE_KEY: &'static str;
/// Depth of this type in the object inheritance tree.
///
/// The root [`Object`] has depth zero, and every registered subtype has
/// depth one greater than its parent. This value must be non-negative and
/// agree with the runtime type table entry for `Self`.
const TYPE_DEPTH: i32;
/// Whether every instance of this type has exactly `Self::type_index()`.
///
/// A final type has no separately registered object-system subtype.
#[doc(hidden)]
const TYPE_FINAL: bool = false;
// return the type index of the object
fn type_index() -> i32;
/// Return the object header
/// This function is implemented as a static function so
///
/// # Arguments
/// * `this` - The object to get the header
///
/// # Returns
/// * `&mut TVMFFIObject` - The object header
/// \return The object header
///
/// # Safety
/// The caller must have exclusive access to the header and preserve the
/// managed allocation's address, reference counts, deleter and dynamic type.
unsafe fn object_header_mut(this: &mut Self) -> &mut TVMFFIObject;
}
/// Traits for objects with extra items that follows the object
///
/// This extra trait can be helpful to implement array types and string types
pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
/// type of extra items storage that follows the object
type ExtraItem;
/// Return the number of extra items
fn extra_items_count(this: &Self) -> usize;
/// Return the extra items data pointer
unsafe fn extra_items(this: &Self) -> &[Self::ExtraItem] {
let extra_items_ptr = (this as *const Self as *const u8).add(std::mem::size_of::<Self>());
std::slice::from_raw_parts(
extra_items_ptr as *const Self::ExtraItem,
Self::extra_items_count(this),
)
}
/// Return the extra items data pointer
unsafe fn extra_items_mut(this: &mut Self) -> &mut [Self::ExtraItem] {
let extra_items_ptr = (this as *mut Self as *mut u8).add(std::mem::size_of::<Self>());
std::slice::from_raw_parts_mut(
extra_items_ptr as *mut Self::ExtraItem,
Self::extra_items_count(this),
)
}
}
/// Traits to specify core operations of ObjectRef
///
/// used by the ffi Any system and not user facing
///
/// We mark as unsafe since it moves out the internal of the ObjectRef
///
/// # Safety
///
/// `data`, `into_data`, and `from_data` must preserve the same object
/// allocation and form an ownership-preserving round trip. That allocation must
/// start with a valid `TVMFFIObject` header whose registered object-range
/// runtime type index correctly describes its layout and inheritance.
///
/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
/// produce a non-owning view, while `move_to_any` must transfer ownership of
/// the same object pointer and dynamic type index. `move_from_any_after_check`
/// must be able to reclaim that owned representation exactly once, and a true
/// `check_any_strict` result must guarantee that both after-check constructors
/// are valid for it.
pub unsafe trait ObjectRefCore: Sized + Clone {
type ContainerType: ObjectCore;
fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
/// Construct a reference view from an owning container handle.
///
/// # Safety
///
/// In addition to containing a valid `ContainerType` allocation, `data`
/// must satisfy every semantic invariant imposed by `Self`. This matters
/// for zero-state views that share a container type but accept only a
/// subset of its values, such as a typed expression view.
unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
/// Return whether two object references point to the same allocation.
#[inline]
fn same_as<Other: ObjectRefCore>(&self, other: &Other) -> bool {
unsafe {
ObjectArc::as_raw(Self::data(self)).cast::<()>()
== ObjectArc::as_raw(Other::data(other)).cast::<()>()
}
}
/// Borrow the underlying object as `T` when its runtime type is `T` or a subtype.
///
/// `T` is an object representation type such as `FooObj`, rather than its
/// managed reference `Foo`. Unrelated object types return `None`.
/// Unlike [`ObjectRefCast::try_cast`], this method neither consumes the
/// reference nor changes the object's reference count. The returned borrow
/// cannot outlive `self`.
/// See [`crate::Any::downcast_ref`] for an example.
#[inline(always)]
fn downcast_ref<T: ObjectCore>(&self) -> Option<&T> {
let object = unsafe { ObjectArc::as_raw(Self::data(self)) };
let type_index = unsafe { (*object.cast::<TVMFFIObject>()).type_index };
if !is_instance_of::<T>(type_index) {
return None;
}
// SAFETY: ObjectRefCore guarantees a live object allocation, the type
// check establishes T's layout, and the borrow cannot outlive self.
Some(unsafe { &*object.cast::<T>() })
}
}
/// An owning, hashable identity key for an FFI object.
///
/// The retained strong reference prevents the allocation address from being
/// reused while the key is alive. This makes it suitable for identity-based
/// maps without exposing raw object pointers to downstream code.
#[derive(Clone)]
pub struct ObjectIdentity {
data: ObjectArc<Object>,
}
impl ObjectIdentity {
/// Retain the allocation referenced by `value` as an identity key.
pub fn of<T: ObjectRefCore>(value: &T) -> Self {
unsafe {
let ptr = ObjectArc::as_raw(T::data(value)) as *mut TVMFFIObject;
unsafe_::inc_ref(ptr);
Self {
data: ObjectArc::from_raw(ptr.cast::<Object>()),
}
}
}
#[inline]
fn as_ptr(&self) -> *const TVMFFIObject {
unsafe { ObjectArc::as_raw(&self.data).cast::<TVMFFIObject>() }
}
}
impl PartialEq for ObjectIdentity {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_ptr() == other.as_ptr()
}
}
impl Eq for ObjectIdentity {}
impl std::hash::Hash for ObjectIdentity {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_ptr().hash(state);
}
}
impl std::fmt::Debug for ObjectIdentity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ObjectIdentity")
.field(&self.as_ptr())
.finish()
}
}
/// Check whether a runtime type index refers to `Target` or one of its
/// subtypes.
///
/// The subtype relation lives in the process-wide type table maintained by the
/// tvm-ffi library: every registered type records its depth in the single
/// inheritance tree together with the chain of its ancestors. The check is
/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
/// ancestor array exactly at `target`'s depth.
///
/// This is a hidden support function for derive-generated object checks. Object
/// indices in the registered range must refer to entries in the runtime type
/// table.
#[doc(hidden)]
#[inline(always)]
pub fn is_instance_of<Target: ObjectCore>(object_type_index: i32) -> bool {
let target_type_index = Target::type_index();
if object_type_index == target_type_index {
return true;
}
// A final type cannot have a separately registered subtype. Keep common
// borrowed checks, such as `IntImmObj`, to one integer comparison.
if Target::TYPE_FINAL {
return false;
}
let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
// Only object types participate in the type hierarchy.
if object_type_index < object_begin || target_type_index < object_begin {
return false;
}
// Parent indices are always smaller than their descendants.
if object_type_index < target_type_index {
return false;
}
unsafe {
let object_info = TVMFFIGetTypeInfo(object_type_index);
if object_info.is_null() {
return false;
}
let target_depth = Target::TYPE_DEPTH;
if (*object_info).type_depth <= target_depth {
return false;
}
let ancestor = *(*object_info).type_acenstors.add(target_depth as usize);
!ancestor.is_null() && (*ancestor).type_index == target_type_index
}
}
/// Runtime-checked casting between arbitrary `ObjectRef` types.
///
/// The cast uses the target's [`AnyCompatible::check_any_strict`] implementation,
/// mirroring the semantics of `ObjectRef::as<T>` in C++. This supports both
/// object hierarchies and parameterized object containers.
///
/// This trait is blanket-implemented for every [`ObjectRefCore`] type that is
/// also [`AnyCompatible`].
pub trait ObjectRefCast: ObjectRefCore + AnyCompatible {
/// Consume `self` and rewrap the underlying object as `B` without copying.
#[inline(always)]
fn try_cast<B>(self) -> crate::error::Result<B>
where
B: ObjectRefCore + AnyCompatible,
{
let mut any_data = TVMFFIAny::new();
unsafe {
// Keep ownership in `self` while the target check runs. This makes
// the failure and panic paths unwind normally instead of stranding
// an owned object inside a raw TVMFFIAny.
Self::copy_to_any_view(&self, &mut any_data);
debug_assert!(any_data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32);
// SAFETY: ObjectRefCore's contract requires its AnyCompatible
// representation to contain a valid object-range type index.
std::hint::assert_unchecked(
any_data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32,
);
if B::check_any_strict(&any_data) {
// Transfer ownership only after the borrowed representation has
// passed the target's complete hierarchy/container check.
Self::move_to_any(self, &mut any_data);
Ok(B::move_from_any_after_check(&mut any_data))
} else {
let msg = format!(
"Cannot convert from type `{}` to `{}`",
B::get_mismatch_type_info(&any_data),
B::type_str()
);
Err(crate::error::Error::new(crate::error::TYPE_ERROR, &msg, ""))
}
}
}
}
impl<T: ObjectRefCore + AnyCompatible> ObjectRefCast for T {}
/// A generic managed reference to a TVM-FFI object.
///
/// Holds an `ObjectArc<Object>` that manages the underlying object's lifetime
/// through reference counting.
#[repr(C)]
#[derive(ObjectRef, Clone)]
pub struct ObjectRef {
data: ObjectArc<Object>,
}
/// Unsafe operations on object
#[doc(hidden)]
pub mod unsafe_ {
use tvm_ffi_sys::{
COMBINED_REF_COUNT_BOTH_ONE, COMBINED_REF_COUNT_MASK_U32, COMBINED_REF_COUNT_STRONG_ONE,
COMBINED_REF_COUNT_WEAK_ONE,
};
use std::ffi::c_void;
use std::sync::atomic::{fence, Ordering};
use tvm_ffi_sys::TVMFFIObject;
use tvm_ffi_sys::TVMFFIObjectDeleterFlagBitMask::{
kTVMFFIObjectDeleterFlagBitMaskBoth, kTVMFFIObjectDeleterFlagBitMaskStrong,
kTVMFFIObjectDeleterFlagBitMaskWeak,
};
/// Increase the strong reference count of the object
///
/// This function is same as TVMFFIObjectIncRef but implemented natively in Rust
///
/// # Arguments
/// * `obj` - The object to increase the reference count
#[inline]
pub unsafe fn inc_ref(handle: *mut TVMFFIObject) {
let obj = &*handle;
obj.combined_ref_count.fetch_add(1, Ordering::Relaxed);
}
/// Decrease the strong reference count of the object
///
/// This function is same as TVMFFIObjectDecRef but implemented natively in Rust
///
/// # Arguments
/// * `obj` - The object to decrease the reference count
#[inline]
pub(crate) unsafe fn dec_ref(handle: *mut TVMFFIObject) {
let old_combined_count = (*handle)
.combined_ref_count
.fetch_sub(COMBINED_REF_COUNT_STRONG_ONE, Ordering::Relaxed);
if old_combined_count == COMBINED_REF_COUNT_BOTH_ONE {
if let Some(deleter) = (*handle).deleter {
fence(Ordering::Acquire);
deleter(
handle.cast::<c_void>(),
kTVMFFIObjectDeleterFlagBitMaskBoth as i32,
);
}
} else if (old_combined_count & COMBINED_REF_COUNT_MASK_U32)
== COMBINED_REF_COUNT_STRONG_ONE
{
// slow path, there is still a weak reference left
// need to run two phase decrement
fence(Ordering::Acquire);
if let Some(deleter) = (*handle).deleter {
deleter(
handle.cast::<c_void>(),
kTVMFFIObjectDeleterFlagBitMaskStrong as i32,
);
}
let old_weak_count = (*handle)
.combined_ref_count
.fetch_sub(COMBINED_REF_COUNT_WEAK_ONE, Ordering::Release);
if old_weak_count == COMBINED_REF_COUNT_WEAK_ONE {
fence(Ordering::Acquire);
if let Some(deleter) = (*handle).deleter {
deleter(
handle.cast::<c_void>(),
kTVMFFIObjectDeleterFlagBitMaskWeak as i32,
);
}
}
}
}
#[inline]
pub(crate) unsafe fn strong_count(handle: *mut TVMFFIObject) -> usize {
let obj = &*handle;
(obj.combined_ref_count.load(Ordering::Relaxed) & COMBINED_REF_COUNT_MASK_U32) as usize
}
#[inline]
pub(crate) unsafe fn weak_count(handle: *mut TVMFFIObject) -> usize {
let obj = &*handle;
(obj.combined_ref_count.load(Ordering::Relaxed) >> 32) as usize
}
/// Generic object deleter for objects allocated through Rust's global allocator.
pub(crate) unsafe extern "C" fn object_deleter_for_new<T>(ptr: *mut c_void, flags: i32)
where
T: super::ObjectCore,
{
let obj = ptr as *mut T;
if flags & kTVMFFIObjectDeleterFlagBitMaskStrong as i32 != 0 {
std::ptr::drop_in_place(obj);
}
if flags & kTVMFFIObjectDeleterFlagBitMaskWeak as i32 != 0 {
std::alloc::dealloc(ptr as *mut u8, std::alloc::Layout::new::<T>());
}
}
/// Layout of a `T` followed by `count` extra items, kept at least one word past the
/// header: once a weak reference outlives the data, the deleter stores `count` there.
pub(crate) fn extra_items_layout<T, U>(count: usize) -> std::alloc::Layout {
let size = std::mem::size_of::<T>() + count * std::mem::size_of::<U>();
let min_size = std::mem::size_of::<TVMFFIObject>() + std::mem::size_of::<u64>();
std::alloc::Layout::from_size_align(size.max(min_size), std::mem::align_of::<T>()).unwrap()
}
pub(crate) unsafe extern "C" fn object_deleter_for_new_with_extra_items<T, U>(
ptr: *mut c_void,
flags: i32,
) where
T: super::ObjectCoreWithExtraItems<ExtraItem = U>,
{
let obj = ptr as *mut T;
if flags == kTVMFFIObjectDeleterFlagBitMaskBoth as i32 {
let extra_items_count = T::extra_items_count(&(*obj));
std::ptr::drop_in_place(obj);
std::alloc::dealloc(
ptr as *mut u8,
extra_items_layout::<T, U>(extra_items_count),
);
} else {
// Weak references keep using the header, so the weak phase finds the item
// count in the first word after it.
let count_slot = (ptr as *mut u8).add(std::mem::size_of::<TVMFFIObject>()) as *mut u64;
if flags & kTVMFFIObjectDeleterFlagBitMaskStrong as i32 != 0 {
let extra_items_count = T::extra_items_count(&(*obj));
std::ptr::drop_in_place(obj);
std::ptr::write(count_slot, extra_items_count as u64);
}
if flags & kTVMFFIObjectDeleterFlagBitMaskWeak as i32 != 0 {
let extra_items_count = std::ptr::read(count_slot) as usize;
std::alloc::dealloc(
ptr as *mut u8,
extra_items_layout::<T, U>(extra_items_count),
);
}
}
}
}
//---------------------
// Object
//---------------------
impl Object {
pub fn new() -> Self {
Self {
header: TVMFFIObject::new(),
}
}
}
unsafe impl ObjectCore for Object {
const TYPE_KEY: &'static str = "ffi.Object";
const TYPE_DEPTH: i32 = 0;
#[inline]
fn type_index() -> i32 {
TypeIndex::kTVMFFIStaticObjectBegin as i32
}
#[inline]
unsafe fn object_header_mut(this: &mut Self) -> &mut TVMFFIObject {
&mut this.header
}
}
//---------------------
// ObjectArc
//---------------------
impl<T: ObjectCore> ObjectArc<T> {
/// Allocate an initialized object at its stable managed address.
pub fn new(data: T) -> Self {
unsafe {
let layout = std::alloc::Layout::new::<T>();
let raw_data_ptr = std::alloc::alloc(layout);
if raw_data_ptr.is_null() {
std::alloc::handle_alloc_error(layout);
}
let ptr = raw_data_ptr as *mut T;
std::ptr::write(ptr, data);
// now override the header directly
std::ptr::write(
ptr as *mut TVMFFIObject,
TVMFFIObject {
combined_ref_count: AtomicU64::new(COMBINED_REF_COUNT_BOTH_ONE),
type_index: T::type_index(),
__padding: 0,
deleter: Some(unsafe_::object_deleter_for_new::<T>),
},
);
// move into the object arc ptr
Self {
ptr: std::ptr::NonNull::new_unchecked(ptr as *mut T),
_phantom: std::marker::PhantomData,
}
}
}
/// Allocate a stable object followed by its type's extra item storage.
pub fn new_with_extra_items<U>(data: T) -> Self
where
T: ObjectCoreWithExtraItems<ExtraItem = U>,
{
unsafe {
// ensure strict alignment requirements
// so we can have { T, U*extra_items } layout
assert_eq!(std::mem::align_of::<T>() % std::mem::align_of::<U>(), 0);
assert_eq!(std::mem::size_of::<T>() % std::mem::align_of::<U>(), 0);
let extra_items_count = T::extra_items_count(&data);
let layout = unsafe_::extra_items_layout::<T, U>(extra_items_count);
let raw_data_ptr = std::alloc::alloc(layout);
if raw_data_ptr.is_null() {
std::alloc::handle_alloc_error(layout);
}
let ptr = raw_data_ptr as *mut T;
std::ptr::write(ptr, data);
// now override the header directly
std::ptr::write(
ptr as *mut TVMFFIObject,
TVMFFIObject {
combined_ref_count: AtomicU64::new(COMBINED_REF_COUNT_BOTH_ONE),
type_index: T::type_index(),
__padding: 0,
deleter: Some(unsafe_::object_deleter_for_new_with_extra_items::<T, U>),
},
);
// move into the object arc ptr
Self {
ptr: std::ptr::NonNull::new_unchecked(ptr as *mut T),
_phantom: std::marker::PhantomData,
}
}
}
/// Whether `this` holds a null pointer, as all-zero bytes do: no
/// `ObjectArc`, but the field of an object that a creator left undefined.
///
/// # Safety
///
/// `this` points to an `ObjectArc<T>` or to all-zero bytes.
#[doc(hidden)]
#[inline]
pub unsafe fn is_null(this: *const Self) -> bool {
(*std::ptr::addr_of!((*this).ptr).cast::<*const T>()).is_null()
}
/// Move a previously allocated object into the ObjectArc
///
/// # Arguments
/// * `ptr` - The raw pointer to move into the ObjectArc
///
/// # Returns
/// * `ObjectArc<T>` - The ObjectArc
/// \return The ObjectArc
///
/// # Safety
/// `ptr` must own one strong reference to a live, stable managed allocation
/// whose dynamic type is compatible with `T` and whose payload is valid.
/// Ownership of that reference is transferred into the returned handle.
#[inline]
pub unsafe fn from_raw(ptr: *const T) -> Self {
Self {
ptr: std::ptr::NonNull::new_unchecked(ptr as *mut T),
_phantom: std::marker::PhantomData,
}
}
/// Move the ObjectArc into a raw pointer
///
/// # Arguments
/// * `this` - The ObjectArc to move into a raw pointer
///
/// # Returns
/// * `*const T` - The raw pointer
#[inline]
pub unsafe fn into_raw(this: Self) -> *const T {
let droped_this = std::mem::ManuallyDrop::new(this);
droped_this.ptr.as_ptr() as *const T
}
/// Get the raw pointer from the ObjectArc
///
/// Caller should view this as a non-owning reference
///
/// # Arguments
/// * `this` - The ObjectArc to get the raw pointer
///
/// # Returns
/// * `*const T` - The raw pointer
/// \return The raw pointer
///
/// # Safety
/// Access must respect existing borrows, and the pointer cannot outlive an
/// owning handle. The object and header must not be moved or replaced.
#[inline]
pub unsafe fn as_raw(this: &Self) -> *const T {
this.ptr.as_ptr() as *const T
}
/// Get the raw mutable pointer from the ObjectArc
///
/// Caller should view this as a non-owning reference
///
/// # Arguments
/// * `this` - The ObjectArc to get the raw pointer
///
/// # Returns
/// * `*mut T` - The raw pointer
///
/// # Safety
/// The handle need not be unique. The caller must establish exclusive access
/// for each mutation and must not move or replace the object or its header.
#[inline]
pub unsafe fn as_raw_mut(this: &mut Self) -> *mut T {
this.ptr.as_ptr()
}
/// Get the strong reference count of the ObjectArc
///
/// # Arguments
/// * `this` - The ObjectArc to get the strong reference count
///
/// # Returns
/// * `usize` - The strong reference count
#[inline]
pub fn strong_count(this: &Self) -> usize {
unsafe { unsafe_::strong_count(this.ptr.as_ptr().cast::<TVMFFIObject>()) }
}
/// Get the weak reference count of the ObjectArc
///
/// # Arguments
/// * `this` - The ObjectArc to get the weak reference count
///
/// # Returns
/// * `usize` - The weak reference count
#[inline]
pub fn weak_count(this: &Self) -> usize {
unsafe { unsafe_::weak_count(this.ptr.as_ptr().cast::<TVMFFIObject>()) }
}
}
// implement Deref for ObjectArc
impl<T: ObjectCore> Deref for ObjectArc<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { self.ptr.as_ref() }
}
}
// implement Drop for ObjectArc
impl<T: ObjectCore> Drop for ObjectArc<T> {
fn drop(&mut self) {
unsafe { unsafe_::dec_ref(self.ptr.as_ptr().cast::<TVMFFIObject>()) }
}
}
// implement Clone for ObjectArc
impl<T: ObjectCore> Clone for ObjectArc<T> {
#[inline]
fn clone(&self) -> Self {
unsafe { unsafe_::inc_ref(self.ptr.as_ptr().cast::<TVMFFIObject>()) }
Self {
ptr: self.ptr,
_phantom: std::marker::PhantomData,
}
}
}