memkit-async 0.2.0-beta.1

Async-aware memory allocators for memkit
//! Task-local allocation context.

use std::sync::Arc;

/// Task-local allocation context.
///
/// Provides isolated allocation for each async task,
/// with optional parent-child inheritance.
pub struct MkTaskLocal {
    inner: Arc<TaskLocalInner>,
}

struct TaskLocalInner {
    parent: Option<Arc<TaskLocalInner>>,
    // TODO: Add task-local state
}

impl MkTaskLocal {
    /// Create a new root task-local context.
    pub fn new() -> Self {
        Self {
            inner: Arc::new(TaskLocalInner { parent: None }),
        }
    }

    /// Create a child context that inherits from this one.
    pub fn child(&self) -> Self {
        Self {
            inner: Arc::new(TaskLocalInner {
                parent: Some(Arc::clone(&self.inner)),
            }),
        }
    }

    /// Check if this context has a parent.
    pub fn has_parent(&self) -> bool {
        self.inner.parent.is_some()
    }
}

impl Default for MkTaskLocal {
    fn default() -> Self {
        Self::new()
    }
}

impl Clone for MkTaskLocal {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}