cycle_ptr 0.1.0

Smart pointers, with cycles
//! Module for an enum that can hold either [DynObjectPtr][crate::object::DynObjectPtr] or [DynMTObjectPtr].
//!
//! This module only exists if the `multi_thread` feature is enabled.
use crate::generation::edge::MTEdge;
use crate::object::DynMTObjectPtr;
use std::backtrace::Backtrace;
use std::pin::Pin;
use std::sync::Arc;

/// Enum to indicate a single-thread origin, or hold on to [DynMTObjectPtr] origin.
pub(super) enum SingleOrMultiThreadDynPtr {
    /// We don't store the origin of the object pointer:
    /// - we can't make the [sync::GcMtMemberPtr][crate::sync::GcMtMemberPtr] sendable if we hold on to a [DynObjectPtr][crate::object::DynObjectPtr].
    /// - this also means we can't report an error if you dereference member-pointer during [drop][Drop::drop].
    SingleThread(Option<Arc<Backtrace>>),
    /// [DynMTObjectPtr], and the [MTEdge] of the member-pointer, for multi-thread case.
    MultiThread(Pin<DynMTObjectPtr>, Pin<Box<MTEdge>>),
}

impl SingleOrMultiThreadDynPtr {
    /// Get the backtrace of when the origin object was created.
    pub(super) fn created_backtrace(&self) -> &Option<Arc<Backtrace>> {
        match self {
            SingleOrMultiThreadDynPtr::SingleThread(backtrace) => backtrace,
            SingleOrMultiThreadDynPtr::MultiThread(ptr, _) => ptr.created_backtrace(),
        }
    }
}