cycle_ptr 0.1.0

Smart pointers, with cycles
//! Module for an enum that can hold either [ObjectPtr] or [MTObjectPtr].
//!
//! This module only exists if the `multi_thread` feature is enabled.
use crate::object::{MTObjectPtr, ObjectPtr};
use std::pin::Pin;

/// Enum to hold on to a pointer to either [Object][crate::object::Object] or [MTObject][crate::object::MTObject].
pub(super) enum SingleOrMultiThreadPtr<T>
where
    T: 'static,
{
    /// Pointer to [Object][crate::object::Object], for single-thread case.
    SingleThread(Pin<ObjectPtr<T>>),
    /// Pointer to [MTObject][crate::object::MTObject], for multi-thread case.
    MultiThread(Pin<MTObjectPtr<T>>),
}

impl<T> Clone for SingleOrMultiThreadPtr<T>
where
    T: 'static,
{
    fn clone(&self) -> Self {
        match self {
            SingleOrMultiThreadPtr::SingleThread(ptr) => {
                SingleOrMultiThreadPtr::SingleThread(ptr.clone())
            }
            SingleOrMultiThreadPtr::MultiThread(ptr) => {
                SingleOrMultiThreadPtr::MultiThread(ptr.clone())
            }
        }
    }
}

impl<T> From<Pin<ObjectPtr<T>>> for SingleOrMultiThreadPtr<T>
where
    T: 'static,
{
    fn from(ptr: Pin<ObjectPtr<T>>) -> Self {
        SingleOrMultiThreadPtr::SingleThread(ptr)
    }
}

impl<T> From<Pin<MTObjectPtr<T>>> for SingleOrMultiThreadPtr<T>
where
    T: 'static,
{
    fn from(ptr: Pin<MTObjectPtr<T>>) -> Self {
        SingleOrMultiThreadPtr::MultiThread(ptr)
    }
}